|
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.iter.ImmutableIterator; |
|
40 |
| import edu.rice.cs.plt.iter.ComposedIterator; |
|
41 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
42 |
| import edu.rice.cs.plt.tuple.Pair; |
|
43 |
| import edu.rice.cs.plt.object.Composite; |
|
44 |
| import edu.rice.cs.plt.object.ObjectUtil; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| public class UnionRelation<T1, T2> extends AbstractRelation<T1, T2> implements Composite, Serializable { |
|
50 |
| private final Relation<T1, T2> _rel1; |
|
51 |
| private final Relation<T1, T2> _rel2; |
|
52 |
| private final Relation<T1, T2> _rel2Extras; |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
0
| public UnionRelation(Relation<T1, T2> rel1, Relation<T1, T2> rel2) {
|
|
59 |
0
| _rel1 = rel1;
|
|
60 |
0
| _rel2 = rel2;
|
|
61 |
0
| _rel2Extras = new ComplementRelation<T1, T2>(rel2, rel1);
|
|
62 |
| } |
|
63 |
| |
|
64 |
0
| public int compositeHeight() { return ObjectUtil.compositeHeight(_rel1, _rel2) + 1; }
|
|
65 |
0
| public int compositeSize() { return ObjectUtil.compositeSize(_rel1, _rel2) + 1; }
|
|
66 |
| |
|
67 |
0
| public boolean contains(T1 first, T2 second) {
|
|
68 |
0
| return _rel1.contains(first, second) || _rel2.contains(first, second);
|
|
69 |
| } |
|
70 |
| |
|
71 |
0
| public boolean contains(Object o) {
|
|
72 |
0
| return _rel1.contains(o) || _rel2.contains(o);
|
|
73 |
| } |
|
74 |
| |
|
75 |
0
| public Iterator<Pair<T1, T2>> iterator() {
|
|
76 |
0
| return ImmutableIterator.make(ComposedIterator.make(_rel1.iterator(), _rel2Extras.iterator()));
|
|
77 |
| } |
|
78 |
| |
|
79 |
0
| public PredicateSet<T1> firstSet() {
|
|
80 |
0
| return new UnionSet<T1>(_rel1.firstSet(), _rel2.firstSet());
|
|
81 |
| } |
|
82 |
| |
|
83 |
0
| public PredicateSet<T2> matchFirst(T1 first) {
|
|
84 |
0
| return new UnionSet<T2>(_rel1.matchFirst(first), _rel2.matchFirst(first));
|
|
85 |
| } |
|
86 |
| |
|
87 |
0
| public PredicateSet<T2> secondSet() {
|
|
88 |
0
| return new UnionSet<T2>(_rel1.secondSet(), _rel2.secondSet());
|
|
89 |
| } |
|
90 |
| |
|
91 |
0
| public PredicateSet<T1> matchSecond(T2 second) {
|
|
92 |
0
| return new UnionSet<T1>(_rel1.matchSecond(second), _rel2.matchSecond(second));
|
|
93 |
| } |
|
94 |
| |
|
95 |
0
| public boolean isInfinite() { return IterUtil.isInfinite(_rel1) || IterUtil.isInfinite(_rel2); }
|
|
96 |
0
| public boolean hasFixedSize() { return IterUtil.hasFixedSize(_rel1) && IterUtil.hasFixedSize(_rel2); }
|
|
97 |
0
| public boolean isStatic() { return IterUtil.isStatic(_rel1) && IterUtil.isStatic(_rel2); }
|
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
0
| @Override public int size() {
|
|
102 |
0
| return _rel1.size() + _rel2Extras.size();
|
|
103 |
| } |
|
104 |
| |
|
105 |
0
| @Override public int size(int bound) {
|
|
106 |
0
| int size1 = IterUtil.sizeOf(_rel1, bound);
|
|
107 |
0
| int bound2 = bound - size1;
|
|
108 |
0
| int size2 = (bound2 > 0) ? IterUtil.sizeOf(_rel2Extras, bound) : 0;
|
|
109 |
0
| return size1 + size2;
|
|
110 |
| } |
|
111 |
| |
|
112 |
0
| @Override public boolean isEmpty() {
|
|
113 |
0
| return _rel1.isEmpty() && _rel2.isEmpty();
|
|
114 |
| } |
|
115 |
| |
|
116 |
| } |