HashMap< K, V, HASH, ENTRY_HANDLER, ALLOCATOR, MODE, INITIAL_CAPACITY, LOAD_FACTOR > Class Template Reference Data Structures

#include <hashmap.h>

Inheritance diagram for HashMap< K, V, HASH, ENTRY_HANDLER, ALLOCATOR, MODE, INITIAL_CAPACITY, LOAD_FACTOR >:

详细描述

template<typename K, typename V, typename HASH = DefaultCompare, typename ENTRY_HANDLER = HashMapKeyValuePair, typename ALLOCATOR = DefaultAllocator, HASHMAP_MODE MODE = HASHMAP_MODE::DEFAULT, Int INITIAL_CAPACITY = 16, Int LOAD_FACTOR = (MODE == HASHMAP_MODE::SYNCHRONIZED) ? 0 : 10>
class maxon::HashMap< K, V, HASH, ENTRY_HANDLER, ALLOCATOR, MODE, INITIAL_CAPACITY, LOAD_FACTOR >

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:

using IntStringMap = HashMap<Int, String>; IntStringMap map; ... // now store "Hello World" at key 42 Bool created = false ; String & s = map.InsertKey(42, created) iferr_return ; if (created) // if created is false, there already exists an entry for 42 in the map { // initialize the new value s = "Hello World" _s; } ... // now check if there is an entry at key 42 String * s = map.FindValue(42); if (s) { DiagnosticOutput ( "Found value @" , *s); }

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:

IntStringMap::Entry& e = map.Insert(42, "Hello World" _s) iferr_return ;

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:

for (IntStringMap::Entry* e = map.Find(42); e; e = e->GetNextWithSameKey()) { ... }

You can also use the foreach iterator returned by FindAll() :

for ( const IntStringMap::Entry& e : map.FindAll(42)) { ... }

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.

for (IntStringMap::ConstIterator i = map.Begin(); i != map.End(); ++i) { DiagnosticOutput ( "@ -> @" , i->GetKey(), i->GetValue()); } for ( const IntStringMap::Entry& entry : map) { DiagnosticOutput ( "@ -> @" , entry.GetKey(), entry.GetValue()); }

When the template parameter MODE 被设为 { 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.

Template Parameters
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.
另请参阅
$ref maps

Classes

class   ConstIteratorTemplate
struct   DefaultBucket
class   Entry
class   EntryIteratorBase
class   Hash
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  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< HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, K, V, EmptyClass, DefaultCompare >
using  MapType = HashMap < K, V, DefaultCompare , HashMapKeyValuePair , DefaultAllocator , HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT== HASHMAP_MODE::SYNCHRONIZED ) ? 0 :10 >
using  Super = BaseCollection < HashMap < K, V, DefaultCompare , HashMapKeyValuePair , DefaultAllocator , HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT== HASHMAP_MODE::SYNCHRONIZED ) ? 0 :10 >, EmptyClass >
using  KeyType = K
using  ValueType = V
-  Public Types inherited from BaseCollection< HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, EmptyClass >
using  IsCollection = std::true_type

