@Generated(date="2017-07-11T19:16:33+0200",
value="KTypeHashSet.java")
public class ObjectHashSet<KType>
extends AbstractObjectCollection<KType>
implements ObjectLookupContainer<KType>, ObjectSet<KType>, java.lang.Cloneable
Objects, implemented using using open
addressing with linear probing for collision resolution.
In addition, the hashing strategy can be changed
by overriding (equalKeys(Object, Object) and hashKey(Object)) together,
which then replaces the usual (equals(Object) and hashCode()) from the keys themselves.
This is useful to define the equivalence of keys when the user has no control over the keys implementation.
The internal buffers of this implementation (keys, etc...)
are always allocated to the nearest size that is a power of two. When
the capacity exceeds the given load factor, the buffer size is doubled.
This implementation supports null keys.
Important note. The implementation uses power-of-two tables and linear probing, which may cause poor performance (many collisions) if hash values are not properly distributed.
Robin-Hood hashing algorithm is also used to minimize variance in insertion and search-related operations, for an all-around smother operation at the cost of smaller peak performance:
- Pedro Celis (1986) for the original Robin-Hood hashing paper,
- MoonPolySoft/Cliff Moon for the initial Robin-hood on HPPC implementation,
- Vincent Sonnier for the present implementation using cached hashes.
| Modifier and Type | Class and Description |
|---|---|
class |
ObjectHashSet.EntryIterator
An iterator implementation for
iterator(). |
| Modifier and Type | Field and Description |
|---|---|
boolean |
allocatedDefaultKey
True if key = 0/null is in the map.
|
protected int |
assigned
Cached number of assigned slots in
keys. |
protected IteratorPool<ObjectCursor<KType>,ObjectHashSet.EntryIterator> |
entryIteratorPool
internal pool of EntryIterator
|
protected int[] |
hash_cache
* Caches the hash value = hash(keys[i]) & mask, if keys[i] !
|
java.lang.Object[] |
keys
Hash-indexed array holding all set entries.
|
protected double |
loadFactor
The load factor for this map (fraction of allocated slots
before the buffers must be rehashed or reallocated).
|
containsNegateTestPredicate, containsTestPredicate, negatePredicate, testContainer, testPredicate| Constructor and Description |
|---|
ObjectHashSet()
Default constructor: Creates a hash set with the default capacity of
Containers.DEFAULT_EXPECTED_ELEMENTS,
load factor of HashContainers.DEFAULT_LOAD_FACTOR. |
ObjectHashSet(int initialCapacity)
Creates a hash set with the given capacity,
load factor of
HashContainers.DEFAULT_LOAD_FACTOR. |
ObjectHashSet(int initialCapacity,
double loadFactor)
Creates a hash set with the given capacity and load factor.
|
ObjectHashSet(ObjectContainer<KType> container)
Creates a hash set from elements of another container.
|
| Modifier and Type | Method and Description |
|---|---|
int |
add(KType... elements)
Vararg-signature method for adding elements to this set.
|
boolean |
add(KType key)
Adds
k to the set. |
int |
add(KType e1,
KType e2)
Adds two elements to the set.
|
int |
addAll(java.lang.Iterable<? extends ObjectCursor<? extends KType>> iterable)
Adds all elements from a given iterable to this set.
|
int |
addAll(ObjectContainer<? extends KType> container)
Adds all elements from a given container to this set.
|
int |
capacity()
Return the maximum number of elements this container is guaranteed to hold without reallocating.
|
void |
clear()
Removes all elements from this collection.
|
ObjectHashSet<KType> |
clone() |
boolean |
contains(KType key)
Lookup a given element in the container.
|
protected boolean |
equalKeys(KType a,
KType b)
Override this method together with
hashKey(Object)
to customize the hashing strategy. |
boolean |
equals(java.lang.Object obj) |
<T extends ObjectPredicate<? super KType>> |
forEach(T predicate)
Applies a
predicate to container elements, as long as the predicate
returns true. |
<T extends ObjectProcedure<? super KType>> |
forEach(T procedure)
Applies a
procedure to all container elements. |
static <KType> ObjectHashSet<KType> |
from(KType... elements)
Create a set from a variable number of arguments or an array of
Object. |
static <KType> ObjectHashSet<KType> |
from(ObjectContainer<KType> container)
Create a set from elements of another container.
|
int |
hashCode() |
protected int |
hashKey(KType key)
Override this method, together with
equalKeys(Object, Object)
to customize the hashing strategy. |
ObjectHashSet.EntryIterator |
iterator()
Returns an iterator to a cursor traversing the collection.
|
static <KType> ObjectHashSet<KType> |
newInstance()
Create a new hash set with default parameters (shortcut
instead of using a constructor).
|
static <KType> ObjectHashSet<KType> |
newInstance(int initialCapacity,
double loadFactor)
Returns a new object of this class with no need to declare generic type (shortcut
instead of using a constructor).
|
boolean |
remove(KType key)
Remove all elements of the set matching key.
|
int |
removeAll(KType key)
Removes all occurrences of
e from this collection. |
int |
removeAll(ObjectPredicate<? super KType> predicate)
Removes all elements in this collection for which the
given predicate returns
true. |
int |
size()
Return the current number of elements in this container.
|
KType[] |
toArray(KType[] target)
Default implementation for:
Copies all elements of this container to an existing array of the same type.
|
isEmpty, removeAll, retainAll, retainAll, toArray, toArray, toStringfinalize, getClass, notify, notifyAll, wait, wait, waitremoveAll, retainAll, retainAllisEmpty, toArray, toArraypublic java.lang.Object[] keys
Direct set iteration: iterate {keys[i]} for i in [0; keys.length[ where keys[i] != 0/null, then also
{0/null} is in the set if allocatedDefaultKey = true.
protected int[] hash_cache
public boolean allocatedDefaultKey
protected int assigned
keys.protected final double loadFactor
protected final IteratorPool<ObjectCursor<KType>,ObjectHashSet.EntryIterator> entryIteratorPool
public ObjectHashSet()
Containers.DEFAULT_EXPECTED_ELEMENTS,
load factor of HashContainers.DEFAULT_LOAD_FACTOR.public ObjectHashSet(int initialCapacity)
HashContainers.DEFAULT_LOAD_FACTOR.public ObjectHashSet(int initialCapacity,
double loadFactor)
public ObjectHashSet(ObjectContainer<KType> container)
protected int hashKey(KType key)
equalKeys(Object, Object)
to customize the hashing strategy. Note that this method is guaranteed
to be called with a non-null key argument.
By default, this method calls key.hashCode().key - Object to be hashed.hashCode();hashCode(),
equalKeys(Object, Object)protected boolean equalKeys(KType a, KType b)
hashKey(Object)
to customize the hashing strategy. Note that this method is guaranteed
to be called with both non-null arguments.
By default, this method calls a.#equals(b).a - not-null Object to be comparedb - not-null Object to be comparedequals(Object).equals(Object),
hashKey(Object)public boolean add(KType key)
k to the set.public int add(KType... elements)
This method is handy, but costly if used in tight loops (anonymous array passing)
public int addAll(ObjectContainer<? extends KType> container)
public int addAll(java.lang.Iterable<? extends ObjectCursor<? extends KType>> iterable)
public int removeAll(KType key)
e from this collection.removeAll in interface ObjectCollection<KType>key - Element to be removed from this collection, if present.public boolean remove(KType key)
ObjectCollection.removeAll(key) > 0remove in interface ObjectSet<KType>ObjectCollection.removeAll(KType)public boolean contains(KType key)
contains in interface ObjectContainer<KType>contains in interface ObjectLookupContainer<KType>true if this container has an element
equal to e.public void clear()
Does not release internal buffers.
clear in interface ObjectCollection<KType>public int size()
O(n) time, although implementing classes
should try to maintain the current size and return in constant time.size in interface ObjectContainer<KType>public int capacity()
O(n) time.capacity in interface ObjectContainer<KType>public int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic ObjectHashSet.EntryIterator iterator()
The iterator is implemented as a
cursor and it returns the same cursor instance on every call to
Iterator.next() (to avoid boxing of primitive types). To read the current
list's value (or index in the list) use the cursor's public fields. An example is
shown below.
for (ObjectCursor<Object> c : container) {
System.out.println("index=" + c.index + " value=" + c.value);
}
iterator in interface ObjectContainer<KType>iterator in interface java.lang.Iterable<ObjectCursor<KType>>public <T extends ObjectProcedure<? super KType>> T forEach(T procedure)
procedure to all container elements. Returns the argument (any
subclass of ObjectProcedure. This lets the caller to call methods of the argument
by chaining the call (even if the argument is an anonymous type) to retrieve computed values,
for example (IntContainer):
int count = container.forEach(new IntProcedure() {
int count; // this is a field declaration in an anonymous class.
public void apply(int value) { count++; }}).count;
forEach in interface ObjectContainer<KType>public KType[] toArray(KType[] target)
toArray in interface ObjectContainer<KType>toArray in class AbstractObjectCollection<KType>target - The target array must be large enough to hold all elements, i.e >= ObjectContainer.size().public ObjectHashSet<KType> clone()
clone in class java.lang.Objectpublic <T extends ObjectPredicate<? super KType>> T forEach(T predicate)
predicate to container elements, as long as the predicate
returns true. The iteration is interrupted otherwise.forEach in interface ObjectContainer<KType>public int removeAll(ObjectPredicate<? super KType> predicate)
true.removeAll in interface ObjectCollection<KType>public static <KType> ObjectHashSet<KType> from(KType... elements)
Object.public static <KType> ObjectHashSet<KType> from(ObjectContainer<KType> container)
public static <KType> ObjectHashSet<KType> newInstance()
public static <KType> ObjectHashSet<KType> newInstance(int initialCapacity, double loadFactor)
Copyright © 2017. All rights reserved.