annotate flys-artifacts/src/main/java/de/intevation/flys/utils/KMIndex.java @ 4734:0b4e1a493045

Removed x-bit from DateGuesser.java
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 30 Dec 2012 10:56:52 +0100
parents 46511b4d8357
children
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
4318
46511b4d8357 KMIndex: Minor doc.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents: 3158
diff changeset
10 /** km to value, searchable. Tolerance is at 10cm. */
3013
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 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
12 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
13 {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 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
15
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 public static class Entry<A>
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 implements Serializable, Comparable<Entry<A>>
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 protected double km;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 protected A value;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 public Entry(double km) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 this.km = km;
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 public Entry(double km, A value) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 this.km = km;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 this.value = value;
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 public double getKm() {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 return km;
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 public A getValue() {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 return value;
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 public void setValue(A value) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 this.value = value;
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 @Override
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 public int compareTo(Entry<A> other) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 double diff = km - other.km;
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 if (diff > +EPSILON) return +1;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 return 0;
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 public boolean epsilonEquals(double km) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 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
53 }
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 } // class Entry
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 protected List<Entry<A>> entries;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 public KMIndex() {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 this(10);
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 public KMIndex(int capacity) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 entries = new ArrayList<Entry<A>>(capacity);
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 public void add(double km, A value) {
3158
0e8929d5e9e3 Use generics more correctly
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3026
diff changeset
68 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
69 }
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 public void sort() {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 Collections.sort(entries);
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 public Entry<A> search(double km) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 for (Entry<A> entry: entries) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 if (entry.epsilonEquals(km)) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 return entry;
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 }
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 return null;
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 public Entry<A> binarySearch(double km) {
3158
0e8929d5e9e3 Use generics more correctly
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3026
diff changeset
85 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
86 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
87 }
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
88
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 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
90 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
91 }
3013
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 }
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org