-
Notifications
You must be signed in to change notification settings - Fork 11
/
tabGepV1.java
79 lines (66 loc) · 1.79 KB
/
tabGepV1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package net.freertr.tab;
import java.util.Map;
import java.util.TreeMap;
import net.freertr.addr.addrPrefix;
import net.freertr.addr.addrType;
/**
* one prefix tree
*
* @param <T> type of elements in the list
* @author matecsaba
*/
public class tabGepV1<T extends addrType> {
private TreeMap<tabGepV1nod<T>, tabRouteEntry<T>> tree;
/**
* create one generic tree
*/
public tabGepV1() {
tree = new TreeMap<tabGepV1nod<T>, tabRouteEntry<T>>();
}
/**
* add one entry
*
* @param val value to add
* @return old value, null if freshly added
*/
public tabRouteEntry<T> add(tabRouteEntry<T> val) {
return tree.put(new tabGepV1nod<T>(val), val);
}
/**
* add one entry
*
* @param val value to add
* @return removed value, null if not found
*/
public tabRouteEntry<T> del(tabRouteEntry<T> val) {
return tree.remove(new tabGepV1nod<T>(val));
}
/**
* find one container entry
*
* @param val value to look up
* @return found value
*/
public tabRouteEntry<T> search(tabRouteEntry<T> val) {
Map.Entry<tabGepV1nod<T>, tabRouteEntry<T>> res = tree.floorEntry(new tabGepV1nod<T>(val));
for (;;) {
if (res == null) {
return null;
}
tabRouteEntry<T> v = res.getValue();
if (v.prefix.supernet(val.prefix, true)) {
return v;
}
res = tree.lowerEntry(res.getKey());
}
}
}
class tabGepV1nod<T extends addrType> implements Comparable<tabGepV1nod<T>> {
public addrPrefix<T> pfx;
public tabGepV1nod(tabRouteEntry<T> val) {
pfx = val.prefix;
}
public int compareTo(tabGepV1nod<T> o) {
return pfx.compare(pfx, o.pfx);
}
}