公共成员函数

  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  重置 ()
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 >
V *  FindValue (const KEY &key)
template<typename KEYHASH = HASH, typename KEY >
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)
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, UInt 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 ( UInt hash, const KEY &key) const
template<typename KEY , typename KEYHASH >
Entry FindEntryImpl ( UInt hash, const KEY &key)
-  Public Member Functions inherited from MapBase< HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, 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 > & >  ToMap ()
MapImpl < const HashMap < K, V, DefaultCompare , HashMapKeyValuePair , DefaultAllocator , HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT== HASHMAP_MODE::SYNCHRONIZED ) ? 0 :10 > & >  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 > & > ()
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 > & > () const
-  Public Member Functions inherited from MapBase0< HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, K, V, EmptyClass, DefaultCompare >
MAXON_ATTRIBUTE_FORCE_INLINE   MapBase0 (ARGS &&... args)
MAXON_ATTRIBUTE_FORCE_INLINE Bool   Contains (typename ByValueParam < K >::type key) const
MAXON_ATTRIBUTE_FORCE_INLINE SFINAEHelper < Bool , typename PAIR::KeyType >::type  Contains (const PAIR &pair) const
ResultRef < V >  Append (const K &key)
SFINAEHelper < ResultRef < V >, typename PAIR::KeyType >::type  Append (const PAIR &pair)
Result < void >  添加 (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags= COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY )
Result < void >  AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags= COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY )
Result < void >  AppendAllInverse (COLLECTION2 &&other)
Bool   ContainsAllImpl (COLLECTION2 &&other, OverloadRank0 ) const
Result < void >  SubtractImpl (COLLECTION2 &&other, OverloadRank0 )
Bool   IsEqualImpl (const COLLECTION2 &other, COMPARE &&cmp, OverloadRank0 ) const
HashInt   GetHashCode () const
-  Public Member Functions inherited from BaseCollection< HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, EmptyClass >
MAXON_ATTRIBUTE_FORCE_INLINE   BaseCollection (ARGS &&... args)
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type  operator== (const COLLECTION2 &other) const
MAXON_ATTRIBUTE_FORCE_INLINE std::enable_if< maxon::IsCollection< COLLECTION2 >::value, Bool >::type  operator!= (const COLLECTION2 &other) const
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
MAXON_ATTRIBUTE_FORCE_INLINE Result < void >  AppendAll (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags= COLLECTION_RESIZE_FLAGS::ON_GROW_RESERVE_CAPACITY )
MAXON_ATTRIBUTE_FORCE_INLINE Result < void >  CopyFrom (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags= COLLECTION_RESIZE_FLAGS::FIT_TO_SIZE )
MAXON_ATTRIBUTE_FORCE_INLINE Result < void >  Subtract (COLLECTION2 &&other)
MAXON_ATTRIBUTE_FORCE_INLINE Result < void >  Intersect (const COLLECTION2 &other)
Bool   Intersects (const COLLECTION2 &other) const
MAXON_ATTRIBUTE_FORCE_INLINE Result < void >  CopyFromImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, OverloadRank0 )
Result < void >  AppendAllImpl (COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags, Bool overwrite, OverloadRank0 )
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
MAXON_ATTRIBUTE_FORCE_INLINE Bool   ContainsAll (COLLECTION2 &&other) const
Bool   ContainsAllImpl (COLLECTION2 &&other, OverloadRank0 ) const

静态公共成员函数

static const Entry GetEntry (const V *value)
static Entry GetEntry (typename std::remove_const< V >::type *value)
-  Static Public Member Functions inherited from MapBase0< HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, K, V, EmptyClass, DefaultCompare >
static const K &  GetMapKey (const K &key)
static const K &  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 *)
  MAXON_DISALLOW_COPY_AND_ASSIGN ( HashMap )
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

ALLOCATOR  _allocator
Bucket _table
Int   _tableLengthM1
Bucket **  _nonemptyBuckets
Int   _nonemptyBucketCount
Int   _size
Int   _resizeThreshold
const Float   _obsoleteLoadFactor

Additional Inherited Members

-  Static Public Attributes inherited from MapBase0< HashMap< K, V, DefaultCompare, HashMapKeyValuePair, DefaultAllocator, HASHMAP_MODE::DEFAULT, 16,(HASHMAP_MODE::DEFAULT==HASHMAP_MODE::SYNCHRONIZED) ? 0 :10 >, K, V, EmptyClass, DefaultCompare >
static const COLLECTION_KIND   KIND

Member Typedef Documentation

◆  Super

using Super = MapBase < HashMap , K, V, EmptyClass , HASH>

◆  HashType

using HashType = HASH

◆  IsHashMap

using IsHashMap = std::true_type

◆  Iterator

using Iterator = IteratorTemplate < EntryIteratorBase >

◆  ConstIterator

using ConstIterator = ConstIteratorTemplate < EntryIteratorBase >

◆  KeyIterator

using KeyIterator = IteratorTemplate < KeyIteratorBase >

◆  ConstKeyIterator

using ConstKeyIterator = ConstIteratorTemplate < KeyIteratorBase >

◆  ValueIterator

using ValueIterator = IteratorTemplate < ValueIteratorBase >

