Debian Codegraph @

net/sf/saxon/ma/trie/ImmutableHashTrieMap.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.ma.trie;
9
10 import java.util.Collections;
11 import java.util.Iterator;
12 import java.util.LinkedList;
13 import java.util.List;
14
15 // Original author: Michael Froh (published on Github). Released under MPL 2.0
16 // by Saxonica Limited with permission from the author
17
18 public abstract class ImmutableHashTrieMap<K, V>
19 implements ImmutableMap<K,V>, Iterable<Tuple2<K, V>> {
20
21 private static final ImmutableHashTrieMap EMPTY_NODE = new EmptyHashNode();
22
23 public static <K, V> ImmutableHashTrieMap<K, V> empty() {
24 return EMPTY_NODE;
25 }
26
27 private static <K> int getBucket(int shift, K key) {
28 return key.hashCode() >> shift & MASK;
29 }
30
31
32 public ImmutableHashTrieMap<K, V> put(K key, V value) {
33 return put(0, key, value);
34 }
35
36
37 public ImmutableHashTrieMap<K, V> remove(K key) {
38 return remove(0, key);
39 }
40
41 public V get(K key) {
42 return get(0, key);
43 }
44
45 /*
46 * In the methods below, "shift" denotes how far (in bits) through the
47 * hash code we are currently looking. At each level, we add "bits",
48 * defined below, to shift.
49 */
50
51 private static final int BITS = 5;
52 private static final int FANOUT = 1 << BITS;
53 private static final int MASK = FANOUT - 1;
54
55 abstract ImmutableHashTrieMap<K, V> put(int shift, K key,
56 V value);
57
58 abstract ImmutableHashTrieMap<K, V> remove(int shift, K key);
59
60 abstract V get(int shift, K key);
61
62 abstract boolean isArrayNode();
63
64 /**
65 * Implementation for an empty map
66 * @param <K> the key type
67 * @param <V> the value type
68 */
69
70 private static class EmptyHashNode<K, V>
71 extends ImmutableHashTrieMap<K, V> {
72 @Override
73 ImmutableHashTrieMap<K, V> put(final int shift, final K key,
74 final V value) {
75 return new EntryHashNode<>(key, value);
76 }
77
78 @Override
79 ImmutableHashTrieMap<K, V> remove(final int shift,
80 final K key) {
81 return this;
82 }
83
84 @Override
85 boolean isArrayNode() {
86 return false;
87 }
88
89 @Override
90 V get(final int shift, final K key) {
91 return null;
92 }
93
94 public Iterator<Tuple2<K, V>> iterator() {
95 return Collections.<Tuple2<K, V>>emptySet().iterator();
96 }
97 }
98
99 /**
100 * Implementation for a single-entry map
101 * @param <K> the key type
102 * @param <V> the value type
103 */
104
105 private static class EntryHashNode<K, V>
106 extends ImmutableHashTrieMap<K, V> {
107 private final K key;
108 private final V value;
109
110 private EntryHashNode(final K key,
111 final V value) {
112 this.key = key;
113 this.value = value;
114 }
115
116 @Override
117 ImmutableHashTrieMap<K, V> put(final int shift, final K key,
118 final V value) {
119 if (this.key.equals(key)) {
120 // Overwriting this entry
121 return new EntryHashNode<>(key, value);
122 } else if (this.key.hashCode() == key.hashCode()) {
123 // This is a collision. Return a new ListHashNode.
124 return new ListHashNode<>(new Tuple2<>(this.key, this.value),
125 new Tuple2<>(key, value));
126 }
127 // Split this node into an ArrayHashNode with this and the new value
128 // as entries.
129 return newArrayHashNode(shift, this.key.hashCode(), this,
130 key.hashCode(), new EntryHashNode<K, V>(key, value));
131 }
132
133 @Override
134 ImmutableHashTrieMap<K, V> remove(final int shift,
135 final K key) {
136 if (this.key.equals(key)) {
137 return empty();
138 }
139 return this;
140 }
141
142 @Override
143 boolean isArrayNode() {
144 return false;
145 }
146
147 @Override
148 V get(final int shift, final K key) {
149 if (this.key.equals(key)) {
150 return value;
151 }
152 return null;
153 }
154
155 public Iterator<Tuple2<K, V>> iterator() {
156 return Collections.singleton(new Tuple2<>(key, value)).iterator();
157 }
158 }
159
160 /**
161 * Implementation for a set of entries where the keys of all entries share the same hash code
162 * @param <K> the key type
163 * @param <V> the value type
164 */
165
166 private static class ListHashNode<K, V> extends ImmutableHashTrieMap<K, V> {
167 private final ImmutableList<Tuple2<K, V>> entries;
168
169 public ListHashNode(Tuple2<K, V> entry1,
170 Tuple2<K, V> entry2) {
171 // These entries must collide
172 assert entry1._1.hashCode() == entry2._1.hashCode();
173 entries = ImmutableList.<Tuple2<K, V>>empty().prepend(entry1)
174 .prepend(entry2);
175 }
176
177 private ListHashNode(final ImmutableList<Tuple2<K, V>> entries) {
178 // Size should be at least 2
179 assert !entries.isEmpty();
180 assert !entries.tail().isEmpty();
181 this.entries = entries;
182 }
183
184 @Override
185 ImmutableHashTrieMap<K, V> put(final int shift, final K key,
186 final V value) {
187 if (entries.head()._1.hashCode() != key.hashCode()) {
188 return newArrayHashNode(shift,
189 entries.head()._1.hashCode(),
190 this,
191 key.hashCode(),
192 new EntryHashNode<>(
193 key, value));
194 }
195 ImmutableList<Tuple2<K, V>> newList = ImmutableList.empty();
196 boolean found = false;
197 for (Tuple2<K, V> entry : entries) {
198 if (entry._1.equals(key)) {
199 // Node replacement
200 newList =
201 newList.prepend(new Tuple2<>(key, value));
202 found = true;
203 } else {
204 newList = newList.prepend(entry);
205 }
206 }
207 if (!found) {
208 // Adding a new entry
209 newList = newList.prepend(new Tuple2<>(key, value));
210 }
211 return new ListHashNode<>(newList);
212 }
213
214 @Override
215 ImmutableHashTrieMap<K, V> remove(final int shift,
216 final K key) {
217 ImmutableList<Tuple2<K, V>> newList = ImmutableList.empty();
218 int size = 0;
219 for (Tuple2<K, V> entry : entries) {
220 if (!entry._1.equals(key)) {
221 newList = newList.prepend(entry);
222 size++;
223 }
224 }
225 if (size == 1) {
226 Tuple2<K, V> entry = newList.head();
227 return new EntryHashNode<>(entry._1, entry._2);
228 }
229 return new ListHashNode<>(newList);
230 }
231
232 @Override
233 boolean isArrayNode() {
234 return false;
235 }
236
237 @Override
238 V get(final int shift, final K key) {
239 for (Tuple2<K, V> entry : entries) {
240 if (entry._1.equals(key)) {
241 return entry._2;
242 }
243 }
244 return null;
245 }
246
247 public Iterator<Tuple2<K, V>> iterator() {
248 return new Iterator<Tuple2<K, V>>() {
249 private ImmutableList<Tuple2<K, V>> curList =
250 ListHashNode.this.entries;
251
252 public boolean hasNext() {
253 return !curList.isEmpty();
254 }
255
256 public Tuple2<K, V> next() {
257 Tuple2<K, V> retVal = curList.head();
258 curList = curList.tail();
259 return retVal;
260 }
261
262 public void remove() {
263 throw new UnsupportedOperationException();
264 }
265 };
266 }
267 }
268
269 /**
270 * Create a new node combining two existing nodes
271 * @param shift the shift to be applied to the hash code at this level in the tree
272 * @param hash1 the hash code of the first node
273 * @param subNode1 the first node
274 * @param hash2 the hash code of the second node
275 * @param subNode2 the second node
276 * @param <K> the key type
277 * @param <V> the value type
278 * @return the new node
279 */
280
281 private static <K, V> ImmutableHashTrieMap<K, V>
282 newArrayHashNode(int shift,
283 int hash1,
284 ImmutableHashTrieMap<K, V> subNode1,
285 int hash2,
286 ImmutableHashTrieMap<K, V> subNode2) {
287 int curShift = shift;
288 int h1 = hash1 >> shift & MASK;
289 int h2 = hash2 >> shift & MASK;
290 List<Integer> buckets = new LinkedList<>();
291 while (h1 == h2) {
292 buckets.add(0, h1);
293 curShift += BITS;
294 h1 = hash1 >> curShift & MASK;
295 h2 = hash2 >> curShift & MASK;
296 }
297 ImmutableHashTrieMap<K, V> newNode =
298 new BranchedArrayHashNode<K,V>(h1, subNode1, h2, subNode2);
299 for (Integer bucket : buckets) {
300 newNode = new SingletonArrayHashNode<K,V>(bucket, newNode);
301 }
302 return newNode;
303
304 }
305
306 private static abstract class ArrayHashNode<K,V>
307 extends ImmutableHashTrieMap<K,V> {
308 @Override
309 boolean isArrayNode() {
310 return true;
311 }
312 }
313
314 private static class BranchedArrayHashNode<K, V>
315 extends ArrayHashNode<K, V> {
316 private final ImmutableHashTrieMap<K, V>[] subnodes;
317 private final int size;
318
319 public BranchedArrayHashNode(int h1,
320 ImmutableHashTrieMap<K, V> subNode1,
321 int h2,
322 ImmutableHashTrieMap<K, V> subNode2) {
323 assert h1 != h2;
324 size = 2;
325 subnodes = new ImmutableHashTrieMap[FANOUT];
326 for (int i = 0; i < FANOUT; i++) {
327 if (i == h1) {
328 subnodes[i] = subNode1;
329 } else if (i == h2) {
330 subnodes[i] = subNode2;
331 } else {
332 subnodes[i] = EMPTY_NODE;
333 }
334 }
335 }
336
337 public BranchedArrayHashNode(int size,
338 final ImmutableHashTrieMap<K, V>[] subnodes) {
339 assert subnodes.length == FANOUT;
340 this.size = size;
341 this.subnodes = subnodes;
342 }
343
344 @Override
345 ImmutableHashTrieMap<K, V> put(final int shift, final K key,
346 final V value) {
347 final int bucket = getBucket(shift, key);
348 ImmutableHashTrieMap<K, V>[] newNodes = new ImmutableHashTrieMap[FANOUT];
349 System.arraycopy(subnodes, 0, newNodes, 0, FANOUT);
350
351 final int newSize =
352 newNodes[bucket] == EMPTY_NODE ? size + 1 : size;
353 newNodes[bucket] = newNodes[bucket].put(shift + BITS,
354 key, value);
355 return new BranchedArrayHashNode<K, V>(newSize, newNodes);
356 }
357
358 @Override
359 ImmutableHashTrieMap<K, V> remove(final int shift,
360 final K key) {
361 final int bucket = getBucket(shift, key);
362 if (subnodes[bucket] == EMPTY_NODE) {
363 return this;
364 }
365 ImmutableHashTrieMap<K, V>[] newNodes = new ImmutableHashTrieMap[FANOUT];
366 System.arraycopy(subnodes, 0, newNodes, 0, FANOUT);
367 newNodes[bucket] = newNodes[bucket].remove(shift + BITS,
368 key);
369 final int newSize =
370 newNodes[bucket] == EMPTY_NODE ? size - 1 : size;
371 if (newSize == 1) {
372 int orphanedBucket = -1;
373 for (int i = 0; i < FANOUT; i++) {
374 if (newNodes[i] != EMPTY_NODE) {
375 orphanedBucket = i;
376 break;
377 }
378 }
379 ImmutableHashTrieMap<K, V> orphanedEntry =
380 subnodes[orphanedBucket];
381 if (orphanedEntry.isArrayNode()) {
382 return new SingletonArrayHashNode<>(orphanedBucket, orphanedEntry);
383 }
384 return orphanedEntry;
385 }
386 return new BranchedArrayHashNode<>(newSize, newNodes);
387 }
388
389 @Override
390 V get(final int shift, final K key) {
391 final int bucket = getBucket(shift, key);
392 return subnodes[bucket].get(shift + BITS, key);
393 }
394
395 public Iterator<Tuple2<K, V>> iterator() {
396 return new Iterator<Tuple2<K, V>>() {
397 private int bucket = 0;
398 private Iterator<Tuple2<K, V>> childIterator =
399 subnodes[0].iterator();
400
401 public boolean hasNext() {
402 if (childIterator.hasNext()) {
403 return true;
404 }
405 bucket++;
406 while (bucket < FANOUT) {
407 childIterator = subnodes[bucket].iterator();
408 if (childIterator.hasNext()) {
409 return true;
410 }
411 bucket++;
412 }
413 return false;
414 }
415
416 public Tuple2<K, V> next() {
417 return childIterator.next();
418 }
419
420 public void remove() {
421 throw new UnsupportedOperationException();
422 }
423 };
424 }
425 }
426
427 private static class SingletonArrayHashNode<K, V> extends
428 ArrayHashNode<K, V> {
429 private final int bucket;
430 private final ImmutableHashTrieMap<K, V> subnode;
431
432 private SingletonArrayHashNode(final int bucket,
433 final ImmutableHashTrieMap<K, V> subnode) {
434 assert subnode instanceof ArrayHashNode;
435 this.bucket = bucket;
436 this.subnode = subnode;
437 }
438
439 @Override
440 ImmutableHashTrieMap<K, V> put(final int shift, final K key,
441 final V value) {
442 final int bucket = getBucket(shift, key);
443 if (bucket == this.bucket) {
444 return new SingletonArrayHashNode<>(bucket,
445 subnode.put(shift + BITS, key, value));
446 }
447 return new BranchedArrayHashNode<>(this.bucket, subnode,
448 bucket, new EntryHashNode<>(key, value));
449 }
450
451 @Override
452 ImmutableHashTrieMap<K, V> remove(final int shift, final K key) {
453 final int bucket = getBucket(shift, key);
454 if (bucket == this.bucket) {
455 ImmutableHashTrieMap<K, V> newNode =
456 subnode.remove(shift + BITS, key);
457 if (!newNode.isArrayNode()) {
458 return newNode;
459 }
460 return new SingletonArrayHashNode<>(bucket, newNode);
461 }
462 return this;
463 }
464
465 @Override
466 V get(final int shift, final K key) {
467 final int bucket = getBucket(shift, key);
468 if (bucket == this.bucket) {
469 return subnode.get(shift + BITS, key);
470 }
471 return null;
472 }
473
474 public Iterator<Tuple2<K, V>> iterator() {
475 return subnode.iterator();
476 }
477 }
478
479 }