Package arc.struct

Class ObjectSet<T>

java.lang.Object
arc.struct.ObjectSet<T>
All Implemented Interfaces:
Eachable<T>, Iterable<T>
Direct Known Subclasses:
OrderedSet

public class ObjectSet<T> extends Object implements Iterable<T>, Eachable<T>
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. clear(int) and shrink(int) can be used to reduce the capacity. OrderedSet provides much faster iteration.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates 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 Type
    Method
    Description
    boolean
    add(T key)
    Returns true if the key was not already in the set.
    void
     
    void
    addAll(Seq<? extends T> array)
     
    void
    addAll(Seq<? extends T> array, int offset, int length)
     
    void
    addAll(T... array)
     
    void
    addAll(T[] array, int offset, int length)
     
    void
    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
    contains(T key)
     
    void
    each(Cons<? super T> cons)
     
    void
    ensureCapacity(int additionalCapacity)
    Increases the size of the backing array to accommodate the specified number of additional items.
    boolean
     
     
    get(T key)
     
    int
     
    boolean
    Returns true if the set is empty.
    Returns an iterator for the keys in the set.
    boolean
    remove(T key)
    Returns true if the key was removed.
    void
    removeAll(Seq<? extends T> array)
     
    void
    removeAll(T[] array)
     
    void
    removeAll(T[] array, int offset, int length)
     
    select(Boolf<T> predicate)
    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.
     
     
    toString(String separator)
     
    static <T> ObjectSet<T>
    with(Seq<T> array)
     
    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

      public ObjectSet(ObjectSet set)
      Creates a new set identical to the specified set.
  • Method Details

    • with

      public static <T> ObjectSet<T> with(T... array)
    • with

      public static <T> ObjectSet<T> with(Seq<T> array)
    • select

      public ObjectSet<T> select(Boolf<T> predicate)
      Allocates a new set with all elements that match the predicate.
    • toSeq

      public Seq<T> toSeq()
    • each

      public void each(Cons<? super T> cons)
      Specified by:
      each in interface Eachable<T>
    • add

      public boolean add(T key)
      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

      public void addAll(Seq<? extends T> array)
    • addAll

      public void addAll(Seq<? extends T> array, int offset, int length)
    • addAll

      public void addAll(T... array)
    • addAll

      public void addAll(T[] array, int offset, int length)
    • addAll

      public void addAll(ObjectSet<T> set)
    • removeAll

      public void removeAll(T[] array, int offset, int length)
    • removeAll

      public void removeAll(T[] array)
    • removeAll

      public void removeAll(Seq<? extends T> array)
    • remove

      public boolean remove(T key)
      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

      public boolean contains(T key)
    • get

      public T get(T key)
      Returns:
      May be null.
    • first

      public T 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()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(String separator)
    • iterator

      public ObjectSet<T>.ObjectSetIterator 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 the ObjectSet<T>.ObjectSetIterator constructor for nested or multithreaded iteration.
      Specified by:
      iterator in interface Iterable<T>