Class SimpleHashSet<E>

java.lang.Object
org.jacop.util.SimpleHashSet<E>
Type Parameters:
E - Class being stored in SimpleHashSet.

public class SimpleHashSet<E> extends 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.9
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    (package private) class 
     
  • Field Summary

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

    Constructors
    Constructor
    Description
    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

    Modifier and Type
    Method
    Description
    boolean
    add(E element)
    Adds the specified element to this set.
    void
    Removes all elements from this set.
    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
    Returns a hash value for the specified object.
    (package private) static int
    indexFor(int h, int length)
    Returns index for hash code h.
    boolean
    Returns true if this set contains no elements.
    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
    Returns the number of elements in this set.
    Returns string representation of the hash set.
    (package private) void
    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 Details

    • DEFAULT_INITIAL_CAPACITY

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

      static final float DEFAULT_LOAD_FACTOR
      The load factor used when none specified in constructor.
      See Also:
    • 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 invalid input: '<'= 1invalid input: '<'invalid input: '<'30.
      See Also:
    • firstEntry

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

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

      transient SimpleHashSet<E>.Entry<E> lastEntry
      It points to the last Entry being add.
    • 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 Details

    • 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:
      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:
      IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive.
  • Method Details

    • hash

      static int hash(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 Object clone()
      Clones this set.
      Overrides:
      clone in class 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 String toString()
      Returns string representation of the hash set.
      Overrides:
      toString in class Object
    • transfer

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