Debian Codegraph @

net/sf/saxon/z/IntArraySet.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 import java.util.Arrays;
14
15 /**
16 * Set of int values. This class is modelled on the java.net.Set interface, but it does
17 * not implement this interface, because the set members are int's rather than Objects.
18 * <p>This implementation of a set of integers is optimized to use very little storage
19 * and to provide fast comparison of two sets. The equals() method determines whether
20 * two sets contain the same integers.</p>
21 * <p>This implementation is not efficient at adding new integers to the set. It creates a new
22 * array each time you do that.</p>
23 * <p>Not thread safe.</p>
24 *
25 * @author Michael Kay
26 */
27 public class IntArraySet implements IntSet {
28
29 public static final int[] EMPTY_INT_ARRAY = new int[0];
30
31 /**
32 * The array of integers, which will always be sorted
33 */
34
35 private int[] contents;
36
37 /**
38 * Hashcode, evaluated lazily
39 */
40
41 private int hashCode = -1;
42
43 /**
44 * Create an empty set
45 */
46 public IntArraySet() {
47 contents = EMPTY_INT_ARRAY;
48 }
49
50 /**
51 * Create a set containing integers from the specified IntHashSet
52 *
53 * @param input the set to be copied
54 */
55
56 public IntArraySet(IntHashSet input) {
57 // exploits the fact that getValues() constructs a new array
58 contents = input.getValues();
59 //System.err.println("new IntArraySet(" + contents.length + ")");
60 Arrays.sort(contents);
61 }
62
63 /**
64 * Create one IntArraySet as a copy of another
65 *
66 * @param input the set to be copied
67 */
68
69 public IntArraySet(IntArraySet input) {
70 contents = new int[input.contents.length];
71 System.arraycopy(input.contents, 0, contents, 0, contents.length);
72 }
73
74 public IntSet copy() {
75 IntArraySet i2 = new IntArraySet();
76 i2.contents = new int[contents.length];
77 System.arraycopy(contents, 0, i2.contents, 0, contents.length);
78 //i2.contents = Arrays.copyOf(contents, contents.length);
79 return i2;
80 }
81
82 public IntSet mutableCopy() {
83 return copy();
84 }
85
86 public void clear() {
87 contents = EMPTY_INT_ARRAY;
88 hashCode = -1;
89 }
90
91 public int size() {
92 return contents.length;
93 }
94
95 public boolean isEmpty() {
96 return contents.length == 0;
97 }
98
99 /**
100 * Get the set of integer values as an array
101 *
102 * @return a sorted array of integers
103 */
104
105 public int[] getValues() {
106 return contents;
107 }
108
109
110 public boolean contains(int value) {
111 return Arrays.binarySearch(contents, value) >= 0;
112 }
113
114 public boolean remove(int value) {
115 hashCode = -1;
116 int pos = Arrays.binarySearch(contents, value);
117 if (pos < 0) {
118 return false;
119 }
120 int[] newArray = new int[contents.length - 1];
121 if (pos > 0) {
122 // copy the items before the one that's being removed
123 System.arraycopy(contents, 0, newArray, 0, pos);
124 }
125 if (pos < newArray.length) {
126 // copy the items after the one that's being removed
127 System.arraycopy(contents, pos + 1, newArray, pos, contents.length - pos);
128 }
129 contents = newArray;
130 return true;
131 }
132
133 /**
134 * Add an integer to the set
135 *
136 * @param value the integer to be added
137 * @return true if the integer was added, false if it was already present
138 */
139
140 public boolean add(int value) {
141 hashCode = -1;
142 if (contents.length == 0) {
143 contents = new int[]{value};
144 return true;
145 }
146 int pos = Arrays.binarySearch(contents, value);
147 if (pos >= 0) {
148 return false; // value was already present
149 }
150 pos = -pos - 1; // new insertion point
151 int[] newArray = new int[contents.length + 1];
152 if (pos > 0) {
153 // copy the items before the insertion point
154 System.arraycopy(contents, 0, newArray, 0, pos);
155 }
156 newArray[pos] = value;
157 if (pos < contents.length) {
158 // copy the items after the insertion point
159 System.arraycopy(contents, pos, newArray, pos + 1, newArray.length - pos);
160 }
161 contents = newArray;
162 return true;
163 }
164
165 /**
166 * Get the first value in the set.
167 *
168 * @return the first value in the set, in sorted order
169 * @throws ArrayIndexOutOfBoundsException if the set is empty
170 */
171
172 public int getFirst() {
173 return contents[0];
174 }
175
176 /**
177 * Get an iterator over the values
178 *
179 * @return an iterator over the values, which will be delivered in sorted order
180 */
181
182 public IntIterator iterator() {
183 return new IntArrayIterator(contents, contents.length);
184 }
185
186 /**
187 * Form a new set that is the union of this set with another set.
188 *
189 * @param other the other set
190 * @return the union of the two sets
191 */
192
193 public IntSet union(IntSet other) {
194 // Look for special cases: one set empty, or both sets equal
195 if (size() == 0) {
196 return other.copy();
197 } else if (other.isEmpty()) {
198 return copy();
199 } else if (other == IntUniversalSet.getInstance()) {
200 return other;
201 } else if (other instanceof IntComplementSet) {
202 return other.union(this);
203 }
204 if (equals(other)) {
205 return copy();
206 }
207 if (other instanceof IntArraySet) {
208 // Form the union by a merge of the two sorted arrays
209 int[] merged = new int[size() + other.size()];
210 int[] a = contents;
211 int[] b = ((IntArraySet) other).contents;
212 int m = a.length, n = b.length;
213 int o = 0, i = 0, j = 0;
214 while (true) {
215 if (a[i] < b[j]) {
216 merged[o++] = a[i++];
217 } else if (b[j] < a[i]) {
218 merged[o++] = b[j++];
219 } else {
220 merged[o++] = a[i++];
221 j++;
222 }
223 if (i == m) {
224 System.arraycopy(b, j, merged, o, n - j);
225 o += (n - j);
226 return make(merged, o);
227 } else if (j == n) {
228 System.arraycopy(a, i, merged, o, m - i);
229 o += (m - i);
230 return make(merged, o);
231 }
232 }
233 } else {
234 return IntSet.super.union(other);
235 }
236 }
237
238 /**
239 * Factory method to construct a set from an array of integers
240 *
241 * @param in the array of integers, which must be in ascending order
242 * @param size the number of elements in the array that are significant
243 * @return the constructed set
244 */
245
246 public static IntArraySet make(int[] in, int size) {
247 int[] out;
248 if (in.length == size) {
249 out = in;
250 } else {
251 out = new int[size];
252 System.arraycopy(in, 0, out, 0, size);
253 }
254 return new IntArraySet(out);
255 }
256
257 private IntArraySet(int[] content) {
258 contents = content;
259 }
260
261 public String toString() {
262 FastStringBuffer sb = new FastStringBuffer(contents.length * 4);
263 for (int i = 0; i < contents.length; i++) {
264 if (i == contents.length - 1) {
265 sb.append(contents[i] + "");
266 } else if (contents[i] + 1 != contents[i + 1]) {
267 sb.append(contents[i] + ",");
268 } else {
269 int j = i + 1;
270 while (contents[j] == contents[j - 1] + 1) {
271 j++;
272 if (j == contents.length) {
273 break;
274 }
275 }
276 sb.append(contents[i] + "-" + contents[j - 1] + ",");
277 i = j - 1;
278 }
279 }
280 return sb.toString();
281 }
282
283
284 /**
285 * Test if this set has overlapping membership with another set
286 */
287
288 // public boolean containsSome(IntArraySet other) {
289 // IntIterator it = other.iterator();
290 // while (it.hasNext()) {
291 // if (contains(it.next())) {
292 // return true;
293 // }
294 // }
295 // return false;
296 // }
297
298 /**
299 * Test whether this set has exactly the same members as another set
300 */
301
302 public boolean equals(Object other) {
303 if (other instanceof IntArraySet) {
304 IntArraySet s = (IntArraySet) other;
305 return hashCode() == other.hashCode() && Arrays.equals(contents, s.contents);
306 } else
307 return other instanceof IntSet &&
308 contents.length == ((IntSet) other).size() &&
309 containsAll((IntSet) other);
310 }
311
312 /**
313 * Construct a hash key that supports the equals() test
314 */
315
316 public int hashCode() {
317 // Note, hashcodes are the same as those used by IntHashSet
318 if (hashCode == -1) {
319 int h = 936247625;
320 IntIterator it = iterator();
321 while (it.hasNext()) {
322 h += it.next();
323 }
324 hashCode = h;
325 }
326 return hashCode;
327 }
328
329 /**
330 * Iterator class: iterate over an array of integers
331 */
332
333 public static class IntArrayIterator implements IntIterator {
334
335 private int[] contents;
336 private int limit;
337 private int i = 0;
338
339 /**
340 * Create an iterator over the integers in an array (in positions 0 to n-1
341 * inclusive, were n is the value of the limit argument)
342 *
343 * @param contents the array over which to iterate
344 * @param limit the number of items to be included in the iteration
345 */
346
347 public IntArrayIterator(int[] contents, int limit) {
348 i = 0;
349 this.contents = contents;
350 this.limit = limit;
351 }
352
353 public boolean hasNext() {
354 return i < limit;
355 }
356
357 public int next() {
358 return contents[i++];
359 }
360 }
361
362
363 }
364