File: ArraySet.drv

package info (click to toggle)
libfastutil-java 8.5.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,076 kB
  • sloc: java: 19,670; sh: 1,188; makefile: 473; xml: 354
file content (449 lines) | stat: -rw-r--r-- 13,116 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * Copyright (C) 2007-2024 Sebastiano Vigna
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package PACKAGE;

import java.util.Collection;
import java.util.NoSuchElementException;
import java.util.Set;

#if KEYS_REFERENCE
import java.util.function.Consumer;
#endif


/** A simple, brute-force implementation of a set based on a backing array.
 *
 * <p>The main purpose of this
 * implementation is that of wrapping cleanly the brute-force approach to the storage of a very
 * small number of items: just put them into an array and scan linearly to find an item.
 */

public class ARRAY_SET KEY_GENERIC extends ABSTRACT_SET KEY_GENERIC implements java.io.Serializable, Cloneable {

	private static final long serialVersionUID = 1L;
	/** The backing array (valid up to {@link #size}, excluded). */
	protected transient KEY_TYPE[] a;
	/** The number of valid entries in {@link #a}. */
	protected int size;

	/** Creates a new array set using the given backing array. The resulting set will have as many elements as the array.
	 *
	 * <p>It is the responsibility of the caller to ensure that the elements of {@code a} are distinct.
	 *
	 * @param a the backing array.
	 */
	public ARRAY_SET(final KEY_TYPE[] a) {
		this.a = a;
		size = a.length;
	}

	/** Creates a new empty array set.
	 */
	public ARRAY_SET() { this.a = ARRAYS.EMPTY_ARRAY; }

	/** Creates a new empty array set of given initial capacity.
	 *
	 * @param capacity the initial capacity.
	 */
	public ARRAY_SET(final int capacity) { this.a = new KEY_TYPE[capacity]; }

	/** Creates a new array set copying the contents of a given collection.
	 * @param c a collection.
	 */
	public ARRAY_SET(COLLECTION KEY_GENERIC c) {
		this(c.size());
		addAll(c);
	}

	/** Creates a new array set copying the contents of a given set.
	 * @param c a collection.
	 */
	public ARRAY_SET(final Collection<? extends KEY_GENERIC_CLASS> c) {
		this(c.size());
		addAll(c);
	}

	/** Creates a new array set copying the contents of a given collection.
	 * @param c a collection.
	 */
	public ARRAY_SET(SET KEY_GENERIC c) {
		this(c.size());
		int i = 0;
		for(KEY_TYPE x : c) {
			a[i] = x;
			i++;
		}
		size = i;
	}

	/** Creates a new array set copying the contents of a given set.
	 * @param c a collection.
	 */
	public ARRAY_SET(final Set<? extends KEY_GENERIC_CLASS> c) {
		this(c.size());
		int i = 0;
		for(KEY_GENERIC_CLASS x: c) {
			a[i] = KEY_CLASS2TYPE(x);
			i++;
		}
		size = i;
	}


	/** Creates a new array set using the given backing array and the given number of elements of the array.
	 *
	 * <p>It is the responsibility of the caller to ensure that the first {@code size} elements of {@code a} are distinct.
	 *
	 * @param a the backing array.
	 * @param size the number of valid elements in {@code a}.
	 */
	public ARRAY_SET(final KEY_TYPE[] a, final int size) {
		this.a = a;
		this.size = size;
		if (size > a.length) throw new IllegalArgumentException("The provided size (" + size + ") is larger than or equal to the array size (" + a.length + ")");
	}

	// The 0 and 1 arg overloads allow us to skip the temporary hash set creation. 

	/** Creates a new empty array set. 
	 *
	 * @return a new empty array set.
	 */
	public static KEY_GENERIC ARRAY_SET KEY_GENERIC of() {
		return ofUnchecked();
	}

	/** Creates a new array set using the element given.
	 *
	 * @param e the element that the returned set will contain.
	 * @return a new array set containing {@code e}.
	 */
	public static KEY_GENERIC ARRAY_SET KEY_GENERIC of(final KEY_GENERIC_TYPE e) {
		return ofUnchecked(e);
	}

	/** Creates a new array set using an array of elements.
	 *
	 * <p>Unlike the array accepting constructors, this method will throw if duplicate elements
	 * are encountered. This adds a non-trivial validation burden. Use {@link #ofUnchecked} if you
	 * know your input has no duplicates, which will skip this validation.
	 *
	 * @param a the backing array of the returned array set.
	 * @throws IllegalArgumentException if there were duplicate entries.
	 * @return a new array set containing the elements in {@code a}.
	 */
	SAFE_VARARGS
	public static KEY_GENERIC ARRAY_SET KEY_GENERIC of(final KEY_GENERIC_TYPE... a) {
		if (a.length == 2) {
			if (KEY_EQUALS(a[0], a[1])) {
				throw new IllegalArgumentException("Duplicate element: " + a[1]);
			}
		} else if (a.length > 2) {
			// Will throw on a duplicate entry for us.
			OPEN_HASH_SET.of(a);
		}
		return ofUnchecked(a);
	}

