diff artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java @ 6434:898afcce1d0a

Partial fix for flys/1303
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 26 Jun 2013 13:33:15 +0200
parents af13ceeba52a
children eb4ca9a7eaca
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java	Wed Jun 26 13:26:43 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java	Wed Jun 26 13:33:15 2013 +0200
@@ -13,6 +13,7 @@
 import gnu.trove.TDoubleArrayList;
 
 import java.util.Arrays;
+import java.util.Comparator;
 
 import org.apache.log4j.Logger;
 
@@ -24,6 +25,17 @@
 
     public static final double DEFAULT_STEP_PRECISION = 1e6;
 
+    public static final Comparator<double []> DOUBLE_PAIR_CMP =
+        new Comparator<double []>() {
+            @Override
+            public int compare(double [] a, double [] b) {
+                double diff = a[0] - b[0];
+                if (diff < 0d) return -1;
+                if (diff > 0d) return +1;
+                return 0;
+            }
+        };
+
     /** EPSILON for comparison of double precision values. */
     public static final double EPSILON = 1e-4;
 
@@ -225,6 +237,26 @@
         return max;
     }
 
+
+
+    /** Sort a and b with a as key. b is ordered accordingly */
+    public static final void sortByFirst(double [] a, double [] b) {
+        // XXX: Not efficient but bulletproof.
+        double [][] pairs = new double[a.length][2];
+        for (int i = 0; i < a.length; ++i) {
+            double [] p = pairs[i];
+            p[0] = a[i];
+            p[1] = b[i];
+
+        }
+        Arrays.sort(pairs, DOUBLE_PAIR_CMP);
+        for (int i = 0; i < a.length; ++i) {
+            double [] p = pairs[i];
+            a[i] = p[0];
+            b[i] = p[1];
+        }
+    }
+
     public static void removeNaNs(TDoubleArrayList [] arrays) {
 
         int dest = 0;

http://dive4elements.wald.intevation.org