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 the Set
All Implemented Interfaces:
Iterable<T>, Collection<T>, Set<T>

public class ObjectHashSet<T> extends 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:
  • Field Details

    • DEFAULT_INITIAL_CAPACITY

      public static final int DEFAULT_INITIAL_CAPACITY
      The initial capacity used when none is specified in the constructor.
      See Also:
    • MISSING_VALUE

      static final 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<T>.ObjectIterator iterator
    • resizeNotifier

      private IntConsumer resizeNotifier
  • Constructor Details

    • ObjectHashSet

      public ObjectHashSet()
      Construct a hash set with DEFAULT_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

      public void resizeNotifier(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 interface Collection<T>
      Specified by:
      add in interface Set<T>
      Overrides:
      add in class AbstractCollection<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

      public boolean remove(Object value)
      Specified by:
      remove in interface Collection<T>
      Specified by:
      remove in interface Set<T>
      Overrides:
      remove in class AbstractCollection<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

      public boolean contains(Object value)
      Specified by:
      contains in interface Collection<T>
      Specified by:
      contains in interface Set<T>
      Overrides:
      contains in class AbstractCollection<T>
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface Set<T>
      Specified by:
      size in class AbstractCollection<T>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<T>
      Specified by:
      isEmpty in interface Set<T>
      Overrides:
      isEmpty in class AbstractCollection<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>
      Specified by:
      clear in interface Set<T>
      Overrides:
      clear in class AbstractCollection<T>
    • containsAll

      public boolean containsAll(Collection<?> coll)
      Specified by:
      containsAll in interface Collection<T>
      Specified by:
      containsAll in interface Set<T>
      Overrides:
      containsAll in class AbstractCollection<T>
    • addAll

      public boolean addAll(Collection<? extends T> coll)
      Specified by:
      addAll in interface Collection<T>
      Specified by:
      addAll in interface Set<T>
      Overrides:
      addAll in class AbstractCollection<T>
    • addAll

      public boolean addAll(ObjectHashSet<T> coll)
      Alias for addAll(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(Collection<?> coll)
      Specified by:
      removeAll in interface Collection<T>
      Specified by:
      removeAll in interface Set<T>
      Overrides:
      removeAll in class AbstractSet<T>
    • removeAll

      public boolean removeAll(ObjectHashSet<T> coll)
      Alias for removeAll(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
    • disjunction

      private static <T> boolean disjunction(Collection<T> coll, Predicate<T> predicate)
    • iterator

      public ObjectHashSet<T>.ObjectIterator iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface Set<T>
      Specified by:
      iterator in class AbstractCollection<T>
    • copy

      public void copy(ObjectHashSet<T> that)
      Copy data from the provided ObjectHashSet into this one.
      Parameters:
      that - set to copy data from.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<T>
    • equals

      public boolean equals(Object other)
      Specified by:
      equals in interface Collection<T>
      Specified by:
      equals in interface Set<T>
      Overrides:
      equals in class AbstractSet<T>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<T>
      Specified by:
      hashCode in interface Set<T>
      Overrides:
      hashCode in class AbstractSet<T>
    • forEach

      public void forEach(Consumer<? super T> action)