# HG changeset patch # User Sascha L. Teichmann # Date 1405443142 -7200 # Node ID b6e7cfcabf2c2958dfd5ef0ea58c54fcd0b1fe7b # Parent 5e3f4b4fcb28301bc48477e1c1e022384d947724 Wire the neighbors to the measurement stations together. This eases the lookup for same types (suspended or bed load). diff -r 5e3f4b4fcb28 -r b6e7cfcabf2c 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 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> 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 values: grainFractions) { + double value = findValueByLoadId(values, id); + if (!Double.isNaN(value)) { + return value; + } + } + return Double.NaN; + } + + private static final double findValueByLoadId( + List 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> stations; + private List> stations; public SedimentLoadData() { - stations = new TreeMap>(EpsilonComparator.CMP); } - public void addStation(Station station) { - Double key = station.getStation(); - List sts = stations.get(key); - if (sts == null) { - sts = new ArrayList(2); - stations.put(key, sts); - } - sts.add(station); + public SedimentLoadData(Collection stations) { + setStations(stations); } - - public NavigableMap> range(double from, double to) { + public void setStations(Collection stations) { + TreeMap> same = + new TreeMap>(EpsilonComparator.CMP); + for (Station station: stations) { + Double key = station.getStation(); + List sts = same.get(key); + if (sts == null) { + sts = new ArrayList(2); + same.put(key, sts); + } + sts.add(station); + } + this.stations = new ArrayList>(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 : diff -r 5e3f4b4fcb28 -r b6e7cfcabf2c artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataFactory.java --- 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; }