[PATCH] Sediment load: Ensure that the lists of values are always ordered station wise. This was a hidden bug. :-/

Wald Commits scm-commit at wald.intevation.org
Wed Jul 30 18:54:19 CEST 2014


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1406739254 -7200
# Node ID 50102c41151e7e19df0d808cd0c9ed1d04a09396
# Parent  41d9014bfa02d5d7659738aea597785324f307a9
Sediment load: Ensure that the lists of values are always ordered station wise. This was a hidden bug. :-/

diff -r 41d9014bfa02 -r 50102c41151e artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java	Wed Jul 30 18:34:48 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java	Wed Jul 30 18:54:14 2014 +0200
@@ -11,6 +11,7 @@
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -69,6 +70,17 @@
         return densities;
     }
 
+    private static final Comparator<SedimentDensityValue> BY_KM =
+        new Comparator<SedimentDensityValue>() {
+            @Override
+            public int compare(SedimentDensityValue a, SedimentDensityValue b) {
+                double diff = a.getKm() - b.getKm();
+                if (diff < 0.0) return -1;
+                if (diff > 0.0) return +1;
+                return 0;
+            }
+        };
+
     public void addDensity(double km, double density, int year) {
 
         logger.debug("adding " + year);
@@ -82,7 +94,17 @@
             densities.put(key, list);
         }
 
-        list.add(new SedimentDensityValue(km, density, year));
+        // Keep list sorted by km.
+        SedimentDensityValue sdv = new SedimentDensityValue(km, density, year);
+        int index = Collections.binarySearch(list, sdv, BY_KM);
+
+        if (index < 0) {
+            // index = -(insertion point) - 1
+            // -(index + 1) = insertion point
+            index = -(index + 1);
+        }
+
+        list.add(index, sdv);
     }
 
     /**
@@ -102,6 +124,7 @@
                 ? getDensityAtKm(entry.getValue(), km)
                 : SEDIMNET_DENSITY_FACTOR;
         }
+
         Iterator<Map.Entry<Integer, List<SedimentDensityValue>>> iter =
             densities.entrySet().iterator();
 
@@ -146,19 +169,16 @@
     /** Get points  km,density (sorted by km), for a given year. */
     public double[][] getDensities(int year)
     {
-        TreeMap<Double, Double> map = new TreeMap<Double,Double>();
         List<SedimentDensityValue> list = densities.get(year);
         if (list == null) {
             return new double[2][0];
         }
-        for (SedimentDensityValue sdv: densities.get(year)) {
-            map.put(sdv.getKm(), sdv.getDensity());
-        }
-        double[][] points = new double[2][map.size()];
+        // List is sorted in km.
+        double[][] points = new double[2][list.size()];
         int i = 0;
-        for (Map.Entry<Double, Double> kmDens: map.entrySet()) {
-            points[0][i] = kmDens.getKey();
-            points[1][i] = kmDens.getValue();
+        for (SedimentDensityValue sdv: list) {
+            points[0][i] = sdv.getKm();
+            points[1][i] = sdv.getDensity();
             i++;
         }
 
@@ -166,14 +186,13 @@
     }
 
     /** Get value at km, interpolated. */
-    private double getDensityAtKm(
+    private static double getDensityAtKm(
         List<SedimentDensityValue> values,
         double km
     ) {
         SedimentDensityValue prev = null;
         SedimentDensityValue next = null;
         for (SedimentDensityValue sdv: values) {
-            logger.debug("year: " + sdv.getYear());
             if (Math.abs(sdv.getKm() - km) < 0.00001) {
                 return prev.getDensity();
             }


More information about the Dive4Elements-commits mailing list