Package org.plumelib.util
Class ArraySet<E extends @UnknownSignedness Object>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
org.plumelib.util.ArraySet<E>
- Type Parameters:
E- the type of the set elements
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Set<E>
A set backed by an array. It permits null values and its iterator has deterministic ordering.
Compared to a HashSet or LinkedHashSet: For very small sets, this uses much less space, has comparable performance, and (like a LinkedHashSet) is deterministic, with elements returned in the order they were inserted. For large sets, this is significantly less performant than other set implementations.
Compared to a TreeSet: This uses somewhat less space, and it does not require defining a comparator. This isn't sorted but does have deterministic ordering. For large sets, this is significantly less performant than other set implementations.
Other ArraySet implementations include:
- https://docs.oracle.com/en/database/oracle/oracle-database/19/olapi/oracle/olapi/ArraySet.html
- https://developer.android.com/reference/android/util/ArraySet
- https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/CopyOnWriteArraySet.html
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classAn iterator over the ArraySet. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LessThan({"values.length + 1"}),@org.checkerframework.checker.index.qual.IndexOrHigh({"values"}) intThe number of used slots in the representation of this.(package private) intThe number of times this set's size has been modified by adding or removing an element.private @Nullable E[]The values. -
Constructor Summary
ConstructorsModifierConstructorDescriptionArraySet()Constructs an emptyArraySetwith the default initial capacity.ArraySet(int initialCapacity) Constructs an emptyArraySetwith the specified initial capacity.privatePrivate constructor.ArraySet(Collection<? extends E> m) Constructs a newArraySetwith the same elements as the given collection. -
Method Summary
Modifier and TypeMethodDescriptionprivate booleanAdds an element to this set.booleanbooleanaddAll(Collection<? extends E> c) voidclear()clone()Returns a copy of this.booleanvoidprivate voidgrow()Increases the capacity of the array.private intReturns the index of the given value, or -1 if it does not appear.booleanisEmpty()iterator()static <E> Set<E>newArraySetOrHashSet(int capacity) Returns a new ArraySet or HashSet with the given capacity.static <E> Set<E>Returns a new ArraySet or HashSet with the given elements.static <E> Set<E>newArraySetOrLinkedHashSet(int capacity) Returns a new ArraySet or LinkedHashSet with the given capacity.static <E> Set<E>Returns a new ArraySet or LinkedHashSet with the given elements.booleanbooleanremoveAll(Collection<?> c) private booleanremoveIndex(@org.checkerframework.checker.index.qual.GTENegativeOne int index) Remove the element at the given index.(package private) Stringrepr()Returns the internal representation, printed.@org.checkerframework.checker.index.qual.NonNegative intsize()voidsort()Sorts the internal representation of this.voidsort(Comparator<? super E> comparator) Sorts the internal representation of this, using the given comparator.Methods inherited from class java.util.AbstractSet
equals, hashCodeMethods inherited from class java.util.AbstractCollection
containsAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
containsAll, retainAll, spliterator, toArray, toArray
-
Field Details
-
values
The values. Null if capacity=0. -
size
private @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LessThan({"values.length + 1"}),@org.checkerframework.checker.index.qual.IndexOrHigh({"values"}) int sizeThe number of used slots in the representation of this. -
sizeModificationCount
transient int sizeModificationCountThe number of times this set's size has been modified by adding or removing an element. This field is used to make view iterators fail-fast.
-
-
Constructor Details
-
ArraySet
@SideEffectFree public ArraySet(int initialCapacity) Constructs an emptyArraySetwith the specified initial capacity.- Parameters:
initialCapacity- the initial capacity- Throws:
IllegalArgumentException- if the initial capacity is negative
-
ArraySet
@SideEffectFree public ArraySet()Constructs an emptyArraySetwith the default initial capacity. -
ArraySet
@SideEffectFree private ArraySet(E[] values, @org.checkerframework.checker.index.qual.LTEqLengthOf({"values"}) int size) Private constructor. Installs the given objects in this as its representation, without making defensive copies.- Parameters:
values- the valuessize- the number of used items in the array; may be less than its length
-
ArraySet
Constructs a newArraySetwith the same elements as the given collection.- Parameters:
m- the collection whose elements are to be placed in the new set- Throws:
NullPointerException- if the given set is null
-
-
Method Details
-
newArraySetOrHashSet
Returns a new ArraySet or HashSet with the given capacity. Uses an ArraySet if the capacity is small, and a HashSet otherwise.- Type Parameters:
E- the type of the elements- Parameters:
capacity- the expected maximum number of elements in the set- Returns:
- a new ArraySet or HashSet with the given capacity
-
newArraySetOrHashSet
Returns a new ArraySet or HashSet with the given elements. Uses an ArraySet if the capacity is small, and a HashSet otherwise.- Type Parameters:
E- the type of the elements- Parameters:
s- the elements to put in the returned set- Returns:
- a new ArraySet or HashSet with the given elements
-
newArraySetOrLinkedHashSet
Returns a new ArraySet or LinkedHashSet with the given capacity. Uses an ArraySet if the capacity is small, and a LinkedHashSet otherwise.- Type Parameters:
E- the type of the elements- Parameters:
capacity- the expected maximum number of elements in the set- Returns:
- a new ArraySet or LinkedHashSet with the given capacity
-
newArraySetOrLinkedHashSet
Returns a new ArraySet or LinkedHashSet with the given elements. Uses an ArraySet if the capacity is small, and a LinkedHashSet otherwise.- Type Parameters:
E- the type of the elements- Parameters:
s- the elements to put in the returned set- Returns:
- a new ArraySet or LinkedHashSet with the given elements
-
add
Adds an element to this set.- Parameters:
index- the index ofvalueinvalues. If -1, add a new element. Otherwise, do nothing.value- the value- Returns:
- true if the method modified this set
-
grow
private void grow()Increases the capacity of the array. -
removeIndex
private boolean removeIndex(@org.checkerframework.checker.index.qual.GTENegativeOne int index) Remove the element at the given index. Does nothing if index is -1.- Parameters:
index- the index of the element to remove- Returns:
- true if this set was modified
-
size
@Pure public @org.checkerframework.checker.index.qual.NonNegative int size() -
isEmpty
@Pure public boolean isEmpty() -
indexOf
Returns the index of the given value, or -1 if it does not appear. UsesObjects.equalsfor comparison.- Parameters:
value- a value to find- Returns:
- the index of the given value, or -1 if it does not appear
-
contains
-
add
-
remove
-
addAll
-
removeAll
-
clear
public void clear() -
iterator
- Specified by:
iteratorin interfaceCollection<E extends @UnknownSignedness Object>- Specified by:
iteratorin interfaceIterable<E extends @UnknownSignedness Object>- Specified by:
iteratorin interfaceSet<E extends @UnknownSignedness Object>- Specified by:
iteratorin classAbstractCollection<E extends @UnknownSignedness Object>
-
forEach
-
clone
Returns a copy of this. -
repr
Returns the internal representation, printed.- Returns:
- the internal representation, printed
-
sort
public void sort()Sorts the internal representation of this. Side-effects the representation, but not the abstract value, of this. Requires that the elements of this are comparable. -
sort
Sorts the internal representation of this, using the given comparator. Side-effects the representation, but not the abstract value, of this.- Parameters:
comparator- imposes an ordering on the elements of this
-