blob: 9e9314fba4c4b3fe65763138ec76fdbe1f8e834a [file] [log] [blame]
Dianne Hackborn21ab6f42013-06-10 15:59:15 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.util;
18
Adam Lesinski776abc22014-03-07 11:30:59 -050019import libcore.util.EmptyArray;
20
Dianne Hackborn21ab6f42013-06-10 15:59:15 -070021import java.lang.reflect.Array;
22import java.util.Collection;
23import java.util.Iterator;
24import java.util.Map;
25import java.util.Set;
26
27/**
28 * ArraySet is a generic set data structure that is designed to be more memory efficient than a
29 * traditional {@link java.util.HashSet}. The design is very similar to
30 * {@link ArrayMap}, with all of the caveats described there. This implementation is
31 * separate from ArrayMap, however, so the Object array contains only one item for each
32 * entry in the set (instead of a pair for a mapping).
33 *
34 * <p>Note that this implementation is not intended to be appropriate for data structures
35 * that may contain large numbers of items. It is generally slower than a traditional
36 * HashSet, since lookups require a binary search and adds and removes require inserting
37 * and deleting entries in the array. For containers holding up to hundreds of items,
Dianne Hackbornb993f412013-07-12 17:46:45 -070038 * the performance difference is not significant, less than 50%.</p>
Dianne Hackborn21ab6f42013-06-10 15:59:15 -070039 *
Dianne Hackborn21ab6f42013-06-10 15:59:15 -070040 * <p>Because this container is intended to better balance memory use, unlike most other
41 * standard Java containers it will shrink its array as items are removed from it. Currently
42 * you have no control over this shrinking -- if you set a capacity and then remove an
43 * item, it may reduce the capacity to better match the current size. In the future an
Dianne Hackbornb993f412013-07-12 17:46:45 -070044 * explicit call to set the capacity should turn off this aggressive shrinking behavior.</p>
Dianne Hackborn21ab6f42013-06-10 15:59:15 -070045 */
46public final class ArraySet<E> implements Collection<E>, Set<E> {
47 private static final boolean DEBUG = false;
48 private static final String TAG = "ArraySet";
49
50 /**
51 * The minimum amount by which the capacity of a ArraySet will increase.
52 * This is tuned to be relatively space-efficient.
53 */
54 private static final int BASE_SIZE = 4;
55
56 /**
57 * Maximum number of entries to have in array caches.
58 */
59 private static final int CACHE_SIZE = 10;
60
61 /**
62 * Caches of small array objects to avoid spamming garbage. The cache
63 * Object[] variable is a pointer to a linked list of array objects.
64 * The first entry in the array is a pointer to the next array in the
65 * list; the second entry is a pointer to the int[] hash code array for it.
66 */
67 static Object[] mBaseCache;
68 static int mBaseCacheSize;
69 static Object[] mTwiceBaseCache;
70 static int mTwiceBaseCacheSize;
71
Jeff Sharkey3d1cb6a2016-02-27 21:10:34 -070072 final boolean mIdentityHashCode;
Dianne Hackborn21ab6f42013-06-10 15:59:15 -070073 int[] mHashes;
74 Object[] mArray;
75 int mSize;
76 MapCollections<E, E> mCollections;
77
78 private int indexOf(Object key, int hash) {
79 final int N = mSize;
80
81 // Important fast case: if nothing is in here, nothing to look for.
82 if (N == 0) {
83 return ~0;
84 }
85
Dianne Hackborn3e82ba12013-07-16 13:23:55 -070086 int index = ContainerHelpers.binarySearch(mHashes, N, hash);
Dianne Hackborn21ab6f42013-06-10 15:59:15 -070087
88 // If the hash code wasn't found, then we have no entry for this key.
89 if (index < 0) {
90 return index;
91 }
92
93 // If the key at the returned index matches, that's what we want.
Dianne Hackborn62d708f2013-07-25 16:42:59 -070094 if (key.equals(mArray[index])) {
Dianne Hackborn21ab6f42013-06-10 15:59:15 -070095 return index;
96 }
97
98 // Search for a matching key after the index.
99 int end;
100 for (end = index + 1; end < N && mHashes[end] == hash; end++) {
Dianne Hackborn62d708f2013-07-25 16:42:59 -0700101 if (key.equals(mArray[end])) return end;
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700102 }
103
104 // Search for a matching key before the index.
105 for (int i = index - 1; i >= 0 && mHashes[i] == hash; i--) {
Dianne Hackborn62d708f2013-07-25 16:42:59 -0700106 if (key.equals(mArray[i])) return i;
107 }
108
109 // Key not found -- return negative value indicating where a
110 // new entry for this key should go. We use the end of the
111 // hash chain to reduce the number of array entries that will
112 // need to be copied when inserting.
113 return ~end;
114 }
115
116 private int indexOfNull() {
117 final int N = mSize;
118
119 // Important fast case: if nothing is in here, nothing to look for.
120 if (N == 0) {
121 return ~0;
122 }
123
124 int index = ContainerHelpers.binarySearch(mHashes, N, 0);
125
126 // If the hash code wasn't found, then we have no entry for this key.
127 if (index < 0) {
128 return index;
129 }
130
131 // If the key at the returned index matches, that's what we want.
132 if (null == mArray[index]) {
133 return index;
134 }
135
136 // Search for a matching key after the index.
137 int end;
138 for (end = index + 1; end < N && mHashes[end] == 0; end++) {
139 if (null == mArray[end]) return end;
140 }
141
142 // Search for a matching key before the index.
143 for (int i = index - 1; i >= 0 && mHashes[i] == 0; i--) {
144 if (null == mArray[i]) return i;
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700145 }
146
147 // Key not found -- return negative value indicating where a
148 // new entry for this key should go. We use the end of the
149 // hash chain to reduce the number of array entries that will
150 // need to be copied when inserting.
151 return ~end;
152 }
153
154 private void allocArrays(final int size) {
155 if (size == (BASE_SIZE*2)) {
156 synchronized (ArraySet.class) {
157 if (mTwiceBaseCache != null) {
158 final Object[] array = mTwiceBaseCache;
159 mArray = array;
160 mTwiceBaseCache = (Object[])array[0];
161 mHashes = (int[])array[1];
162 array[0] = array[1] = null;
163 mTwiceBaseCacheSize--;
164 if (DEBUG) Log.d(TAG, "Retrieving 2x cache " + mHashes
165 + " now have " + mTwiceBaseCacheSize + " entries");
166 return;
167 }
168 }
169 } else if (size == BASE_SIZE) {
170 synchronized (ArraySet.class) {
171 if (mBaseCache != null) {
172 final Object[] array = mBaseCache;
173 mArray = array;
174 mBaseCache = (Object[])array[0];
175 mHashes = (int[])array[1];
176 array[0] = array[1] = null;
177 mBaseCacheSize--;
178 if (DEBUG) Log.d(TAG, "Retrieving 1x cache " + mHashes
179 + " now have " + mBaseCacheSize + " entries");
180 return;
181 }
182 }
183 }
184
185 mHashes = new int[size];
186 mArray = new Object[size];
187 }
188
189 private static void freeArrays(final int[] hashes, final Object[] array, final int size) {
190 if (hashes.length == (BASE_SIZE*2)) {
191 synchronized (ArraySet.class) {
192 if (mTwiceBaseCacheSize < CACHE_SIZE) {
193 array[0] = mTwiceBaseCache;
194 array[1] = hashes;
195 for (int i=size-1; i>=2; i--) {
196 array[i] = null;
197 }
198 mTwiceBaseCache = array;
199 mTwiceBaseCacheSize++;
200 if (DEBUG) Log.d(TAG, "Storing 2x cache " + array
201 + " now have " + mTwiceBaseCacheSize + " entries");
202 }
203 }
204 } else if (hashes.length == BASE_SIZE) {
205 synchronized (ArraySet.class) {
206 if (mBaseCacheSize < CACHE_SIZE) {
207 array[0] = mBaseCache;
208 array[1] = hashes;
209 for (int i=size-1; i>=2; i--) {
210 array[i] = null;
211 }
212 mBaseCache = array;
213 mBaseCacheSize++;
214 if (DEBUG) Log.d(TAG, "Storing 1x cache " + array
215 + " now have " + mBaseCacheSize + " entries");
216 }
217 }
218 }
219 }
220
221 /**
222 * Create a new empty ArraySet. The default capacity of an array map is 0, and
223 * will grow once items are added to it.
224 */
225 public ArraySet() {
Jeff Sharkey3d1cb6a2016-02-27 21:10:34 -0700226 this(0, false);
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700227 }
228
229 /**
230 * Create a new ArraySet with a given initial capacity.
231 */
232 public ArraySet(int capacity) {
Jeff Sharkey3d1cb6a2016-02-27 21:10:34 -0700233 this(capacity, false);
234 }
235
236 /** {@hide} */
237 public ArraySet(int capacity, boolean identityHashCode) {
238 mIdentityHashCode = identityHashCode;
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700239 if (capacity == 0) {
Adam Lesinski776abc22014-03-07 11:30:59 -0500240 mHashes = EmptyArray.INT;
241 mArray = EmptyArray.OBJECT;
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700242 } else {
243 allocArrays(capacity);
244 }
245 mSize = 0;
246 }
247
248 /**
249 * Create a new ArraySet with the mappings from the given ArraySet.
250 */
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700251 public ArraySet(ArraySet<E> set) {
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700252 this();
253 if (set != null) {
254 addAll(set);
255 }
256 }
257
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700258 /** {@hide} */
259 public ArraySet(Collection<E> set) {
260 this();
261 if (set != null) {
262 addAll(set);
263 }
264 }
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700265
266 /**
267 * Make the array map empty. All storage is released.
268 */
269 @Override
270 public void clear() {
271 if (mSize != 0) {
272 freeArrays(mHashes, mArray, mSize);
Adam Lesinski776abc22014-03-07 11:30:59 -0500273 mHashes = EmptyArray.INT;
274 mArray = EmptyArray.OBJECT;
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700275 mSize = 0;
276 }
277 }
278
279 /**
280 * Ensure the array map can hold at least <var>minimumCapacity</var>
281 * items.
282 */
283 public void ensureCapacity(int minimumCapacity) {
284 if (mHashes.length < minimumCapacity) {
285 final int[] ohashes = mHashes;
286 final Object[] oarray = mArray;
287 allocArrays(minimumCapacity);
288 if (mSize > 0) {
289 System.arraycopy(ohashes, 0, mHashes, 0, mSize);
290 System.arraycopy(oarray, 0, mArray, 0, mSize);
291 }
292 freeArrays(ohashes, oarray, mSize);
293 }
294 }
295
296 /**
297 * Check whether a value exists in the set.
298 *
299 * @param key The value to search for.
300 * @return Returns true if the value exists, else false.
301 */
302 @Override
303 public boolean contains(Object key) {
Adam Lesinski4e9c07c2014-08-26 11:53:32 -0700304 return indexOf(key) >= 0;
305 }
306
307 /**
308 * Returns the index of a value in the set.
309 *
310 * @param key The value to search for.
311 * @return Returns the index of the value if it exists, else a negative integer.
312 */
313 public int indexOf(Object key) {
Jeff Sharkey3d1cb6a2016-02-27 21:10:34 -0700314 return key == null ? indexOfNull()
315 : indexOf(key, mIdentityHashCode ? System.identityHashCode(key) : key.hashCode());
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700316 }
317
318 /**
319 * Return the value at the given index in the array.
320 * @param index The desired index, must be between 0 and {@link #size()}-1.
321 * @return Returns the value stored at the given index.
322 */
323 public E valueAt(int index) {
324 return (E)mArray[index];
325 }
326
327 /**
328 * Return true if the array map contains no items.
329 */
330 @Override
331 public boolean isEmpty() {
332 return mSize <= 0;
333 }
334
335 /**
336 * Adds the specified object to this set. The set is not modified if it
337 * already contains the object.
338 *
339 * @param value the object to add.
340 * @return {@code true} if this set is modified, {@code false} otherwise.
341 * @throws ClassCastException
342 * when the class of the object is inappropriate for this set.
343 */
344 @Override
345 public boolean add(E value) {
Dianne Hackborn62d708f2013-07-25 16:42:59 -0700346 final int hash;
347 int index;
348 if (value == null) {
349 hash = 0;
350 index = indexOfNull();
351 } else {
Jeff Sharkey3d1cb6a2016-02-27 21:10:34 -0700352 hash = mIdentityHashCode ? System.identityHashCode(value) : value.hashCode();
Dianne Hackborn62d708f2013-07-25 16:42:59 -0700353 index = indexOf(value, hash);
354 }
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700355 if (index >= 0) {
356 return false;
357 }
358
359 index = ~index;
360 if (mSize >= mHashes.length) {
361 final int n = mSize >= (BASE_SIZE*2) ? (mSize+(mSize>>1))
362 : (mSize >= BASE_SIZE ? (BASE_SIZE*2) : BASE_SIZE);
363
364 if (DEBUG) Log.d(TAG, "add: grow from " + mHashes.length + " to " + n);
365
366 final int[] ohashes = mHashes;
367 final Object[] oarray = mArray;
368 allocArrays(n);
369
370 if (mHashes.length > 0) {
371 if (DEBUG) Log.d(TAG, "add: copy 0-" + mSize + " to 0");
372 System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length);
373 System.arraycopy(oarray, 0, mArray, 0, oarray.length);
374 }
375
376 freeArrays(ohashes, oarray, mSize);
377 }
378
379 if (index < mSize) {
380 if (DEBUG) Log.d(TAG, "add: move " + index + "-" + (mSize-index)
381 + " to " + (index+1));
382 System.arraycopy(mHashes, index, mHashes, index + 1, mSize - index);
383 System.arraycopy(mArray, index, mArray, index + 1, mSize - index);
384 }
385
386 mHashes[index] = hash;
387 mArray[index] = value;
388 mSize++;
389 return true;
390 }
391
392 /**
393 * Perform a {@link #add(Object)} of all values in <var>array</var>
394 * @param array The array whose contents are to be retrieved.
395 */
Dianne Hackborn497175b2014-07-01 12:56:08 -0700396 public void addAll(ArraySet<? extends E> array) {
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700397 final int N = array.mSize;
398 ensureCapacity(mSize + N);
399 if (mSize == 0) {
400 if (N > 0) {
401 System.arraycopy(array.mHashes, 0, mHashes, 0, N);
402 System.arraycopy(array.mArray, 0, mArray, 0, N);
403 mSize = N;
404 }
405 } else {
406 for (int i=0; i<N; i++) {
407 add(array.valueAt(i));
408 }
409 }
410 }
411
412 /**
413 * Removes the specified object from this set.
414 *
415 * @param object the object to remove.
416 * @return {@code true} if this set was modified, {@code false} otherwise.
417 */
418 @Override
419 public boolean remove(Object object) {
Adam Lesinski4e9c07c2014-08-26 11:53:32 -0700420 final int index = indexOf(object);
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700421 if (index >= 0) {
422 removeAt(index);
423 return true;
424 }
425 return false;
426 }
427
428 /**
429 * Remove the key/value mapping at the given index.
430 * @param index The desired index, must be between 0 and {@link #size()}-1.
431 * @return Returns the value that was stored at this index.
432 */
433 public E removeAt(int index) {
Dianne Hackborn62d708f2013-07-25 16:42:59 -0700434 final Object old = mArray[index];
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700435 if (mSize <= 1) {
436 // Now empty.
437 if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to 0");
438 freeArrays(mHashes, mArray, mSize);
Adam Lesinski776abc22014-03-07 11:30:59 -0500439 mHashes = EmptyArray.INT;
440 mArray = EmptyArray.OBJECT;
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700441 mSize = 0;
442 } else {
443 if (mHashes.length > (BASE_SIZE*2) && mSize < mHashes.length/3) {
444 // Shrunk enough to reduce size of arrays. We don't allow it to
445 // shrink smaller than (BASE_SIZE*2) to avoid flapping between
446 // that and BASE_SIZE.
447 final int n = mSize > (BASE_SIZE*2) ? (mSize + (mSize>>1)) : (BASE_SIZE*2);
448
449 if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to " + n);
450
451 final int[] ohashes = mHashes;
452 final Object[] oarray = mArray;
453 allocArrays(n);
454
455 mSize--;
456 if (index > 0) {
457 if (DEBUG) Log.d(TAG, "remove: copy from 0-" + index + " to 0");
458 System.arraycopy(ohashes, 0, mHashes, 0, index);
459 System.arraycopy(oarray, 0, mArray, 0, index);
460 }
461 if (index < mSize) {
462 if (DEBUG) Log.d(TAG, "remove: copy from " + (index+1) + "-" + mSize
463 + " to " + index);
464 System.arraycopy(ohashes, index + 1, mHashes, index, mSize - index);
465 System.arraycopy(oarray, index + 1, mArray, index, mSize - index);
466 }
467 } else {
468 mSize--;
469 if (index < mSize) {
470 if (DEBUG) Log.d(TAG, "remove: move " + (index+1) + "-" + mSize
471 + " to " + index);
472 System.arraycopy(mHashes, index + 1, mHashes, index, mSize - index);
473 System.arraycopy(mArray, index + 1, mArray, index, mSize - index);
474 }
475 mArray[mSize] = null;
476 }
477 }
Dianne Hackborn62d708f2013-07-25 16:42:59 -0700478 return (E)old;
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700479 }
480
481 /**
Andreas Gampef9345e92015-03-04 17:14:10 -0800482 * Perform a {@link #remove(Object)} of all values in <var>array</var>
483 * @param array The array whose contents are to be removed.
484 */
485 public boolean removeAll(ArraySet<? extends E> array) {
486 // TODO: If array is sufficiently large, a marking approach might be beneficial. In a first
487 // pass, use the property that the sets are sorted by hash to make this linear passes
488 // (except for hash collisions, which means worst case still n*m), then do one
489 // collection pass into a new array. This avoids binary searches and excessive memcpy.
490 final int N = array.mSize;
491
492 // Note: ArraySet does not make thread-safety guarantees. So instead of OR-ing together all
493 // the single results, compare size before and after.
494 final int originalSize = mSize;
495 for (int i = 0; i < N; i++) {
496 remove(array.valueAt(i));
497 }
498 return originalSize != mSize;
499 }
500
501 /**
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700502 * Return the number of items in this array map.
503 */
504 @Override
505 public int size() {
506 return mSize;
507 }
508
509 @Override
510 public Object[] toArray() {
511 Object[] result = new Object[mSize];
512 System.arraycopy(mArray, 0, result, 0, mSize);
513 return result;
514 }
515
516 @Override
517 public <T> T[] toArray(T[] array) {
518 if (array.length < mSize) {
519 @SuppressWarnings("unchecked") T[] newArray
520 = (T[]) Array.newInstance(array.getClass().getComponentType(), mSize);
521 array = newArray;
522 }
523 System.arraycopy(mArray, 0, array, 0, mSize);
524 if (array.length > mSize) {
525 array[mSize] = null;
526 }
527 return array;
528 }
529
530 /**
531 * {@inheritDoc}
532 *
533 * <p>This implementation returns false if the object is not a set, or
534 * if the sets have different sizes. Otherwise, for each value in this
535 * set, it checks to make sure the value also exists in the other set.
536 * If any value doesn't exist, the method returns false; otherwise, it
537 * returns true.
538 */
539 @Override
540 public boolean equals(Object object) {
541 if (this == object) {
542 return true;
543 }
544 if (object instanceof Set) {
545 Set<?> set = (Set<?>) object;
546 if (size() != set.size()) {
547 return false;
548 }
549
550 try {
551 for (int i=0; i<mSize; i++) {
552 E mine = valueAt(i);
553 if (!set.contains(mine)) {
554 return false;
555 }
556 }
557 } catch (NullPointerException ignored) {
558 return false;
559 } catch (ClassCastException ignored) {
560 return false;
561 }
562 return true;
563 }
564 return false;
565 }
566
567 /**
568 * {@inheritDoc}
569 */
570 @Override
571 public int hashCode() {
572 final int[] hashes = mHashes;
573 int result = 0;
574 for (int i = 0, s = mSize; i < s; i++) {
575 result += hashes[i];
576 }
577 return result;
578 }
579
Dianne Hackborna17c0f52013-06-20 18:59:11 -0700580 /**
581 * {@inheritDoc}
582 *
583 * <p>This implementation composes a string by iterating over its values. If
584 * this set contains itself as a value, the string "(this Set)"
585 * will appear in its place.
586 */
587 @Override
588 public String toString() {
589 if (isEmpty()) {
590 return "{}";
591 }
592
593 StringBuilder buffer = new StringBuilder(mSize * 14);
594 buffer.append('{');
595 for (int i=0; i<mSize; i++) {
596 if (i > 0) {
597 buffer.append(", ");
598 }
599 Object value = valueAt(i);
600 if (value != this) {
601 buffer.append(value);
602 } else {
603 buffer.append("(this Set)");
604 }
605 }
606 buffer.append('}');
607 return buffer.toString();
608 }
609
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700610 // ------------------------------------------------------------------------
611 // Interop with traditional Java containers. Not as efficient as using
612 // specialized collection APIs.
613 // ------------------------------------------------------------------------
614
615 private MapCollections<E, E> getCollection() {
616 if (mCollections == null) {
617 mCollections = new MapCollections<E, E>() {
618 @Override
619 protected int colGetSize() {
620 return mSize;
621 }
622
623 @Override
624 protected Object colGetEntry(int index, int offset) {
625 return mArray[index];
626 }
627
628 @Override
629 protected int colIndexOfKey(Object key) {
Adam Lesinski4e9c07c2014-08-26 11:53:32 -0700630 return indexOf(key);
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700631 }
632
633 @Override
634 protected int colIndexOfValue(Object value) {
Adam Lesinski4e9c07c2014-08-26 11:53:32 -0700635 return indexOf(value);
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700636 }
637
638 @Override
639 protected Map<E, E> colGetMap() {
640 throw new UnsupportedOperationException("not a map");
641 }
642
643 @Override
644 protected void colPut(E key, E value) {
645 add(key);
646 }
647
648 @Override
649 protected E colSetValue(int index, E value) {
650 throw new UnsupportedOperationException("not a map");
651 }
652
653 @Override
654 protected void colRemoveAt(int index) {
655 removeAt(index);
656 }
657
658 @Override
659 protected void colClear() {
660 clear();
661 }
662 };
663 }
664 return mCollections;
665 }
666
Dianne Hackbornf16747d2015-06-10 17:49:43 -0700667 /**
668 * Return an {@link java.util.Iterator} over all values in the set.
669 *
670 * <p><b>Note:</b> this is a fairly inefficient way to access the array contents, it
671 * requires generating a number of temporary objects and allocates additional state
672 * information associated with the container that will remain for the life of the container.</p>
673 */
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700674 @Override
675 public Iterator<E> iterator() {
676 return getCollection().getKeySet().iterator();
677 }
678
Dianne Hackbornf16747d2015-06-10 17:49:43 -0700679 /**
680 * Determine if the array set contains all of the values in the given collection.
681 * @param collection The collection whose contents are to be checked against.
682 * @return Returns true if this array set contains a value for every entry
683 * in <var>collection</var>, else returns false.
684 */
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700685 @Override
686 public boolean containsAll(Collection<?> collection) {
687 Iterator<?> it = collection.iterator();
688 while (it.hasNext()) {
689 if (!contains(it.next())) {
690 return false;
691 }
692 }
693 return true;
694 }
695
Dianne Hackbornf16747d2015-06-10 17:49:43 -0700696 /**
697 * Perform an {@link #add(Object)} of all values in <var>collection</var>
698 * @param collection The collection whose contents are to be retrieved.
699 */
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700700 @Override
701 public boolean addAll(Collection<? extends E> collection) {
702 ensureCapacity(mSize + collection.size());
703 boolean added = false;
704 for (E value : collection) {
705 added |= add(value);
706 }
707 return added;
708 }
709
Dianne Hackbornf16747d2015-06-10 17:49:43 -0700710 /**
711 * Remove all values in the array set that exist in the given collection.
712 * @param collection The collection whose contents are to be used to remove values.
713 * @return Returns true if any values were removed from the array set, else false.
714 */
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700715 @Override
716 public boolean removeAll(Collection<?> collection) {
717 boolean removed = false;
718 for (Object value : collection) {
719 removed |= remove(value);
720 }
721 return removed;
722 }
723
Dianne Hackbornf16747d2015-06-10 17:49:43 -0700724 /**
725 * Remove all values in the array set that do <b>not</b> exist in the given collection.
726 * @param collection The collection whose contents are to be used to determine which
727 * values to keep.
728 * @return Returns true if any values were removed from the array set, else false.
729 */
Dianne Hackborn21ab6f42013-06-10 15:59:15 -0700730 @Override
731 public boolean retainAll(Collection<?> collection) {
732 boolean removed = false;
733 for (int i=mSize-1; i>=0; i--) {
734 if (!collection.contains(mArray[i])) {
735 removeAt(i);
736 removed = true;
737 }
738 }
739 return removed;
740 }
741}