[PATCH] Directly chain stations with the same km position together. This ease some summations

Wald Commits scm-commit at wald.intevation.org
Wed Jul 16 13:06:14 CEST 2014


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1405508769 -7200
# Node ID aa622bddfdacf50452ccdf80cb4ff9a8f015210f
# Parent  1de6256c9786eeb70474e9c14d8d35bedc7ec8e9
Directly chain stations with the same km position together. This ease some summations.

diff -r 1de6256c9786 -r aa622bddfdac artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java	Wed Jul 16 11:55:46 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java	Wed Jul 16 13:06:09 2014 +0200
@@ -40,7 +40,7 @@
     }
 
     public interface Visitor {
-        void visit(List<Station> stations);
+        void visit(Station station);
     }
 
     public static class Value implements Serializable {
@@ -125,6 +125,8 @@
         private Station next;
         private Station prev;
 
+        private Station chain;
+
         public Station() {
             this(BED_LOAD, 0.0);
         }
@@ -170,6 +172,15 @@
             return isKMUp ? getPrev() : getNext();
         }
 
+        public Station nextChain() {
+            return chain;
+        }
+
+        public void append(Station station) {
+            station.chain = chain;
+            chain = station;
+        }
+
         public void addValue(int grainFraction, Value value) {
             List<Value> values = grainFractions.get(grainFraction);
             if (values == null) {
@@ -233,10 +244,10 @@
             return Double.NaN;
         }
 
-        public static Station firstOfType(List<Station> stations, int type) {
-            for (Station station: stations) {
-                if (station.getType() == type) {
-                    return station;
+        public Station firstOfType(int type) {
+            for (Station curr = this; curr != null; curr = curr.chain) {
+                if (curr.getType() == type) {
+                    return curr;
                 }
             }
             return null;
@@ -244,7 +255,7 @@
     } // class Station
 
 
-    private List<List<Station>> stations;
+    private List<Station> stations;
 
     public SedimentLoadData() {
     }
@@ -254,32 +265,32 @@
     }
 
     public void setStations(Collection<Station> stations) {
-        TreeMap<Double, List<Station>> same =
-            new TreeMap<Double, List<Station>>(EpsilonComparator.CMP);
+        TreeMap<Double, Station> same =
+            new TreeMap<Double, 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);
+            Station st = same.get(key);
+            if (st == null) {
+                same.put(key, station);
+            } else {
+                st.append(station);
             }
-            sts.add(station);
         }
-        this.stations = new ArrayList<List<Station>>(same.values());
+        this.stations = new ArrayList<Station>(same.values());
         wireNeighbors();
     }
 
     private void wireNeighbors() {
         for (int i = 0, N = stations.size(); i < N-1; ++i) {
-            for (Station current: stations.get(i)) {
-                int type = current.getType();
+            for (Station curr = stations.get(i); curr != null; curr = curr.nextChain()) {
+                int type = curr.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;
-                        }
+                    Station next = stations.get(j).firstOfType(type);
+                    if (next != null) {
+                        curr.setNext(next);
+                        next.setPrev(curr);
+                        break NEXT;
                     }
                 }
             }
@@ -293,15 +304,15 @@
     ) {
         while (lo >= hi) {
             int mid = (lo+hi)/2;
-            List<Station> sts = stations.get(mid);
-            double station = sts.get(0).getStation();
+            Station st = stations.get(mid);
+            double station = st.getStation();
             if (station < a) {
                 hi = mid-1;
             } else if (station > b) {
                 lo = mid+1;
             } else {
                 recursiveFindStations(a, b, lo, mid-1, visitor);
-                visitor.visit(sts);
+                visitor.visit(st);
                 lo = mid+1;
             }
         }
@@ -314,12 +325,12 @@
         recursiveFindStations(a, b, 0, stations.size()-1, visitor);
     }
 
-    public List<List<Station>> findStations(double a, double b) {
-        final List<List<Station>> result = new ArrayList<List<Station>>();
+    public List<Station> findStations(double a, double b) {
+        final List<Station> result = new ArrayList<Station>();
         findStations(a, b, new Visitor() {
             @Override
-            public void visit(List<Station> stations) {
-                result.add(stations);
+            public void visit(Station station) {
+                result.add(station);
             }
         });
         return result;


More information about the Dive4Elements-commits mailing list