#include <hashmap.h>
A HashMap maps keys to values with the help of hash codes for the keys. It expects a function static HashInt HASH::GetHashCode(const K&) in the class HASH (given as template argument) to compute a hash code for a key, and then it uses the lower bits of the hash code as an index into a table of buckets. Each bucket contains a singly linked list of entries (key-value-pairs) having the same lower bits, and the function static Bool HASH::IsEqual(const K&, const K&) is used to find the entry with matching key, if any. By default, DefaultCompare is used for HASH, this handles keys of integral type, pointers and objects which have a GetHashCode member function themselves as well as the == operator. For other keys, you have to define your own class for HASH, see CStringCompare as an example.
This example uses a HashMap to store String values addressed by Int keys:
Instead of InsertKey(), you can also use Insert() if you don't need to differentiate between the cases whether the entry existed before or not:
A HashMap can also be used as a multi-map, which means that there can be more than one value per key. You have to use InsertMultiEntry() to add entries for a key, this function won't overwrite or remove existing entries for that key. To get all entries for a key, you have to iterate over them in the following way:
You can also use the foreach iterator returned by FindAll():
The larger the table of buckets, the less is the chance of several entries within a single bucket. The HashMap uses a load factor to automatically double the size of the table if the number of entries exceeds the table size multiplied by the load factor (re-hashing). So with a load factor of 0.5 there are at most half as many entries as there are buckets, and then with evenly distributed hash codes there is only a negligible number of buckets with more than one entry. The default load factor is 10/16 (0.625). If you use a load factor <= 0, the automatic increase of table size is switched off, thus the HashMap will keep the initial size of the table.
Performance characteristics: A HashMap performs the map operations in constant time if the hash function computes evenly distributed hash codes for the keys. All operations (insertion, erasure, lookup) are typically performed in constant time O(1) (i.e., independent of the total number of entries).
There are applications of the HashMap where the values already contain the keys (e.g., think of a map from names to objects, and the objects know their names). Then it might be wasteful to store the keys again in the HashMap entries. In such a case, you have to specify a class as argument for the template parameter ENTRY_HANDLER which contains the function static const K& ENTRY_HANDLER::GetKey(const V&). This function will be used to extract keys from values. You also have to make sure that each HashMap entry has a valid value. I.e., when you have added a new entry with Insert(), then you have to initialize it with a value whose key is the same key as the one used for Insert().
If you want to iterate over the entries of a HashMap, you can either use Iterator, ConstIterator or a ranged-based for loop, or you can use GetNonEmptyBucketCount() and GetNonEmptyBucket() to loop over the non-empty buckets, and then HashMap::Entry::GetNextInBucket() to run through the entries of a bucket.
When the template parameter MODE
is set to {HASHMAP_MODE::SYNCHRONIZED}, the HashMap (partially) behaves as a thread-safe, lock-free map. But note that this only holds if the only modification is the addition of new entries. You must not erase entries unless you make sure that this only happens when no other thread accesses the HashMap, and that a proper synchronization happens afterwards. Also you have to set the load factor to zero to disable re-hashing and set a sufficiently large capacity at the beginning using SetCapacityHint(). Iterators are generally less efficient when
MODE
is {HASHMAP_MODE::SYNCHRONIZED} or
{HASHMAP_MODE::NO_NONEMPTY_BUCKET_TABLE} as they have to loop over all buckets and not just over the non-empty buckets.
K | Type of keys. |
V | Type of values. |
HASH | Class to be used to compute the hash code of keys, and to compare keys for equality (DefaultCompare by default) |
ENTRY_HANDLER | Use this class if the keys shall be extracted from values, rather than being stored separately. With the default argument HashMapKeyValuePair, keys and values are stored separately in the entries as key-value-pairs. |
ALLOCATOR | Class for memory allocation. |
MODE | The HashMap mode, see HASHMAP_MODE. |
INITIAL_CAPACITY | The initial capacity of the HashMap, this is used when the first entry is added. |
LOAD_FACTOR | The load factor of the HashMap in 1/16. |
ENTRY_ALLOCATOR | An optional memory allocator for the entries only. |
Classes | |
class | ConstIteratorTemplate |
struct | DefaultBucket |
class | Entry |
class | EntryIteratorBase |
class | Hash |
class | HashMapAllocator |
class | HashMapAllocator< ONE_ALLOCATOR, ONE_ALLOCATOR > |
class | IteratorTemplate |
class | IteratorTemplateBase |
class | KeyIteratorBase |
struct | LambdaEntryConstructor |
struct | LambdaEntryConstructor< KEY &, LAMBDA, true > |
class | MultiEntryIterator |
union | SimpleBucket |
class | ValueIteratorBase |
Public Types | |
using | Super = MapBase< HashMap, K, V, EmptyClass, HASH > |
using | HashType = HASH |
using | EntryBase = HashMapEntryBase< K, V, ENTRY_HANDLER > |
using | HashValueType = typename ENTRY_HANDLER::HashValueType |
using | IsHashMap = std::true_type |
using | Iterator = IteratorTemplate< EntryIteratorBase > |
using | ConstIterator = ConstIteratorTemplate< EntryIteratorBase > |
using | KeyIterator = IteratorTemplate< KeyIteratorBase > |
using | ConstKeyIterator = ConstIteratorTemplate< KeyIteratorBase > |
using | ValueIterator = IteratorTemplate< ValueIteratorBase > |
using | ConstValueIterator = ConstIteratorTemplate< ValueIteratorBase > |
Public Types inherited from MapBase0< COLLECTION, KEYTYPE, VALUETYPE, SUPER, HASH > | |
using | MapType = COLLECTION |
using | Super = BaseCollection< COLLECTION, SUPER > |
using | KeyType = KEYTYPE |
using | ValueType = VALUETYPE |
Public Types inherited from BaseCollection< COLLECTION, SUPER > | |
using | IsCollection = std::true_type |
using | IsBaseArray = std::false_type |
Public Member Functions | |
HashMap () | |
HashMap (const ALLOCATOR &alloc) | |
~HashMap () | |
HashMap (HashMap &&src) | |
MAXON_OPERATOR_MOVE_ASSIGNMENT (HashMap) | |
ResultMem | SetCapacityHint (Int capacity, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY) |
ResultMem | ResizeTable (Int capacity) |
void | Reset () |
void | Flush () |
Int | GetCount () const |
Int | GetTableSize () const |
Int | GetMemorySize () const |
Int | GetNonEmptyBucketCount () const |
Entry * | GetNonEmptyBucket (Int i) |
const Entry * | GetNonEmptyBucket (Int i) const |
Int | GetOperationCountForSearch () const |
template<typename MAP , typename COMPARE > | |
SFINAEHelper< Bool, typename MAP::IsHashMap >::type | IsEqualImpl (const MAP &other, COMPARE &&cmp, OverloadRank1) const |
template<typename KEYHASH = HASH, typename KEY > | |
Entry * | Find (const KEY &key) |
template<typename KEYHASH = HASH, typename KEY > | |
const Entry * | Find (const KEY &key) const |
template<typename KEYHASH = HASH, typename KEY > | |
Opt< V & > | FindValue (const KEY &key) |
template<typename KEYHASH = HASH, typename KEY > | |
Opt< const V & > | FindValue (const KEY &key) const |
template<typename KEYHASH = HASH, typename KEY , typename C > | |
Result< Entry * > | InsertCtor (KEY &&key, C &&constructor, Bool &created=BoolLValue()) |
ResultRef< Entry > | InsertEntry (const K &key, Bool &created=BoolLValue()) |
ResultRef< Entry > | InsertEntry (K &&key, Bool &created=BoolLValue()) |
template<typename KEYHASH = HASH, typename KEY > | |
ResultRef< Entry > | InsertEntry (KEY &&key, Bool &created=BoolLValue()) |
ResultRef< V > | InsertKey (const K &key, Bool &created=BoolLValue()) |
ResultRef< V > | InsertKey (K &&key, Bool &created=BoolLValue()) |
template<typename KEYHASH = HASH, typename KEY > | |
ResultRef< V > | InsertKey (KEY &&key, Bool &created=BoolLValue()) |
template<typename KEYHASH = HASH, typename KEY , typename LAMBDA > | |
Result< Entry * > | InsertLambda (KEY &&key, LAMBDA &&lambda, Bool &created=BoolLValue()) |
template<typename KEYHASH = HASH, typename KEY > | |
ResultRef< Entry > | Insert (KEY &&key, const V &value, Bool &created=BoolLValue()) |
template<typename KEYHASH = HASH, typename KEY > | |
ResultRef< Entry > | Insert (KEY &&key, V &&value, Bool &created=BoolLValue()) |
template<typename KEYHASH = HASH, typename KEY > | |
ResultRef< Entry > | InsertMultiEntry (KEY &&key) |
ResultMem | InsertMultiEntry (Entry *e, HashValueType hash) |
ResultOk< void > | Erase (const Entry *entry, Bool deleteEntry=true) |
ResultOk< void > | Erase (Entry *entry, Bool deleteEntry=true) |
ResultOk< void > | Erase (const Entry &entry, Bool deleteEntry=true) |
ResultOk< void > | Erase (Entry &entry, Bool deleteEntry=true) |
void | DeleteEntry (const Entry *e) |
template<typename KEYHASH = HASH, typename KEY > | |
ResultOk< Bool > | Erase (const KEY &key) |
template<typename SET > | |
Result< void > | IntersectImpl (SET &&set, OverloadRank0) |
template<typename HASHMAP , typename = typename std::enable_if<std::is_same<typename std::decay<HASHMAP>::type, HashMap>::value>::type> | |
Result< void > | CopyFromImpl (HASHMAP &&other, COLLECTION_RESIZE_FLAGS resizeFlags, OverloadRank1) |
template<typename S > | |
SFINAEHelper< Result< void >, typename std::remove_reference< S >::type::MapType >::type | AppendAllImpl (S &&src, COLLECTION_RESIZE_FLAGS resizeFlags, Bool overwrite, OverloadRank1) |
MultiEntryIterator< false > | FindAll (const K &key) |
MultiEntryIterator< true > | FindAll (const K &key) const |
KeyIterator | GetKeys () |
ConstKeyIterator | GetKeys () const |
ValueIterator | GetValues () |
ConstValueIterator | GetValues () const |
Iterator | Begin () |
Iterator | End () |
ConstIterator | Begin () const |
ConstIterator | End () const |
template<template< Bool > class SUPER> | |
IteratorTemplate< SUPER > | Erase (const IteratorTemplate< SUPER > &it, Bool deleteEntry=true) |
Iterator | GetIterator (const Entry *e) |
ConstIterator | GetIterator (const Entry *e) const |
SFINAEHelper< String, V >::type | ToString (const FormatStatement *formatStatement=nullptr) const |
template<typename KEY , typename KEYHASH > | |
const Entry * | FindEntryImpl (HashValueType hash, const KEY &key) const |
template<typename KEY , typename KEYHASH > | |
Entry * | FindEntryImpl (HashValueType hash, const KEY &key) |
template<typename COLLECTION2 > | |
Result< void > | AppendAllImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, Bool overwrite, OverloadRank0) |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | CopyFromImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, OverloadRank0) |
Public Member Functions inherited from MapBase< HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10, DefaultAllocator >, K, V, EmptyClass, DefaultCompare > | |
MAXON_ATTRIBUTE_FORCE_INLINE | MapBase (ARGS &&... args) |
MapImpl< HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10, DefaultAllocator > & > | ToMap () |
MapImpl< const HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10, DefaultAllocator > & > | ToMap () const |
MAXON_ATTRIBUTE_FORCE_INLINE | operator MapImpl< HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10, DefaultAllocator > & > () |
MAXON_ATTRIBUTE_FORCE_INLINE | operator MapImpl< const HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10, DefaultAllocator > & > () const |
Public Member Functions inherited from MapBase0< COLLECTION, KEYTYPE, VALUETYPE, SUPER, HASH > | |
template<typename... ARGS> | |
MAXON_ATTRIBUTE_FORCE_INLINE | MapBase0 (ARGS &&... args) |
MAXON_ATTRIBUTE_FORCE_INLINE Bool | Contains (typename ByValueParam< KEYTYPE >::type key) const |
template<typename PAIR > | |
MAXON_ATTRIBUTE_FORCE_INLINE SFINAEHelper< Bool, typename PAIR::KeyType >::type | Contains (const PAIR &pair) const |
ResultRef< VALUETYPE > | Append (const KEYTYPE &key) |
template<typename PAIR > | |
SFINAEHelper< ResultRef< VALUETYPE >, typename PAIR::KeyType >::type | Append (const PAIR &pair) |
template<typename COLLECTION2 > | |
Result< void > | Add (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY) |
template<typename COLLECTION2 > | |
Result< void > | AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY) |
template<typename COLLECTION2 > | |
Result< void > | AppendAllInverse (COLLECTION2 &&other) |
template<typename COLLECTION2 > | |
Bool | ContainsAllImpl (COLLECTION2 &&other, OverloadRank0) const |
template<typename COLLECTION2 > | |
Result< void > | SubtractImpl (COLLECTION2 &&other, OverloadRank0) |
template<typename COLLECTION2 , typename COMPARE > | |
Bool | IsEqualImpl (const COLLECTION2 &other, COMPARE &&cmp, OverloadRank0) const |
HashInt | GetHashCode () const |
UniqueHash | GetUniqueHashCode () const |
Public Member Functions inherited from BaseCollection< COLLECTION, SUPER > | |
template<typename... ARGS> | |
MAXON_ATTRIBUTE_FORCE_INLINE | BaseCollection (ARGS &&... args) |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type | operator== (const COLLECTION2 &other) const |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type | operator!= (const COLLECTION2 &other) const |
template<typename COMPARE = EqualityCompare, typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value &&!STD_IS_REPLACEMENT(same, typename std::decay< COMPARE >::type, EQUALITY), Bool >::type | IsEqual (const COLLECTION2 &other, COMPARE &&cmp=COMPARE()) const |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY) |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | CopyFrom (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::FIT_TO_SIZE) |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | Subtract (COLLECTION2 &&other) |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | Intersect (const COLLECTION2 &other) |
template<typename COLLECTION2 > | |
Bool | Intersects (const COLLECTION2 &other) const |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > | CopyFromImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, OverloadRank0) |
template<typename COLLECTION2 > | |
Result< void > | AppendAllImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, Bool overwrite, OverloadRank0) |
template<typename COLLECTION2 > | |
Result< void > | IntersectImpl (COLLECTION2 &&other, OverloadRank0) |
MAXON_ATTRIBUTE_FORCE_INLINE Bool | IsEmpty () const |
MAXON_ATTRIBUTE_FORCE_INLINE Bool | IsPopulated () const |
String | ToString (const FormatStatement *formatStatement=nullptr) const |
template<typename COLLECTION2 > | |
MAXON_ATTRIBUTE_FORCE_INLINE Bool | ContainsAll (COLLECTION2 &&other) const |
template<typename COLLECTION2 > | |
Bool | ContainsAllImpl (COLLECTION2 &&other, OverloadRank0) const |
Static Public Member Functions | |
static const Entry * | GetEntry (const V *value) |
static Entry * | GetEntry (typename std::remove_const< V >::type *value) |
Static Public Member Functions inherited from MapBase0< COLLECTION, KEYTYPE, VALUETYPE, SUPER, HASH > | |
static const KEYTYPE & | GetMapKey (const KEYTYPE &key) |
template<typename PAIR > | |
static const KEYTYPE & | GetMapKey (const PAIR &pair) |
Protected Types | |
using | Bucket = typename std::conditional< MODE==HASHMAP_MODE::DEFAULT, DefaultBucket, SimpleBucket >::type |
Protected Member Functions | |
Bool | ResizeTableImpl (Int length) |
Entry * | AddEntryImpl (Entry *e, Entry *prev, Bool &created, Bool multi, void *) |
Entry * | AddEntryImpl (Entry *e, Entry *prev, Bool &created, Bool multi, Char *) |
void | ResetImpl (Bool destructor) |
void | FlushEntriesImpl () |
const Char * | GetSignature (void *) const |
const Char * | GetSignature (Char *) const |
Static Protected Member Functions | |
static Entry * | LoadRelaxed (AtomicPtr< Entry > &e) |
static Entry * | LoadAcquire (AtomicPtr< Entry > &e) |
static void | StoreRelaxed (AtomicPtr< Entry > &e, Entry *newValue) |
static Bool | TryCompareAndSwap (AtomicPtr< Entry > &e, Entry *newValue, Entry *compare) |
static Entry * | LoadRelaxed (Entry *e) |
static Entry * | LoadAcquire (Entry *e) |
static void | StoreRelaxed (Entry *&e, Entry *newValue) |
static Bool | TryCompareAndSwap (Entry *&e, Entry *newValue, Entry *compare) |
Protected Attributes | |
HashMapAllocator< ALLOCATOR, ENTRY_ALLOCATOR > | _allocator |
Bucket * | _table |
Int | _tableLengthM1 |
Bucket ** | _nonemptyBuckets |
Int | _nonemptyBucketCount |
Int | _size |
Int | _resizeThreshold |
Additional Inherited Members | |
Static Public Attributes inherited from MapBase0< COLLECTION, KEYTYPE, VALUETYPE, SUPER, HASH > | |
static const COLLECTION_KIND | KIND |
using Super = MapBase<HashMap, K, V, EmptyClass, HASH> |
using HashType = HASH |
using EntryBase = HashMapEntryBase<K, V, ENTRY_HANDLER> |
using HashValueType = typename ENTRY_HANDLER::HashValueType |
using IsHashMap = std::true_type |
using Iterator = IteratorTemplate<EntryIteratorBase> |
using KeyIterator = IteratorTemplate<KeyIteratorBase> |
|
protected |
HashMap | ( | ) |
Constructs a new HashMap with an optional load factor. This will not allocate any memory. Memory allocation can be done explicitly by SetCapacityHint(), or it will be done implicitly when needed.
|
explicit |
Initializes the underlying allocator and constructs a new HashMap with an optional load factor. This will not allocate any memory. Memory allocation can be done explicitly by SetCapacityHint(), or it will be done implicitly when needed.
[in] | alloc | Used to initialize the underlying allocator by its copy constructor. |
~HashMap | ( | ) |
Destructs all entries and frees any allocated memory.
HashMap | ( | HashMap< K, V, HASH, ENTRY_HANDLER, ALLOCATOR, MODE, INITIAL_CAPACITY, LOAD_FACTOR, ENTRY_ALLOCATOR > && | src | ) |
MAXON_OPERATOR_MOVE_ASSIGNMENT | ( | HashMap< K, V, HASH, ENTRY_HANDLER, ALLOCATOR, MODE, INITIAL_CAPACITY, LOAD_FACTOR, ENTRY_ALLOCATOR > | ) |
ResultMem SetCapacityHint | ( | Int | capacity, |
COLLECTION_RESIZE_FLAGS | resizeFlags = COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY |
||
) |
Ensures that the bucket table is large enough to hold at least capacity entries, taking into account the load factor (see explanation of the class HashMap itself).
[in] | capacity | The number of entries which can be stored without the need for re-hashing. |
[in] | resizeFlags | Not used by HashMap. |
Resizes the bucket table of the HashMap. Usually, with a positive load factor, this is done automatically when needed. You can force a resize if you know that a large number of entries will be added, this will eliminate some intermediate resizing. For a non-positive load factor, you have to manually resize the table if advisable. This function can also be used to reduce the table size (it gets never reduced automatically).
[in] | capacity | The number of entries which can be stored without the need for re-hashing. |
void Reset | ( | ) |
Resets the map. This destructs all entries and frees any memory held by the map, so the map will be in a state as if it had been newly constructed.
void Flush | ( | ) |
Flushes the map. This destructs and frees all entries, but does not free the bucket table.
Int GetCount | ( | ) | const |
Returns the number of entries in this map.
Int GetTableSize | ( | ) | const |
Returns the current size of the bucket table.
Int GetMemorySize | ( | ) | const |
Calculates the memory usage for this map. Keys and Values must have a public member GetMemorySize that return the element size.
Int GetNonEmptyBucketCount | ( | ) | const |
Returns the number of non-empty buckets in this map. This can be used together with GetNonEmptyBucket() to iterate over non-empty buckets.
Returns the i-th non-empty bucket of this map.
[in] | i | Index into the list of non-empty buckets (from 0 to GetNonEmptyBucketCount() - 1) |
Returns the i-th non-empty bucket of this map.
[in] | i | Index into the list of non-empty buckets (from 0 to GetNonEmptyBucketCount() - 1) |
Int GetOperationCountForSearch | ( | ) | const |
Returns an estimate of the number of operations needed to locate a given key in this map. This is used when two collections are compared: The iteration goes over the collection which would require more operations for search, and each entry is searched in the other collection.
SFINAEHelper<Bool, typename MAP::IsHashMap>::type IsEqualImpl | ( | const MAP & | other, |
COMPARE && | cmp, | ||
OverloadRank1 | |||
) | const |
Entry* Find | ( | const KEY & | key | ) |
Finds the entry with the given key in this map. The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
[in] | key | Key to search for. |
const Entry* Find | ( | const KEY & | key | ) | const |
Finds the entry with the given key in this map. The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
[in] | key | Key to search for. |
Opt<V&> FindValue | ( | const KEY & | key | ) |
Finds the value associated with the given key in this map. The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
[in] | key | Key to search for. |
Opt<const V&> FindValue | ( | const KEY & | key | ) | const |
Finds the value associated with the given key in this map. The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
[in] | key | Key to search for. |
Result<Entry*> InsertCtor | ( | KEY && | key, |
C && | constructor, | ||
Bool & | created = BoolLValue() |
||
) |
Finds the entry with the given key, or creates such an entry if it doesn't exist yet. If a new entry has to be created, it is constructed with the help of the object passed to the constructor parameter: Its class C has to provide a function Int C::GetHashMapEntrySize(const K& key)
to compute the size of a new entry for key and a function Result<void> C::ConstructHashMapEntry(void* ptr, HashInt hash, const K& key)
which uses the memory in ptr to construct a new entry for the key. If the constructor does not initialize the value of the new entry, this has to be done afterwards.
The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
C | Type of the constructor argument. |
[in] | key | Key of the entry to find or create. |
[in] | constructor | The functions constructor.GetHashMapEntrySize(ptr, hash, key) and constructor.ConstructHashMapEntry(ptr, hash, key) will be used to construct a new entry from the memory in ptr. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
ResultRef<Entry> InsertEntry | ( | const K & | key, |
Bool & | created = BoolLValue() |
||
) |
Finds the entry with the given key, or creates such an entry if it doesn't exist yet. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked).
[in] | key | Key of the entry to find or create. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
ResultRef<Entry> InsertEntry | ( | K && | key, |
Bool & | created = BoolLValue() |
||
) |
Finds the entry with the given key, or creates such an entry if it doesn't exist yet. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked).
[in] | key | Key of the entry to find or create. If a new entry is created, its key will be constructed by move-semantics if possible. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
ResultRef<Entry> InsertEntry | ( | KEY && | key, |
Bool & | created = BoolLValue() |
||
) |
Finds the entry with the given key, or creates such an entry if it doesn't exist yet. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked). The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
[in] | key | Key of the entry to find or create. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
ResultRef<V> InsertKey | ( | const K & | key, |
Bool & | created = BoolLValue() |
||
) |
Finds the value associated with the given key, or creates a corresponding entry if it doesn't exist yet. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked).
[in] | key | Key of the value to find or create. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
ResultRef<V> InsertKey | ( | K && | key, |
Bool & | created = BoolLValue() |
||
) |
Finds the value associated with the given key, or creates a corresponding entry if it doesn't exist yet. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked).
[in] | key | Key of the entry to find or create. If a new entry is created, its key will be constructed by move-semantics if possible. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
ResultRef<V> InsertKey | ( | KEY && | key, |
Bool & | created = BoolLValue() |
||
) |
Finds the value associated with the given key, or creates a corresponding entry if it doesn't exist yet. The value of a new entry has to be initialized afterwards (but its default constructor has already been invoked). The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
[in] | key | Key of the value to find or create. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
Result<Entry*> InsertLambda | ( | KEY && | key, |
LAMBDA && | lambda, | ||
Bool & | created = BoolLValue() |
||
) |
Finds the entry with the given key, or creates such an entry if it doesn't exist yet. If a new entry has to be created, the passed lambda
function is invoked with the newly created entry as Entry&
argument in order to initialize the value of the entry. The lambda function has to return Result<void>
, for example [](MyMap::Entry& e) -> Result<void> { e.SetValue(42); return OK; }
.
The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
Within the lambda you must not make any additional change to the hash map.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
LAMBDA | Type of the function. |
[in] | key | Key of the entry to find or create. |
[in] | lambda | The function which will be invoked as return lambda(entry); to initialize the value of a newly created entry. |
[out] | created | This will be set to true if a new entry has been created successfully, otherwise it will be set to false. |
ResultRef<Entry> Insert | ( | KEY && | key, |
const V & | value, | ||
Bool & | created = BoolLValue() |
||
) |
Associates the given value with the given key. This adds a new entry for key if necessary, and then sets its value to the given value, whether the entry existed before or not. The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
[in] | key | Key which shall map to the value. |
[in] | value | Value to which the key shall map. |
[out] | created | This will be set to true if a new entry has been created, otherwise it will be set to false. |
ResultRef<Entry> Insert | ( | KEY && | key, |
V && | value, | ||
Bool & | created = BoolLValue() |
||
) |
Associates the given value with the given key. This adds a new entry for key if necessary, and then sets its value to the given value, whether the entry existed before or not. The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
[in] | key | Key which shall map to the value. |
[in] | value | Value to which the key shall map. It will be moved into the HashMap. |
[out] | created | This will be set to true if a new entry has been created, otherwise it will be set to false. |
Adds an entry for the given key, even if an entry for the key already exists. In the latter case, the HashMap becomes a multi-map with more than one value per key. To iterate over all entries for a key, use FindAll() or Entry::GetNextWithSameKey(). The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key. Default is HASH. |
KEY | Type of key. |
[in] | key | Key of the entry to add. |
ResultMem InsertMultiEntry | ( | Entry * | e, |
HashValueType | hash | ||
) |
Adds an entry for the given key, even if an entry for the key already exists. In the latter case, the HashMap becomes a multi-map with more than one value per key. To iterate over all entries for a key, use FindAll() or Entry::GetNextWithSameKey(). The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)) unless the default HASH class is already able to do so.
Removes the given entry from this HashMap.
[in] | entry | The entry to remove. |
[in] | deleteEntry | If true (the default), the entry is also deleted, i.e., destructed and freed. If false, it won't be deleted, and you have to do this afterwards using DeleteEntry(). |
Removes the given entry from this HashMap.
[in] | entry | The entry to remove. |
[in] | deleteEntry | If true (the default), the entry is also deleted, i.e., destructed and freed. If false, it won't be deleted, and you have to do this afterwards using DeleteEntry(). |
void DeleteEntry | ( | const Entry * | e | ) |
Deletes an entry which has been removed from this HashMap previously. You don't have to invoke this function yourself unless you removed an entry using Erase() with a value of false for the deleteEntry parameter.
[in] | e | An entry which has been removed from this HashMap previously, but not yet deleted. |
Removes an entry with the given key from this HashMap (if possible). In case of a multi-map, this only removes a single entry. The type KEY of the given key need not be the same as K, but then you have to provide an additional class KEYHASH to compute the hash code of the specified key (function KEYHASH::GetHashCode(const KEY&)), and to compare a key of type KEY with a key of type K for equality (function KEYHASH::IsEqual(const KEY&, const K&)) unless the default HASH class is already able to do so.
KEYHASH | Hash class to compute the hash code of key, and to compare key with the map's keys. Default is HASH. |
KEY | Type of key. |
[in] | key | An entry having this key shall be removed. |
Result<void> IntersectImpl | ( | SET && | set, |
OverloadRank0 | |||
) |
Removes all entries from this map whose keys are not contained in set. A key
is in set
if set.Contains(key)
returns true, or if that is not a valid expression, if set(key)
returns true. I.e., you can use a lambda expression as set
.
[in] | set | A set with which this map gets intersected. Can be a lambda expression. |
SET | Type of set . |
Result<void> CopyFromImpl | ( | HASHMAP && | other, |
COLLECTION_RESIZE_FLAGS | resizeFlags, | ||
OverloadRank1 | |||
) |
SFINAEHelper<Result<void>, typename std::remove_reference<S>::type::MapType>::type AppendAllImpl | ( | S && | src, |
COLLECTION_RESIZE_FLAGS | resizeFlags, | ||
Bool | overwrite, | ||
OverloadRank1 | |||
) |
|
static |
MultiEntryIterator<false> FindAll | ( | const K & | key | ) |
Returns a foreach iterator to iterate over all entries having the given key. This is only needed for a multi-map.
[in] | key | Key to search for. |
MultiEntryIterator<true> FindAll | ( | const K & | key | ) | const |
Returns a foreach iterator to iterate over all entries having the given key. This is only needed for a multi-map.
[in] | key | Key to search for. |
KeyIterator GetKeys | ( | ) |
Returns a foreach iterator to iterate over all keys of this map.
ConstKeyIterator GetKeys | ( | ) | const |
Returns a foreach iterator to iterate over all keys of this map.
ValueIterator GetValues | ( | ) |
Returns a foreach iterator to iterate over all values of this map.
ConstValueIterator GetValues | ( | ) | const |
Returns a foreach iterator to iterate over all values of this map.
Iterator Begin | ( | ) |
Iterator End | ( | ) |
ConstIterator Begin | ( | ) | const |
ConstIterator End | ( | ) | const |
IteratorTemplate<SUPER> Erase | ( | const IteratorTemplate< SUPER > & | it, |
Bool | deleteEntry = true |
||
) |
ConstIterator GetIterator | ( | const Entry * | e | ) | const |
SFINAEHelper<String, V>::type ToString | ( | const FormatStatement * | formatStatement = nullptr | ) | const |
const Entry* FindEntryImpl | ( | HashValueType | hash, |
const KEY & | key | ||
) | const |
Entry* FindEntryImpl | ( | HashValueType | hash, |
const KEY & | key | ||
) |
|
protected |
|
protected |
|
staticprotected |
|
protected |
|
protected |
Result<void> AppendAllImpl | ( | typename COLLECTION2 | ) |
MAXON_ATTRIBUTE_FORCE_INLINE Result<void> CopyFromImpl | ( | typename COLLECTION2 | ) |
|
protected |
The allocator to use.
|
protected |
Pointer to the bucket table.
|
protected |
Length - 1 of the bucket table.
|
protected |
Pointer to the array of pointers to non-empty buckets.
|
protected |
Number of non-empty buckets.
|
protected |
When _size exceeds this threshold, a re-hashing has to be done to satisfy the load factor setting.