Class ObjectSet<T>

  • All Implemented Interfaces:
    java.lang.Iterable<T>
    Direct Known Subclasses:
    OrderedSet

    public class ObjectSet<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>
    An unordered set where the keys are objects. Null keys are not allowed. No allocation is done except when growing the table size.

    This class performs fast contains and remove (typically O(1), worst case O(n) but that is rare in practice). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT size.

    Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with OrderedSet and OrderedMap.

    This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int mask
      A bitmask used to confine hashcodes to the size of the table.
      protected int shift
      Used by place(Object) to bit shift the upper bits of a long into a usable range (>= 0 and <= mask).
      int size  
    • Constructor Summary

      Constructors 
      Constructor Description
      ObjectSet()
      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.
      ObjectSet​(ObjectSet<? extends T> set)
      Creates a new set identical to the specified set.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(T key)
      Returns true if the key was added to the set or false if it was already in the set.
      void addAll​(Array<? extends T> array)  
      void addAll​(Array<? extends T> array, int offset, int length)  
      void addAll​(ObjectSet<T> set)  
      boolean addAll​(T... array)  
      boolean addAll​(T[] array, int offset, int length)  
      void clear()
      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 / loadFactor, if they are larger.
      boolean contains​(T key)  
      void ensureCapacity​(int additionalCapacity)
      Increases the size of the backing array to accommodate the specified number of additional items / loadFactor.
      boolean equals​(java.lang.Object obj)  
      T first()  
      T get​(T key)  
      int hashCode()  
      boolean isEmpty()
      Returns true if the set is empty.
      ObjectSet.ObjectSetIterator<T> iterator()
      Returns an iterator for the keys in the set.
      boolean notEmpty()
      Returns true if the set has one or more items.
      protected int place​(T item)
      Returns an index >= 0 and <= mask for the specified item.
      boolean remove​(T key)
      Returns true if the key was removed.
      void shrink​(int maximumCapacity)
      Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less.
      java.lang.String toString()  
      java.lang.String toString​(java.lang.String separator)  
      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 Detail

      • size

        public int size
      • shift

        protected int shift
        Used by place(Object) to bit shift the upper bits of a long into a usable range (>= 0 and <= mask). The shift can be negative, which is convenient to match the number of bits in mask: if mask is a 7-bit number, a shift of -7 shifts the upper 7 bits into the lowest 7 positions. This class sets the shift > 32 and < 64, which if used with an int will still move the upper bits of an int to the lower bits due to Java's implicit modulus on shifts.

        mask can also be used to mask the low bits of a number, which may be faster for some hashcodes, if place(Object) is overridden.

      • mask

        protected int mask
        A bitmask used to confine hashcodes to the size of the table. Must be all 1 bits in its low positions, ie a power of two minus 1. If place(Object) is overriden, this can be used instead of shift to isolate usable bits of a hash.
    • Constructor Detail

      • 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 - The backing array size is initialCapacity / loadFactor, increased to the next 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 - The backing array size is initialCapacity / loadFactor, increased to the next power of two.
      • ObjectSet

        public ObjectSet​(ObjectSet<? extends T> set)
        Creates a new set identical to the specified set.
    • Method Detail

      • place

        protected int place​(T item)
        Returns an index >= 0 and <= mask for the specified item.

        The default implementation uses Fibonacci hashing on the item's Object.hashCode(): the hashcode is multiplied by a long constant (2 to the 64th, divided by the golden ratio) then the uppermost bits are shifted into the lowest positions to obtain an index in the desired range. Multiplication by a long may be slower than int (eg on GWT) but greatly improves rehashing, allowing even very poor hashcodes, such as those that only differ in their upper bits, to be used without high collision rates. Fibonacci hashing has increased collision rates when all or most hashcodes are multiples of larger Fibonacci numbers (see Malte Skarupke's blog post).

        This method can be overriden to customizing hashing. This may be useful eg in the unlikely event that most hashcodes are Fibonacci numbers, if keys provide poor or incorrect hashcodes, or to simplify hashing if keys provide high quality hashcodes and don't need Fibonacci hashing: return item.hashCode() & mask;

      • add

        public boolean add​(T key)
        Returns true if the key was added to the set or false if it was already in the set. If this set already contains the key, the call leaves the set unchanged and returns false.
      • addAll

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

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

        public boolean addAll​(T... array)
      • addAll

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

        public void addAll​(ObjectSet<T> set)
      • remove

        public boolean remove​(T key)
        Returns true if the key was removed.
      • notEmpty

        public boolean notEmpty()
        Returns true if the set has one or more items.
      • 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 / loadFactor, 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 / loadFactor, 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

        @Null
        public T get​(T key)
      • 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 / loadFactor. Useful before adding many items to avoid multiple backing array resizes.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toString

        public java.lang.String toString​(java.lang.String separator)
      • with

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