Package arc.struct
Class ObjectSet<T>
java.lang.Object
arc.struct.ObjectSet<T>
- Direct Known Subclasses:
OrderedSet
An unordered set where the keys are objects. This implementation uses cuckoo hashing using 3 hashes, random walking, and a
small stash for problematic keys. Null keys are not allowed. No allocation is done except when growing the table size.
This set performs very fast contains and remove (typically O(1), worst case O(log(n))). Add may be a bit slower, depending on hash collisions. Load factors greater than 0.91 greatly increase the chances the set will have to rehash to the next higher POT size.
Iteration can be very slow for a set with a large capacity.
This set performs very fast contains and remove (typically O(1), worst case O(log(n))). Add may be a bit slower, depending on hash collisions. Load factors greater than 0.91 greatly increase the chances the set will have to rehash to the next higher POT size.
Iteration can be very slow for a set with a large capacity.
clear(int)
and shrink(int)
can be used to reduce
the capacity. OrderedSet
provides much faster iteration.-
Nested Class Summary
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates a new set with an initial capacity of 51 and a load factor of 0.8.ObjectSet
(int initialCapacity) Creates a new set with a load factor of 0.8.ObjectSet
(int initialCapacity, float loadFactor) Creates a new set with the specified initial capacity and load factor.Creates a new set identical to the specified set. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns true if the key was not already in the set.void
void
void
void
void
void
clear()
Clears the set, leaving the backing arrays at the current capacity.void
clear
(int maximumCapacity) Clears the set and reduces the size of the backing arrays to be the specified capacity, if they are larger.boolean
void
void
ensureCapacity
(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items.boolean
first()
int
hashCode()
boolean
isEmpty()
Returns true if the set is empty.iterator()
Returns an iterator for the keys in the set.boolean
Returns true if the key was removed.void
void
void
Allocates a new set with all elements that match the predicate.void
shrink
(int maximumCapacity) Reduces the size of the backing arrays to be the specified capacity or less.toSeq()
toString()
static <T> ObjectSet<T>
static <T> ObjectSet<T>
with
(T... array) Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
size
public int size
-
-
Constructor Details
-
ObjectSet
public ObjectSet()Creates a new set with an initial capacity of 51 and a load factor of 0.8. -
ObjectSet
public ObjectSet(int initialCapacity) Creates a new set with a load factor of 0.8.- Parameters:
initialCapacity
- If not a power of two, it is increased to the next nearest power of two.
-
ObjectSet
public ObjectSet(int initialCapacity, float loadFactor) Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity items before growing the backing table.- Parameters:
initialCapacity
- If not a power of two, it is increased to the next nearest power of two.
-
ObjectSet
Creates a new set identical to the specified set.
-
-
Method Details
-
with
-
with
-
select
Allocates a new set with all elements that match the predicate. -
toSeq
-
each
-
add
Returns true if the key was not already in the set. If this set already contains the key, the call leaves the set unchanged and returns false. -
addAll
-
addAll
-
addAll
-
addAll
-
addAll
-
removeAll
-
removeAll
-
removeAll
-
remove
Returns true if the key was removed. -
isEmpty
public boolean isEmpty()Returns true if the set is empty. -
shrink
public void shrink(int maximumCapacity) Reduces the size of the backing arrays to be the specified capacity or less. If the capacity is already less, nothing is done. If the set contains more items than the specified capacity, the next highest power of two capacity is used instead. -
clear
public void clear(int maximumCapacity) Clears the set and reduces the size of the backing arrays to be the specified capacity, if they are larger. The reduction is done by allocating new arrays, though for large arrays this can be faster than clearing the existing array. -
clear
public void clear()Clears the set, leaving the backing arrays at the current capacity. When the capacity is high and the population is low, iteration can be unnecessarily slow.clear(int)
can be used to reduce the capacity. -
contains
-
get
- Returns:
- May be null.
-
first
-
ensureCapacity
public void ensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes. -
hashCode
public int hashCode() -
equals
-
toString
-
toString
-
iterator
Returns an iterator for the keys in the set. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use theObjectSet<T>.ObjectSetIterator
constructor for nested or multithreaded iteration.
-