|
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.Set; |
|
38 |
| import java.util.Map; |
|
39 |
| import java.util.Iterator; |
|
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 IndexedRelation<T1, T2> extends AbstractRelation<T1, T2> implements Serializable { |
|
47 |
| private final RelationIndex<T1, T2> _firstIndex; |
|
48 |
| private final RelationIndex<T2, T1> _secondIndex; |
|
49 |
| |
|
50 |
| |
|
51 |
1
| public IndexedRelation() {
|
|
52 |
1
| this(CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
|
|
53 |
| CollectUtil.<T2>hashSetFactory(4), |
|
54 |
| CollectUtil.<T2, PredicateSet<T1>>hashMapFactory(), |
|
55 |
| CollectUtil.<T1>hashSetFactory(4)); |
|
56 |
| } |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
1
| public IndexedRelation(boolean indexSecond) {
|
|
63 |
1
| if (indexSecond) {
|
|
64 |
0
| _firstIndex = makeFirstIndex(CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
|
|
65 |
| CollectUtil.<T2>hashSetFactory(4)); |
|
66 |
0
| _secondIndex = makeSecondIndex(CollectUtil.<T2, PredicateSet<T1>>hashMapFactory(),
|
|
67 |
| CollectUtil.<T1>hashSetFactory(4)); |
|
68 |
| } |
|
69 |
| else { |
|
70 |
1
| _firstIndex = makeFirstIndex(CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
|
|
71 |
| CollectUtil.<T2>hashSetFactory(4)); |
|
72 |
1
| _secondIndex = new LazyRelationIndex<T2, T1>(IterUtil.map(_firstIndex, Pair.<T1, T2>inverter()));
|
|
73 |
| } |
|
74 |
| } |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
1
| public IndexedRelation(Thunk<Map<T1, PredicateSet<T2>>> firstIndexFactory,
|
|
84 |
| Thunk<Set<T2>> firstIndexEntryFactory, |
|
85 |
| Thunk<Map<T2, PredicateSet<T1>>> secondIndexFactory, |
|
86 |
| Thunk<Set<T1>> secondIndexEntryFactory) { |
|
87 |
1
| _firstIndex = makeFirstIndex(firstIndexFactory, firstIndexEntryFactory);
|
|
88 |
1
| _secondIndex = makeSecondIndex(secondIndexFactory, secondIndexEntryFactory);
|
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
0
| public IndexedRelation(Thunk<Map<T1, PredicateSet<T2>>> firstIndexFactory,
|
|
99 |
| Thunk<Set<T2>> firstIndexEntryFactory) { |
|
100 |
0
| _firstIndex = makeFirstIndex(firstIndexFactory, firstIndexEntryFactory);
|
|
101 |
0
| _secondIndex = new LazyRelationIndex<T2, T1>(IterUtil.map(_firstIndex, Pair.<T1, T2>inverter()));
|
|
102 |
| } |
|
103 |
| |
|
104 |
2
| private RelationIndex<T1, T2> makeFirstIndex(Thunk<Map<T1, PredicateSet<T2>>> mapFactory,
|
|
105 |
| Thunk<Set<T2>> setFactory) { |
|
106 |
2
| return new ConcreteRelationIndex<T1, T2>(mapFactory, setFactory) {
|
|
107 |
0
| public void addToRelation(T1 first, T2 second) { _secondIndex.added(second, first); }
|
|
108 |
0
| public void removeFromRelation(T1 first, T2 second) { _secondIndex.removed(second, first); }
|
|
109 |
0
| public void clearRelation() { _secondIndex.cleared(); }
|
|
110 |
| }; |
|
111 |
| } |
|
112 |
| |
|
113 |
1
| private RelationIndex<T2, T1> makeSecondIndex(Thunk<Map<T2, PredicateSet<T1>>> mapFactory,
|
|
114 |
| Thunk<Set<T1>> setFactory) { |
|
115 |
1
| return new ConcreteRelationIndex<T2, T1>(mapFactory, setFactory) {
|
|
116 |
0
| public void addToRelation(T2 second, T1 first) { _firstIndex.added(first, second); }
|
|
117 |
0
| public void removeFromRelation(T2 second, T1 first) { _firstIndex.removed(first, second); }
|
|
118 |
0
| public void clearRelation() { _firstIndex.cleared(); }
|
|
119 |
| }; |
|
120 |
| } |
|
121 |
| |
|
122 |
8
| @Override public boolean isEmpty() { return _firstIndex.isEmpty(); }
|
|
123 |
32
| @Override public int size() { return _firstIndex.size(); }
|
|
124 |
0
| @Override public int size(int bound) { return _firstIndex.size(bound); }
|
|
125 |
0
| public boolean isInfinite() { return false; }
|
|
126 |
0
| public boolean hasFixedSize() { return false; }
|
|
127 |
0
| public boolean isStatic() { return false; }
|
|
128 |
| |
|
129 |
0
| public boolean contains(T1 first, T2 second) {
|
|
130 |
0
| return _firstIndex.contains(first, second);
|
|
131 |
| } |
|
132 |
| |
|
133 |
0
| public boolean contains(Object obj) {
|
|
134 |
0
| if (obj instanceof Pair<?, ?>) {
|
|
135 |
0
| Pair<?, ?> p = (Pair<?, ?>) obj;
|
|
136 |
0
| return _firstIndex.contains(p.first(), p.second());
|
|
137 |
| } |
|
138 |
0
| else { return false; }
|
|
139 |
| } |
|
140 |
| |
|
141 |
12
| public Iterator<Pair<T1, T2>> iterator() { return _firstIndex.iterator(); }
|
|
142 |
| |
|
143 |
64
| public PredicateSet<T1> firstSet() { return _firstIndex.keys(); }
|
|
144 |
24
| public PredicateSet<T2> matchFirst(T1 first) { return _firstIndex.match(first); }
|
|
145 |
84
| public PredicateSet<T2> secondSet() { return _secondIndex.keys(); }
|
|
146 |
44
| public PredicateSet<T1> matchSecond(T2 second) { return _secondIndex.match(second); }
|
|
147 |
| |
|
148 |
10
| @Override public boolean add(T1 first, T2 second) {
|
|
149 |
10
| boolean result = !_firstIndex.contains(first, second);
|
|
150 |
10
| if (result) {
|
|
151 |
8
| _firstIndex.added(first, second);
|
|
152 |
8
| _secondIndex.added(second, first);
|
|
153 |
| } |
|
154 |
10
| return result;
|
|
155 |
| } |
|
156 |
| |
|
157 |
0
| @Override public boolean remove(T1 first, T2 second) {
|
|
158 |
0
| boolean result = _firstIndex.contains(first, second);
|
|
159 |
0
| if (result) {
|
|
160 |
0
| _firstIndex.removed(first, second);
|
|
161 |
0
| _secondIndex.removed(second, first);
|
|
162 |
| } |
|
163 |
0
| return result;
|
|
164 |
| } |
|
165 |
| |
|
166 |
2
| @Override public void clear() {
|
|
167 |
2
| if (!_firstIndex.isEmpty()) {
|
|
168 |
2
| _firstIndex.cleared();
|
|
169 |
2
| _secondIndex.cleared();
|
|
170 |
| } |
|
171 |
| } |
|
172 |
| |
|
173 |
| |
|
174 |
0
| public static <T1, T2> IndexedRelation<T1, T2> makeHashBased() {
|
|
175 |
0
| return new IndexedRelation<T1, T2>(CollectUtil.<T1, PredicateSet<T2>>hashMapFactory(),
|
|
176 |
| CollectUtil.<T2>hashSetFactory(4), |
|
177 |
| CollectUtil.<T2, PredicateSet<T1>>hashMapFactory(), |
|
178 |
| CollectUtil.<T1>hashSetFactory(4)); |
|
179 |
| } |
|
180 |
| |
|
181 |
| |
|
182 |
0
| public static <T1, T2> IndexedRelation<T1, T2> makeLinkedHashBased() {
|
|
183 |
0
| return new IndexedRelation<T1, T2>(CollectUtil.<T1, PredicateSet<T2>>linkedHashMapFactory(),
|
|
184 |
| CollectUtil.<T2>linkedHashSetFactory(4), |
|
185 |
| CollectUtil.<T2, PredicateSet<T1>>linkedHashMapFactory(), |
|
186 |
| CollectUtil.<T1>linkedHashSetFactory(4)); |
|
187 |
| } |
|
188 |
| |
|
189 |
| |
|
190 |
0
| public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>>
|
|
191 |
| IndexedRelation<T1, T2> makeTreeBased() { |
|
192 |
0
| return new IndexedRelation<T1, T2>(CollectUtil.<T1, PredicateSet<T2>>treeMapFactory(),
|
|
193 |
| CollectUtil.<T2>treeSetFactory(), |
|
194 |
| CollectUtil.<T2, PredicateSet<T1>>treeMapFactory(), |
|
195 |
| CollectUtil.<T1>treeSetFactory()); |
|
196 |
| } |
|
197 |
| |
|
198 |
| } |