annotate flys-artifacts/src/main/java/de/intevation/flys/utils/KMIndex.java @ 3018:fb07f936b5ad

New helper. flys-artifacts/trunk@4584 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 05 Jun 2012 13:28:11 +0000
parents ba62c1751f07
children 65b6e27c6f25
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;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import java.io.Serializable;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 public class KMIndex<A>
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 implements Serializable
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 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
13
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 public static class Entry<A>
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 implements Serializable, Comparable<Entry<A>>
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 protected double km;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 protected A value;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 public Entry(double km) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 this.km = km;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 }
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 public Entry(double km, A value) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 this.km = km;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 this.value = value;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 }
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 public double getKm() {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 return km;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 }
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 public A getValue() {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 return value;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 }
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 public void setValue(A value) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 this.value = value;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 }
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 @Override
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 public int compareTo(Entry<A> other) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 double diff = km - other.km;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 if (diff < -EPSILON) return -1;
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 return 0;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 }
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 public boolean epsilonEquals(double km) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 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
51 }
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 } // class Entry
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 protected List<Entry<A>> entries;
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 public KMIndex() {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 this(10);
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
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 public KMIndex(int capacity) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 entries = new ArrayList<Entry<A>>(capacity);
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 }
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 public void add(double km, A value) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 entries.add(new Entry(km, value));
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 }
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 public void sort() {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 Collections.sort(entries);
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 }
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 public Entry<A> search(double km) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 for (Entry<A> entry: entries) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 if (entry.epsilonEquals(km)) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 return entry;
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 }
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 return null;
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
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 public Entry<A> binarySearch(double km) {
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 int index = Collections.binarySearch(entries, new Entry(km));
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 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
85 }
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 }
ba62c1751f07 FixA: Added new serializable km inedx structure.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org