◆  ConstValueIterator

using ConstValueIterator = ConstIteratorTemplate < ValueIteratorBase >

◆  Bucket

using Bucket = typename std::conditional<MODE == HASHMAP_MODE::DEFAULT , DefaultBucket , SimpleBucket >::type
protected

构造函数 & 析构函数文档编制

◆  HashMap() [1/3]

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.

◆  HashMap() [2/3]

HashMap ( const ALLOCATOR &  alloc )
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()

~ HashMap ( )

Destructs all entries and frees any allocated memory.

◆  HashMap() [3/3]

HashMap ( HashMap < K, V, HASH, ENTRY_HANDLER, ALLOCATOR, MODE, INITIAL_CAPACITY, LOAD_FACTOR > &&  src )

成员函数文档编制

◆  MAXON_OPERATOR_MOVE_ASSIGNMENT()

MAXON_OPERATOR_MOVE_ASSIGNMENT ( HashMap < K, V, HASH, ENTRY_HANDLER, ALLOCATOR, MODE, INITIAL_CAPACITY, LOAD_FACTOR >  )

◆  SetCapacityHint()

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 .
返回
True if memory allocations succeeded.

◆  ResizeTable()

ResultMem ResizeTable ( Int   capacity )

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 resizings. 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.
返回
True if memory allocations succeeded. If not, the HashMap will still be in a valid state, but still with the previous table size.

◆  Reset()

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.

另请参阅
Flush()

◆  Flush()

void Flush ( )

Flushes the map. This destructs and frees all entries, but does not free the bucket table.

另请参阅
Reset()

◆  GetCount()

Int GetCount ( ) const

Returns the number of entries in this map.

返回
Number of entries.

◆  GetTableSize()

Int GetTableSize ( ) const

Returns the current size of the bucket table.

返回
Size of bucket table.

◆  GetMemorySize()

Int GetMemorySize ( ) const

Calculates the memory usage for this map. Keys and Values must have a public member GetMemorySize that return the element size.

返回
Memory size in bytes.

◆  GetNonEmptyBucketCount()

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.

返回
Number of non-empty buckets.

◆  GetNonEmptyBucket() [1/2]

Entry * GetNonEmptyBucket ( Int   i )

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)
返回
I-th non-empty bucket.

◆  GetNonEmptyBucket() [2/2]

const Entry * GetNonEmptyBucket ( Int   i ) const

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)
返回
I-th non-empty bucket.

◆  GetOperationCountForSearch()

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.

返回
Estimate for the number of operations.

◆  IsEqualImpl()

SFINAEHelper < Bool , typename MAP::IsHashMap>::type IsEqualImpl ( const MAP &  other ,
COMPARE &&  cmp ,
OverloadRank1    
) const

◆  Find() [1/2]

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.

Template Parameters
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.
返回
Entry having the given key, or nullptr if no such entry exists in this map.

◆  Find() [2/2]

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.

Template Parameters
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.
返回
Entry having the given key, or nullptr if no such entry exists in this map.

◆  FindValue() [1/2]

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.

Template Parameters
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.
返回
Pointer to value for the given key, or nullptr if no entry exists for the key.

◆  FindValue() [2/2]

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.

Template Parameters
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.
返回
Pointer to value for the given key, or nullptr if no entry exists for the key.

◆  InsertCtor()

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, UInt 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.

Template Parameters
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.
返回
Entry for the given key, or nullptr if the entry didn't exist and allocation of a new entry failed.

◆  InsertEntry() [1/3]

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.
返回
Entry for the given key, or nullptr if the entry didn't exist and allocation of a new entry failed.

◆  InsertEntry() [2/3]

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.
返回
Entry for the given key, or nullptr if the entry didn't exist and allocation of a new entry failed.

◆  InsertEntry() [3/3]

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.

Template Parameters
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.
返回
Entry for the given key, or nullptr if the entry didn't exist and allocation of a new entry failed.

◆  InsertKey() [1/3]

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.
返回
Pointer to value for the given key, or nullptr if an entry didn't exist and allocation of a new entry failed.

