changeset 3027:52aac4941d2a

FixA: Added binary search methods for parameters. flys-artifacts/trunk@4596 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 05 Jun 2012 17:17:26 +0000
parents 65b6e27c6f25
children 77b5f5a26700
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java
diffstat 2 files changed, 50 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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	<sascha.teichmann@intevation.de>
+
+	* 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	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/DeltaWtExporter.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 :

http://dive4elements.wald.intevation.org