# HG changeset patch # User Sascha L. Teichmann # Date 1405575978 -7200 # Node ID 549f18bf0008202f0a199e4f439b7441551211a2 # Parent 86fa217c24d55d5798e1d1b8c775bdc0e02ed850# Parent bd0dea643440d49c06969ed6457f8d9d78e1aad7 Merged. diff -r bd0dea643440 -r 549f18bf0008 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 19:11:31 2014 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java Thu Jul 17 07:46:18 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]); } }