|
cds
2.3.2
|
Michael's hash map. More...
#include <cds/container/michael_map.h>
Public Types | |
| typedef GC | gc |
| Garbage collector. | |
| typedef OrderedList | ordered_list |
| type of ordered list to be used as a bucket | |
| typedef Traits | traits |
| Map traits. | |
| typedef ordered_list::key_type | key_type |
| key type | |
| typedef ordered_list::mapped_type | mapped_type |
| value type | |
| typedef ordered_list::value_type | value_type |
| key/value pair stored in the map | |
| typedef traits::allocator | allocator |
| Bucket table allocator. | |
| typedef ordered_list::key_comparator | key_comparator |
| key compare functor | |
| typedef ordered_list::stat | stat |
| typedef ordered_list::guarded_ptr | guarded_ptr |
Guarded pointer - a result of get() and extract() functions. | |
| typedef cds::opt::v::hash_selector< typename traits::hash >::type | hash |
| Hash functor for key_type and all its derivatives that you use. | |
| typedef traits::item_counter | item_counter |
| Item counter type. | |
Public Member Functions | |
| MichaelHashMap (size_t nMaxItemCount, size_t nLoadFactor) | |
| Initializes the map. More... | |
| ~MichaelHashMap () | |
| Clears hash map and destroys it. | |
| template<typename K > | |
| bool | insert (K &&key) |
| Inserts new node with key and default value. More... | |
| template<typename K , typename V > | |
| bool | insert (K &&key, V &&val) |
| Inserts new node. More... | |
| template<typename K , typename Func > | |
| bool | insert_with (K &&key, Func func) |
| Inserts new node and initialize it by a functor. More... | |
| template<typename K , typename Func > | |
| std::pair< bool, bool > | update (K &&key, Func func, bool bAllowInsert=true) |
Updates data by key. More... | |
| template<typename Q , typename V > | |
| std::pair< bool, bool > | upsert (Q &&key, V &&val, bool bAllowInsert=true) |
Inserts or updates the node (only for IterableKVList) More... | |
| template<typename K , typename... Args> | |
| bool | emplace (K &&key, Args &&... args) |
For key key inserts data of type mapped_type created from args. More... | |
| template<typename K > | |
| bool | erase (K const &key) |
Deletes key from the map. More... | |
| template<typename K , typename Less > | |
| bool | erase_with (K const &key, Less pred) |
Deletes the item from the map using pred predicate for searching. More... | |
| template<typename K , typename Func > | |
| bool | erase (K const &key, Func f) |
Deletes key from the map. More... | |
| template<typename K , typename Less , typename Func > | |
| bool | erase_with (K const &key, Less pred, Func f) |
Deletes the item from the map using pred predicate for searching. More... | |
| bool | erase_at (iterator const &iter) |
Deletes the item pointed by iterator iter (only for IterableList based map) More... | |
| template<typename K > | |
| guarded_ptr | extract (K const &key) |
Extracts the item with specified key. More... | |
| template<typename K , typename Less > | |
| guarded_ptr | extract_with (K const &key, Less pred) |
Extracts the item using compare functor pred. More... | |
| template<typename K , typename Func > | |
| bool | find (K const &key, Func f) |
Finds the key key. More... | |
| template<typename K > | |
| iterator | find (K const &key) |
Finds key and returns iterator pointed to the item found (only for IterableList) More... | |
| template<typename K , typename Less , typename Func > | |
| bool | find_with (K const &key, Less pred, Func f) |
Finds the key val using pred predicate for searching. More... | |
| template<typename K , typename Less > | |
| iterator | find_with (K const &key, Less pred) |
Finds key using pred predicate and returns iterator pointed to the item found (only for IterableList) More... | |
| template<typename K > | |
| bool | contains (K const &key) |
Checks whether the map contains key. More... | |
| template<typename K , typename Less > | |
| bool | contains (K const &key, Less pred) |
Checks whether the map contains key using pred predicate for searching. More... | |
| template<typename K > | |
| guarded_ptr | get (K const &key) |
Finds key and return the item found. More... | |
| template<typename K , typename Less > | |
| guarded_ptr | get_with (K const &key, Less pred) |
Finds key and return the item found. More... | |
| void | clear () |
| Clears the map (not atomic) | |
| bool | empty () const |
| Checks if the map is empty. More... | |
| size_t | size () const |
| Returns item count in the map. More... | |
| size_t | bucket_count () const |
| Returns the size of hash table. More... | |
| stat const & | statistics () const |
| Returns const reference to internal statistics. | |
Static Public Attributes | |
| static constexpr const size_t | c_nHazardPtrCount = ordered_list::c_nHazardPtrCount |
| Count of hazard pointer required. | |
Forward iterators (only for debugging purpose) | |
| typedef iterator_type< false > | iterator |
| Forward iterator. More... | |
| typedef iterator_type< true > | const_iterator |
| Const forward iterator. | |
| iterator | begin () |
| Returns a forward iterator addressing the first element in a map. More... | |
| iterator | end () |
| Returns an iterator that addresses the location succeeding the last element in a map. More... | |
| const_iterator | begin () const |
| Returns a forward const iterator addressing the first element in a map. | |
| const_iterator | cbegin () const |
| Returns a forward const iterator addressing the first element in a map. | |
| const_iterator | end () const |
| Returns an const iterator that addresses the location succeeding the last element in a map. | |
| const_iterator | cend () const |
| Returns an const iterator that addresses the location succeeding the last element in a map. | |
Michael's hash map.
Michael's hash table algorithm is based on lock-free ordered list and it is very simple. The main structure is an array T of size M. Each element in T is basically a pointer to a hash bucket, implemented as a singly linked list. The array of buckets cannot be dynamically expanded. However, each bucket may contain unbounded number of items.
Template parameters are:
GC - Garbage collector used. You may use any Garbage collector from the libcds library. Note the GC must be the same as the GC used for OrderedList OrderedList - ordered key-value list implementation used as bucket for hash map, for example, MichaelKVList, LazyKVList, IterableKVList. The ordered list implementation specifies the Key and Value types stored in the hash-map, the reclamation schema GC used by hash-map, the comparison functor for the type Key and other features specific for the ordered list.Traits - map traits, default is michael_map::traits. Instead of defining Traits struct you may use option-based syntax with michael_map::make_traits metafunction.Many of the class function take a key argument of type K that in general is not key_type. key_type and an argument of template type K must meet the following requirements:
key_type should be constructible from value of type K;key of type K: hash( key_type(key)) == hash( key ) key_type and K should be comparableThere are the specializations:
cds/container/michael_map_rcu.h, see MichaelHashMap<RCU>.cds::gc::nogc declared in cds/container/michael_map_nogc.h, see MichaelHashMap<gc::nogc>.Suppose, you want to make int to int map for Hazard Pointer garbage collector. You should choose suitable ordered list class that will be used as a bucket for the map; it may be MichaelKVList.
You may use option-based declaration:
| typedef iterator_type< false > cds::container::MichaelHashMap< GC, OrderedList, Traits >::iterator |
Forward iterator.
The forward iterator for Michael's map has some features:
gc::HP), a guard is a limited resource per thread, so an exception (or assertion) "no free guard" may be thrown if the limit of guard count per thread is exceeded.Iterator thread safety depends on type of OrderedList:
MichaelKVList and LazyKVList: iterator guarantees safety even if you delete the item that iterator points to because that item is guarded by hazard pointer. However, in case of concurrent deleting operations it is no guarantee that you iterate all item in the map. Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread. Use this iterator on the concurrent container for debugging purpose only.IterableList: iterator is thread-safe. You may use it freely in concurrent environment.The iterator interface:
| typedef ordered_list::stat cds::container::MichaelHashMap< GC, OrderedList, Traits >::stat |
Internal statistics
|
inline |
Initializes the map.
The Michael's hash map is non-expandable container. You should point the average count of items nMaxItemCount when you create an object. nLoadFactor parameter defines average count of items per bucket and it should be small number between 1 and 10. Remember, since the bucket implementation is an ordered list, searching in the bucket is linear [O(nLoadFactor)]. Note, that many popular STL hash map implementation uses load factor 1.
The ctor defines hash table size as rounding nMacItemCount / nLoadFactor up to nearest power of two.
| nMaxItemCount | estimation of max item count in the hash map |
| nLoadFactor | load factor: estimation of max number of items in the bucket |
|
inline |
|
inline |
Returns the size of hash table.
Since MichaelHashMap cannot dynamically extend the hash table size, the value returned is an constant depending on object initialization parameters; see MichaelHashMap::MichaelHashMap for explanation.
|
inline |
Checks whether the map contains key.
The function searches the item with key equal to key and returns true if it is found, and false otherwise.
|
inline |
Checks whether the map contains key using pred predicate for searching.
The function is an analog of contains( key ) but pred is used for key comparing. Less functor has the interface like std::less. Less must imply the same element order as the comparator used for building the map.
|
inline |
For key key inserts data of type mapped_type created from args.
key_type should be constructible from type K
Returns true if inserting successful, false otherwise.
|
inline |
Checks if the map is empty.
atomicity::empty_item_counter in traits::item_counter, the function always returns true.
|
inline |
|
inline |
|
inline |
|
inline |
Deletes the item pointed by iterator iter (only for IterableList based map)
Returns true if the operation is successful, false otherwise. The function can return false if the node the iterator points to has already been deleted by other thread.
The function does not invalidate the iterator, it remains valid and can be used for further traversing.
erase_at() is supported only for MichaelHashMap based on IterableList.
|
inline |
Deletes the item from the map using pred predicate for searching.
The function is an analog of erase(K const&) but pred is used for key comparing. Less functor has the interface like std::less. Less must imply the same element order as the comparator used for building the map.
|
inline |
Deletes the item from the map using pred predicate for searching.
The function is an analog of erase(K const&, Func) but pred is used for key comparing. Less functor has the interface like std::less. Less must imply the same element order as the comparator used for building the map.
|
inline |
Extracts the item with specified key.
The function searches an item with key equal to key, unlinks it from the map, and returns it as guarded_ptr. If key is not found the function returns an empty guarded pointer.
Note the compare functor should accept a parameter of type K that may be not the same as key_type.
The extracted item is freed automatically when returned guarded_ptr object will be destroyed or released.
guarded_ptr object uses the GC's guard that can be limited resource.Usage:
|
inline |
Extracts the item using compare functor pred.
The function is an analog of extract(K const&) but pred predicate is used for key comparing.
Less functor has the semantics like std::less but should take arguments of type key_type and K in any order. pred must imply the same element order as the comparator used for building the map.
|
inline |
Finds the key key.
The function searches the item with key equal to key and calls the functor f for item found. The interface of Func functor is:
where item is the item found.
The functor may change item.second. Note that the functor is only guarantee that item cannot be disposed during functor is executing. The functor does not serialize simultaneous access to the map's item. If such access is possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
The function returns true if key is found, false otherwise.
|
inline |
Finds key and returns iterator pointed to the item found (only for IterableList)
If key is not found the function returns end().
IterableList
|
inline |
Finds the key val using pred predicate for searching.
The function is an analog of find(K const&, Func) but pred is used for key comparing. Less functor has the interface like std::less. Less must imply the same element order as the comparator used for building the map.
|
inline |
Finds key using pred predicate and returns iterator pointed to the item found (only for IterableList)
The function is an analog of find(K&) but pred is used for key comparing. Less functor has the interface like std::less. pred must imply the same element order as the comparator used for building the set.
If key is not found the function returns end().
IterableList
|
inline |
Finds key and return the item found.
The function searches the item with key equal to key and returns the guarded pointer to the item found. If key is not found the function returns an empty guarded pointer,
guarded_ptr object uses one GC's guard which can be limited resource.Usage:
Note the compare functor specified for OrderedList template parameter should accept a parameter of type K that can be not the same as key_type.
|
inline |
Finds key and return the item found.
The function is an analog of get( K const&) but pred is used for comparing the keys.
Less functor has the semantics like std::less but should take arguments of type key_type and K in any order. pred must imply the same element order as the comparator used for building the map.
|
inline |
Inserts new node with key and default value.
The function creates a node with key and default value, and then inserts the node created into the map.
Preconditions:
key_type should be constructible from value of type K. In trivial case, K is equal to key_type.mapped_type should be default-constructible.Returns true if inserting successful, false otherwise.
|
inline |
Inserts new node.
The function creates a node with copy of val value and then inserts the node created into the map.
Preconditions:
key_type should be constructible from key of type K.mapped_type should be constructible from val of type V.Returns true if val is inserted into the map, false otherwise.
|
inline |
Inserts new node and initialize it by a functor.
This function inserts new node with key key and if inserting is successful then it calls func functor with signature
The argument item of user-defined functor func is the reference to the map's item inserted:
item.first is a const reference to item's key that cannot be changed.item.second is a reference to item's value that may be changed.The user-defined functor is called only if inserting is successful.
The key_type should be constructible from value of type K.
The function allows to split creating of new item into two part:
key;func functorThis can be useful if complete initialization of object of mapped_type is heavyweight and it is preferable that the initialization should be completed only if inserting is successful.
|
inline |
Returns item count in the map.
If you use atomicity::empty_item_counter in traits::item_counter, the function always returns 0.
|
inline |
Updates data by key.
The operation performs inserting or replacing the element with lock-free manner.
If the key not found in the map, then the new item created from key will be inserted into the map iff bAllowInsert is true. (note that in this case the key_type should be constructible from type K). Otherwise, if key is found, the functor func is called with item found.
The functor func signature depends on OrderedList:
for MichaelKVList, LazyKVList
with arguments:
bNew - true if the item has been inserted, false otherwiseitem - the item found or insertedThe functor may change any fields of the item.second that is mapped_type.
for IterableKVList
where
val - a new data constructed from key old - old value that will be retired. If new item has been inserted then old is nullptr.The functor may change non-key fields of val; however, func must guarantee that during changing no any other modifications could be made on this item by concurrent threads.
std::pair<bool, bool> where first is true if operation is successful, second is true if new item has been added or false if the item with key already exists.
|
inline |
Inserts or updates the node (only for IterableKVList)
The operation performs inserting or changing data with lock-free manner.
If key is not found in the map, then key is inserted iff bAllowInsert is true. Otherwise, the current element is changed to val, the old element will be retired later.
Returns std::pair<bool, bool> where first is true if operation is successful, second is true if val has been added or false if the item with that key already in the map.