	/** Creates a new empty array set. 
	 * 
	 * @return a new empty array set.
	 */
	public static KEY_GENERIC ARRAY_SET KEY_GENERIC ofUnchecked() {
		return new ARRAY_SET KEY_GENERIC_DIAMOND();
	}

	// No 1 element overload; we want the temporary array constructed for us in the varargs overload

	/** Creates a new array set using an array of elements.
	 *
	 * <p>It is the responsibility of the caller to ensure that the elements of {@code a} are distinct.
	 *
	 * @param a the backing array of the returned array set.
	 * @return a new array set containing the elements in {@code a}.
	 */
	SAFE_VARARGS
	public static KEY_GENERIC ARRAY_SET KEY_GENERIC ofUnchecked(final KEY_GENERIC_TYPE... a) {
		return new ARRAY_SET KEY_GENERIC(a);
	}

	private int findKey(final KEY_TYPE o) {
		final KEY_TYPE[] a = this.a;
		for(int i = size; i-- != 0;) if (KEY_EQUALS(a[i], o)) return i;
		return -1;
	}

	// TODO Maybe make this return a list-iterator like the LinkedXHashSets do?

	@Override
	SUPPRESS_WARNINGS_KEY_UNCHECKED
	public KEY_ITERATOR KEY_GENERIC iterator() {
		return new KEY_ITERATOR KEY_GENERIC () {
			int curr = -1, next = 0;

			@Override
			public boolean hasNext() { return next < size; }

			@Override
			public KEY_GENERIC_TYPE NEXT_KEY() {
				if (! hasNext()) throw new NoSuchElementException();
				return KEY_GENERIC_CAST a[curr = next++];
			}

			@Override
			public void remove() {
				if (curr == -1) throw new IllegalStateException();
				curr = -1;
				final int tail = size-- - next--;
				System.arraycopy(a, next + 1, a, next, tail);
#if KEYS_REFERENCE
				a[size] = null;
#endif
			}

			@Override
			public int skip(int n) {
				if (n < 0) throw new IllegalArgumentException("Argument must be nonnegative: " + n);
				n = Math.min(n, size - next);
				next += n;
				if (n != 0) curr = next - 1;
				return n;
			}

			@Override
			public void forEachRemaining(final METHOD_ARG_KEY_CONSUMER action) {
				final KEY_TYPE a[] = ARRAY_SET.this.a;
				while (next < size) action.accept(KEY_GENERIC_CAST a[next++]);
			}
		};
	}

	// If you update this, you will probably want to update ArrayList as well
	private final class Spliterator implements KEY_SPLITERATOR KEY_GENERIC {
		// Until we split, we will track the size of the set
		// Once we split, then we stop updating on structural modifications.
		// Aka, size is late-binding.
		boolean hasSplit = false;
		int pos, max;

#ifdef TEST
		// Sentinal to make sure we don't accidentally use size when we mean max
		@Deprecated
		private final Object size = null;
#endif

		public Spliterator() {
			this(0, ARRAY_SET.this.size, false);
		}

		private Spliterator(int pos, int max, boolean hasSplit) {
			assert pos <= max : "pos " + pos + " must be <= max " + max;
			this.pos = pos;
			this.max = max;
			this.hasSplit = hasSplit;
		}

		private int getWorkingMax() {
			return hasSplit ? max : ARRAY_SET.this.size;
		}

		@Override
		public int characteristics() { return SPLITERATORS.SET_SPLITERATOR_CHARACTERISTICS | java.util.Spliterator.SUBSIZED | java.util.Spliterator.ORDERED; }

		@Override
		public long estimateSize() { return getWorkingMax() - pos; }

		SUPPRESS_WARNINGS_KEY_UNCHECKED
		@Override
		public boolean tryAdvance(final METHOD_ARG_KEY_CONSUMER action) {
			if (pos >= getWorkingMax()) return false;
			action.accept(KEY_GENERIC_CAST a[pos++]);
			return true;
		}

		SUPPRESS_WARNINGS_KEY_UNCHECKED
		@Override
		public void forEachRemaining(final METHOD_ARG_KEY_CONSUMER action) {
			final KEY_TYPE[] a = ARRAY_SET.this.a;
			for (final int max = getWorkingMax(); pos < max; ++pos) {
				action.accept(KEY_GENERIC_CAST a[pos]);
			}
		}

		@Override
		public long skip(long n) {
			if (n < 0) throw new IllegalArgumentException("Argument must be nonnegative: " + n);
			final int max = getWorkingMax();
			if (pos >= max) return 0;
			final int remaining = max - pos;
			if (n < remaining) {
				pos = it.unimi.dsi.fastutil.SafeMath.safeLongToInt(pos + n);
				return n;
			}
			n = remaining;
			pos = max;
			return n;
		}

