|
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.Iterator; |
|
38 |
| import java.util.Map; |
|
39 |
| import java.io.Serializable; |
|
40 |
| import edu.rice.cs.plt.tuple.Pair; |
|
41 |
| import edu.rice.cs.plt.lambda.Lambda; |
|
42 |
| import edu.rice.cs.plt.lambda.Runnable1; |
|
43 |
| import edu.rice.cs.plt.iter.MappedIterator; |
|
44 |
| import edu.rice.cs.plt.iter.EmptyIterator; |
|
45 |
| import edu.rice.cs.plt.iter.MutableSingletonIterator; |
|
46 |
| import edu.rice.cs.plt.object.ObjectUtil; |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| public abstract class AbstractInjectiveRelation<T1, T2> extends AbstractRelation<T1, T2> |
|
56 |
| implements InjectiveRelation<T1, T2> { |
|
57 |
| |
|
58 |
| public abstract boolean isStatic(); |
|
59 |
| public abstract LambdaMap<T2, T1> injectionMap(); |
|
60 |
| public abstract PredicateSet<T1> firstSet(); |
|
61 |
| public abstract PredicateSet<T2> matchFirst(T1 first); |
|
62 |
| |
|
63 |
| |
|
64 |
0
| @Override public boolean isEmpty() { return injectionMap().isEmpty(); }
|
|
65 |
| |
|
66 |
0
| @Override public int size() { return injectionMap().size(); }
|
|
67 |
| |
|
68 |
0
| @Override public int size(int bound) { return injectionMap().keySet().size(bound); }
|
|
69 |
| |
|
70 |
0
| public boolean isInfinite() { return injectionMap().keySet().isInfinite(); }
|
|
71 |
| |
|
72 |
0
| public boolean hasFixedSize() { return injectionMap().keySet().hasFixedSize(); }
|
|
73 |
| |
|
74 |
| |
|
75 |
0
| public boolean contains(T1 first, T2 second) {
|
|
76 |
0
| LambdaMap<T2, T1> map = injectionMap();
|
|
77 |
0
| return map.containsKey(second) && ObjectUtil.equal(map.get(second), first);
|
|
78 |
| } |
|
79 |
| |
|
80 |
| |
|
81 |
0
| public boolean contains(Object obj) {
|
|
82 |
0
| if (obj instanceof Pair<?, ?>) {
|
|
83 |
0
| Pair<?, ?> p = (Pair<?, ?>) obj;
|
|
84 |
0
| LambdaMap<T2, T1> map = injectionMap();
|
|
85 |
0
| return map.containsKey(p.second()) && ObjectUtil.equal(map.get(p.second()), p.first());
|
|
86 |
| } |
|
87 |
0
| else { return false; }
|
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
0
| public Iterator<Pair<T1, T2>> iterator() {
|
|
92 |
0
| return MappedIterator.make(injectionMap().entrySet().iterator(),
|
|
93 |
| new Lambda<Map.Entry<T2, T1>, Pair<T1, T2>>() { |
|
94 |
0
| public Pair<T1, T2> value(Map.Entry<T2, T1> entry) {
|
|
95 |
0
| return new Pair<T1, T2>(entry.getValue(), entry.getKey());
|
|
96 |
| } |
|
97 |
| }); |
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
0
| public PredicateSet<T2> secondSet() { return injectionMap().keySet(); }
|
|
102 |
| |
|
103 |
| |
|
104 |
0
| @Override public boolean containsSecond(T2 second) { return injectionMap().containsKey(second); }
|
|
105 |
| |
|
106 |
| |
|
107 |
0
| public PredicateSet<T1> matchSecond(T2 second) { return new MatchSecondSet(second); }
|
|
108 |
| |
|
109 |
| |
|
110 |
0
| public T1 antecedent(T2 second) { return injectionMap().get(second); }
|
|
111 |
| |
|
112 |
| |
|
113 |
0
| @Override public FunctionalRelation<T2, T1> inverse() { return new InverseInjectiveRelation(); }
|
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| private final class MatchSecondSet extends AbstractPredicateSet<T1> implements Serializable { |
|
121 |
| private final T2 _key; |
|
122 |
| |
|
123 |
0
| public MatchSecondSet(T2 second) { _key = second; }
|
|
124 |
| |
|
125 |
0
| @Override public boolean isEmpty() { return !injectionMap().containsKey(_key); }
|
|
126 |
0
| @Override public int size() { return injectionMap().containsKey(_key) ? 1 : 0; }
|
|
127 |
0
| @Override public int size(int bound) { return (bound == 0) ? 0 : size(); }
|
|
128 |
0
| public boolean isInfinite() { return false; }
|
|
129 |
0
| public boolean hasFixedSize() { return AbstractInjectiveRelation.this.isStatic(); }
|
|
130 |
0
| public boolean isStatic() { return AbstractInjectiveRelation.this.isStatic(); }
|
|
131 |
| |
|
132 |
0
| public boolean contains(Object val) {
|
|
133 |
0
| return AbstractInjectiveRelation.this.contains(Pair.make(val, _key));
|
|
134 |
| } |
|
135 |
| |
|
136 |
0
| public Iterator<T1> iterator() {
|
|
137 |
0
| final LambdaMap<T2, T1> map = injectionMap();
|
|
138 |
0
| if (map.containsKey(_key)) {
|
|
139 |
0
| return new MutableSingletonIterator<T1>(map.get(_key), new Runnable1<T1>() {
|
|
140 |
0
| public void run(T1 val) { map.remove(_key); }
|
|
141 |
| }); |
|
142 |
| } |
|
143 |
0
| else { return EmptyIterator.make(); }
|
|
144 |
| } |
|
145 |
| |
|
146 |
0
| @Override public boolean add(T1 val) {
|
|
147 |
0
| boolean result = !AbstractInjectiveRelation.this.contains(val, _key);
|
|
148 |
0
| if (result) { injectionMap().put(_key, val); }
|
|
149 |
0
| return result;
|
|
150 |
| } |
|
151 |
| |
|
152 |
0
| @Override public boolean remove(Object val) {
|
|
153 |
0
| boolean result = AbstractInjectiveRelation.this.contains(Pair.make(val, _key));
|
|
154 |
0
| if (result) { injectionMap().remove(_key); }
|
|
155 |
0
| return result;
|
|
156 |
| } |
|
157 |
| |
|
158 |
0
| @Override public void clear() { injectionMap().remove(_key); }
|
|
159 |
| } |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| protected class InverseInjectiveRelation extends InverseRelation implements FunctionalRelation<T2, T1> { |
|
166 |
0
| public T1 value(T2 first) { return AbstractInjectiveRelation.this.antecedent(first); }
|
|
167 |
0
| public LambdaMap<T2, T1> functionMap() { return AbstractInjectiveRelation.this.injectionMap(); }
|
|
168 |
0
| @Override public Relation<T1, T2> inverse() { return AbstractInjectiveRelation.this; }
|
|
169 |
| } |
|
170 |
| |
|
171 |
| } |