Package org.agrona.collections
Class ObjectHashSet<T>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<T>
-
- org.agrona.collections.ObjectHashSet<T>
-
- Type Parameters:
T
- type of values stored in theSet
- All Implemented Interfaces:
java.lang.Iterable<T>
,java.util.Collection<T>
,java.util.Set<T>
public class ObjectHashSet<T> extends java.util.AbstractSet<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:
ObjectHashSet.ObjectIterator
,Set
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
ObjectHashSet.ObjectIterator
Iterator over the set which can be optionally cached to avoid allocation.
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_INITIAL_CAPACITY
The initial capacity used when none is specified in the constructor.private ObjectHashSet.ObjectIterator
iterator
private float
loadFactor
(package private) static java.lang.Object
MISSING_VALUE
private java.util.function.IntConsumer
resizeNotifier
private int
resizeThreshold
private boolean
shouldAvoidAllocation
private int
size
private T[]
values
-
Constructor Summary
Constructors Constructor Description ObjectHashSet()
Construct 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(T value)
boolean
addAll(java.util.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
contains(java.lang.Object value)
boolean
containsAll(java.util.Collection<?> coll)
void
copy(ObjectHashSet<T> that)
Copy data from the providedObjectHashSet
into this one.ObjectHashSet<T>
difference(ObjectHashSet<T> other)
Fast Path set difference for comparison with another ObjectHashSet.boolean
equals(java.lang.Object other)
void
forEach(java.util.function.Consumer<? super T> action)
int
hashCode()
private void
increaseCapacity()
boolean
isEmpty()
ObjectHashSet.ObjectIterator
iterator()
float
loadFactor()
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
remove(java.lang.Object value)
boolean
removeAll(java.util.Collection<?> coll)
boolean
removeAll(ObjectHashSet<T> coll)
Alias forremoveAll(Collection)
for the specialized case when removing another ObjectHashSet, avoids boxing and allocations.void
resizeNotifier(java.util.function.IntConsumer resizeNotifier)
Add a Consumer that will be called when the collection is re-sized.int
resizeThreshold()
Get the actual threshold which when reached the map will resize.int
size()
java.lang.String
toString()
-
-
-
Field Detail
-
DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_INITIAL_CAPACITY
The initial capacity used when none is specified in the constructor.- See Also:
- Constant Field Values
-
MISSING_VALUE
static final java.lang.Object MISSING_VALUE
-
shouldAvoidAllocation
private final boolean shouldAvoidAllocation
-
loadFactor
private final float loadFactor
-
resizeThreshold
private int resizeThreshold
-
size
private int size
-
values
private T[] values
-
iterator
private ObjectHashSet.ObjectIterator iterator
-
resizeNotifier
private java.util.function.IntConsumer resizeNotifier
-
-
Constructor Detail
-
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 Detail
-
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
public void resizeNotifier(java.util.function.IntConsumer resizeNotifier)
Add a Consumer that will be called when the collection is re-sized.- Parameters:
resizeNotifier
- IntConsumer containing the new resizeThreshold
-
add
public boolean add(T value)
- Specified by:
add
in interfacejava.util.Collection<T>
- Specified by:
add
in interfacejava.util.Set<T>
- Overrides:
add
in classjava.util.AbstractCollection<T>
- Parameters:
value
- the value to add.- Returns:
- true if the collection has changed, false otherwise.
- Throws:
java.lang.NullPointerException
- if the value is null.
-
increaseCapacity
private void increaseCapacity()
-
rehash
private void rehash(int newCapacity)
-
remove
public boolean remove(java.lang.Object value)
-
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
public boolean contains(java.lang.Object value)
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
clear
public void clear()
-
containsAll
public boolean containsAll(java.util.Collection<?> coll)
-
addAll
public boolean addAll(java.util.Collection<? extends T> coll)
-
addAll
public boolean addAll(ObjectHashSet<T> coll)
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
public ObjectHashSet<T> difference(ObjectHashSet<T> other)
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
public boolean removeAll(java.util.Collection<?> coll)
-
removeAll
public boolean removeAll(ObjectHashSet<T> coll)
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
public ObjectHashSet.ObjectIterator iterator()
-
copy
public void copy(ObjectHashSet<T> that)
Copy data from the providedObjectHashSet
into this one.- Parameters:
that
- set to copy data from.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.util.AbstractCollection<T>
-
equals
public boolean equals(java.lang.Object other)
-
hashCode
public int hashCode()
-
forEach
public void forEach(java.util.function.Consumer<? super T> action)
-
-