/
SetIterables.java
163 lines (143 loc) · 5.94 KB
/
SetIterables.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright (c) 2017 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.utility.internal;
import java.util.Set;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.set.ImmutableSet;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.SetIterable;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.set.mutable.SetAdapter;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.set.strategy.mutable.UnifiedSetWithHashingStrategy;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.LazyIterate;
/**
* Set algebra operations.
* <p>
* Most operations are non-destructive, i.e. no input sets are modified during execution.
* The exception is operations ending in "Into." These accept the target collection of
* the final calculation as the first parameter.
*/
public final class SetIterables
{
private SetIterables()
{
throw new AssertionError("Suppress default constructor for noninstantiability");
}
public static <E> MutableSet<E> union(
SetIterable<? extends E> setA,
SetIterable<? extends E> setB)
{
return SetIterables.unionInto(setA, setB, UnifiedSet.newSet());
}
public static <E, R extends Set<E>> R unionInto(
SetIterable<? extends E> setA,
SetIterable<? extends E> setB,
R targetSet)
{
Iterate.addAllIterable(setA, targetSet);
Iterate.addAllIterable(setB, targetSet);
return targetSet;
}
public static <E> MutableSet<E> intersect(
SetIterable<? extends E> setA,
SetIterable<? extends E> setB)
{
return SetIterables.intersectInto(setA, setB, UnifiedSet.newSet());
}
public static <E, R extends Set<E>> R intersectInto(
SetIterable<? extends E> setA,
SetIterable<? extends E> setB,
R targetSet)
{
MutableSet<E> adapted = SetAdapter.adapt(targetSet);
adapted.addAllIterable(setA);
adapted.retainAllIterable(setB);
return targetSet;
}
public static <E> MutableSet<E> difference(
SetIterable<? extends E> minuendSet,
SetIterable<? extends E> subtrahendSet)
{
return SetIterables.differenceInto(minuendSet, subtrahendSet, UnifiedSet.newSet());
}
public static <E, R extends Set<E>> R differenceInto(
SetIterable<? extends E> minuendSet,
SetIterable<? extends E> subtrahendSet,
R targetSet)
{
MutableSet<E> adapted = SetAdapter.adapt(targetSet);
adapted.addAllIterable(minuendSet);
adapted.removeAllIterable(subtrahendSet);
return targetSet;
}
public static <E> MutableSet<E> symmetricDifference(
SetIterable<? extends E> setA,
SetIterable<? extends E> setB)
{
return SetIterables.symmetricDifferenceInto(setA, setB, UnifiedSet.newSet());
}
public static <E, R extends Set<E>> R symmetricDifferenceInto(
SetIterable<? extends E> setA,
SetIterable<? extends E> setB,
R targetSet)
{
return SetIterables.unionInto(
SetIterables.difference(setA, setB),
SetIterables.difference(setB, setA),
targetSet);
}
public static <E> boolean isSubsetOf(
SetIterable<? extends E> candidateSubset,
SetIterable<? extends E> candidateSuperset)
{
return candidateSubset.size() <= candidateSuperset.size()
&& candidateSuperset.containsAllIterable(candidateSubset);
}
public static <E> boolean isProperSubsetOf(
SetIterable<? extends E> candidateSubset,
SetIterable<? extends E> candidateSuperset)
{
return candidateSubset.size() < candidateSuperset.size()
&& candidateSuperset.containsAllIterable(candidateSubset);
}
public static <T> MutableSet<MutableSet<T>> powerSet(Set<T> set)
{
MutableSet<MutableSet<T>> seed = UnifiedSet.newSetWith(UnifiedSet.newSet());
return powerSetWithSeed(set, seed);
}
public static <T> MutableSet<MutableSet<T>> powerSet(UnifiedSetWithHashingStrategy<T> set)
{
MutableSet<MutableSet<T>> seed = UnifiedSet.newSetWith(set.newEmpty());
return powerSetWithSeed(set, seed);
}
private static <T> MutableSet<MutableSet<T>> powerSetWithSeed(Set<T> set, MutableSet<MutableSet<T>> seed)
{
return Iterate.injectInto(seed, set, (accumulator, element) -> SetIterables.union(accumulator, accumulator.collect(innerSet -> innerSet.clone().with(element))));
}
/**
* Returns an Immutable version of powerset where the inner sets are also immutable.
*/
public static <T> ImmutableSet<ImmutableSet<T>> immutablePowerSet(Set<T> set)
{
return powerSet(set).collect(MutableSet<T>::toImmutable).toImmutable();
}
public static <A, B> LazyIterable<Pair<A, B>> cartesianProduct(SetIterable<A> set1, SetIterable<B> set2)
{
return SetIterables.cartesianProduct(set1, set2, Tuples::pair);
}
public static <A, B, C> LazyIterable<C> cartesianProduct(SetIterable<A> set1, SetIterable<B> set2, Function2<A, B, C> function)
{
return LazyIterate.flatCollect(set1, first -> LazyIterate.collect(set2, second -> function.value(first, second)));
}
}