Package org.agrona.collections
Class ObjectHashSet<T>
java.lang.Object
java.util.AbstractCollection<T>
java.util.AbstractSet<T>
org.agrona.collections.ObjectHashSet<T>
- Type Parameters:
T
- type of values stored in theSet
- All Implemented Interfaces:
Iterable<T>
,Collection<T>
,Set<T>
Open-addressing with linear-probing expandable hash set. Allocation free in steady state use when expanded.
Ability to be notified when resizing occurs so that appropriate sizing can be implemented.
Not Threadsafe.
This HashSet caches its iterator object by default, which can be overridden, so nested iteration is not supported. You can override this behaviour at construction by indicating that the iterator should not be cached.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal class
Iterator over the set which can be optionally cached to avoid allocation. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The initial capacity used when none is specified in the constructor.private ObjectHashSet<T>.ObjectIterator
private final float
(package private) static final Object
private IntConsumer
private int
private final boolean
private int
private T[]
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a hash set withDEFAULT_INITIAL_CAPACITY
,Hashing.DEFAULT_LOAD_FACTOR
, and iterator caching support.ObjectHashSet
(int proposedCapacity) Construct a hash set with a proposed initial capacity,Hashing.DEFAULT_LOAD_FACTOR
, and iterator caching support.ObjectHashSet
(int proposedCapacity, float loadFactor) Construct a hash set with a proposed initial capacity, load factor, and iterator caching support.ObjectHashSet
(int proposedCapacity, float loadFactor, boolean shouldAvoidAllocation) Construct a hash set with a proposed initial capacity, load factor, and indicated iterator caching support. -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends T> coll) boolean
addAll
(ObjectHashSet<T> coll) Alias foraddAll(Collection)
for the specialized case when adding another ObjectHashSet, avoids boxing and allocations.int
capacity()
Get the total capacity for the set to which the load factor with be a fraction of.void
clear()
void
compact()
Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.(package private) void
compactChain
(int deleteIndex) boolean
boolean
containsAll
(Collection<?> coll) void
copy
(ObjectHashSet<T> that) Copy data from the providedObjectHashSet
into this one.difference
(ObjectHashSet<T> other) Fast Path set difference for comparison with another ObjectHashSet.boolean
void
int
hashCode()
private void
boolean
isEmpty()
iterator()
float
Get the load factor beyond which the set will increase size.private static int
next
(int index, int mask) private void
rehash
(int newCapacity) boolean
boolean
removeAll
(Collection<?> coll) boolean
removeAll
(ObjectHashSet<T> coll) Alias forremoveAll(Collection)
for the specialized case when removing another ObjectHashSet, avoids boxing and allocations.void
resizeNotifier
(IntConsumer resizeNotifier) Add a Consumer that will be called when the collection is re-sized.int
Get the actual threshold which when reached the map will resize.int
size()
toString()
Methods inherited from class java.util.AbstractCollection
retainAll, toArray, toArray
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.Set
retainAll, spliterator, toArray, toArray
-
Field Details
-
DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_INITIAL_CAPACITYThe initial capacity used when none is specified in the constructor.- See Also:
-
MISSING_VALUE
-
shouldAvoidAllocation
private final boolean shouldAvoidAllocation -
loadFactor
private final float loadFactor -
resizeThreshold
private int resizeThreshold -
size
private int size -
values
-
iterator
-
resizeNotifier
-
-
Constructor Details
-
ObjectHashSet
public ObjectHashSet()Construct a hash set withDEFAULT_INITIAL_CAPACITY
,Hashing.DEFAULT_LOAD_FACTOR
, and iterator caching support. -
ObjectHashSet
public ObjectHashSet(int proposedCapacity) Construct a hash set with a proposed initial capacity,Hashing.DEFAULT_LOAD_FACTOR
, and iterator caching support.- Parameters:
proposedCapacity
- for the initial capacity of the set.
-
ObjectHashSet
public ObjectHashSet(int proposedCapacity, float loadFactor) Construct a hash set with a proposed initial capacity, load factor, and iterator caching support.- Parameters:
proposedCapacity
- for the initial capacity of the set.loadFactor
- to be used for resizing.
-
ObjectHashSet
public ObjectHashSet(int proposedCapacity, float loadFactor, boolean shouldAvoidAllocation) Construct a hash set with a proposed initial capacity, load factor, and indicated iterator caching support.- Parameters:
proposedCapacity
- for the initial capacity of the set.loadFactor
- to be used for resizing.shouldAvoidAllocation
- should the iterator be cached to avoid further allocation.
-
-
Method Details
-
loadFactor
public float loadFactor()Get the load factor beyond which the set will increase size.- Returns:
- load factor for when the set should increase size.
-
capacity
public int capacity()Get the total capacity for the set to which the load factor with be a fraction of.- Returns:
- the total capacity for the set.
-
resizeThreshold
public int resizeThreshold()Get the actual threshold which when reached the map will resize. This is a function of the current capacity and load factor.- Returns:
- the threshold when the map will resize.
-
resizeNotifier
Add a Consumer that will be called when the collection is re-sized.- Parameters:
resizeNotifier
- IntConsumer containing the new resizeThreshold
-
add
- Specified by:
add
in interfaceCollection<T>
- Specified by:
add
in interfaceSet<T>
- Overrides:
add
in classAbstractCollection<T>
- Parameters:
value
- the value to add.- Returns:
- true if the collection has changed, false otherwise.
- Throws:
NullPointerException
- if the value is null.
-
increaseCapacity
private void increaseCapacity() -
rehash
private void rehash(int newCapacity) -
remove
- Specified by:
remove
in interfaceCollection<T>
- Specified by:
remove
in interfaceSet<T>
- Overrides:
remove
in classAbstractCollection<T>
- Parameters:
value
- the value to remove.- Returns:
- true if the value was present, false otherwise.
-
next
private static int next(int index, int mask) -
compactChain
void compactChain(int deleteIndex) -
compact
public void compact()Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor. -
contains
- Specified by:
contains
in interfaceCollection<T>
- Specified by:
contains
in interfaceSet<T>
- Overrides:
contains
in classAbstractCollection<T>
-
size
public int size()- Specified by:
size
in interfaceCollection<T>
- Specified by:
size
in interfaceSet<T>
- Specified by:
size
in classAbstractCollection<T>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<T>
- Specified by:
isEmpty
in interfaceSet<T>
- Overrides:
isEmpty
in classAbstractCollection<T>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<T>
- Specified by:
clear
in interfaceSet<T>
- Overrides:
clear
in classAbstractCollection<T>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<T>
- Specified by:
containsAll
in interfaceSet<T>
- Overrides:
containsAll
in classAbstractCollection<T>
-
addAll
- Specified by:
addAll
in interfaceCollection<T>
- Specified by:
addAll
in interfaceSet<T>
- Overrides:
addAll
in classAbstractCollection<T>
-
addAll
Alias foraddAll(Collection)
for the specialized case when adding another ObjectHashSet, avoids boxing and allocations.- Parameters:
coll
- containing the values to be added.- Returns:
true
if this set changed as a result of the call.
-
difference
Fast Path set difference for comparison with another ObjectHashSet.NB: garbage free in the identical case, allocates otherwise.
- Parameters:
other
- the other set to subtract.- Returns:
- null if identical, otherwise the set of differences.
-
removeAll
- Specified by:
removeAll
in interfaceCollection<T>
- Specified by:
removeAll
in interfaceSet<T>
- Overrides:
removeAll
in classAbstractSet<T>
-
removeAll
Alias forremoveAll(Collection)
for the specialized case when removing another ObjectHashSet, avoids boxing and allocations.- Parameters:
coll
- containing the values to be removed.- Returns:
true
if this set changed as a result of the call.
-
iterator
-
copy
Copy data from the providedObjectHashSet
into this one.- Parameters:
that
- set to copy data from.
-
toString
- Overrides:
toString
in classAbstractCollection<T>
-
equals
- Specified by:
equals
in interfaceCollection<T>
- Specified by:
equals
in interfaceSet<T>
- Overrides:
equals
in classAbstractSet<T>
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceCollection<T>
- Specified by:
hashCode
in interfaceSet<T>
- Overrides:
hashCode
in classAbstractSet<T>
-
forEach
-