# HG changeset patch # User Sascha L. Teichmann # Date 1405575914 -7200 # Node ID 86fa217c24d55d5798e1d1b8c775bdc0e02ed850 # Parent 9342d7fe0ee716fc67128a98211f88de85e028d3 Sediment load: Merge measurement stations if they have the same km. The chaining was too complicated to build algorithms on top of it. diff -r 9342d7fe0ee7 -r 86fa217c24d5 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 18:33:28 2014 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java Thu Jul 17 07:45:14 2014 +0200 @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.TreeMap; @@ -118,8 +119,8 @@ public static class Station implements Serializable { - public static final int BED_LOAD = 0; - public static final int SUSPENDED = 1; + public static final int BED_LOAD = 1; + public static final int SUSPENDED = 2; private double station; @@ -130,8 +131,6 @@ private Station next; private Station prev; - private Station chain; - public Station() { this(BED_LOAD, 0.0); } @@ -153,6 +152,10 @@ return type; } + public boolean isType(int type) { + return (this.type & type) == type; + } + public void setNext(Station next) { this.next = next; } @@ -177,13 +180,46 @@ return isKMUp ? getPrev() : getNext(); } - public Station nextChain() { - return chain; + public void merge(Station other) { + this.type |= other.type; + for (int i = 0, N = grainFractions.size(); i < N; ++i) { + grainFractions.set(i, + mergeValues(grainFractions.get(i), other.grainFractions.get(i))); + } } - public void append(Station station) { - station.chain = chain; - chain = station; + private static final Comparator ID_CMP = new Comparator() { + @Override + public int compare(Value a, Value b) { + return a.getLoad().getId() - b.getLoad().getId(); + } + }; + + private static List mergeValues(List a, List b) { + if (a == null) return b; + if (b == null) return a; + a.addAll(b); + // re-establish id order. + Collections.sort(a, ID_CMP); + return a; + } + + public Station nextByType(int type, boolean isKMUp) { + for (Station curr = this; curr != null; curr = curr.getNext(isKMUp)) { + if (curr.isType(type)) { + return curr; + } + } + return null; + } + + public Station prevByType(int type, boolean isKMUp) { + for (Station curr = this; curr != null; curr = curr.getPrev(isKMUp)) { + if (curr.isType(type)) { + return curr; + } + } + return null; } public void addValue(int grainFraction, Value value) { @@ -248,15 +284,6 @@ return Double.NaN; } - - public Station firstOfType(int type) { - for (Station curr = this; curr != null; curr = curr.chain) { - if (curr.getType() == type) { - return curr; - } - } - return null; - } } // class Station @@ -279,7 +306,7 @@ if (st == null) { same.put(key, station); } else { - st.append(station); + st.merge(station); } } this.stations = new Station[same.size()]; @@ -291,18 +318,9 @@ } private void wireNeighbors() { - for (int i = 0, N = stations.length; i < N-1; ++i) { - for (Station curr = stations[i]; curr != null; curr = curr.nextChain()) { - int type = curr.getType(); - NEXT: for (int j = i+1; j < N; ++j) { - Station next = stations[j].firstOfType(type); - if (next != null) { - curr.setNext(next); - next.setPrev(curr); - break NEXT; - } - } - } + for (int i = 1; i < stations.length; ++i) { + stations[i-1].setNext(stations[i]); + stations[i].setPrev(stations[i-1]); } }