		@Override
		public KEY_SPLITERATOR KEY_GENERIC trySplit() {
			final int max = getWorkingMax();
			int retLen = (max - pos) >> 1;
			if (retLen <= 1) return null;
			// Update instance max with the last seen list size (if needed) before continuing
			this.max = max;
			int myNewPos = pos + retLen;
			int retMax = myNewPos;
			int oldPos = pos;
			this.pos = myNewPos;
			this.hasSplit = true;
			return new Spliterator(oldPos, retMax, true);
		}
	}

	/** {@inheritDoc}
	 *
	 * <p>In addition to the usual trait of {@link java.util.Spliterator#DISTINCT DISTINCT} for
	 * sets, the returned spliterator will also {@linkplain java.util.Spliterator#characteristics() report}
	 * the trait {@link java.util.Spliterator#ORDERED ORDERED}.
	 *
	 * <p>The returned spliterator is late-binding; it will track structural changes
	 * after the current item, up until the first {@link java.util.Spliterator#trySplit() trySplit()},
	 * at which point the maximum index will be fixed.
	 * <br>Structural changes before the current item or after the first
	 * {@link java.util.Spliterator#trySplit() trySplit()} will result in unspecified behavior.
	 */
	@Override
	public KEY_SPLITERATOR KEY_GENERIC spliterator() {
		return new Spliterator();
	}

	@Override
	public boolean contains(final KEY_TYPE k) { return findKey(k) != -1; }

	@Override
	public int size() { return size; }

	@Override
	public boolean remove(final KEY_TYPE k) {
		final int pos = findKey(k);
		if (pos == -1) return false;
		final int tail = size - pos - 1;
		for(int i = 0; i < tail; i++) a[pos + i] = a[pos + i + 1];
		size--;
#if KEYS_REFERENCE
		a[size] = null;
#endif
		return true;
	}

	@Override
	public boolean add(final KEY_GENERIC_TYPE k) {
		final int pos = findKey(k);
		if (pos != -1) return false;
		if (size == a.length) {
			final KEY_TYPE[] b = new KEY_TYPE[size == 0 ? 2 : size * 2];
			for(int i = size; i-- != 0;) b[i] = a[i];
			a = b;
		}
		a[size++] = k;
		return true;
	}

	@Override
	public void clear() {
#if KEYS_REFERENCE
		java.util.Arrays.fill(a, 0, size, null);
#endif
		size = 0;
	}

	@Override
	public boolean isEmpty() { return size == 0; }
	
#if KEYS_PRIMITIVE
	@Override
	public KEY_TYPE[] TO_KEY_ARRAY() {
		if (size == 0) return ARRAYS.EMPTY_ARRAY;
		return java.util.Arrays.copyOf(a, size);
	}

	@Override
	public KEY_TYPE[] toArray(KEY_TYPE[] a) {
		if (a == null || a.length < size) a = new KEY_TYPE[size];
		System.arraycopy(this.a, 0, a, 0, size);
		return a;
	}
#else // KEYS_REFERENCE
	@Override
	public Object[] toArray() {
		final int size = size();
		// A subtle part of the spec says the returned array must be Object[] exactly.
		if (size == 0) return it.unimi.dsi.fastutil.objects.ObjectArrays.EMPTY_ARRAY;
		return java.util.Arrays.copyOf(a, size, Object[].class);
	}

	SUPPRESS_WARNINGS_KEY_UNCHECKED
	@Override
	public <T> T[] toArray(T[] a) {
		if (a == null) {
			a = (T[]) new Object[size];
		} else if (a.length < size) {
			a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
		}
		System.arraycopy(this.a, 0, a, 0, size);
		if (a.length > size) {
			a[size] = null;
		}
		return a;
	}
#endif

	/** Returns a deep copy of this set.
	 *
	 * <p>This method performs a deep copy of this array set; the data stored in the
	 * set, however, is not cloned. Note that this makes a difference only for object keys.
	 *
	 *  @return a deep copy of this set.
	 */
	@Override
	SUPPRESS_WARNINGS_KEY_UNCHECKED
	public ARRAY_SET KEY_GENERIC clone() {
		ARRAY_SET KEY_GENERIC c;
		try {
			c = (ARRAY_SET KEY_GENERIC)super.clone();
		}
		catch(CloneNotSupportedException cantHappen) {
			throw new InternalError();
		}
		c.a = a.clone();
		return c;
	}

	private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
		s.defaultWriteObject();
		final KEY_TYPE[] a = this.a;
		for(int i = 0; i < size; i++) s.WRITE_KEY(a[i]);
	}

	private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
		s.defaultReadObject();
		final KEY_TYPE[] a = this.a = new KEY_TYPE[size];
		for(int i = 0; i < size; i++) a[i] = s.READ_KEY();
	}
}