Debian Codegraph @

net/sf/saxon/z/IntToIntHashMap.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 net.sf.saxon.tree.util.FastStringBuffer;
11
12 /**
13 * A hash table that maps int keys to int values.
14 *
15 * @author Dave Hale, Landmark Graphics
16 * @author Dominique Devienne
17 * @author Michael Kay: created this class based on IntHashMap
18 */
19 public class IntToIntHashMap implements IntToIntMap {
20
21 /**
22 * Initializes a map with a capacity of 8 and a load factor of 0,25.
23 */
24 public IntToIntHashMap() {
25 this(8, 0.25);
26 }
27
28 /**
29 * Initializes a map with the given capacity and a load factor of 0,25.
30 *
31 * @param capacity the initial capacity.
32 */
33 public IntToIntHashMap(int capacity) {
34 this(capacity, 0.25);
35 }
36
37 /**
38 * Constructs a new map with initial capacity, and load factor.
39 * <p>The capacity is the number of keys that can be mapped without resizing
40 * the arrays in which keys and values are stored. For efficiency, only
41 * a fraction of the elements in those arrays are used. That fraction is
42 * the specified load factor. The initial length of the arrays equals the
43 * smallest power of two not less than the ratio capacity/factor. The
44 * capacity of the map is increased, as necessary. The maximum number
45 * of keys that can be mapped is 2^30.</p>
46 *
47 * @param capacity the initial capacity.
48 * @param factor the load factor.
49 */
50 public IntToIntHashMap(int capacity, double factor) {
51 _factor = factor;
52 setCapacity(capacity);
53 }
54
55 /**
56 * Set the value to be returned to indicate an unused entry
57 *
58 * @param defaultValue the value to be returned by {@link #get(int)} if no entry
59 * exists for the supplied key
60 */
61 public void setDefaultValue(int defaultValue) {
62 _defaultValue = defaultValue;
63 }
64
65 /**
66 * Get the default value used to indicate an unused entry
67 *
68 * @return the value to be returned by {@link #get(int)} if no entry
69 * exists for the supplied key
70 */
71
72 public int getDefaultValue() {
73 return _defaultValue;
74 }
75
76 /**
77 * Clears the map.
78 */
79 public void clear() {
80 _n = 0;
81 for (int i = 0; i < _nmax; ++i) {
82 _filled[i] = false;
83 }
84 }
85
86 /**
87 * Finds a key in the map.
88 *
89 * @param key Key
90 * @return true if the key is mapped
91 */
92 public boolean find(int key) {
93 return _filled[indexOf(key)];
94 }
95
96 /**
97 * Gets the value for this key.
98 *
99 * @param key Key
100 * @return the value, or the default value if not found.
101 */
102 public int get(int key) {
103 int i = indexOf(key);
104 return _filled[i] ? _value[i] : _defaultValue;
105 }
106
107 /**
108 * Gets the size of the map.
109 *
110 * @return the size
111 */
112 public int size() {
113 return _n;
114 }
115
116 /**
117 * Removes a key from the map.
118 *
119 * @param key Key to remove
120 * @return true if the value was removed
121 */
122 public boolean remove(int key) {
123 // Knuth, v. 3, 527, Algorithm R.
124 int i = indexOf(key);
125 if (!_filled[i]) {
126 return false;
127 }
128 --_n;
129 for (; ; ) {
130 _filled[i] = false;
131 int j = i;
132 int r;
133 do {
134 i = (i - 1) & _mask;
135 if (!_filled[i]) {
136 return true;
137 }
138 r = hash(_key[i]);
139 } while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
140 _key[j] = _key[i];
141 _value[j] = _value[i];
142 _filled[j] = _filled[i];
143 }
144 }
145
146 /**
147 * Adds a key-value pair to the map.
148 *
149 * @param key Key
150 * @param value Value
151 */
152 public void put(int key, int value) {
153 int i = indexOf(key);
154 if (_filled[i]) {
155 _value[i] = value;
156 } else {
157 _key[i] = key;
158 _value[i] = value;
159 _filled[i] = true;
160 grow();
161 }
162 }
163
164 /**
165 * Get an iterator over the integer key values held in the hash map
166 *
167 * @return an iterator whose next() call returns the key values (in arbitrary order)
168 */
169
170 /*@NotNull*/
171 public IntIterator keyIterator() {
172 return new IntToIntHashMapKeyIterator();
173 }
174
175
176 ///////////////////////////////////////////////////////////////////////////
177 // private
178
179 private static final int NBIT = 30; // NMAX = 2^NBIT
180 private static final int NMAX = 1 << NBIT; // maximum number of keys mapped
181 private double _factor; // 0.0 <= _factor <= 1.0
182 private int _defaultValue = Integer.MAX_VALUE;
183 private int _nmax; // 0 <= _nmax = 2^nbit <= 2^NBIT = NMAX
184 private int _n; // 0 <= _n <= _nmax <= NMAX
185 private int _nlo; // _nmax*_factor (_n<=_nlo, if possible)
186 private int _nhi; // NMAX*_factor (_n< _nhi, if possible)
187 private int _shift; // _shift = 1 + NBIT - nbit (see function hash() below)
188 private int _mask; // _mask = _nmax - 1
189 private int[] _key; // array[_nmax] of keys
190 //@SuppressWarnings(value = {"unchecked"})
191 private int[] _value; // array[_nmax] of values
192 private boolean[] _filled; // _filled[i]==true iff _key[i] is mapped
193
194 private int hash(int key) {
195 // Knuth, v. 3, 509-510. Randomize the 31 low-order bits of c*key
196 // and return the highest nbits (where nbits <= 30) bits of these.
197 // The constant c = 1327217885 approximates 2^31 * (sqrt(5)-1)/2.
198 return ((1327217885 * key) >> _shift) & _mask;
199 }
200
201 private int indexOf(int key) {
202 int i = hash(key);
203 while (_filled[i]) {
204 if (_key[i] == key) {
205 return i;
206 }
207 i = (i - 1) & _mask;
208 }
209 return i;
210 }
211
212 private void grow() {
213 ++_n;
214 if (_n > NMAX) {
215 throw new RuntimeException("number of keys mapped exceeds " + NMAX);
216 }
217 if (_nlo < _n && _n <= _nhi) {
218 setCapacity(_n);
219 }
220 }
221
222 private void setCapacity(int capacity) {
223 if (capacity < _n) {
224 capacity = _n;
225 }
226 double factor = (_factor < 0.01) ? 0.01 : (_factor > 0.99) ? 0.99 : _factor;
227 int nbit, nmax;
228 for (nbit = 1, nmax = 2; nmax * factor < capacity && nmax < NMAX; ++nbit, nmax *= 2) {
229 // no-op
230 }
231 int nold = _nmax;
232 if (nmax == nold) {
233 return;
234 }
235 _nmax = nmax;
236 _nlo = (int) (nmax * factor);
237 _nhi = (int) (NMAX * factor);
238 _shift = 1 + NBIT - nbit;
239 _mask = nmax - 1;
240 int[] key = _key;
241 int[] value = _value;
242 boolean[] filled = _filled;
243 _n = 0;
244 _key = new int[nmax];
245 // semantically equivalent to _value = new V[nmax]
246 _value = new int[nmax];
247 _filled = new boolean[nmax];
248 if (key != null) {
249 for (int i = 0; i < nold; ++i) {
250 if (filled[i]) {
251 put(key[i], value[i]);
252 }
253 }
254 }
255 }
256
257 /**
258 * The toString() method on this class is designed for diagnostics only, and the output
259 * should not be regarded as stable between releases. Up to 100 key-value pairs are output in the format
260 * key:value, with comma separators, enclosed between braces: for example "{1:100,2:200}".
261 * @return A string representation of some of the key-value pairs
262 */
263
264 @Override
265 public String toString() {
266 // For diagnostics
267 FastStringBuffer buffer = new FastStringBuffer(256);
268 buffer.append("{");
269 IntIterator keys = keyIterator();
270 int count = 0;
271 while (keys.hasNext()) {
272 int k = keys.next();
273 int v = get(k);
274 buffer.append(" " + k + ":" + v + ",");
275 if (count++ >= 100) {
276 buffer.append("....");
277 break;
278 }
279 }
280 buffer.setCharAt(buffer.length() - 1, '}');
281 return buffer.toString();
282 }
283
284
285 /**
286 * Iterator over keys
287 */
288 private class IntToIntHashMapKeyIterator implements IntIterator {
289
290 private int i = 0;
291
292 public IntToIntHashMapKeyIterator() {
293 i = 0;
294 }
295
296 public boolean hasNext() {
297 while (i < _key.length) {
298 if (_filled[i]) {
299 return true;
300 } else {
301 i++;
302 }
303 }
304 return false;
305 }
306
307 public int next() {
308 return _key[i++];
309 }
310 }
311
312 }
313