# HG changeset patch # User Sascha L. Teichmann # Date 1338916646 0 # Node ID 52aac4941d2a095d026549dcdd4c2e404a29e598 # Parent 65b6e27c6f25ec7002c2c88c641c7732bc0c6a8f FixA: Added binary search methods for parameters. flys-artifacts/trunk@4596 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 65b6e27c6f25 -r 52aac4941d2a flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Tue Jun 05 16:25:51 2012 +0000 +++ b/flys-artifacts/ChangeLog Tue Jun 05 17:17:26 2012 +0000 @@ -1,3 +1,8 @@ +2012-06-05 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/artifacts/model/Parameters.java: + Added some binary search methods. TODO: Sort by column. + 2012-06-05 Sascha L. Teichmann * src/main/java/de/intevation/flys/exports/DeltaWtExporter.java: diff -r 65b6e27c6f25 -r 52aac4941d2a flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java Tue Jun 05 16:25:51 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java Tue Jun 05 17:17:26 2012 +0000 @@ -116,5 +116,50 @@ : Double.NaN; } } + + public int binarySearch(String columnName, double value) { + return binarySearch(columnIndex(columnName), value); + } + + public int binarySearch(int columnIndex, double value) { + TDoubleArrayList column = columns[columnIndex]; + return column.binarySearch(value); + } + + public int binarySearch(String columnName, double value, double epsilon) { + return binarySearch(columnIndex(columnName), value, epsilon); + } + + public int binarySearch(int columnIndex, double value, double epsilon) { + if (epsilon < 0d) epsilon = -epsilon; + double vl = value - epsilon; + double vh = value + epsilon; + + TDoubleArrayList column = columns[columnIndex]; + int lo = 0, hi = column.size()-1; + while (hi >= lo) { + int mid = (lo+hi) >> 1; + double v = column.getQuick(mid); + if (v < vl) hi = mid - 1; + else if (v > vh) lo = mid + 1; + else return mid; + } + + return -1; + } + + public boolean isSorted(String columnName) { + return isSorted(columnIndex(columnName)); + } + + public boolean isSorted(int columnIndex) { + TDoubleArrayList column = columns[columnIndex]; + for (int i = 1, N = column.size(); i < N; ++i) { + if (column.getQuick(i-1) > column.getQuick(i)) { + return false; + } + } + return true; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :