Debian Codegraph @

net/sf/saxon/z/IntHashSet.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
11 import net.sf.saxon.tree.util.FastStringBuffer;
12
13 /**
14 * Set of int values. This class is modelled on the java.net.Set interface, but it does
15 * not implement this interface, because the set members are nint's rather than Objects.
16 * <p>Not thread safe.</p>
17 *
18 * @author Dominique Devienne
19 * @author Michael Kay: retrofitted to JDK 1.4, added iterator()
20 */
21 public class IntHashSet implements IntSet {
22
23 private static final int NBIT = 30; // MAX_SIZE = 2^NBIT
24
25 /**
26 * The maximum number of elements this container can contain.
27 */
28 public static final int MAX_SIZE = 1 << NBIT; // maximum number of keys mapped
29
30 /**
31 * This set's NO-DATA-VALUE.
32 */
33 public final int ndv;
34
35 // private
36
37 //private double _factor; // 0.0 <= _factor <= 1.0 - changed by MHK to assume factor = 0.25
38 private int _nmax; // 0 <= _nmax = 2^nbit <= 2^NBIT = MAX_SIZE
39 private int _size; // 0 <= _size <= _nmax <= MAX_SIZE
40 private int _nlo; // _nmax*_factor (_size<=_nlo, if possible)
41 private int _nhi; // MAX_SIZE*_factor (_size< _nhi, if possible)
42 private int _shift; // _shift = 1 + NBIT - nbit (see function hash() below)
43 private int _mask; // _mask = _nmax - 1
44 private int[] _values; // array[_nmax] of values
45
46
47 /**
48 * Initializes a set with a capacity of 8 and a load factor of 0,25.
49 */
50 public IntHashSet() {
51 this(8, Integer.MIN_VALUE);
52 }
53
54 /**
55 * Initializes a set with the given capacity and a load factor of 0,25.
56 *
57 * @param capacity the initial capacity.
58 */
59 public IntHashSet(int capacity) {
60 this(capacity, Integer.MIN_VALUE);
61 }
62
63 /**
64 * Initializes a set with a load factor of 0,25.
65 *
66 * @param capacity the initial capacity.
67 * @param noDataValue the value to use for non-values.
68 */
69 public IntHashSet(int capacity, int noDataValue) {
70 ndv = noDataValue;
71 //_factor = 0.25;
72 setCapacity(capacity);
73 }
74
75 public IntSet copy() {
76 if (_size == 0) {
77 return IntEmptySet.getInstance();
78 } else {
79 IntHashSet s = new IntHashSet(_size, ndv);
80 s._nmax = _nmax;
81 s._size = _size;
82 s._nlo = _nlo;
83 s._nhi = _nhi;
84 s._shift = _shift;
85 s._size = _size;
86 s._values = new int[_values.length];
87 System.arraycopy(_values, 0, s._values, 0, _values.length);
88 //s._values = Arrays.copyOf(_values, _values.length);
89 return s;
90 }
91 }
92
93 public IntSet mutableCopy() {
94 return copy();
95 }
96
97 public void clear() {
98 _size = 0;
99 for (int i = 0; i < _nmax; ++i) {
100 _values[i] = ndv;
101 }
102 }
103
104 public int size() {
105 return _size;
106 }
107
108 public boolean isEmpty() {
109 return _size == 0;
110 }
111
112 public int[] getValues() {
113 int index = 0;
114 final int[] values = new int[_size];
115 for (int _value : _values) {
116 if (_value != ndv) {
117 values[index++] = _value;
118 }
119 }
120 return values;
121 }
122
123
124 public boolean contains(int value) {
125 return (_values[indexOf(value)] != ndv);
126 }
127
128
129 public boolean remove(int value) {
130 // Knuth, v. 3, 527, Algorithm R.
131 int i = indexOf(value);
132 if (_values[i] == ndv) {
133 return false;
134 }
135 --_size;
136 for (; ; ) {
137 _values[i] = ndv;
138 int j = i;
139 int r;
140 do {
141 i = (i - 1) & _mask;
142 if (_values[i] == ndv) {
143 return true;
144 }
145 r = hash(_values[i]);
146 } while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
147 _values[j] = _values[i];
148 }
149 }
150
151
152 public boolean add(int value) {
153 if (value == ndv) {
154 throw new IllegalArgumentException("Can't add the 'no data' value");
155 }
156 int i = indexOf(value);
157 if (_values[i] == ndv) {
158 ++_size;
159 _values[i] = value;
160
161 // Check new size
162 if (_size > MAX_SIZE) {
163 throw new RuntimeException("Too many elements (> " + MAX_SIZE + ')');
164 }
165 if (_nlo < _size && _size <= _nhi) {
166 setCapacity(_size);
167 }
168 return true;
169 } else {
170 return false; // leave set unchanged
171 }
172 }
173
174 ///////////////////////////////////////////////////////////////////////////
175
176 private int hash(int key) {
177 // Knuth, v. 3, 509-510. Randomize the 31 low-order bits of c*key
178 // and return the highest nbits (where nbits <= 30) bits of these.
179 // The constant c = 1327217885 approximates 2^31 * (sqrt(5)-1)/2.
180 return ((1327217885 * key) >> _shift) & _mask;
181 }
182
183 /**
184 * Gets the index of the value, if it exists, or the index at which
185 * this value would be added if it does not exist yet.
186 */
187 private int indexOf(int value) {
188 int i = hash(value);
189 while (_values[i] != ndv) {
190 if (_values[i] == value) {
191 return i;
192 }
193 i = (i - 1) & _mask;
194 }
195 return i;
196 }
197
198 private void setCapacity(int capacity) {
199 // Changed MHK in 8.9 to use a constant factor of 0.25, thus avoiding floating point arithmetic
200 if (capacity < _size) {
201 capacity = _size;
202 }
203 //double factor = 0.25;
204 int nbit, nmax;
205 for (nbit = 1, nmax = 2; nmax < capacity * 4 && nmax < MAX_SIZE; ++nbit, nmax *= 2) {
206 // do nothing
207 }
208 int nold = _nmax;
209 if (nmax == nold) {
210 return;
211 }
212
213 _nmax = nmax;
214 _nlo = nmax / 4;
215 _nhi = MAX_SIZE / 4;
216 _shift = 1 + NBIT - nbit;
217 _mask = nmax - 1;
218
219 _size = 0;
220 int[] values = _values;
221 _values = new int[nmax];
222 java.util.Arrays.fill(_values, ndv); // empty all values
223 if (values != null) {
224 for (int i = 0; i < nold; ++i) {
225 int value = values[i];
226 if (value != ndv) {
227 // Don't use add, because the capacity is necessarily large enough,
228 // and the value is necessarily unique (since in this set already)!
229 //add(values[i]);
230 ++_size;
231 _values[indexOf(value)] = value;
232 }
233 }
234 }
235 }
236
237 /**
238 * Get an iterator over the values
239 */
240
241 public IntIterator iterator() {
242 return new IntHashSetIterator();
243 }
244
245
246 /**
247 * Test if one set has overlapping membership with another set
248 */
249
250 public static boolean containsSome(IntSet one, IntSet two) {
251 if (two instanceof IntEmptySet) {
252 return false;
253 }
254 if (two instanceof IntUniversalSet) {
255 return !one.isEmpty();
256 }
257 if (two instanceof IntComplementSet) {
258 return !((IntComplementSet) two).getExclusions().containsAll(one);
259 }
260 IntIterator it = two.iterator();
261 while (it.hasNext()) {
262 if (one.contains(it.next())) {
263 return true;
264 }
265 }
266 return false;
267 }
268
269 /**
270 * Test whether this set has exactly the same members as another set
271 */
272
273 public boolean equals(Object other) {
274 if (other instanceof IntSet) {
275 IntHashSet s = (IntHashSet) other;
276 return (size() == s.size() && containsAll(s));
277 } else {
278 return false;
279 }
280 }
281
282 /**
283 * Construct a hash key that supports the equals() test
284 */
285
286 public int hashCode() {
287 // Note, hashcodes are the same as those used by IntArraySet
288 int h = 936247625;
289 IntIterator it = iterator();
290 while (it.hasNext()) {
291 h += it.next();
292 }
293 return h;
294 }
295
296 public String toString() {
297 return toString(iterator());
298 }
299
300 public static String toString(IntIterator it) {
301 FastStringBuffer sb = new FastStringBuffer(100);
302 while (it.hasNext()) {
303 if (sb.isEmpty()) {
304 sb.append("" + it.next());
305 } else {
306 sb.append(" " + it.next());
307 }
308 }
309 return sb.toString();
310 }
311
312 /**
313 * Diagnostic output
314 */
315
316 public void diagnosticDump() {
317 System.err.println("Contents of IntHashSet");
318 FastStringBuffer sb = new FastStringBuffer(100);
319 for (int i = 0; i < _values.length; i++) {
320 if (i % 10 == 0) {
321 System.err.println(sb.toString());
322 sb.setLength(0);
323 }
324 if (_values[i] == ndv) {
325 sb.append("*, ");
326 } else {
327 sb.append(_values[i] + ", ");
328 }
329 }
330 System.err.println(sb.toString());
331 sb.setLength(0);
332 System.err.println("size: " + _size);
333 System.err.println("ndv: " + ndv);
334 System.err.println("nlo: " + _nlo);
335 System.err.println("nhi: " + _nhi);
336 System.err.println("nmax: " + _nmax);
337 System.err.println("shift: " + _shift);
338 System.err.println("mask: " + _mask);
339 System.err.println("Result of iterator:");
340 IntIterator iter = iterator();
341 int i = 0;
342 while (iter.hasNext()) {
343 if (i++ % 10 == 0) {
344 System.err.println(sb.toString());
345 sb.setLength(0);
346 }
347 sb.append(iter.next() + ", ");
348 }
349 System.err.println(sb.toString());
350 System.err.println("=====================");
351 }
352
353 /**
354 * Iterator class
355 *
356 * @author Saxonica Limited
357 */
358
359 private class IntHashSetIterator implements IntIterator {
360
361 private int i = 0;
362
363 public IntHashSetIterator() {
364 i = 0;
365 }
366
367 public boolean hasNext() {
368 while (i < _values.length) {
369 if (_values[i] != ndv) {
370 return true;
371 } else {
372 i++;
373 }
374 }
375 return false;
376 }
377
378 public int next() {
379 return _values[i++];
380 }
381 }
382
383 }
384