Package org.jacop.util
Class SimpleHashSet<E>
java.lang.Object
org.jacop.util.SimpleHashSet<E>
- Type Parameters:
E
- Class being stored in SimpleHashSet.
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 -
Field Summary
FieldsModifier and TypeFieldDescription(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.private SimpleHashSet.Entry[]
The set, resized as necessary.(package private) int
The next size value at which to resize (capacity * load factor). -
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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 TypeMethodDescriptionboolean
Adds the specified element to this set.void
clear()
Removes all elements from this set.clone()
Clones this set.boolean
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
isEmpty()
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
size()
Returns the number of elements in this set.toString()
Returns string representation of the hash set.(package private) void
transfer
(SimpleHashSet.Entry[] oldTable) Transfer all entries from current table to newTable.
-
Field Details
-
DEFAULT_INITIAL_CAPACITY
static final int DEFAULT_INITIAL_CAPACITYThe default initial capacity - MUST be a power of two.- See Also:
-
DEFAULT_LOAD_FACTOR
static final float DEFAULT_LOAD_FACTORThe load factor used when none specified in constructor.- See Also:
-
MAXIMUM_CAPACITY
static final int MAXIMUM_CAPACITYThe 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
It points to the first Entry to be removed. -
initialCapacity
int initialCapacityThe initial capacity for the hash set. -
lastEntry
It points to the last Entry being add. -
loadFactor
final float loadFactorThe load factor for the hash set. -
size
transient int sizeThe number of elements contained in this set. -
table
The set, resized as necessary. Length MUST Always be a power of two. -
threshold
int thresholdThe 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
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
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
Clones this set. -
contains
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
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
Returns string representation of the hash set. -
transfer
Transfer all entries from current table to newTable.
-