◆  InsertKey() [2/3]

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.
返回
Pointer to value for the given key, or nullptr if an entry didn't exist and allocation of a new entry failed.

◆  InsertKey() [3/3]

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.

Template Parameters
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.
返回
Pointer to value for the given key, or nullptr if an entry didn't exist and allocation of a new entry failed.

◆  InsertLambda()

Result < Entry *> InsertLambda ( KEY &&  key ,
LAMBDA &&  lambda  
)

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.

Template Parameters
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.
返回
Entry for the given key, or nullptr if the entry didn't exist and allocation of a new entry failed.

◆  Insert() [1/2]

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.

Template Parameters
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.
返回
Entry for the key-value-association, or nullptr if such an entry didn't exist before and allocation of a new entry failed.

◆  Insert() [2/2]

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.

Template Parameters
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.
返回
Entry for the key-value-association, or nullptr if such an entry didn't exist before and allocation of a new entry failed.

◆  InsertMultiEntry() [1/2]

ResultRef < Entry > InsertMultiEntry ( KEY &&  key )

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.

Template Parameters
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.
返回
Added entry for the given key, or nullptr if the allocation failed.

◆  InsertMultiEntry() [2/2]

ResultMem InsertMultiEntry ( Entry e ,
UInt   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.

参数
[in] e Entry to add.
[in] hash Hash value of the key.
返回
OK on success.

◆  Erase() [1/6]

ResultOk <void> Erase ( const Entry entry ,
Bool   deleteEntry = true  
)

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() .
返回
OK

◆  Erase() [2/6]

ResultOk <void> Erase ( Entry entry ,
Bool   deleteEntry = true  
)

◆  Erase() [3/6]

ResultOk <void> Erase ( const Entry entry ,
Bool   deleteEntry = true  
)

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() .
返回
OK

◆  Erase() [4/6]

ResultOk <void> Erase ( Entry entry ,
Bool   deleteEntry = true  
)

◆  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.

◆  Erase() [5/6]

ResultOk < Bool > Erase ( const KEY &  key )

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.

Template Parameters
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.
返回
True if an entry was found and removed for #key, otherwise false.

◆  IntersectImpl()

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.
Template Parameters
SET Type of set .

◆  CopyFromImpl()

Result <void> CopyFromImpl ( HASHMAP &&  other ,
COLLECTION_RESIZE_FLAGS   resizeFlags ,
OverloadRank1    
)

◆  AppendAllImpl()

SFINAEHelper < Result <void>, typename std::remove_reference<S>::type::MapType>::type AppendAllImpl ( S &&  src ,
COLLECTION_RESIZE_FLAGS   resizeFlags ,
Bool   overwrite ,
OverloadRank1    
)

◆  GetEntry() [1/2]

static const Entry * GetEntry ( const V *  value )
static

Returns the pointer to the entry to which #value belongs. You must not use this function if you cannot guarantee that value is a part of an entry.

参数
[in] value A pointer to a value which is known to belong to an entry.
返回
The entry of which #value is a part.

◆  GetEntry() [2/2]

static Entry * GetEntry ( typename std::remove_const< V >::type *  value )
static

Returns the pointer to the entry to which #value belongs. You must not use this function if you cannot guarantee that value is a part of an entry.

参数
[in] value A pointer to a value which is known to belong to an entry.
返回
The entry of which #value is a part.

◆  FindAll() [1/2]

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.
返回
Iterator which yields all entries having the given key.

◆  FindAll() [2/2]

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.
返回
Iterator which yields all entries having the given key.

◆  GetKeys() [1/2]

KeyIterator GetKeys ( )

Returns a foreach iterator to iterate over all keys of this map.

返回
Foreach iterator over all keys.

◆  GetKeys() [2/2]

ConstKeyIterator GetKeys ( ) const

Returns a foreach iterator to iterate over all keys of this map.

返回
Foreach iterator over all keys.

◆  GetValues() [1/2]

ValueIterator GetValues ( )

Returns a foreach iterator to iterate over all values of this map.

返回
Foreach iterator over all values.

◆  GetValues() [2/2]

ConstValueIterator GetValues ( ) const

Returns a foreach iterator to iterate over all values of this map.

返回
Foreach iterator over all values.

◆  Begin() [1/2]

Iterator Begin ( )

◆  End() [1/2]

Iterator End ( )

◆  Begin() [2/2]

ConstIterator Begin ( ) const

◆  End() [2/2]

ConstIterator End ( ) const

◆  Erase() [6/6]

IteratorTemplate <SUPER> Erase ( const IteratorTemplate < SUPER > &  it ,
Bool   deleteEntry = true  
)

◆  GetIterator() [1/2]

Iterator GetIterator ( const Entry e )

◆  GetIterator() [2/2]

ConstIterator GetIterator ( const Entry e ) const

◆  ToString()

SFINAEHelper < String , V>::type ToString ( const FormatStatement formatStatement = nullptr ) const

◆  FindEntryImpl() [1/2]

const Entry * FindEntryImpl ( UInt   hash ,
const KEY &  key  
) const

◆  FindEntryImpl() [2/2]

Entry * FindEntryImpl ( UInt   hash ,
const KEY &  key  
)

◆  ResizeTableImpl()

Bool ResizeTableImpl ( Int   length )
protected

◆  AddEntryImpl() [1/2]

Entry * AddEntryImpl ( Entry e ,
Entry prev ,
Bool created ,
Bool   multi ,
void *   
)
protected

◆  AddEntryImpl() [2/2]

Entry * AddEntryImpl ( Entry e ,
Entry prev ,
Bool created ,
Bool   multi ,
Char  
)
protected

◆  MAXON_DISALLOW_COPY_AND_ASSIGN()

MAXON_DISALLOW_COPY_AND_ASSIGN ( HashMap < K, V, HASH, ENTRY_HANDLER, ALLOCATOR, MODE, INITIAL_CAPACITY, LOAD_FACTOR >  )
protected

◆  LoadRelaxed() [1/2]

static Entry * LoadRelaxed ( AtomicPtr < Entry > &  e )
static protected

◆  LoadAcquire() [1/2]

static Entry * LoadAcquire ( AtomicPtr < Entry > &  e )
static protected

◆  StoreRelaxed() [1/2]

static void StoreRelaxed ( AtomicPtr < Entry > &  e ,
Entry newValue  
)
static protected

◆  TryCompareAndSwap() [1/2]

static Bool TryCompareAndSwap ( AtomicPtr < Entry > &  e ,
Entry newValue ,
Entry compare  
)
static protected

◆  LoadRelaxed() [2/2]

static Entry * LoadRelaxed ( Entry e )
static protected

◆  LoadAcquire() [2/2]

static Entry * LoadAcquire ( Entry e )
static protected

◆  StoreRelaxed() [2/2]

static void StoreRelaxed ( Entry *&  e ,
Entry newValue  
)
static protected

◆  TryCompareAndSwap() [2/2]

static Bool TryCompareAndSwap ( Entry *&  e ,
Entry newValue ,
Entry compare  
)
static protected

◆  GetSignature() [1/2]

const Char * GetSignature ( void *  ) const
protected

◆  GetSignature() [2/2]

const Char * GetSignature ( Char ) const
protected

Member Data Documentation

◆  _allocator

ALLOCATOR _allocator protected

The allocator to use.

◆  _table

Bucket * _table
protected

Pointer to the bucket table.

◆  _tableLengthM1

Int _tableLengthM1
protected

Length - 1 of the bucket table.

◆  _nonemptyBuckets

Bucket ** _nonemptyBuckets
protected

Pointer to the array of pointers to non-empty buckets.

◆  _nonemptyBucketCount

Int _nonemptyBucketCount
protected

Number of non-empty buckets.

◆  _size

Int _size
protected

Number of entries in this HashMap .

◆  _resizeThreshold

Int _resizeThreshold
protected

When _size exceeds this threshold, a re-hashing has to be done to satisfy the load factor setting.

◆  _obsoleteLoadFactor

const Float _obsoleteLoadFactor
protected

Unused, just exists for compatibility.

iferr_return
#define iferr_return
定义: resultbase.h:1434
String::String
String()
Default constructor.
定义: c4d_string.h:55
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
String
定义: c4d_string.h:38