|
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.io.Serializable; |
|
39 |
| import edu.rice.cs.plt.tuple.Pair; |
|
40 |
| import edu.rice.cs.plt.tuple.Option; |
|
41 |
| import edu.rice.cs.plt.iter.ReadOnlyIterator; |
|
42 |
| import edu.rice.cs.plt.iter.EmptyIterator; |
|
43 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
44 |
| import edu.rice.cs.plt.lambda.Predicate; |
|
45 |
| import edu.rice.cs.plt.lambda.Lambda; |
|
46 |
| import edu.rice.cs.plt.object.ObjectUtil; |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| public class ComposedRelation<T1, T2, T3> extends AbstractRelation<T1, T3> implements Serializable { |
|
54 |
| |
|
55 |
| private final Relation<T1, T2> _rel1; |
|
56 |
| private final Relation<T2, T3> _rel2; |
|
57 |
| private final PredicateSet<T1> _firstSet; |
|
58 |
| private final PredicateSet<T3> _secondSet; |
|
59 |
| |
|
60 |
0
| public ComposedRelation(Relation<T1, T2> rel1,
|
|
61 |
| Relation<T2, T3> rel2) { |
|
62 |
0
| _rel1 = rel1;
|
|
63 |
0
| _rel2 = rel2;
|
|
64 |
0
| _firstSet = new FilteredSet<T1>(rel1.firstSet(), new Predicate<T1>() {
|
|
65 |
0
| public boolean contains(T1 first) {
|
|
66 |
0
| for (T2 middle : _rel1.matchFirst(first)) {
|
|
67 |
0
| if (_rel2.containsFirst(middle)) { return true; }
|
|
68 |
| } |
|
69 |
0
| return false;
|
|
70 |
| } |
|
71 |
| }); |
|
72 |
0
| _secondSet = new FilteredSet<T3>(rel2.secondSet(), new Predicate<T3>() {
|
|
73 |
0
| public boolean contains(T3 second) {
|
|
74 |
0
| for (T2 middle : _rel2.matchSecond(second)) {
|
|
75 |
0
| if (_rel1.containsSecond(middle)) { return true; }
|
|
76 |
| } |
|
77 |
0
| return false;
|
|
78 |
| } |
|
79 |
| }); |
|
80 |
| } |
|
81 |
| |
|
82 |
0
| public int compositeHeight() { return ObjectUtil.compositeHeight(_rel1, _rel2) + 1; }
|
|
83 |
0
| public int compositeSize() { return ObjectUtil.compositeSize(_rel1, _rel2) + 1; }
|
|
84 |
| |
|
85 |
0
| @Override public boolean isEmpty() { return _firstSet.isEmpty(); }
|
|
86 |
0
| public boolean isInfinite() { return false; }
|
|
87 |
0
| public boolean hasFixedSize() { return false; }
|
|
88 |
0
| public boolean isStatic() { return _rel1.isStatic() && _rel2.isStatic(); }
|
|
89 |
| |
|
90 |
0
| public boolean contains(T1 first, T3 second) {
|
|
91 |
0
| return matchFirst(first).contains(second);
|
|
92 |
| } |
|
93 |
| |
|
94 |
0
| public boolean contains(Object o) {
|
|
95 |
0
| if (o instanceof Pair<?, ?>) {
|
|
96 |
0
| Pair<?, ?> p = (Pair<?, ?>) o;
|
|
97 |
0
| Option<T1> first = CollectUtil.castIfContains(_firstSet, p.first());
|
|
98 |
0
| return first.isSome() && matchFirst(first.unwrap()).contains(p.second());
|
|
99 |
| } |
|
100 |
0
| else { return false; }
|
|
101 |
| } |
|
102 |
| |
|
103 |
| |
|
104 |
0
| public Iterator<Pair<T1, T3>> iterator() {
|
|
105 |
0
| return new ReadOnlyIterator<Pair<T1, T3>>() {
|
|
106 |
| private final Iterator<T1> _firsts = _firstSet.iterator(); |
|
107 |
| private T1 _currentFirst = null; |
|
108 |
| private Iterator<T2> _currentMiddles = EmptyIterator.<T2>make(); |
|
109 |
| private T2 _currentMiddle = null; |
|
110 |
| private Iterator<? extends T3> _currentSeconds = EmptyIterator.<T3>make(); |
|
111 |
| |
|
112 |
0
| public boolean hasNext() {
|
|
113 |
0
| return _currentSeconds.hasNext() || _currentMiddles.hasNext() || _firsts.hasNext();
|
|
114 |
| } |
|
115 |
| |
|
116 |
0
| public Pair<T1, T3> next() {
|
|
117 |
| |
|
118 |
0
| if (!_currentSeconds.hasNext()) {
|
|
119 |
0
| if (!_currentMiddles.hasNext()) {
|
|
120 |
0
| _currentFirst = _firsts.next();
|
|
121 |
0
| _currentMiddles = _rel1.matchFirst(_currentFirst).iterator();
|
|
122 |
| } |
|
123 |
0
| _currentMiddle = _currentMiddles.next();
|
|
124 |
0
| _currentSeconds = _rel2.matchFirst(_currentMiddle).iterator();
|
|
125 |
| } |
|
126 |
0
| return new Pair<T1, T3>(_currentFirst, _currentSeconds.next());
|
|
127 |
| } |
|
128 |
| }; |
|
129 |
| } |
|
130 |
| |
|
131 |
0
| public PredicateSet<T1> firstSet() { return _firstSet; }
|
|
132 |
| |
|
133 |
0
| public PredicateSet<T3> matchFirst(T1 first) {
|
|
134 |
0
| Iterable<PredicateSet<T3>> seconds =
|
|
135 |
| IterUtil.map(_rel1.matchFirst(first), new Lambda<T2, PredicateSet<T3>>() { |
|
136 |
0
| public PredicateSet<T3> value(T2 middle) { return _rel2.matchFirst(middle); }
|
|
137 |
| }); |
|
138 |
0
| return new IterableSet<T3>(IterUtil.collapse(seconds));
|
|
139 |
| } |
|
140 |
| |
|
141 |
0
| public PredicateSet<T3> secondSet() { return _secondSet; }
|
|
142 |
| |
|
143 |
0
| public PredicateSet<T1> matchSecond(T3 second) {
|
|
144 |
0
| Iterable<PredicateSet<T1>> firsts =
|
|
145 |
| IterUtil.map(_rel2.matchSecond(second), new Lambda<T2, PredicateSet<T1>>() { |
|
146 |
0
| public PredicateSet<T1> value(T2 middle) { return _rel1.matchSecond(middle); }
|
|
147 |
| }); |
|
148 |
0
| return new IterableSet<T1>(IterUtil.collapse(firsts));
|
|
149 |
| } |
|
150 |
| |
|
151 |
| } |