Class SimpleHashSet<E>

  • Type Parameters:
    E - Class being stored in SimpleHashSet.

    public class SimpleHashSet<E>
    extends java.lang.Object
    This class provides very simple HashSet functionality. Designed specially for maintaining pending constraints for evaluation. It's implementation was partially based on standard hash set implementation as implemented in java util class.
    Version:
    4.8
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) class  SimpleHashSet.Entry<E>  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static int DEFAULT_INITIAL_CAPACITY
      The default initial capacity - MUST be a power of two.
      (package private) static float DEFAULT_LOAD_FACTOR
      The load factor used when none specified in constructor.
      (package private) SimpleHashSet.Entry<E> firstEntry
      It points to the first Entry to be removed.
      (package private) int initialCapacity
      The initial capacity for the hash set.
      (package private) SimpleHashSet.Entry<E> lastEntry
      It points to the last Entry being add.
      (package private) float loadFactor
      The load factor for the hash set.
      (package private) static int MAXIMUM_CAPACITY
      The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments.
      (package private) int size
      The number of elements contained in this set.
      private SimpleHashSet.Entry[] table
      The set, resized as necessary.
      (package private) int threshold
      The next size value at which to resize (capacity * load factor).
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleHashSet()
      Constructs an empty HashSet with the default initial capacity (16) and the default load factor (0.75).
      SimpleHashSet​(int initialCapacity)
      Constructs an empty HashSet with the specified initial capacity and the default load factor (0.75).
      SimpleHashSet​(int initialCapacity, float loadFactor)
      Constructs an empty HashSet with the specified initial capacity and load factor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E element)
      Adds the specified element to this set.
      void clear()
      Removes all elements from this set.
      java.lang.Object clone()
      Clones this set.
      boolean contains​(E element)
      Returns the boolean value which specifies if given element is already in this identity hash set.
      (package private) static int hash​(java.lang.Object x)
      Returns a hash value for the specified object.
      (package private) static int indexFor​(int h, int length)
      Returns index for hash code h.
      boolean isEmpty()
      Returns true if this set contains no elements.
      E removeFirst()
      Removes and returns an entry removed from the HashSet.
      (package private) void resize​(int newCapacity)
      Rehashes the contents of this set into a new array with a larger capacity.
      int size()
      Returns the number of elements in this set.
      java.lang.String toString()
      Returns string representation of the hash set.
      (package private) void transfer​(SimpleHashSet.Entry[] oldTable)
      Transfer all entries from current table to newTable.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • DEFAULT_INITIAL_CAPACITY

        static final int DEFAULT_INITIAL_CAPACITY
        The default initial capacity - MUST be a power of two.
        See Also:
        Constant Field Values
      • DEFAULT_LOAD_FACTOR

        static final float DEFAULT_LOAD_FACTOR
        The load factor used when none specified in constructor.
        See Also:
        Constant Field Values
      • MAXIMUM_CAPACITY

        static final int MAXIMUM_CAPACITY
        The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments. MUST be a power of two <= 1<<30.
        See Also:
        Constant Field Values
      • firstEntry

        transient SimpleHashSet.Entry<E> firstEntry
        It points to the first Entry to be removed.
      • initialCapacity

        int initialCapacity
        The initial capacity for the hash set.
      • loadFactor

        final float loadFactor
        The load factor for the hash set.
      • size

        transient int size
        The number of elements contained in this set.
      • table

        private transient SimpleHashSet.Entry[] table
        The set, resized as necessary. Length MUST Always be a power of two.
      • threshold

        int threshold
        The next size value at which to resize (capacity * load factor).
    • Constructor Detail

      • SimpleHashSet

        public SimpleHashSet()
        Constructs an empty HashSet with the default initial capacity (16) and the default load factor (0.75).
      • SimpleHashSet

        public SimpleHashSet​(int initialCapacity)
        Constructs an empty HashSet with the specified initial capacity and the default load factor (0.75).
        Parameters:
        initialCapacity - the initial capacity.
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity is negative.
      • SimpleHashSet

        public SimpleHashSet​(int initialCapacity,
                             float loadFactor)
        Constructs an empty HashSet with the specified initial capacity and load factor.
        Parameters:
        initialCapacity - The initial capacity.
        loadFactor - The load factor.
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive.
    • Method Detail

      • hash

        static int hash​(java.lang.Object x)
        Returns a hash value for the specified object. In addition to the object's own hashCode, this method applies a "supplemental hash function," which defends against poor quality hash functions. This is critical because SimpleHashSet uses power-of two length hash tables.

        The shift distances in this function were chosen as the result of an automated search over the entire four-dimensional search space.

        This hash code function implementation is original Sun function proposed in util package.

      • indexFor

        static int indexFor​(int h,
                            int length)
        Returns index for hash code h.
      • add

        public boolean add​(E element)
        Adds the specified element to this set.
        Parameters:
        element - element with which the specified value is to be associated.
        Returns:
        true if object is inserted and false if object was already in the set.
      • clear

        public void clear()
        Removes all elements from this set.
      • clone

        public java.lang.Object clone()
        Clones this set.
        Overrides:
        clone in class java.lang.Object
      • contains

        public boolean contains​(E element)
        Returns the boolean value which specifies if given element is already in this identity hash set.
        Parameters:
        element - the element whose existence in the hash set is to be checked.
        Returns:
        the boolean value which specifies if given element exists in a hash set.
      • isEmpty

        public boolean isEmpty()
        Returns true if this set contains no elements.
        Returns:
        true if this set contains no elements.
      • removeFirst

        public E removeFirst()
        Removes and returns an entry removed from the HashSet. Returns null if the HashSet contains no entry.
        Returns:
        the first entry which has been removed.
      • resize

        void resize​(int newCapacity)
        Rehashes the contents of this set into a new array with a larger capacity. This method is called automatically when the number of elements in this set reaches its threshold.

        If current capacity is MAXIMUM_CAPACITY, this method does not resize the set, but sets threshold to Integer.MAX_VALUE. This has the effect of preventing future calls.

        Parameters:
        newCapacity - the new capacity, MUST be a power of two; must be greater than current capacity unless current capacity is MAXIMUM_CAPACITY (in which case value is irrelevant).
      • size

        public int size()
        Returns the number of elements in this set.
        Returns:
        the number of elements in this set.
      • toString

        public java.lang.String toString()
        Returns string representation of the hash set.
        Overrides:
        toString in class java.lang.Object
      • transfer

        void transfer​(SimpleHashSet.Entry[] oldTable)
        Transfer all entries from current table to newTable.