forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashSet.java
More file actions
248 lines (217 loc) · 6.06 KB
/
HashSet.java
File metadata and controls
248 lines (217 loc) · 6.06 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package fj.data;
import fj.Equal;
import fj.Hash;
import fj.Unit;
import static fj.Equal.anyEqual;
import static fj.Hash.anyHash;
import static fj.Unit.unit;
import java.util.Collection;
import java.util.Iterator;
/**
* A mutable hash set that guarantees uniqueness of its elements providing O(1) lookup.
*
* @version %build.number%
* @see HashMap
*/
public final class HashSet<A> implements Iterable<A> {
/**
* Returns an iterator for this hash set. This method exists to permit the use in a <code>for</code>-each loop.
*
* @return A iterator for this hash set.
*/
public Iterator<A> iterator() {
return toCollection().iterator();
}
private final HashMap<A, Unit> m;
/**
* Construct a hash set with the given equality and hashing strategy.
*
* @param e The equality strategy.
* @param h The hashing strategy.
*/
public HashSet(final Equal<A> e, final Hash<A> h) {
m = new HashMap<>(e, h);
}
/**
* Construct a hash set with the given equality and hashing strategy.
*
* @param e The equality strategy.
* @param h The hashing strategy.
* @param initialCapacity The initial capacity.
*/
public HashSet(final Equal<A> e, final Hash<A> h, final int initialCapacity) {
m = new HashMap<>(e, h, initialCapacity);
}
/**
* Construct a hash set with the given equality and hashing strategy.
*
* @param e The equality strategy.
* @param h The hashing strategy.
* @param initialCapacity The initial capacity.
* @param loadFactor The load factor.
*/
public HashSet(final Equal<A> e, final Hash<A> h, final int initialCapacity, final float loadFactor) {
m = new HashMap<>(e, h, initialCapacity, loadFactor);
}
/**
* Compare two values for equality using the underlying equality strategy.
*
* @param a1 One value to compare.
* @param a2 The other value to compare.
* @return <code>true</code> if the two values are equal, <code>false</code> otherwise.
*/
public boolean eq(final A a1, final A a2) {
return m.eq(a1, a2);
}
/**
* Compute the hash of the given value using the underlying hashing strategy.
*
* @param a The value to computer the hash of.
* @return The hash of the given value.
*/
public int hash(final A a) {
return m.hash(a);
}
/**
* Creates a new HashSet using the given Equal and Hash
*/
public static <A> HashSet<A> empty(final Equal<A> e, final Hash<A> h) {
return new HashSet<>(e, h);
}
/**
* Creates an empty HashSet
*/
public static <A> HashSet<A> empty() {
return empty(anyEqual(), anyHash());
}
/**
* Create a HashSet from the Iterable.
*/
public static <A> HashSet<A> iterableHashSet(final Iterable<A> it) {
return iterableHashSet(anyEqual(), anyHash(), it);
}
/**
* Create a HashSet from the Iterable.
*/
public static <A> HashSet<A> iterableHashSet(final Equal<A> e, final Hash<A> h, final Iterable<A> it) {
final HashSet<A> hs = empty(e, h);
for (A a: it) {
hs.set(a);
}
return hs;
}
/**
* Create a HashSet from the Iterator.
*/
public static <A> HashSet<A> iteratorHashSet(final Iterator<A> it) {
return iterableHashSet(() -> it);
}
/**
* Create a HashSet from the Iterator.
*/
public static <A> HashSet<A> iteratorHashSet(final Equal<A> e, final Hash<A> h, final Iterator<A> it) {
return iterableHashSet(e, h, () -> it);
}
/**
* Create a HashSet from the array.
*/
@SafeVarargs
public static <A> HashSet<A> arrayHashSet(final A...as) {
return iterableHashSet(Array.array(as));
}
/**
* Create a HashSet from the array.
*/
@SafeVarargs
public static <A> HashSet<A> arrayHashSet(final Equal<A> e, final Hash<A> h, final A...as) {
return iterableHashSet(e, h, Array.array(as));
}
/**
* Create a HashSet from the array.
*/
@SafeVarargs
public static <A> HashSet<A> hashSet(final A...as) {
return arrayHashSet(as);
}
/**
* Create a HashSet from the array.
*/
@SafeVarargs
public static <A> HashSet<A> hashSet(final Equal<A> e, final Hash<A> h, final A...as) {
return arrayHashSet(e, h, as);
}
/**
* Determines if this hash set contains the given element.
*
* @param a The element to look for in this hash set.
* @return <code>true</code> if this hash set contains the given element, <code>false</code> otherwise.
*/
public boolean contains(final A a) {
return m.contains(a);
}
/**
* Insert the given element into this hash set.
*
* @param a The element to insert.
*/
public void set(final A a) {
m.set(a, unit());
}
/**
* Clear all elements from this hash set.
*/
public void clear() {
m.clear();
}
/**
* Determines if this hash set contains any elements.
*
* @return <code>true</code> if this hash set contains no elements, <code>false</code> otherwise.
*/
public boolean isEmpty() {
return m.isEmpty();
}
/**
* Returns the number of entries in this hash set.
*
* @return The number of entries in this hash set.
*/
public int size() {
return m.size();
}
/**
* Deletes the given element from this hash set.
*
* @param a The element to delete from this hash set.
* @return <code>true</code> if this hash set contained the given element prior to deletion, <code>false</code>
* otherwise.
*/
public boolean delete(final A a) {
return m.getDelete(a).isSome();
}
/**
* Returns a list projection of this hash set.
*
* @return A list projection of this hash set.
*/
public List<A> toList() {
return m.keys();
}
public java.util.List<A> toJavaList() {
return toList().toJavaList();
}
public java.util.Set<A> toJavaSet() {
return new java.util.HashSet<>(toCollection());
}
public static <A> HashSet<A> fromSet(java.util.Set<A> s) {
return iterableHashSet(s);
}
/**
* Projects an immutable collection of this hash set.
*
* @return An immutable collection of this hash set.
*/
public Collection<A> toCollection() {
return toList().toCollection();
}
}