Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/utils/KMIndex.java @ 3720:181e250bf989
Tagged 'flys-artifacts' as 2.9
flys-artifacts/trunk@5375 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 07 Sep 2012 09:26:45 +0000 |
parents | 0e8929d5e9e3 |
children | 46511b4d8357 |
rev | line source |
---|---|
3013
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.utils; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import java.util.ArrayList; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 import java.util.Collections; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 import java.util.List; |
3026
65b6e27c6f25
FixA: Use new data structures in Delta W(t) exporter. Removed obsolete code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3013
diff
changeset
|
6 import java.util.Iterator; |
3013
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 import java.io.Serializable; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 public class KMIndex<A> |
3026
65b6e27c6f25
FixA: Use new data structures in Delta W(t) exporter. Removed obsolete code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3013
diff
changeset
|
11 implements Serializable, Iterable<KMIndex.Entry<A>> |
3013
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 public static final double EPSILON = 1e-4; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 public static class Entry<A> |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 implements Serializable, Comparable<Entry<A>> |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 protected double km; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 protected A value; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 public Entry(double km) { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 this.km = km; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 public Entry(double km, A value) { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 this.km = km; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 this.value = value; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 public double getKm() { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 return km; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 public A getValue() { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 return value; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 public void setValue(A value) { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 this.value = value; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 @Override |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 public int compareTo(Entry<A> other) { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 double diff = km - other.km; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 if (diff < -EPSILON) return -1; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 if (diff > +EPSILON) return +1; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 return 0; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 public boolean epsilonEquals(double km) { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 return Math.abs(this.km - km) < EPSILON; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 } // class Entry |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 protected List<Entry<A>> entries; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 public KMIndex() { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 this(10); |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 public KMIndex(int capacity) { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 entries = new ArrayList<Entry<A>>(capacity); |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 public void add(double km, A value) { |
3158
0e8929d5e9e3
Use generics more correctly
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3026
diff
changeset
|
67 entries.add(new Entry<A>(km, value)); |
3013
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 public void sort() { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 Collections.sort(entries); |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 public Entry<A> search(double km) { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 for (Entry<A> entry: entries) { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 if (entry.epsilonEquals(km)) { |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 return entry; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 return null; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 public Entry<A> binarySearch(double km) { |
3158
0e8929d5e9e3
Use generics more correctly
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3026
diff
changeset
|
84 int index = Collections.binarySearch(entries, new Entry<A>(km)); |
3013
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 return index >= 0 ? entries.get(index) : null; |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 } |
3026
65b6e27c6f25
FixA: Use new data structures in Delta W(t) exporter. Removed obsolete code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3013
diff
changeset
|
87 |
65b6e27c6f25
FixA: Use new data structures in Delta W(t) exporter. Removed obsolete code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3013
diff
changeset
|
88 public Iterator<Entry<A>> iterator() { |
65b6e27c6f25
FixA: Use new data structures in Delta W(t) exporter. Removed obsolete code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3013
diff
changeset
|
89 return entries.iterator(); |
65b6e27c6f25
FixA: Use new data structures in Delta W(t) exporter. Removed obsolete code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3013
diff
changeset
|
90 } |
3013
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 } |
ba62c1751f07
FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |