|
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 AbstractFunctionalRelation<T1, T2> extends AbstractRelation<T1, T2> |
|
56 |
| implements FunctionalRelation<T1, T2> { |
|
57 |
| |
|
58 |
| public abstract boolean isStatic(); |
|
59 |
| public abstract LambdaMap<T1, T2> functionMap(); |
|
60 |
| public abstract PredicateSet<T2> secondSet(); |
|
61 |
| public abstract PredicateSet<T1> matchSecond(T2 second); |
|
62 |
| |
|
63 |
| |
|
64 |
0
| @Override public boolean isEmpty() { return functionMap().isEmpty(); }
|
|
65 |
| |
|
66 |
0
| @Override public int size() { return functionMap().size(); }
|
|
67 |
| |
|
68 |
0
| @Override public int size(int bound) { return functionMap().keySet().size(bound); }
|
|
69 |
| |
|
70 |
0
| public boolean isInfinite() { return functionMap().keySet().isInfinite(); }
|
|
71 |
| |
|
72 |
0
| public boolean hasFixedSize() { return functionMap().keySet().hasFixedSize(); }
|
|
73 |
| |
|
74 |
| |
|
75 |
0
| public boolean contains(T1 first, T2 second) {
|
|
76 |
0
| LambdaMap<T1, T2> map = functionMap();
|
|
77 |
0
| return map.containsKey(first) && ObjectUtil.equal(map.get(first), second);
|
|
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<T1, T2> map = functionMap();
|
|
85 |
0
| return map.containsKey(p.first()) && ObjectUtil.equal(map.get(p.first()), p.second());
|
|
86 |
| } |
|
87 |
0
| else { return false; }
|
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
0
| public Iterator<Pair<T1, T2>> iterator() {
|
|
92 |
0
| return MappedIterator.make(functionMap().entrySet().iterator(),
|
|
93 |
| new Lambda<Map.Entry<T1, T2>, Pair<T1, T2>>() { |
|
94 |
0
| public Pair<T1, T2> value(Map.Entry<T1, T2> entry) {
|
|
95 |
0
| return new Pair<T1, T2>(entry.getKey(), entry.getValue());
|
|
96 |
| } |
|
97 |
| }); |
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
0
| public PredicateSet<T1> firstSet() { return functionMap().keySet(); }
|
|
102 |
| |
|
103 |
| |
|
104 |
0
| @Override public boolean containsFirst(T1 first) { return functionMap().containsKey(first); }
|
|
105 |
| |
|
106 |
| |
|
107 |
0
| public PredicateSet<T2> matchFirst(T1 first) { return new MatchFirstSet(first); }
|
|
108 |
| |
|
109 |
| |
|
110 |
0
| public T2 value(T1 first) { return functionMap().get(first); }
|
|
111 |
| |
|
112 |
| |
|
113 |
0
| @Override public Relation<T2, T1> inverse() { return new InverseFunctionalRelation(); }
|
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| private final class MatchFirstSet extends AbstractPredicateSet<T2> implements Serializable { |
|
121 |
| private final T1 _key; |
|
122 |
| |
|
123 |
0
| public MatchFirstSet(T1 first) { _key = first; }
|
|
124 |
| |
|
125 |
0
| @Override public boolean isEmpty() { return !functionMap().containsKey(_key); }
|
|
126 |
0
| @Override public int size() { return functionMap().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 AbstractFunctionalRelation.this.isStatic(); }
|
|
130 |
0
| public boolean isStatic() { return AbstractFunctionalRelation.this.isStatic(); }
|
|
131 |
| |
|
132 |
0
| public boolean contains(Object val) {
|
|
133 |
0
| return AbstractFunctionalRelation.this.contains(Pair.make(_key, val));
|
|
134 |
| } |
|
135 |
| |
|
136 |
0
| public Iterator<T2> iterator() {
|
|
137 |
0
| final LambdaMap<T1, T2> map = functionMap();
|
|
138 |
0
| if (map.containsKey(_key)) {
|
|
139 |
0
| return new MutableSingletonIterator<T2>(map.get(_key), new Runnable1<T2>() {
|
|
140 |
0
| public void run(T2 val) { map.remove(_key); }
|
|
141 |
| }); |
|
142 |
| } |
|
143 |
0
| else { return EmptyIterator.make(); }
|
|
144 |
| } |
|
145 |
| |
|
146 |
0
| @Override public boolean add(T2 val) {
|
|
147 |
0
| boolean result = !AbstractFunctionalRelation.this.contains(_key, val);
|
|
148 |
0
| if (result) { functionMap().put(_key, val); }
|
|
149 |
0
| return result;
|
|
150 |
| } |
|
151 |
| |
|
152 |
0
| @Override public boolean remove(Object val) {
|
|
153 |
0
| boolean result = AbstractFunctionalRelation.this.contains(Pair.make(_key, val));
|
|
154 |
0
| if (result) { functionMap().remove(_key); }
|
|
155 |
0
| return result;
|
|
156 |
| } |
|
157 |
| |
|
158 |
0
| @Override public void clear() { functionMap().remove(_key); }
|
|
159 |
| } |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| protected class InverseFunctionalRelation extends InverseRelation implements InjectiveRelation<T2, T1> { |
|
166 |
0
| public T2 antecedent(T1 second) { return AbstractFunctionalRelation.this.value(second); }
|
|
167 |
0
| public LambdaMap<T1, T2> injectionMap() { return AbstractFunctionalRelation.this.functionMap(); }
|
|
168 |
0
| @Override public FunctionalRelation<T1, T2> inverse() { return AbstractFunctionalRelation.this; }
|
|
169 |
| } |
|
170 |
| |
|
171 |
| } |