@Generated(date="2016-02-06T03:13:45+0000", value="HPPC generated from: ObjectOpenHashSet.java") public class ObjectOpenHashSet<KType> extends java.lang.Object implements ObjectLookupContainer<KType>, ObjectSet<KType>, java.lang.Cloneable
KType
s, implemented using using open
addressing with linear probing for collision resolution.
The internal buffers of this implementation (keys
), allocated
)
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.
A brief comparison of the API against the Java Collections framework:
java.util.HashSet | ObjectOpenHashSet |
---|---|
boolean add(E) | boolean add(E) |
boolean remove(E) | int removeAllOccurrences(E) |
size, clear, isEmpty | size, clear, isEmpty |
contains(E) | contains(E), lkey() |
iterator | iterator over set values, pseudo-closures |
This implementation supports null
keys.
Important node. The implementation uses power-of-two tables and linear
probing, which may cause poor performance (many collisions) if hash values are
not properly distributed.
This implementation uses MurmurHash3
for rehashing keys.
Modifier and Type | Field and Description |
---|---|
boolean[] |
allocated
Information if an entry (slot) in the
keys table is allocated
or empty. |
int |
assigned
Cached number of assigned slots in
allocated . |
static int |
DEFAULT_CAPACITY
Default capacity.
|
static float |
DEFAULT_LOAD_FACTOR
Default load factor.
|
KType[] |
keys
Hash-indexed array holding all set entries.
|
protected int |
lastSlot
The most recent slot accessed in
contains(KType) . |
float |
loadFactor
The load factor for this map (fraction of allocated slots
before the buffers must be rehashed or reallocated).
|
static int |
MIN_CAPACITY
Minimum capacity for the map.
|
protected int |
perturbation
We perturb hashed values with the array size to avoid problems with
nearly-sorted-by-hash values on iterations.
|
protected int |
resizeAt
Resize buffers when
allocated hits this value. |
Constructor and Description |
---|
ObjectOpenHashSet()
|
ObjectOpenHashSet(int initialCapacity)
Creates a hash set with the given capacity,
load factor of 0.75f.
|
ObjectOpenHashSet(int initialCapacity,
float loadFactor)
Creates a hash set with the given capacity and load factor.
|
ObjectOpenHashSet(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 e)
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.
|
void |
clear()
Removes all elements from this collection.
|
ObjectOpenHashSet<KType> |
clone() |
protected int |
computePerturbationValue(int capacity)
Compute the key perturbation value applied before hashing.
|
boolean |
contains(KType key)
Lookup a given element in the container.
|
boolean |
equals(java.lang.Object obj)
Compares the specified object with this set for equality.
|
<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> ObjectOpenHashSet<KType> |
from(KType... elements)
Create a set from a variable number of arguments or an array of
KType . |
static <KType> ObjectOpenHashSet<KType> |
from(ObjectContainer<KType> container)
Create a set from elements of another container.
|
int |
hashCode() |
boolean |
isEmpty()
Shortcut for
size() == 0 . |
java.util.Iterator<ObjectCursor<KType>> |
iterator()
Returns an iterator to a cursor traversing the collection.
|
KType |
lkey()
Returns the last key saved in a call to
contains(KType) if it returned true . |
int |
lslot() |
static <KType> ObjectOpenHashSet<KType> |
newInstance()
Returns a new object of this class with no need to declare generic type (shortcut
instead of using a constructor).
|
static <KType> ObjectOpenHashSet<KType> |
newInstanceWithCapacity(int initialCapacity,
float loadFactor)
Returns a new object of this class with no need to declare generic type (shortcut
instead of using a constructor).
|
static <KType> ObjectOpenHashSet<KType> |
newInstanceWithExpectedSize(int expectedSize)
Returns a new object of this class with no need to declare generic type (shortcut
instead of using a constructor).
|
static <KType> ObjectOpenHashSet<KType> |
newInstanceWithExpectedSize(int expectedSize,
float loadFactor)
Returns a new object of this class with no need to declare generic type (shortcut
instead of using a constructor).
|
static <KType> ObjectOpenHashSet<KType> |
newInstanceWithoutPerturbations()
Returns a new object with no key perturbations (see
computePerturbationValue(int) ). |
boolean |
remove(KType key)
An alias for the (preferred)
removeAllOccurrences(KType) . |
int |
removeAll(ObjectLookupContainer<? extends KType> c)
Default implementation uses a predicate for removal.
|
int |
removeAll(ObjectPredicate<? super KType> predicate)
Removes all elements in this collection for which the
given predicate returns
true . |
int |
removeAllOccurrences(KType key)
Removes all occurrences of
e from this collection. |
int |
retainAll(ObjectLookupContainer<? extends KType> c)
Default implementation uses a predicate for retaining.
|
int |
retainAll(ObjectPredicate<? super KType> predicate)
Default implementation redirects to
ObjectCollection.removeAll(ObjectPredicate)
and negates the predicate. |
protected void |
shiftConflictingKeys(int slotCurr)
Shift all the slot-conflicting keys allocated to (and including)
slot . |
int |
size()
Return the current number of elements in this container.
|
java.lang.Object[] |
toArray()
Copies all elements from this container to an Object array.
|
KType[] |
toArray(java.lang.Class<? super KType> clazz)
Default implementation of copying to an array.
|
java.lang.String |
toString()
Convert the contents of this container to a human-friendly string.
|
finalize, getClass, notify, notifyAll, wait, wait, wait
removeAll, retainAll, retainAll
toArray
public static final int MIN_CAPACITY
public static final int DEFAULT_CAPACITY
public static final float DEFAULT_LOAD_FACTOR
public KType[] keys
Important!
The actual value in this field is always an instance of Object[]
.
Be warned that javac
emits additional casts when keys
are directly accessed; these casts
may result in exceptions at runtime. A workaround is to cast directly to
Object[]
before accessing the buffer's elements (although it is highly
recommended to use a iterator()
instead.
allocated
public boolean[] allocated
keys
table is allocated
or empty.assigned
public int assigned
allocated
.public final float loadFactor
protected int resizeAt
allocated
hits this value.protected int lastSlot
contains(KType)
.* @see #lkey
protected int perturbation
public ObjectOpenHashSet()
public ObjectOpenHashSet(int initialCapacity)
public ObjectOpenHashSet(int initialCapacity, float loadFactor)
public ObjectOpenHashSet(ObjectContainer<KType> container)
public boolean add(KType e)
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)
protected int computePerturbationValue(int capacity)
Compute the key perturbation value applied before hashing. The returned value should be non-zero and ideally different for each capacity. This matters because keys are nearly-ordered by their hashed values so when adding one container's values to the other, the number of collisions can skyrocket into the worst case possible.
If it is known that hash containers will not be added to each other (will be used for counting only, for example) then some speed can be gained by not perturbing keys before hashing and returning a value of zero for all possible capacities. The speed gain is a result of faster rehash operation (keys are mostly in order).
public int removeAllOccurrences(KType key)
e
from this collection.removeAllOccurrences
in interface ObjectCollection<KType>
key
- Element to be removed from this collection, if present.public boolean remove(KType key)
removeAllOccurrences(KType)
.protected void shiftConflictingKeys(int slotCurr)
slot
.public KType lkey()
contains(KType)
if it returned true
.contains(KType)
public int lslot()
contains(KType)
if
it returned true
.contains(KType)
public boolean contains(KType key)
Saves the associated value for fast access using lkey()
.
if (map.contains(key)) value = map.lkey();
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 boolean isEmpty()
size() == 0
.isEmpty
in interface ObjectContainer<KType>
public int hashCode()
hashCode
in interface ObjectSet<KType>
hashCode
in class java.lang.Object
Set.hashCode()
(sum of hash codes of elements
within the set). Because sum is commutative, this ensures that different order
of elements in a set does not affect the hash code.public boolean equals(java.lang.Object obj)
ObjectSet
and both objects contains exactly the same objects.public java.util.Iterator<ObjectCursor<KType>> 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<KType> 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 java.lang.Object[] toArray()
ObjectContainer.toArray(Class)
.toArray
in interface ObjectContainer<KType>
ObjectContainer.toArray(Class)
public ObjectOpenHashSet<KType> clone()
clone
in class java.lang.Object
public <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> ObjectOpenHashSet<KType> from(KType... elements)
KType
.
The elements are copied from the argument to the internal buffer.public static <KType> ObjectOpenHashSet<KType> from(ObjectContainer<KType> container)
public static <KType> ObjectOpenHashSet<KType> newInstance()
public static <KType> ObjectOpenHashSet<KType> newInstanceWithoutPerturbations()
computePerturbationValue(int)
). Only use when sure the container will not
be used for direct copying of keys to another hash container.public static <KType> ObjectOpenHashSet<KType> newInstanceWithCapacity(int initialCapacity, float loadFactor)
public static <KType> ObjectOpenHashSet<KType> newInstanceWithExpectedSize(int expectedSize)
expectedSize
elements without having to resize.public static <KType> ObjectOpenHashSet<KType> newInstanceWithExpectedSize(int expectedSize, float loadFactor)
expectedSize
elements without having to resize.public int removeAll(ObjectLookupContainer<? extends KType> c)
removeAll
in interface ObjectCollection<KType>
public int retainAll(ObjectLookupContainer<? extends KType> c)
retainAll
in interface ObjectCollection<KType>
public int retainAll(ObjectPredicate<? super KType> predicate)
ObjectCollection.removeAll(ObjectPredicate)
and negates the predicate.retainAll
in interface ObjectCollection<KType>
public KType[] toArray(java.lang.Class<? super KType> clazz)
toArray
in interface ObjectContainer<KType>
public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2016 Carrot Search s.c.. All rights reserved.