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