|
cds
2.3.2
|
Michael's hash set. More...
#include <cds/container/michael_set.h>
Public Types | |
| typedef GC | gc |
| Garbage collector. | |
| typedef OrderedList | ordered_list |
| type of ordered list used as a bucket implementation | |
| typedef Traits | traits |
| Set traits. | |
| typedef ordered_list::value_type | value_type |
| type of value to be stored in the list | |
| typedef ordered_list::key_comparator | key_comparator |
| key comparison functor | |
| typedef ordered_list::stat | stat |
| Internal statistics. | |
| typedef cds::opt::v::hash_selector< typename traits::hash >::type | hash |
| Hash functor for value_type and all its derivatives that you use. | |
| typedef traits::item_counter | item_counter |
| Item counter type. | |
| typedef traits::allocator | allocator |
| Bucket table allocator. | |
| typedef internal_bucket_type::guarded_ptr | guarded_ptr |
Guarded pointer - a result of get() and extract() functions. | |
Public Member Functions | |
| MichaelHashSet (size_t nMaxItemCount, size_t nLoadFactor) | |
| Initialize hash set. More... | |
| ~MichaelHashSet () | |
| Clears hash set and destroys it. | |
| template<typename Q > | |
| bool | insert (Q &&val) |
| Inserts new node. More... | |
| template<typename Q , typename Func > | |
| bool | insert (Q &&val, Func f) |
| Inserts new node. More... | |
| template<typename Q , typename Func > | |
| std::pair< bool, bool > | update (Q &&val, Func func, bool bAllowUpdate=true) |
| Updates the element. More... | |
| template<typename Q > | |
| std::pair< bool, bool > | upsert (Q &&val, bool bAllowInsert=true) |
Inserts or updates the node (only for IterableList) More... | |
| template<typename... Args> | |
| bool | emplace (Args &&... args) |
Inserts data of type value_type constructed from args. More... | |
| template<typename Q > | |
| bool | erase (Q const &key) |
Deletes key from the set. More... | |
| template<typename Q , typename Less > | |
| bool | erase_with (Q const &key, Less pred) |
Deletes the item from the set using pred predicate for searching. More... | |
| template<typename Q , typename Func > | |
| bool | erase (Q const &key, Func f) |
Deletes key from the set. More... | |
| template<typename Q , typename Less , typename Func > | |
| bool | erase_with (Q const &key, Less pred, Func f) |
Deletes the item from the set using pred predicate for searching. More... | |
| bool | erase_at (iterator const &iter) |
Deletes the item pointed by iterator iter (only for IterableList based set) More... | |
| template<typename Q > | |
| guarded_ptr | extract (Q const &key) |
Extracts the item with specified key. More... | |
| template<typename Q , typename Less > | |
| guarded_ptr | extract_with (Q const &key, Less pred) |
Extracts the item using compare functor pred. More... | |
| template<typename Q , typename Func > | |
| bool | find (Q &key, Func f) |
Finds the key key. More... | |
| template<typename Q > | |
| iterator | find (Q &key) |
Finds key and returns iterator pointed to the item found (only for IterableList) More... | |
| template<typename Q , typename Less , typename Func > | |
| bool | find_with (Q &key, Less pred, Func f) |
Finds the key key using pred predicate for searching. More... | |
| template<typename Q , typename Less > | |
| iterator | find_with (Q &key, Less pred) |
Finds key using pred predicate and returns iterator pointed to the item found (only for IterableList) More... | |
| template<typename Q > | |
| bool | contains (Q const &key) |
Checks whether the set contains key. More... | |
| template<typename Q , typename Less > | |
| bool | contains (Q const &key, Less pred) |
Checks whether the set contains key using pred predicate for searching. More... | |
| template<typename Q > | |
| guarded_ptr | get (Q const &key) |
Finds the key key and return the item found. More... | |
| template<typename Q , typename Less > | |
| guarded_ptr | get_with (Q const &key, Less pred) |
Finds the key key and return the item found. More... | |
| void | clear () |
| Clears the set (non-atomic) More... | |
| bool | empty () const |
| Checks if the set is empty. More... | |
| size_t | size () const |
| Returns item count in the set. More... | |
| stat const & | statistics () const |
| Returns const reference to internal statistics. | |
| size_t | bucket_count () const |
| Returns the size of hash table. More... | |
Static Public Attributes | |
| static constexpr const size_t | c_nHazardPtrCount = ordered_list::c_nHazardPtrCount |
| Count of hazard pointer required. | |
Forward iterators | |
| typedef michael_set::details::iterator< internal_bucket_type, false > | iterator |
| Forward iterator. More... | |
| typedef michael_set::details::iterator< internal_bucket_type, true > | const_iterator |
| Const forward iterator. | |
| iterator | begin () |
| Returns a forward iterator addressing the first element in a set. More... | |
| iterator | end () |
| Returns an iterator that addresses the location succeeding the last element in a set. More... | |
| const_iterator | begin () const |
| Returns a forward const iterator addressing the first element in a set. | |
| const_iterator | cbegin () const |
| Returns a forward const iterator addressing the first element in a set. | |
| const_iterator | end () const |
| Returns an const iterator that addresses the location succeeding the last element in a set. | |
| const_iterator | cend () const |
| Returns an const iterator that addresses the location succeeding the last element in a set. | |
Michael's hash set.
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 list implementation used as bucket for hash set, possible implementations: MichaelList, LazyList, IterableList. The ordered list implementation specifies the type T to be stored in the hash-set, the comparing functor for the type T and other features specific for the ordered list.Traits - set traits, default is michael_set::traits. Instead of defining Traits struct you may use option-based syntax with michael_set::make_traits metafunction.There are the specializations:
cd/container/michael_set_rcu.h, see MichaelHashSet<RCU>.cds/container/michael_set_nogc.h, see MichaelHashSet<gc::nogc>.Some member functions of Michael's hash set accept the key parameter of type Q which differs from node type value_type. It is expected that type Q contains full key of node type value_type, and if keys of type Q and value_type are equal the hash values of these keys must be equal too.
The hash functor Traits::hash should accept parameters of both type:
How to use
Suppose, we have the following type Foo that we want to store in our MichaelHashSet:
To use MichaelHashSet for Foo values, you should first choose suitable ordered list class that will be used as a bucket for the set. We will use gc::DHP reclamation schema and MichaelList as a bucket type. Also, for ordered list we should develop a comparator for our Foo struct.
| typedef michael_set::details::iterator< internal_bucket_type, false > cds::container::MichaelHashSet< GC, OrderedList, Traits >::iterator |
Forward iterator.
The forward iterator for Michael's set 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:
MichaelList and LazyList: 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 set. 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:
Forward iterator
|
inline |
Initialize hash set.
The Michael's hash set 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)].
The ctor defines hash table size as rounding nMaxItemCount / nLoadFactor up to nearest power of two.
| nMaxItemCount | estimation of max item count in the hash set |
| nLoadFactor | load factor: estimation of max number of items in the bucket |
|
inline |
|
inline |
Returns the size of hash table.
Since MichaelHashSet cannot dynamically extend the hash table size, the value returned is an constant depending on object initialization parameters; see MichaelHashSet::MichaelHashSet for explanation.
|
inline |
Clears the set (non-atomic)
The function erases all items from the set.
The function is not atomic. It cleans up each bucket and then resets the item counter to zero. If there are a thread that performs insertion while clear is working the result is undefined in general case: empty() may return true but the set may contain item(s). Therefore, clear may be used only for debugging purposes.
|
inline |
Checks whether the set contains key.
The function searches the item with key equal to key and returns true if the key is found, and false otherwise.
Note the hash functor specified for class Traits template parameter should accept a parameter of type Q that can be not the same as value_type.
|
inline |
Checks whether the set 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 set.
|
inline |
Inserts data of type value_type constructed from args.
Returns true if inserting successful, false otherwise.
|
inline |
Checks if the set is empty.
atomicity::empty_item_counter in traits::item_counter, the function always returns true.
|
inline |
|
inline |
Deletes key from the set.
Since the key of MichaelHashSet's item type value_type is not explicitly specified, template parameter Q defines the key type searching in the list. The set item comparator should be able to compare the type value_type and the type Q.
Return true if key is found and deleted, false otherwise.
|
inline |
Deletes key from the set.
The function searches an item with key key, calls f functor and deletes the item. If key is not found, the functor is not called.
The functor Func interface:
where item - the item found.
Since the key of MichaelHashSet's value_type is not explicitly specified, template parameter Q defines the key type searching in the list. The list item comparator should be able to compare the type T of list item and the type Q.
Return true if key is found and deleted, false otherwise
|
inline |
Deletes the item pointed by iterator iter (only for IterableList based set)
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 MichaelHashSet based on IterableList.
|
inline |
Deletes the item from the set using pred predicate for searching.
The function is an analog of erase(Q 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 set.
|
inline |
Deletes the item from the set using pred predicate for searching.
The function is an analog of erase(Q 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 set.
|
inline |
Extracts the item with specified key.
The function searches an item with key equal to key, unlinks it from the set, and returns it as guarded_ptr. If key is not found the function returns an empty guadd pointer.
Note the compare functor should accept a parameter of type Q that may be not the same as value_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(Q const&) but pred predicate is used for key comparing.
Less functor has the semantics like std::less but should take arguments of type value_type and Q in any order. pred must imply the same element order as the comparator used for building the set.
|
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, key is the find function argument.
The functor may change non-key fields of item. 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 set's item. If such access is possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
The key argument is non-const since it can be used as f functor destination i.e., the functor can modify both arguments.
Note the hash functor specified for class Traits template parameter should accept a parameter of type Q that may be not the same as value_type.
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 key using pred predicate for searching.
The function is an analog of find(Q&, 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 set.
|
inline |
Finds key using pred predicate and returns iterator pointed to the item found (only for IterableList)
The function is an analog of find(Q&) 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 the key 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 functin 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 Q that can be not the same as value_type.
|
inline |
Finds the key key and return the item found.
The function is an analog of get( Q const&) but pred is used for comparing the keys.
Less functor has the semantics like std::less but should take arguments of type value_type and Q in any order. pred must imply the same element order as the comparator used for building the set.
|
inline |
Inserts new node.
The function creates a node with copy of val value and then inserts the node created into the set.
The type Q should contain as minimum the complete key for the node. The object of value_type should be constructible from a value of type Q. In trivial case, Q is equal to value_type.
Returns true if val is inserted into the set, false otherwise.
|
inline |
Inserts new node.
The function allows to split creating of new item into two part:
f functor to initialize value-fields of val.The functor signature is:
where val is the item inserted. The user-defined functor is called only if the inserting is success.
|
inline |
Returns item count in the set.
atomicity::empty_item_counter in traits::item_counter, the function always returns 0.
|
inline |
Updates the element.
The operation performs inserting or changing data with lock-free manner.
If the item val not found in the set, then val is inserted iff bAllowInsert is true. Otherwise, the functor func is called with item found.
The functor func signature depends of OrderedList:
for MichaelList, LazyList
with arguments:
bNew - true if the item has been inserted, false otherwiseitem - item of the setval - argument val passed into the update() functionThe functor may change non-key fields of the item.
for IterableList
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. 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 is in the set.
|
inline |
Inserts or updates the node (only for IterableList)
The operation performs inserting or changing data with lock-free manner.
If the item val is not found in the set, then val 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 set.