Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CODU MEU:
- public class DoubleDataFrame implements DataFrame<Double>
- {
- private double[][] data;
- private List<String> columnNames;
- private Map<String,Integer> nameIndices = new HashMap<>();
- public DoubleDataFrame(List<String> columnNames, double [][] data) {
- this.data = data;
- this.columnNames = new ArrayList<>(columnNames);
- int i=0;
- for(String name : columnNames) {
- nameIndices.put(name, i);
- i++;
- }
- }
- @Override
- public int getRowCount() {
- return this.data.length;
- }
- @Override
- public int getColumnCount() {
- return this.getColumnNames().size();
- }
- @Override
- public List<String> getColumnNames() {
- List<String> immutableColumnNames = Collections.unmodifiableList(this.columnNames);
- return immutableColumnNames;
- }
- @Override
- public void setValue(int rowIndex, String colName, Double value)
- throws IndexOutOfBoundsException, IllegalArgumentException {
- if (rowIndex<0 || rowIndex>this.getRowCount()) {
- throw new IndexOutOfBoundsException("Invalid row index provided.");
- }
- if (!this.getColumnNames().contains(colName)) {
- throw new IllegalArgumentException("Non-existing column name.");
- }
- this.data[rowIndex][nameIndices.get(colName)] = value;
- }
- @Override
- public Double getValue(int rowIndex, String colName) throws IndexOutOfBoundsException, IllegalArgumentException {
- if (rowIndex<0 || rowIndex>this.getRowCount()) {
- throw new IndexOutOfBoundsException("Invalid row index provided.");
- }
- if (!this.getColumnNames().contains(colName)) {
- throw new IllegalArgumentException("Non-existing column name.");
- }
- return data[rowIndex][nameIndices.get(colName)];
- }
- TESTERU:
- int size = 10000;
- double [][] data = new double [1][ size ];
- List < String > header = new ArrayList < >( size ) ;
- for (int j =0; j < size ; j ++)
- {
- data [0][ j ] = j ;
- header . add ("x_"+ j ) ;
- }
- DoubleDataFrame df = new DoubleDataFrame ( header , data ) ;
- long time = System . currentTimeMillis () ;
- for (int j =0; j < size ; j ++)
- {
- df . getValue (0 , header . get ( j ) ) ;
- df . setValue (0 , header . get ( j ) , 0d ) ;
- }
- time = System . currentTimeMillis () - time ;
- System . out . println (" Running time : "+ time +"ms") ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement