sascha@2695: package de.intevation.flys.artifacts.model; sascha@2695: sascha@2695: import gnu.trove.TDoubleArrayList; sascha@2695: sascha@2695: import java.io.Serializable; sascha@2695: sascha@2785: import org.apache.log4j.Logger; sascha@2785: sascha@2695: public class Parameters sascha@2695: implements Serializable sascha@2695: { sascha@2785: private static Logger log = Logger.getLogger(Parameters.class); sascha@2785: sascha@2695: protected String [] columnNames; sascha@2695: protected TDoubleArrayList [] columns; sascha@2695: sascha@2695: public Parameters() { sascha@2695: } sascha@2695: sascha@2695: public Parameters(String [] columnNames) { sascha@2695: if (columnNames == null || columnNames.length < 1) { sascha@2695: throw new IllegalArgumentException("columnNames too short."); sascha@2695: } sascha@2695: this.columnNames = columnNames; sascha@2695: columns = new TDoubleArrayList[columnNames.length]; sascha@2695: for (int i = 0; i < columns.length; ++i) { sascha@2695: columns[i] = new TDoubleArrayList(); sascha@2695: } sascha@2695: } sascha@2695: sascha@2695: public int columnIndex(String name) { sascha@2695: for (int i = 0; i < columnNames.length; ++i) { sascha@2993: if (columnNames[i].equals(name)) { sascha@2695: return i; sascha@2695: } sascha@2695: } sascha@2695: return -1; sascha@2695: } sascha@2695: sascha@2729: public int newRow() { sascha@2695: sascha@2695: int N = columns[0].size(); sascha@2695: sascha@2695: for (int i = 0; i < columns.length; ++i) { sascha@2695: columns[i].add(Double.NaN); sascha@2695: } sascha@2695: sascha@2729: return N; sascha@2695: } sascha@2695: sascha@3010: public double get(int row, int index) { sascha@3010: return columns[index].getQuick(row); sascha@2695: } sascha@2695: sascha@2695: public double get(int i, String columnName) { sascha@2695: int index = columnIndex(columnName); sascha@2695: return index >= 0 sascha@2695: ? columns[index].getQuick(i) sascha@2695: : Double.NaN; sascha@2695: } sascha@2695: sascha@3010: public void set(int row, int index, double value) { sascha@3010: columns[index].setQuick(row, value); sascha@3010: } sascha@3010: sascha@2729: public void set(int i, String columnName, double value) { sascha@2729: int idx = columnIndex(columnName); sascha@2729: if (idx >= 0) { sascha@2785: columns[idx].setQuick(i, value); sascha@2729: } sascha@2729: } sascha@2729: sascha@3011: public boolean set(int row, int [] indices, double [] values) { sascha@3011: boolean invalid = false; sascha@3011: for (int i = 0; i < indices.length; ++i) { sascha@3011: double v = values[i]; sascha@3011: if (Double.isNaN(v)) { sascha@3011: invalid = true; sascha@3011: } sascha@3011: else { sascha@3011: columns[indices[i]].setQuick(row, v); sascha@3011: } sascha@3011: } sascha@3011: return invalid; sascha@3011: } sascha@3011: sascha@2695: public int size() { sascha@2695: return columns[0].size(); sascha@2695: } sascha@2695: sascha@2695: public int getNumberColumns() { sascha@2695: return columnNames.length; sascha@2695: } sascha@2695: sascha@2695: public String [] getColumnNames() { sascha@2695: return columnNames; sascha@2695: } sascha@2695: sascha@2695: public void removeNaNs() { sascha@2695: W.removeNaNs(columns); sascha@2695: } sascha@2744: sascha@2744: public int [] columnIndices(String [] columns) { sascha@2744: int [] indices = new int[columns.length]; sascha@2744: for (int i = 0; i < columns.length; ++i) { sascha@2744: indices[i] = columnIndex(columns[i]); sascha@2744: } sascha@2744: return indices; sascha@2744: } sascha@2744: sascha@2744: public void get(int row, int [] columnIndices, double [] values) { sascha@2744: for (int i = 0; i < columnIndices.length; ++i) { sascha@2744: int index = columnIndices[i]; sascha@2744: values[i] = index >= 0 && index < columns.length sascha@2744: ? columns[index].getQuick(row) sascha@2744: : Double.NaN; sascha@2744: } sascha@2744: } sascha@3027: sascha@3027: public int binarySearch(String columnName, double value) { sascha@3027: return binarySearch(columnIndex(columnName), value); sascha@3027: } sascha@3027: sascha@3027: public int binarySearch(int columnIndex, double value) { sascha@3027: TDoubleArrayList column = columns[columnIndex]; sascha@3027: return column.binarySearch(value); sascha@3027: } sascha@3027: sascha@3027: public int binarySearch(String columnName, double value, double epsilon) { sascha@3027: return binarySearch(columnIndex(columnName), value, epsilon); sascha@3027: } sascha@3027: sascha@3027: public int binarySearch(int columnIndex, double value, double epsilon) { sascha@3027: if (epsilon < 0d) epsilon = -epsilon; sascha@3027: double vl = value - epsilon; sascha@3027: double vh = value + epsilon; sascha@3027: sascha@3027: TDoubleArrayList column = columns[columnIndex]; sascha@3027: int lo = 0, hi = column.size()-1; sascha@3027: while (hi >= lo) { sascha@3027: int mid = (lo+hi) >> 1; sascha@3027: double v = column.getQuick(mid); sascha@3027: if (v < vl) hi = mid - 1; sascha@3027: else if (v > vh) lo = mid + 1; sascha@3027: else return mid; sascha@3027: } sascha@3027: sascha@3027: return -1; sascha@3027: } sascha@3027: sascha@3027: public boolean isSorted(String columnName) { sascha@3027: return isSorted(columnIndex(columnName)); sascha@3027: } sascha@3027: sascha@3027: public boolean isSorted(int columnIndex) { sascha@3027: TDoubleArrayList column = columns[columnIndex]; sascha@3027: for (int i = 1, N = column.size(); i < N; ++i) { sascha@3027: if (column.getQuick(i-1) > column.getQuick(i)) { sascha@3027: return false; sascha@3027: } sascha@3027: } sascha@3027: return true; sascha@3027: } sascha@2695: } sascha@2695: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :