Package org.agrona.collections
Class LongHashSet
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<java.lang.Long>
-
- org.agrona.collections.LongHashSet
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Long>
,java.util.Collection<java.lang.Long>
,java.util.Set<java.lang.Long>
public class LongHashSet extends java.util.AbstractSet<java.lang.Long>
Open-addressing with linear-probing expandable hash set. Allocation free in steady state use when expanded.By storing elements as long primitives this significantly reduces memory consumption compared with Java's builtin
HashSet<Long>
. It implementsSet<Long>
for convenience, but calling functionality via those methods can add boxing overhead to your usage.This class is not Threadsafe.
This HashSet caches its iterator object by default, so nested iteration is not supported. You can override this behaviour at construction by indicating that the iterator should not be cached.
- See Also:
LongHashSet.LongIterator
,Set
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
LongHashSet.LongIterator
Iterator which supports unboxed access to the values viaLongHashSet.LongIterator.nextValue()
.
-
Field Summary
Fields Modifier and Type Field Description private boolean
containsMissingValue
static int
DEFAULT_INITIAL_CAPACITY
The initial capacity used when none is specified in the constructor.private LongHashSet.LongIterator
iterator
private float
loadFactor
(package private) static long
MISSING_VALUE
private int
resizeThreshold
private boolean
shouldAvoidAllocation
private int
sizeOfArrayValues
private long[]
values
-
Constructor Summary
Constructors Constructor Description LongHashSet()
Construct a hash set withDEFAULT_INITIAL_CAPACITY
,Hashing.DEFAULT_LOAD_FACTOR
, iterator caching support and0
as a missing value.LongHashSet(int proposedCapacity)
Construct a hash set with a proposed capacity,Hashing.DEFAULT_LOAD_FACTOR
, iterator caching support and0
as a missing value.LongHashSet(int proposedCapacity, float loadFactor)
Construct a hash set with a proposed initial capacity, load factor, iterator caching support and0
as a missing value.LongHashSet(int proposedCapacity, float loadFactor, boolean shouldAvoidAllocation)
Construct a hash set with a proposed initial capacity, load factor, iterator caching support and-1
as a missing value.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(long value)
Primitive specialised overload of {this#add(Long)}.boolean
add(java.lang.Long value)
boolean
addAll(java.util.Collection<? extends java.lang.Long> coll)
boolean
addAll(LongHashSet coll)
Alias foraddAll(Collection)
for the specialized case when adding another LongHashSet, 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(long value)
Contains method that does not box values.boolean
contains(java.lang.Object value)
boolean
containsAll(LongHashSet coll)
LongHashSet specialised variant of {this#containsAll(Collection)}.void
copy(LongHashSet that)
Copy values from anotherLongHashSet
longo this one.private void
copyValues(java.lang.Object[] arrayCopy)
LongHashSet
difference(LongHashSet other)
Fast Path set difference for comparison with another LongHashSet.boolean
equals(java.lang.Object other)
void
forEachLong(java.util.function.LongConsumer action)
Iterate over the collection without boxing.int
hashCode()
private void
increaseCapacity()
boolean
isEmpty()
LongHashSet.LongIterator
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(long value)
Specialised version of {this#remove(Object)} for long.boolean
remove(java.lang.Object value)
boolean
removeAll(java.util.Collection<?> coll)
boolean
removeAll(LongHashSet coll)
Alias forremoveAll(Collection)
for the specialized case when removing another LongHashSet, avoids boxing and allocations.boolean
removeIf(java.util.function.Predicate<? super java.lang.Long> filter)
boolean
removeIfLong(java.util.function.LongPredicate filter)
Removes all the elements of this collection that satisfy the given predicate.int
resizeThreshold()
Get the actual threshold which when reached the map will resize.boolean
retainAll(java.util.Collection<?> coll)
boolean
retainAll(LongHashSet coll)
Alias forretainAll(Collection)
for the specialized case when retaining on another LongHashSet, avoids boxing and allocations.int
size()
java.lang.Object[]
toArray()
<T> T[]
toArray(T[] a)
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 long MISSING_VALUE
- See Also:
- Constant Field Values
-
shouldAvoidAllocation
private final boolean shouldAvoidAllocation
-
containsMissingValue
private boolean containsMissingValue
-
loadFactor
private final float loadFactor
-
resizeThreshold
private int resizeThreshold
-
sizeOfArrayValues
private int sizeOfArrayValues
-
values
private long[] values
-
iterator
private LongHashSet.LongIterator iterator
-
-
Constructor Detail
-
LongHashSet
public LongHashSet()
Construct a hash set withDEFAULT_INITIAL_CAPACITY
,Hashing.DEFAULT_LOAD_FACTOR
, iterator caching support and0
as a missing value.
-
LongHashSet
public LongHashSet(int proposedCapacity)
Construct a hash set with a proposed capacity,Hashing.DEFAULT_LOAD_FACTOR
, iterator caching support and0
as a missing value.- Parameters:
proposedCapacity
- for the initial capacity of the set.
-
LongHashSet
public LongHashSet(int proposedCapacity, float loadFactor)
Construct a hash set with a proposed initial capacity, load factor, iterator caching support and0
as a missing value.- Parameters:
proposedCapacity
- for the initial capacity of the set.loadFactor
- to be used for resizing.
-
LongHashSet
public LongHashSet(int proposedCapacity, float loadFactor, boolean shouldAvoidAllocation)
Construct a hash set with a proposed initial capacity, load factor, iterator caching support and-1
as a missing value.- 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.
-
add
public boolean add(java.lang.Long value)
- Specified by:
add
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
add
in interfacejava.util.Set<java.lang.Long>
- Overrides:
add
in classjava.util.AbstractCollection<java.lang.Long>
-
add
public boolean add(long value)
Primitive specialised overload of {this#add(Long)}.- Parameters:
value
- the value to add.- Returns:
- true if the collection has changed, false otherwise.
-
increaseCapacity
private void increaseCapacity()
-
rehash
private void rehash(int newCapacity)
-
remove
public boolean remove(java.lang.Object value)
- Specified by:
remove
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
remove
in interfacejava.util.Set<java.lang.Long>
- Overrides:
remove
in classjava.util.AbstractCollection<java.lang.Long>
-
remove
public boolean remove(long value)
Specialised version of {this#remove(Object)} for long.- 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
public boolean contains(java.lang.Object value)
- Specified by:
contains
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
contains
in interfacejava.util.Set<java.lang.Long>
- Overrides:
contains
in classjava.util.AbstractCollection<java.lang.Long>
-
contains
public boolean contains(long value)
Contains method that does not box values.- Parameters:
value
- to be checked for if the set contains it.- Returns:
- true if the value is contained in the set otherwise false.
- See Also:
Collection.contains(Object)
-
size
public int size()
- Specified by:
size
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
size
in interfacejava.util.Set<java.lang.Long>
- Specified by:
size
in classjava.util.AbstractCollection<java.lang.Long>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
isEmpty
in interfacejava.util.Set<java.lang.Long>
- Overrides:
isEmpty
in classjava.util.AbstractCollection<java.lang.Long>
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
clear
in interfacejava.util.Set<java.lang.Long>
- Overrides:
clear
in classjava.util.AbstractCollection<java.lang.Long>
-
addAll
public boolean addAll(java.util.Collection<? extends java.lang.Long> coll)
- Specified by:
addAll
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
addAll
in interfacejava.util.Set<java.lang.Long>
- Overrides:
addAll
in classjava.util.AbstractCollection<java.lang.Long>
-
addAll
public boolean addAll(LongHashSet coll)
Alias foraddAll(Collection)
for the specialized case when adding another LongHashSet, avoids boxing and allocations.- Parameters:
coll
- containing the values to be added.- Returns:
true
if this set changed as a result of the call.
-
containsAll
public boolean containsAll(LongHashSet coll)
LongHashSet specialised variant of {this#containsAll(Collection)}.- Parameters:
coll
- long hash set to compare against.- Returns:
- true if every element in other is in this.
-
difference
public LongHashSet difference(LongHashSet other)
Fast Path set difference for comparison with another LongHashSet.Note: garbage free in the identical case, allocates otherwise.
- Parameters:
other
- the other set to subtract.- Returns:
- null if identical, otherwise the set of differences.
-
removeIf
public boolean removeIf(java.util.function.Predicate<? super java.lang.Long> filter)
-
removeIfLong
public boolean removeIfLong(java.util.function.LongPredicate filter)
Removes all the elements of this collection that satisfy the given predicate.NB: Renamed from removeIf to avoid overloading on parameter types of lambda expression, which doesn't play well with type inference in lambda expressions.
- Parameters:
filter
- which returnstrue
for elements to be removed.- Returns:
true
if any elements were removed.
-
removeAll
public boolean removeAll(java.util.Collection<?> coll)
- Specified by:
removeAll
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
removeAll
in interfacejava.util.Set<java.lang.Long>
- Overrides:
removeAll
in classjava.util.AbstractSet<java.lang.Long>
-
removeAll
public boolean removeAll(LongHashSet coll)
Alias forremoveAll(Collection)
for the specialized case when removing another LongHashSet, avoids boxing and allocations.- Parameters:
coll
- containing the values to be removed.- Returns:
true
if this set changed as a result of the call.
-
retainAll
public boolean retainAll(java.util.Collection<?> coll)
- Specified by:
retainAll
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
retainAll
in interfacejava.util.Set<java.lang.Long>
- Overrides:
retainAll
in classjava.util.AbstractCollection<java.lang.Long>
-
retainAll
public boolean retainAll(LongHashSet coll)
Alias forretainAll(Collection)
for the specialized case when retaining on another LongHashSet, avoids boxing and allocations.- Parameters:
coll
- containing elements to be retained in this set.- Returns:
true
if this set changed as a result of the call.
-
iterator
public LongHashSet.LongIterator iterator()
- Specified by:
iterator
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
iterator
in interfacejava.lang.Iterable<java.lang.Long>
- Specified by:
iterator
in interfacejava.util.Set<java.lang.Long>
- Specified by:
iterator
in classjava.util.AbstractCollection<java.lang.Long>
-
forEachLong
public void forEachLong(java.util.function.LongConsumer action)
Iterate over the collection without boxing.- Parameters:
action
- to be taken for each element.
-
copy
public void copy(LongHashSet that)
Copy values from anotherLongHashSet
longo this one.- Parameters:
that
- set to copy values from.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.util.AbstractCollection<java.lang.Long>
-
toArray
public <T> T[] toArray(T[] a)
- Specified by:
toArray
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
toArray
in interfacejava.util.Set<java.lang.Long>
- Overrides:
toArray
in classjava.util.AbstractCollection<java.lang.Long>
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArray
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
toArray
in interfacejava.util.Set<java.lang.Long>
- Overrides:
toArray
in classjava.util.AbstractCollection<java.lang.Long>
-
copyValues
private void copyValues(java.lang.Object[] arrayCopy)
-
equals
public boolean equals(java.lang.Object other)
- Specified by:
equals
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
equals
in interfacejava.util.Set<java.lang.Long>
- Overrides:
equals
in classjava.util.AbstractSet<java.lang.Long>
-
hashCode
public int hashCode()
- Specified by:
hashCode
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
hashCode
in interfacejava.util.Set<java.lang.Long>
- Overrides:
hashCode
in classjava.util.AbstractSet<java.lang.Long>
-
-