forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.java
More file actions
executable file
·349 lines (300 loc) · 9.23 KB
/
HashMap.java
File metadata and controls
executable file
·349 lines (300 loc) · 9.23 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package fj.data;
import fj.*;
import java.util.Collection;
import java.util.Iterator;
import static fj.P.p;
import static fj.data.Option.fromNull;
/**
* A mutable hash map providing O(1) lookup.
*
* @version %build.number%
* @see java.util.HashMap
*/
public final class HashMap<K, V> implements Iterable<K> {
private final class Key<K> {
private final K k;
private final Equal<K> e;
private final Hash<K> h;
Key(final K k, final Equal<K> e, final Hash<K> h) {
this.k = k;
this.e = e;
this.h = h;
}
K k() {
return k;
}
@SuppressWarnings({"unchecked"})
public boolean equals(final Object o) {
return o instanceof Key && e.eq(k, (K) ((Key<?>) o).k());
}
public int hashCode() {
return h.hash(k);
}
}
/**
* Returns an iterator for this map's keys. This method exists to permit the use in a <code>for</code>-each loop.
*
* @return A iterator for this map's keys.
*/
public Iterator<K> iterator() {
return keys().iterator();
}
private final java.util.HashMap<Key<K>, V> m;
private final Equal<K> e;
private final Hash<K> h;
/**
* Construct a hash map with the given equality and hashing strategy.
*
* @param e The equality strategy.
* @param h The hashing strategy.
*/
public HashMap(final Equal<K> e, final Hash<K> h) {
m = new java.util.HashMap<Key<K>, V>();
this.e = e;
this.h = h;
}
public HashMap(java.util.Map<K, V> map, final Equal<K> e, final Hash<K> h) {
this(e, h);
for (K key : map.keySet()) {
set(key, map.get(key));
}
}
/**
* Construct a hash map with the given equality and hashing strategy.
*
* @param e The equality strategy.
* @param h The hashing strategy.
* @param initialCapacity The initial capacity.
*/
public HashMap(final Equal<K> e, final Hash<K> h, final int initialCapacity) {
m = new java.util.HashMap<Key<K>, V>(initialCapacity);
this.e = e;
this.h = h;
}
public HashMap(java.util.Map<K, V> map) {
this(map, Equal.<K>anyEqual(), Hash.<K>anyHash());
}
/**
* Construct a hash map 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 HashMap(final Equal<K> e, final Hash<K> h, final int initialCapacity, final float loadFactor) {
m = new java.util.HashMap<Key<K>, V>(initialCapacity, loadFactor);
this.e = e;
this.h = h;
}
/**
* Construct a hash map that uses {@link Object#equals} and {@link Object#hashCode}.
*
* @return A new hash map that uses {@link Object#equals} and {@link Object#hashCode}.
*/
public static <K, V> HashMap<K, V> hashMap() {
final Equal<K> e = Equal.anyEqual();
final Hash<K> h = Hash.anyHash();
return new HashMap<K, V>(e, h);
}
/**
* Compare two key values for equality using the underlying equality strategy.
*
* @param k1 One key value to compare.
* @param k2 The other key value to compare.
* @return <code>true</code> if the two key values are equal, <code>false</code> otherwise.
*/
public boolean eq(final K k1, final K k2) {
return e.eq(k1, k2);
}
/**
* Compute the hash of the given key value using the underlying hashing strategy.
*
* @param k The key value to computer the hash of.
* @return The hash of the given key value.
*/
public int hash(final K k) {
return h.hash(k);
}
/**
* Returns a potential value that the given key maps to.
*
* @param k The key to look up in the hash map.
* @return A potential value for the given key.
*/
public Option<V> get(final K k) {
return fromNull(m.get(new Key<K>(k, e, h)));
}
/**
* A curried version of {@link #get(Object)}.
*
* @return A curried version of {@link #get(Object)}.
*/
public F<K, Option<V>> get() {
return new F<K, Option<V>>() {
public Option<V> f(final K k) {
return get(k);
}
};
}
/**
* Clear all entries from this hash map.
*/
public void clear() {
m.clear();
}
/**
* Determines if the given key value exists in this hash map.
*
* @param k The key value to look for in this hash map.
* @return <code>true</code> if this hash map contains the given key, <code>false</code> otherwise.
*/
public boolean contains(final K k) {
return m.containsKey(new Key<K>(k, e, h));
}
/**
* Returns all key entries in this hash map.
*
* @return All key entries in this hash map.
*/
public List<K> keys() {
final List.Buffer<K> b = new List.Buffer<K>();
for (final Key<K> k : m.keySet()) {
b.snoc(k.k());
}
return b.toList();
}
/**
* Returns all values in this hash map.
*
* @return All values in this hash map.
*/
public List<V> values() {
return keys().map(new F<K, V>() {
public V f(final K k) {
return m.get(new Key<K>(k, e, h));
}
});
}
/**
* Determines if this hash map has any entries.
*
* @return <code>true</code> if this hash map has no entries, <code>false</code> otherwise.
*/
public boolean isEmpty() {
return m.isEmpty();
}
/**
* Returns the number of entries in this hash map.
*
* @return The number of entries in this hash map.
*/
public int size() {
return m.size();
}
/**
* Inserts the given key and value association into the hash map.
*
* @param k The key to insert.
* @param v The value to insert.
*/
public void set(final K k, final V v) {
if (v != null) {
m.put(new Key<K>(k, e, h), v);
}
}
/**
* Deletes the entry in the hash map that corresponds to the given key.
*
* @param k The key to delete from this hash map.
*/
public void delete(final K k) {
m.remove(new Key<K>(k, e, h));
}
/**
* Deletes the entry in the hash map that corresponds to the given key and returns any associated value.
*
* @param k The key to delete from this hash map.
* @return The value that was associated with the given key, if there was one.
*/
public Option<V> getDelete(final K k) {
return fromNull(m.remove(new Key<K>(k, e, h)));
}
public <A, B> HashMap<A, B> map(F<K, A> keyFunction,
F<V, B> valueFunction,
Equal<A> equal, Hash<A> hash) {
final HashMap<A, B> hashMap = new HashMap<A, B>(equal, hash);
for (K key : keys()) {
final A newKey = keyFunction.f(key);
final B newValue = valueFunction.f(get(key).some());
hashMap.set(newKey, newValue);
}
return hashMap;
}
public <A, B> HashMap<A, B> map(F<K, A> keyFunction,
F<V, B> valueFunction) {
return map(keyFunction, valueFunction, Equal.<A>anyEqual(), Hash.<A>anyHash());
}
public <A, B> HashMap<A, B> map(F<P2<K, V>, P2<A, B>> function, Equal<A> equal, Hash<A> hash) {
return from(toStream().map(function), equal, hash);
}
public <A, B> HashMap<A, B> map(F<P2<K, V>, P2<A, B>> function) {
return from(toStream().map(function));
}
public <A> HashMap<A, V> mapKeys(F<K, A> keyFunction, Equal<A> equal, Hash<A> hash) {
return map(keyFunction, Function.<V>identity(), equal, hash);
}
public <A> HashMap<A, V> mapKeys(F<K, A> function) {
return mapKeys(function, Equal.<A>anyEqual(), Hash.<A>anyHash());
}
public <B> HashMap<K, B> mapValues(F<V, B> function) {
return map(Function.<K>identity(), function, e, h);
}
public void foreach(Effect<P2<K, V>> effect) {
toStream().foreach(effect);
}
public void foreach(F<P2<K, V>, Unit> function) {
toStream().foreach(function);
}
public List<P2<K, V>> toList() {
return keys().map(new F<K, P2<K, V>>() {
public P2<K, V> f(final K k) {
return p(k, get(k).some());
}
});
}
/**
* Projects an immutable collection of this hash map.
*
* @return An immutable collection of this hash map.
*/
public Collection<P2<K, V>> toCollection() {
return toList().toCollection();
}
public Stream<P2<K, V>> toStream() {
return toList().toStream();
}
public Option<P2<K, V>> toOption() {
return toList().toOption();
}
public Array<P2<K, V>> toArray() {
return toList().toArray();
}
public java.util.Map<K, V> toMap() {
final java.util.HashMap<K,V> result = new java.util.HashMap<K, V>();
for (K key : keys()) {
result.put(key, get(key).some());
}
return result;
}
public static <K, V> HashMap<K, V> from(Iterable<P2<K, V>> entries) {
return from(entries, Equal.<K>anyEqual(), Hash.<K>anyHash());
}
public static <K, V> HashMap<K, V> from(Iterable<P2<K, V>> entries, Equal<K> equal, Hash<K> hash) {
final HashMap<K, V> map = new HashMap<K, V>(equal, hash);
for (P2<K, V> entry : entries) {
map.set(entry._1(), entry._2());
}
return map;
}
}