Debian Codegraph @

net/sf/saxon/z/IntHashMap.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.z;
9
10 import java.io.PrintStream;
11 import java.util.Iterator;
12 import java.util.NoSuchElementException;
13
14 /**
15 * A hash table that maps int keys to Object values.
16 *
17 * @author Dave Hale, Landmark Graphics
18 * @author Dominique Devienne
19 * @author Michael Kay: retrofitted to JDK 1.4, added iterator(), modified to disallow null values
20 * Reverted to generics July 2008.
21 */
22
23 public class IntHashMap<T> {
24
25 /**
26 * Initializes a map with a capacity of 8 and a load factor of 0,25.
27 */
28 public IntHashMap() {
29 this(8, 0.25);
30 }
31
32 /**
33 * Initializes a map with the given capacity and a load factor of 0,25.
34 *
35 * @param capacity the initial capacity.
36 */
37 public IntHashMap(int capacity) {
38 this(capacity, 0.25);
39 }
40
41 /**
42 * Constructs a new map with initial capacity, and load factor.
43 * <p>The capacity is the number of keys that can be mapped without resizing
44 * the arrays in which keys and values are stored. For efficiency, only
45 * a fraction of the elements in those arrays are used. That fraction is
46 * the specified load factor. The initial length of the arrays equals the
47 * smallest power of two not less than the ratio capacity/factor. The
48 * capacity of the map is increased, as necessary. The maximum number
49 * of keys that can be mapped is 2^30.</p>
50 *
51 * @param capacity the initial capacity.
52 * @param factor the load factor.
53 */
54 public IntHashMap(int capacity, double factor) {
55 _factor = factor;
56 setCapacity(capacity);
57 }
58
59 /**
60 * Clears the map.
61 */
62 public void clear() {
63 _n = 0;
64 for (int i = 0; i < _nmax; ++i) {
65 _value[i] = null;
66 }
67 }
68
69 /**
70 * Gets the value for this key.
71 *
72 * @param key Key
73 * @return the value, null if not found.
74 */
75 public T get(int key) {
76 return _value[indexOf(key)];
77 }
78
79 /**
80 * Gets the size of the map.
81 *
82 * @return the size (the number of entries in the map)
83 */
84 public int size() {
85 return _n;
86 }
87
88 /**
89 * Removes a key from the map.
90 *
91 * @param key Key to remove
92 * @return true if the value was removed
93 */
94 public boolean remove(int key) {
95 // Knuth, v. 3, 527, Algorithm R.
96 int i = indexOf(key);
97 //if (!_filled[i]) {
98 if (_value[i] == null) {
99 return false;
100 }
101 --_n;
102 for (; ; ) {
103 //_filled[i] = false;
104 _value[i] = null;
105 int j = i;
106 int r;
107 do {
108 i = (i - 1) & _mask;
109 //if (!_filled[i]) {
110 if (_value[i] == null) {
111 return true;
112 }
113 r = hash(_key[i]);
114 } while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
115 _key[j] = _key[i];
116 _value[j] = _value[i];
117 //_filled[j] = _filled[i];
118 }
119 }
120
121 /**
122 * Adds a key-value pair to the map.
123 *
124 * @param key Key
125 * @param value Value
126 * @return the value that was previously associated with the key, or null if there was no previous value
127 */
128 public T put(int key, /*@Nullable*/ T value) {
129 if (value == null) {
130 throw new NullPointerException("IntHashMap does not allow null values");
131 }
132 int i = indexOf(key);
133 T old = _value[i];
134 if (old != null) {
135 _value[i] = value;
136 } else {
137 _key[i] = key;
138 _value[i] = value;
139 grow();
140 }
141 return old;
142 }
143
144
145 ///////////////////////////////////////////////////////////////////////////
146 // private
147
148 private static final int NBIT = 30; // NMAX = 2^NBIT
149 private static final int NMAX = 1 << NBIT; // maximum number of keys mapped
150 private double _factor; // 0.0 <= _factor <= 1.0
151 private int _nmax; // 0 <= _nmax = 2^nbit <= 2^NBIT = NMAX
152 private int _n; // 0 <= _n <= _nmax <= NMAX
153 private int _nlo; // _nmax*_factor (_n<=_nlo, if possible)
154 private int _nhi; // NMAX*_factor (_n< _nhi, if possible)
155 private int _shift; // _shift = 1 + NBIT - nbit (see function hash() below)
156 private int _mask; // _mask = _nmax - 1
157 private int[] _key; // array[_nmax] of keys
158 //@SuppressWarnings(value = {"unchecked"})
159 private T[] _value; // array[_nmax] of values
160 //private boolean[] _filled; // _filled[i]==true iff _key[i] is mapped
161
162 private int hash(int key) {
163 // Knuth, v. 3, 509-510. Randomize the 31 low-order bits of c*key
164 // and return the highest nbits (where nbits <= 30) bits of these.
165 // The constant c = 1327217885 approximates 2^31 * (sqrt(5)-1)/2.
166 return ((1327217885 * key) >> _shift) & _mask;
167 }
168
169 private int indexOf(int key) {
170 int i = hash(key);
171 //while (_filled[i]) {
172 while (_value[i] != null) {
173 if (_key[i] == key) {
174 return i;
175 }
176 i = (i - 1) & _mask;
177 }
178 return i;
179 }
180
181 private void grow() {
182 ++_n;
183 if (_n > NMAX) {
184 throw new RuntimeException("number of keys mapped exceeds " + NMAX);
185 }
186 if (_nlo < _n && _n <= _nhi) {
187 setCapacity(_n);
188 }
189 }
190
191 private void setCapacity(int capacity) {
192 if (capacity < _n) {
193 capacity = _n;
194 }
195 double factor = (_factor < 0.01) ? 0.01 : (_factor > 0.99) ? 0.99 : _factor;
196 int nbit, nmax;
197 for (nbit = 1, nmax = 2; nmax * factor < capacity && nmax < NMAX; ++nbit, nmax *= 2) {
198 // no-op
199 }
200 int nold = _nmax;
201 if (nmax == nold) {
202 return;
203 }
204 _nmax = nmax;
205 _nlo = (int) (nmax * factor);
206 _nhi = (int) (NMAX * factor);
207 _shift = 1 + NBIT - nbit;
208 _mask = nmax - 1;
209 int[] key = _key;
210 T[] value = _value;
211 //boolean[] filled = _filled;
212 _n = 0;
213 _key = new int[nmax];
214 // semantically equivalent to _value = new V[nmax]
215 _value = (T[]) new Object[nmax];
216 //_filled = new boolean[nmax];
217 if (key != null) {
218 for (int i = 0; i < nold; ++i) {
219 //if (filled[i]) {
220 if (value[i] != null) {
221 put(key[i], value[i]);
222 }
223 }
224 }
225 }
226
227 /**
228 * Get an iterator over the keys
229 *
230 * @return an iterator over the integer keys in the map
231 */
232
233 public IntIterator keyIterator() {
234 return new IntHashMapKeyIterator();
235 }
236
237 /**
238 * Get an iterator over the values
239 *
240 * @return an iterator over the values in the map
241 */
242
243 public Iterator<T> valueIterator() {
244 return new IntHashMapValueIterator();
245 }
246
247 /**
248 * Get the set of values
249 * @return the set of values as an iterable collection
250 */
251
252 public Iterable<T> valueSet() {
253 return new Iterable<T>() {
254 public Iterator<T> iterator() {
255 return valueIterator();
256 }
257 };
258 }
259
260 /**
261 * Create a copy of the IntHashMap
262 *
263 * @return a copy of this map
264 */
265
266 public IntHashMap<T> copy() {
267 IntHashMap<T> n = new IntHashMap<T>(size());
268 IntIterator it = keyIterator();
269 while (it.hasNext()) {
270 int k = it.next();
271 n.put(k, get(k));
272 }
273 return n;
274 }
275
276 /**
277 * Diagnostic display of contents
278 */
279
280 public void display(PrintStream ps) {
281 IntIterator iter = new IntHashMapKeyIterator();
282 while (iter.hasNext()) {
283 int key = iter.next();
284 Object value = get(key);
285 ps.println(key + " -> " + value.toString());
286 }
287 }
288
289 /**
290 * Iterator over keys
291 */
292 private class IntHashMapKeyIterator implements IntIterator {
293
294 private int i = 0;
295
296 public IntHashMapKeyIterator() {
297 i = 0;
298 }
299
300 public boolean hasNext() {
301 while (i < _key.length) {
302 if (_value[i] != null) {
303 return true;
304 } else {
305 i++;
306 }
307 }
308 return false;
309 }
310
311 public int next() {
312 return _key[i++];
313 }
314 }
315
316 /**
317 * Iterator over values
318 */
319 private class IntHashMapValueIterator implements Iterator<T> {
320
321 private int i = 0;
322
323 public IntHashMapValueIterator() {
324 i = 0;
325 }
326
327 public boolean hasNext() {
328 while (i < _key.length) {
329 if (_value[i] != null) {
330 return true;
331 } else {
332 i++;
333 }
334 }
335 return false;
336 }
337
338 public T next() {
339 T temp = _value[i++];
340 if (temp == null) {
341 throw new NoSuchElementException();
342 }
343 return temp;
344 }
345
346 /**
347 * Removes from the underlying collection the last element returned by the
348 * iterator (optional operation).
349 *
350 * @throws UnsupportedOperationException if the <tt>remove</tt>
351 * operation is not supported by this Iterator.
352 */
353 public void remove() {
354 throw new UnsupportedOperationException("remove");
355 }
356 }
357
358 /**
359 * Get the set of integer keys present in this IntHashSet
360 *
361 * @return the set of integer keys present in this IntHashSet
362 */
363
364 public IntSet keySet() {
365 return new IntSet() {
366 public void clear() {
367 throw new UnsupportedOperationException("Immutable set");
368 }
369
370 public IntSet copy() {
371 IntHashSet s = new IntHashSet();
372 IntIterator ii = iterator();
373 while (ii.hasNext()) {
374 s.add(ii.next());
375 }
376 return s;
377 }
378
379 public IntSet mutableCopy() {
380 return copy();
381 }
382
383 public int size() {
384 return _n;
385 }
386
387 public boolean isEmpty() {
388 return _n == 0;
389 }
390
391 public boolean contains(int key) {
392 return _value[indexOf(key)] != null;
393 }
394
395 public boolean remove(int value) {
396 throw new UnsupportedOperationException("Immutable set");
397 }
398
399 public boolean add(int value) {
400 throw new UnsupportedOperationException("Immutable set");
401 }
402
403 public IntIterator iterator() {
404 return new IntHashMapKeyIterator();
405 }
406
407 public IntSet union(IntSet other) {
408 return copy().union(other);
409 }
410
411 public IntSet intersect(IntSet other) {
412 return copy().intersect(other);
413 }
414
415 public IntSet except(IntSet other) {
416 return copy().except(other);
417 }
418
419 public boolean containsAll(IntSet other) {
420 return copy().containsAll(other);
421 }
422
423 public String toString() {
424 return IntHashSet.toString(iterator());
425 }
426 };
427 }
428
429 }
430