@Generated(date="2017-07-11T19:16:27+0200",
value="KTypeVTypeHashMap.java")
public class ObjectCharHashMap<KType>
extends java.lang.Object
implements ObjectCharMap<KType>, java.lang.Cloneable
Object to char, implemented 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, values),
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 |
ObjectCharHashMap.EntryIterator
An iterator implementation for
iterator(). |
class |
ObjectCharHashMap.KeysCollection
A view of the keys inside this map.
|
class |
ObjectCharHashMap.KeysIterator
An iterator over the set of keys.
|
class |
ObjectCharHashMap.ValuesCollection
A view over the set of values of this map.
|
class |
ObjectCharHashMap.ValuesIterator
An iterator over the set of values.
|
| Modifier and Type | Field and Description |
|---|---|
boolean |
allocatedDefaultKey
True if key = 0/null is in the map.
|
char |
allocatedDefaultKeyValue
if allocatedDefaultKey = true, contains the associated V to the key = 0/null
|
protected int |
assigned
Cached number of assigned slots in
keys. |
protected char |
defaultValue |
protected IteratorPool<ObjectCharCursor<KType>,ObjectCharHashMap.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 keys.
|
protected double |
loadFactor
The load factor for this map (fraction of allocated slots
before the buffers must be rehashed or reallocated).
|
char[] |
values
Hash-indexed array holding all values associated to the keys.
|
| Constructor and Description |
|---|
ObjectCharHashMap()
Default constructor: Creates a hash map with the default capacity of
Containers.DEFAULT_EXPECTED_ELEMENTS,
load factor of HashContainers.DEFAULT_LOAD_FACTOR. |
ObjectCharHashMap(int initialCapacity)
Creates a hash map with the given initial capacity, default load factor of
HashContainers.DEFAULT_LOAD_FACTOR. |
ObjectCharHashMap(int initialCapacity,
double loadFactor)
Creates a hash map with the given initial capacity,
load factor.
|
ObjectCharHashMap(ObjectCharAssociativeContainer<KType> container)
Create a hash map from all key-value pairs of another container.
|
| Modifier and Type | Method and Description |
|---|---|
char |
addTo(KType key,
char incrementValue)
Adds
incrementValue to any existing value for the given key
or inserts incrementValue if key did not previously exist. |
int |
capacity()
Return the maximum number of keys this container is guaranteed to hold without reallocating.
|
void |
clear()
Clear all keys and values in the container.
|
ObjectCharHashMap<KType> |
clone() |
boolean |
containsKey(KType key)
Returns
true if this container has an association to a value for
the given key. |
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 ObjectCharPredicate<? super KType>> |
forEach(T predicate)
Applies a
predicate to container elements, as long as the predicate
returns true. |
<T extends ObjectCharProcedure<? super KType>> |
forEach(T procedure)
Applies a given procedure to all keys-value pairs in this container.
|
static <KType> ObjectCharHashMap<KType> |
from(KType[] keys,
char[] values)
Creates a hash map from two index-aligned arrays of key-value pairs.
|
static <KType> ObjectCharHashMap<KType> |
from(ObjectCharAssociativeContainer<KType> container)
Create a hash map from another associative container.
|
char |
get(KType key) |
char |
getDefaultValue()
Returns the "default value" value used in containers methods returning
"default value"
|
int |
hashCode() |
protected int |
hashKey(KType key)
Override this method, together with
equalKeys(Object, Object)
to customize the hashing strategy. |
boolean |
isEmpty()
Note that an empty container may still contain many deleted keys (that occupy buffer
space).
|
ObjectCharHashMap.EntryIterator |
iterator()
Returns a cursor over the entries (key-value pairs) in this map.
|
ObjectCharHashMap.KeysCollection |
keys()
Returns a collection view of keys of this container.
|
static <KType> ObjectCharHashMap<KType> |
newInstance()
Create a new hash map without providing the full generic signature
(constructor shortcut).
|
static <KType> ObjectCharHashMap<KType> |
newInstance(int initialCapacity,
double loadFactor)
Create a new hash map with initial capacity and load factor control.
|
char |
put(KType key,
char value)
Place a given key and value in the container.
|
int |
putAll(java.lang.Iterable<? extends ObjectCharCursor<? extends KType>> iterable)
Puts all keys from an iterable cursor to this map, replacing the values
of existing keys, if such keys are present.
|
int |
putAll(ObjectCharAssociativeContainer<? extends KType> container)
Puts all keys from another container to this map, replacing the values
of existing keys, if such keys are present.
|
boolean |
putIfAbsent(KType key,
char value)
Trove-inspired API method.
|
char |
putOrAdd(KType key,
char putValue,
char incrementValue)
If
key exists, putValue is inserted into the map,
otherwise any existing value is incremented by additionValue. |
char |
remove(KType key)
Remove the (key, value) pair associated with the given key, and returns the assocaited value.
|
int |
removeAll(ObjectCharPredicate<? super KType> predicate)
Removes all keys (and associated values) for which the predicate on (key, value) pairs returns
true. |
int |
removeAll(ObjectContainer<? super KType> other)
Removes all keys (and associated values) present in a given container.
|
int |
removeAll(ObjectPredicate<? super KType> predicate)
Removes all keys (and associated values) for which the predicate on keys returns
true. |
void |
setDefaultValue(char defaultValue)
Set the "default value" value to be used in containers methods returning
"default value"
|
int |
size() |
java.lang.String |
toString()
Convert the contents of this map to a human-friendly string.
|
ObjectCharHashMap.ValuesCollection |
values()
Returns a collection view of all values present in this container.
|
protected char defaultValue
public java.lang.Object[] keys
Direct map iteration: iterate {keys[i], values[i]} for i in [0; keys.length[ where keys[i] != 0/null, then also
{0/null, allocatedDefaultKeyValue } is in the map if allocatedDefaultKey = true.
public char[] values
keys.protected int[] hash_cache
public boolean allocatedDefaultKey
public char allocatedDefaultKeyValue
protected int assigned
keys.protected final double loadFactor
protected final IteratorPool<ObjectCharCursor<KType>,ObjectCharHashMap.EntryIterator> entryIteratorPool
public ObjectCharHashMap()
Containers.DEFAULT_EXPECTED_ELEMENTS,
load factor of HashContainers.DEFAULT_LOAD_FACTOR.
See class notes about hash distribution importance.
public ObjectCharHashMap(int initialCapacity)
HashContainers.DEFAULT_LOAD_FACTOR.
See class notes about hash distribution importance.
initialCapacity - Initial capacity (greater than zero and automatically
rounded to the next power of two).public ObjectCharHashMap(int initialCapacity,
double loadFactor)
loadFactor - The load factor (greater than zero and smaller than 1).public ObjectCharHashMap(ObjectCharAssociativeContainer<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 char put(KType key, char value)
put in interface ObjectCharMap<KType>public int putAll(ObjectCharAssociativeContainer<? extends KType> container)
putAll in interface ObjectCharMap<KType>public int putAll(java.lang.Iterable<? extends ObjectCharCursor<? extends KType>> iterable)
putAll in interface ObjectCharMap<KType>public boolean putIfAbsent(KType key, char value)
if (!map.containsKey(key))
map.put(key, value);
putIfAbsent in interface ObjectCharMap<KType>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 char putOrAdd(KType key, char putValue, char incrementValue)
key exists, putValue is inserted into the map,
otherwise any existing value is incremented by additionValue.putOrAdd in interface ObjectCharMap<KType>key - The key of the value to adjust.putValue - The value to put if key does not exist.incrementValue - The value to add to the existing value if key exists.key (after
changes).public char addTo(KType key, char incrementValue)
incrementValue to any existing value for the given key
or inserts incrementValue if key did not previously exist.addTo in interface ObjectCharMap<KType>key - The key of the value to adjust.incrementValue - The value to put or add to the existing value if key exists.key (after changes).public char remove(KType key)
remove in interface ObjectCharMap<KType>public int removeAll(ObjectContainer<? super KType> other)
removeAll in interface ObjectCharAssociativeContainer<KType>public int removeAll(ObjectPredicate<? super KType> predicate)
true.removeAll in interface ObjectCharAssociativeContainer<KType>public int removeAll(ObjectCharPredicate<? super KType> predicate)
true.removeAll in interface ObjectCharAssociativeContainer<KType>public char get(KType key)
get in interface ObjectCharMap<KType>public boolean containsKey(KType key)
true if this container has an association to a value for
the given key.containsKey in interface ObjectCharAssociativeContainer<KType>public void clear()
clear in interface ObjectCharMap<KType>public int size()
size in interface ObjectCharAssociativeContainer<KType>public int capacity()
O(n) time.capacity in interface ObjectCharAssociativeContainer<KType>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 ObjectCharAssociativeContainer<KType>true if this hash map contains no assigned keys.public int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic ObjectCharHashMap.EntryIterator 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 ObjectCharAssociativeContainer<KType>iterator in interface java.lang.Iterable<ObjectCharCursor<KType>>public <T extends ObjectCharProcedure<? super KType>> T forEach(T procedure)
ObjectCharProcedure. 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 ObjectCharAssociativeContainer<KType>public <T extends ObjectCharPredicate<? super KType>> T forEach(T predicate)
predicate to container elements, as long as the predicate
returns true. The iteration is interrupted otherwise.
Returns the argument (any
subclass of ObjectCharPredicate. 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.
The iteration is continued as long as the predicate returns true.forEach in interface ObjectCharAssociativeContainer<KType>public ObjectCharHashMap.KeysCollection keys()
keys in interface ObjectCharAssociativeContainer<KType>public ObjectCharHashMap.ValuesCollection values()
values in interface ObjectCharAssociativeContainer<KType>public ObjectCharHashMap<KType> clone()
clone in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.Objectpublic static <KType> ObjectCharHashMap<KType> from(KType[] keys, char[] values)
public static <KType> ObjectCharHashMap<KType> from(ObjectCharAssociativeContainer<KType> container)
public static <KType> ObjectCharHashMap<KType> newInstance()
public static <KType> ObjectCharHashMap<KType> newInstance(int initialCapacity, double loadFactor)
public char getDefaultValue()
getDefaultValue in interface ObjectCharMap<KType>public void setDefaultValue(char defaultValue)
setDefaultValue in interface ObjectCharMap<KType>Copyright © 2017. All rights reserved.