@Generated(date="2016-02-06T03:13:45+0000", value="HPPC generated from: LongDoubleOpenHashMap.java") public class LongDoubleOpenHashMap extends java.lang.Object implements LongDoubleMap, java.lang.Cloneable
long
to double
, implemented using open
addressing with linear probing for collision resolution.
The internal buffers of this implementation (keys
, values
,
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.
See ObjectObjectOpenHashMap
class for API similarities and differences against Java
Collections.
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 rehashing
using MurmurHash3
.
Modifier and Type | Class and Description |
---|---|
class |
LongDoubleOpenHashMap.KeysContainer
A view of the keys inside this hash map.
|
Modifier and Type | Field and Description |
---|---|
boolean[] |
allocated
Information if an entry (slot) in the
values 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.
|
long[] |
keys
Hash-indexed array holding all keys.
|
protected int |
lastSlot
The most recent slot accessed in
containsKey(long) (required for
lget() ). |
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. |
double[] |
values
Hash-indexed array holding all values associated to the keys
stored in
keys . |
Constructor and Description |
---|
LongDoubleOpenHashMap()
|
LongDoubleOpenHashMap(int initialCapacity)
Creates a hash map with the given initial capacity, default load factor of
0.75f.
|
LongDoubleOpenHashMap(int initialCapacity,
float loadFactor)
Creates a hash map with the given initial capacity,
load factor.
|
LongDoubleOpenHashMap(LongDoubleAssociativeContainer container)
Create a hash map from all key-value pairs of another container.
|
Modifier and Type | Method and Description |
---|---|
double |
addTo(long key,
double additionValue)
An equivalent of calling
|
void |
clear()
Clear all keys and values in the container.
|
LongDoubleOpenHashMap |
clone() |
protected int |
computePerturbationValue(int capacity)
Compute the key perturbation value applied before hashing.
|
boolean |
containsKey(long key)
Returns
true if this container has an association to a value for
the given key. |
boolean |
equals(java.lang.Object obj)
Compares the specified object with this set for equality.
|
<T extends LongDoubleProcedure> |
forEach(T procedure)
Applies a given procedure to all keys-value pairs in this container.
|
static LongDoubleOpenHashMap |
from(long[] keys,
double[] values)
Creates a hash map from two index-aligned arrays of key-value pairs.
|
static LongDoubleOpenHashMap |
from(LongDoubleAssociativeContainer container)
Create a hash map from another associative container.
|
double |
get(long key) |
double |
getOrDefault(long key,
double defaultValue) |
int |
hashCode() |
boolean |
isEmpty() |
java.util.Iterator<LongDoubleCursor> |
iterator()
Returns a cursor over the entries (key-value pairs) in this map.
|
LongDoubleOpenHashMap.KeysContainer |
keys()
Returns a specialized view of the keys of this associated container.
|
double |
lget()
Returns the last value saved in a call to
containsKey(long) . |
double |
lset(double key)
Sets the value corresponding to the key saved in the last
call to
containsKey(long) , if and only if the key exists
in the map already. |
int |
lslot() |
static LongDoubleOpenHashMap |
newInstance()
Create a new hash map without providing the full generic signature (constructor
shortcut).
|
static LongDoubleOpenHashMap |
newInstance(int initialCapacity,
float loadFactor)
Create a new hash map without providing the full generic signature (constructor
shortcut).
|
static LongDoubleOpenHashMap |
newInstanceWithExpectedSize(int expectedSize)
Create a new hash map without providing the full generic signature (constructor
shortcut).
|
static LongDoubleOpenHashMap |
newInstanceWithExpectedSize(int expectedSize,
float loadFactor)
Create a new hash map without providing the full generic signature (constructor
shortcut).
|
static LongDoubleOpenHashMap |
newInstanceWithoutPerturbations()
Returns a new object with no key perturbations (see
computePerturbationValue(int) ). |
double |
put(long key,
double value)
Place a given key and value in the container.
|
int |
putAll(java.lang.Iterable<? extends LongDoubleCursor> iterable)
Puts all key/value pairs from a given iterable into this map.
|
int |
putAll(LongDoubleAssociativeContainer container)
Puts all keys from another container to this map, replacing the values
of existing keys, if such keys are present.
|
boolean |
putIfAbsent(long key,
double value)
Trove-inspired API method.
|
double |
putOrAdd(long key,
double putValue,
double additionValue)
Trove-inspired API method.
|
double |
remove(long key)
Remove all values at the given key.
|
int |
removeAll(LongContainer container)
Removes all keys (and associated values) present in a given container.
|
int |
removeAll(LongPredicate predicate)
Removes all keys (and associated values) for which the predicate returns
true . |
protected void |
shiftConflictingKeys(int slotCurr)
Shift all the slot-conflicting keys allocated to (and including)
slot . |
int |
size() |
java.lang.String |
toString()
Convert the contents of this map to a human-friendly string.
|
DoubleContainer |
values()
Returns a container view of all values present in this container.
|
public static final int MIN_CAPACITY
public static final int DEFAULT_CAPACITY
public static final float DEFAULT_LOAD_FACTOR
public long[] keys
values
public double[] values
keys
.keys
public boolean[] allocated
values
table is allocated
or empty.assigned
public int assigned
allocated
.public final float loadFactor
protected int resizeAt
allocated
hits this value.protected int lastSlot
containsKey(long)
(required for
lget()
).containsKey(long)
,
lget()
protected int perturbation
public LongDoubleOpenHashMap()
public LongDoubleOpenHashMap(int initialCapacity)
See class notes about hash distribution importance.
initialCapacity
- Initial capacity (greater than zero and automatically
rounded to the next power of two).public LongDoubleOpenHashMap(int initialCapacity, float loadFactor)
See class notes about hash distribution importance.
initialCapacity
- Initial capacity (greater than zero and automatically
rounded to the next power of two).loadFactor
- The load factor (greater than zero and smaller than 1).public LongDoubleOpenHashMap(LongDoubleAssociativeContainer container)
public double put(long key, double value)
put
in interface LongDoubleMap
public int putAll(LongDoubleAssociativeContainer container)
putAll
in interface LongDoubleMap
public int putAll(java.lang.Iterable<? extends LongDoubleCursor> iterable)
putAll
in interface LongDoubleMap
public boolean putIfAbsent(long key, double value)
if (!map.containsKey(key)) map.put(value);
This method saves to lastSlot
as a side effect of each call.
key
- The key of the value to check.value
- The value to put if key
does not exist.true
if key
did not exist and value
was placed in the map.public double putOrAdd(long key, double putValue, double additionValue)
lastSlot
):
if (containsKey(key)) { double v = (double) (lget() + additionValue); lset(v); return v; } else { put(key, putValue); return putValue; }
putOrAdd
in interface LongDoubleMap
key
- The key of the value to adjust.putValue
- The value to put if key
does not exist.additionValue
- The value to add to the existing value if key
exists.key
(after changes).public double addTo(long key, double additionValue)
if (containsKey(key)) { double v = (double) (lget() + additionValue); lset(v); return v; } else { put(key, additionValue); return additionValue; }
addTo
in interface LongDoubleMap
key
- The key of the value to adjust.additionValue
- The value to put or add to the existing value if key
exists.key
(after changes).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 double remove(long key)
remove
in interface LongDoubleMap
protected void shiftConflictingKeys(int slotCurr)
slot
.public int removeAll(LongContainer container)
keys().removeAll(container)but with no additional overhead.
removeAll
in interface LongDoubleAssociativeContainer
public int removeAll(LongPredicate predicate)
true
.
An alias to:
keys().removeAll(container)but with no additional overhead.
removeAll
in interface LongDoubleAssociativeContainer
public double get(long key)
Use the following snippet of code to check for key existence first and then retrieve the value if it exists.
if (map.containsKey(key)) value = map.lget();
The above code cannot be used by multiple concurrent
threads because a call to containsKey(long)
stores
the temporary slot number in lastSlot
. An alternative to the above
conditional statement is to use getOrDefault(long, double)
and
provide a custom default value sentinel (not present in the value set).
get
in interface LongDoubleMap
public double getOrDefault(long key, double defaultValue)
getOrDefault
in interface LongDoubleMap
public double lget()
containsKey(long)
.containsKey(long)
public double lset(double key)
containsKey(long)
, if and only if the key exists
in the map already.containsKey(long)
public int lslot()
containsKey(long)
if
it returned true
.containsKey(long)
public boolean containsKey(long key)
true
if this container has an association to a value for
the given key.
Saves the associated value for fast access using lget()
or lset(double)
.
if (map.containsKey(key)) value = map.lget();or, to modify the value at the given key without looking up its slot twice:
if (map.containsKey(key)) map.lset(map.lget() + 1);*
containsKey
in interface LongDoubleAssociativeContainer
public void clear()
Does not release internal buffers.
clear
in interface LongDoubleAssociativeContainer
public int size()
size
in interface LongDoubleAssociativeContainer
public boolean isEmpty()
Note that an empty container may still contain many deleted keys (that occupy buffer space). Adding even a single element to such a container may cause rehashing.
isEmpty
in interface LongDoubleAssociativeContainer
true
if this hash map contains no assigned keys.public int hashCode()
hashCode
in interface LongDoubleMap
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
LongDoubleMap
and both objects contains exactly the same key-value pairs.equals
in interface LongDoubleMap
equals
in class java.lang.Object
public java.util.Iterator<LongDoubleCursor> iterator()
Iterator.next()
. To read the current key and value use the cursor's
public fields. An example is shown below.
for (IntShortCursor c : intShortMap) { System.out.println("index=" + c.index + " key=" + c.key + " value=" + c.value); }
The index
field inside the cursor gives the internal index inside
the container's implementation. The interpretation of this index depends on
to the container.
iterator
in interface LongDoubleAssociativeContainer
iterator
in interface java.lang.Iterable<LongDoubleCursor>
public <T extends LongDoubleProcedure> T forEach(T procedure)
LongDoubleProcedure
. 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.forEach
in interface LongDoubleAssociativeContainer
public LongDoubleOpenHashMap.KeysContainer keys()
ObjectLookupContainer
.keys
in interface LongDoubleAssociativeContainer
public DoubleContainer values()
LongDoubleAssociativeContainer
values
in interface LongDoubleAssociativeContainer
public LongDoubleOpenHashMap clone()
clone
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public static LongDoubleOpenHashMap from(long[] keys, double[] values)
public static LongDoubleOpenHashMap from(LongDoubleAssociativeContainer container)
public static LongDoubleOpenHashMap newInstance()
public static LongDoubleOpenHashMap newInstanceWithoutPerturbations()
computePerturbationValue(int)
). Only use when sure the container will not
be used for direct copying of keys to another hash container.public static LongDoubleOpenHashMap newInstance(int initialCapacity, float loadFactor)
public static LongDoubleOpenHashMap newInstanceWithExpectedSize(int expectedSize)
expectedSize
elements without having to resize.public static LongDoubleOpenHashMap newInstanceWithExpectedSize(int expectedSize, float loadFactor)
expectedSize
elements without having to resize.Copyright © 2016 Carrot Search s.c.. All rights reserved.