changeset 8034:b6e7cfcabf2c

Wire the neighbors to the measurement stations together. This eases the lookup for same types (suspended or bed load).
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 15 Jul 2014 18:52:22 +0200
parents 5e3f4b4fcb28
children f2dc7992b8a3
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataFactory.java
diffstat 2 files changed, 81 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java	Tue Jul 15 12:47:52 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java	Tue Jul 15 18:52:22 2014 +0200
@@ -9,10 +9,9 @@
 
 import java.io.Serializable;
 import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Collection;
 import java.util.Date;
 import java.util.List;
-import java.util.NavigableMap;
 import java.util.TreeMap;
 
 import org.dive4elements.river.utils.EpsilonComparator;
@@ -120,6 +119,9 @@
 
         private List<List<Value>> grainFractions;
 
+        private Station next;
+        private Station prev;
+
         public Station() {
             this(BED_LOAD, 0.0);
         }
@@ -141,6 +143,22 @@
             return type;
         }
 
+        public void setNext(Station next) {
+            this.next = next;
+        }
+
+        public Station getNext() {
+            return next;
+        }
+
+        public void setPrev(Station prev) {
+            this.prev = next;
+        }
+
+        public Station getPrev() {
+            return prev;
+        }
+
         public void addValue(int grainFraction, Value value) {
             grainFractions.get(grainFraction).add(value);
         }
@@ -175,33 +193,77 @@
         public boolean inside(double a, double b) {
             return station >= a && station <= b;
         }
+
+        public double findValueByLoadId(int id) {
+            for (List<Value> values: grainFractions) {
+                double value = findValueByLoadId(values, id);
+                if (!Double.isNaN(value)) {
+                    return value;
+                }
+            }
+            return Double.NaN;
+        }
+
+        private static final double findValueByLoadId(
+            List<Value> values,
+            int         id
+        ) {
+            // List is ordered by station id -> binary search.
+            int lo = 0, hi = values.size()-1;
+            while (lo <= hi) {
+                int mid = (lo + hi)/2;
+                Value v = values.get(mid);
+                int xid = v.getLoad().getId();
+                if      (xid < id) hi = mid-1;
+                else if (xid > id) lo = mid+1;
+                else return v.getValue();
+            }
+
+            return Double.NaN;
+        }
     } // class Station
 
 
-    private TreeMap<Double, List<Station>> stations;
+    private List<List<Station>> stations;
 
     public SedimentLoadData() {
-        stations = new TreeMap<Double, List<Station>>(EpsilonComparator.CMP);
     }
 
-    public void addStation(Station station) {
-        Double key = station.getStation();
-        List<Station> sts = stations.get(key);
-        if (sts == null) {
-            sts = new ArrayList<Station>(2);
-            stations.put(key, sts);
-        }
-        sts.add(station);
+    public SedimentLoadData(Collection<Station> stations) {
+        setStations(stations);
     }
 
-
-    public NavigableMap<Double, List<Station>> range(double from, double to) {
+    public void setStations(Collection<Station> stations) {
+        TreeMap<Double, List<Station>> same =
+            new TreeMap<Double, List<Station>>(EpsilonComparator.CMP);
+        for (Station station: stations) {
+            Double key = station.getStation();
+            List<Station> sts = same.get(key);
+            if (sts == null) {
+                sts = new ArrayList<Station>(2);
+                same.put(key, sts);
+            }
+            sts.add(station);
+        }
+        this.stations = new ArrayList<List<Station>>(same.values());
+        wireNeighbors();
+    }
 
-        if (from > to) {
-            double t = from; from = to; to = t;
+    private void wireNeighbors() {
+        for (int i = 0, N = stations.size()-1; i < N; ++i) {
+            for (Station current: stations.get(i)) {
+                int type = current.getType();
+                NEXT: for (int j = i+1; j < N; ++j) {
+                    for (Station next: stations.get(j)) {
+                        if (next.getType() == type) {
+                            current.setNext(next);
+                            next.setPrev(current);
+                            break NEXT;
+                        }
+                    }
+                }
+            }
         }
-
-        return stations.subMap(from, true, to, true);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataFactory.java	Tue Jul 15 12:47:52 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataFactory.java	Tue Jul 15 18:52:22 2014 +0200
@@ -157,11 +157,7 @@
                 new SedimentLoadData.Value(load, slv_value));
         }
 
-        SedimentLoadData sld = new SedimentLoadData();
-
-        for (SedimentLoadData.Station station: id2station.values()) {
-            sld.addStation(station);
-        }
+        SedimentLoadData sld = new SedimentLoadData(id2station.values());
 
         return sld;
     }

http://dive4elements.wald.intevation.org