|
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 |
| package edu.rice.cs.plt.collect; |
|
36 |
| |
|
37 |
| import java.util.Map; |
|
38 |
| import java.util.HashMap; |
|
39 |
| import java.util.Set; |
|
40 |
| import java.io.Serializable; |
|
41 |
| import edu.rice.cs.plt.tuple.Pair; |
|
42 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
43 |
| import edu.rice.cs.plt.lambda.Thunk; |
|
44 |
| |
|
45 |
| |
|
46 |
| public class IndexedFunctionalRelation<T1, T2> extends AbstractFunctionalRelation<T1, T2> implements Serializable { |
|
47 |
| |
|
48 |
| private Map<T1, T2> _firstMap; |
|
49 |
| private LambdaMap<T1, T2> _functionMap; |
|
50 |
| private RelationIndex<T2, T1> _secondIndex; |
|
51 |
| |
|
52 |
| |
|
53 |
0
| public IndexedFunctionalRelation() {
|
|
54 |
0
| this(CollectUtil.<T1, T2>hashMapFactory(),
|
|
55 |
| CollectUtil.<T2, PredicateSet<T1>>hashMapFactory(), |
|
56 |
| CollectUtil.<T1>hashSetFactory(4)); |
|
57 |
| } |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
0
| public IndexedFunctionalRelation(boolean indexSecond) {
|
|
64 |
0
| if (indexSecond) {
|
|
65 |
0
| _firstMap = new HashMap<T1, T2>();
|
|
66 |
0
| _functionMap = new ImmutableMap<T1, T2>(_firstMap);
|
|
67 |
0
| _secondIndex = makeSecondIndex(CollectUtil.<T2, PredicateSet<T1>>hashMapFactory(),
|
|
68 |
| CollectUtil.<T1>hashSetFactory(4)); |
|
69 |
| } |
|
70 |
| else { |
|
71 |
0
| _firstMap = new HashMap<T1, T2>();
|
|
72 |
0
| _functionMap = new ImmutableMap<T1, T2>(_firstMap);
|
|
73 |
0
| _secondIndex = new LazyRelationIndex<T2, T1>(IterUtil.map(this, Pair.<T1, T2>inverter()));
|
|
74 |
| } |
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
0
| public IndexedFunctionalRelation(Thunk<Map<T1, T2>> firstIndexFactory,
|
|
84 |
| Thunk<Map<T2, PredicateSet<T1>>> secondIndexFactory, |
|
85 |
| Thunk<Set<T1>> secondIndexEntryFactory) { |
|
86 |
0
| _firstMap = firstIndexFactory.value();
|
|
87 |
0
| _functionMap = new ImmutableMap<T1, T2>(_firstMap);
|
|
88 |
0
| _secondIndex = makeSecondIndex(secondIndexFactory, secondIndexEntryFactory);
|
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
0
| public IndexedFunctionalRelation(Thunk<Map<T1, T2>> firstIndexFactory) {
|
|
96 |
0
| _firstMap = firstIndexFactory.value();
|
|
97 |
0
| _functionMap = new ImmutableMap<T1, T2>(_firstMap);
|
|
98 |
0
| _secondIndex = new LazyRelationIndex<T2, T1>(IterUtil.map(this, Pair.<T1, T2>inverter()));
|
|
99 |
| } |
|
100 |
| |
|
101 |
0
| private RelationIndex<T2, T1> makeSecondIndex(Thunk<Map<T2, PredicateSet<T1>>> mapFactory,
|
|
102 |
| Thunk<Set<T1>> setFactory) { |
|
103 |
0
| return new ConcreteRelationIndex<T2, T1>(mapFactory, setFactory) {
|
|
104 |
0
| public void validateAdd(T2 second, T1 first) { IndexedFunctionalRelation.this.validateAdd(first, second); }
|
|
105 |
0
| public void addToRelation(T2 second, T1 first) { _firstMap.put(first, second); }
|
|
106 |
0
| public void removeFromRelation(T2 second, T1 first) { _firstMap.remove(first); }
|
|
107 |
0
| public void clearRelation() { _firstMap.clear(); }
|
|
108 |
| }; |
|
109 |
| } |
|
110 |
| |
|
111 |
0
| public boolean isStatic() { return false; }
|
|
112 |
0
| public LambdaMap<T1, T2> functionMap() { return _functionMap; }
|
|
113 |
0
| public PredicateSet<T2> secondSet() { return _secondIndex.keys(); }
|
|
114 |
0
| public PredicateSet<T1> matchSecond(T2 second) { return _secondIndex.match(second); }
|
|
115 |
| |
|
116 |
0
| @Override public boolean add(T1 first, T2 second) {
|
|
117 |
0
| boolean result = validateAdd(first, second);
|
|
118 |
0
| if (result) {
|
|
119 |
0
| _firstMap.put(first, second);
|
|
120 |
0
| _secondIndex.added(second, first);
|
|
121 |
| } |
|
122 |
0
| return result;
|
|
123 |
| } |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
0
| private boolean validateAdd(T1 first, T2 second) {
|
|
130 |
0
| if (_firstMap.containsKey(first)) {
|
|
131 |
0
| T2 current = _firstMap.get(first);
|
|
132 |
0
| if ((current == null) ? (second == null) : current.equals(second)) {
|
|
133 |
0
| return false;
|
|
134 |
| } |
|
135 |
| else { |
|
136 |
0
| throw new IllegalArgumentException("Relation already contains an entry for " + first);
|
|
137 |
| } |
|
138 |
| } |
|
139 |
0
| else { return true; }
|
|
140 |
| } |
|
141 |
| |
|
142 |
0
| @Override public boolean remove(T1 first, T2 second) {
|
|
143 |
0
| boolean result = contains(first, second);
|
|
144 |
0
| if (result) {
|
|
145 |
0
| _firstMap.remove(first);
|
|
146 |
0
| _secondIndex.removed(second, first);
|
|
147 |
| } |
|
148 |
0
| return result;
|
|
149 |
| } |
|
150 |
| |
|
151 |
0
| @Override public void clear() {
|
|
152 |
0
| _firstMap.clear();
|
|
153 |
0
| _secondIndex.cleared();
|
|
154 |
| } |
|
155 |
| |
|
156 |
| |
|
157 |
0
| public static <T1, T2> IndexedFunctionalRelation<T1, T2> makeHashBased() {
|
|
158 |
0
| return new IndexedFunctionalRelation<T1, T2>(CollectUtil.<T1, T2>hashMapFactory(),
|
|
159 |
| CollectUtil.<T2, PredicateSet<T1>>hashMapFactory(), |
|
160 |
| CollectUtil.<T1>hashSetFactory(4)); |
|
161 |
| } |
|
162 |
| |
|
163 |
| |
|
164 |
0
| public static <T1, T2> IndexedFunctionalRelation<T1, T2> makeLinkedHashBased() {
|
|
165 |
0
| return new IndexedFunctionalRelation<T1, T2>(CollectUtil.<T1, T2>linkedHashMapFactory(),
|
|
166 |
| CollectUtil.<T2, PredicateSet<T1>>linkedHashMapFactory(), |
|
167 |
| CollectUtil.<T1>linkedHashSetFactory(4)); |
|
168 |
| } |
|
169 |
| |
|
170 |
| |
|
171 |
0
| public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>>
|
|
172 |
| IndexedFunctionalRelation<T1, T2> makeTreeBased() { |
|
173 |
0
| return new IndexedFunctionalRelation<T1, T2>(CollectUtil.<T1, T2>treeMapFactory(),
|
|
174 |
| CollectUtil.<T2, PredicateSet<T1>>treeMapFactory(), |
|
175 |
| CollectUtil.<T1>treeSetFactory()); |
|
176 |
| } |
|
177 |
| |
|
178 |
| } |