|
cds
2.3.2
|
Ellen's et al binary search tree. More...
#include <cds/intrusive/impl/ellen_bintree.h>
Public Types | |
| typedef GC | gc |
| Garbage collector. | |
| typedef Key | key_type |
type of a key to be stored in internal nodes; key is a part of value_type | |
| typedef T | value_type |
| type of value stored in the binary tree | |
| typedef Traits | traits |
| Traits template parameter. | |
| typedef traits::hook | hook |
| hook type | |
| typedef hook::node_type | node_type |
| node type | |
| typedef traits::disposer | disposer |
| leaf node disposer | |
| typedef traits::back_off | back_off |
| back-off strategy | |
| typedef gc::template guarded_ptr< value_type > | guarded_ptr |
| Guarded pointer. | |
| typedef implementation_defined | key_comparator |
key compare functor based on Traits::compare and Traits::less | |
| typedef get_node_traits< value_type, node_type, hook >::type | node_traits |
| Node traits. | |
| typedef traits::item_counter | item_counter |
| Item counting policy. | |
| typedef traits::memory_model | memory_model |
Memory ordering, see cds::opt::memory_model. | |
| typedef traits::stat | stat |
| internal statistics type | |
| typedef traits::key_extractor | key_extractor |
| key extracting functor | |
| typedef traits::node_allocator | node_allocator |
| Allocator for internal node. | |
| typedef traits::update_desc_allocator | update_desc_allocator |
| Update descriptor allocator. | |
Public Member Functions | |
| EllenBinTree () | |
| Default constructor. | |
| ~EllenBinTree () | |
| Clears the tree. | |
| bool | insert (value_type &val) |
| Inserts new node. More... | |
| template<typename Func > | |
| bool | insert (value_type &val, Func f) |
| Inserts new node. More... | |
| template<typename Func > | |
| std::pair< bool, bool > | update (value_type &val, Func func, bool bAllowInsert=true) |
| Updates the node. More... | |
| bool | unlink (value_type &val) |
Unlinks the item val from the tree. More... | |
| template<typename Q > | |
| bool | erase (const Q &key) |
| Deletes the item from the tree. More... | |
| template<typename Q , typename Less > | |
| bool | erase_with (const Q &key, Less pred) |
Delete the item from the tree with comparing functor pred. More... | |
| template<typename Q , typename Func > | |
| bool | erase (Q const &key, Func f) |
| Deletes the item from the tree. More... | |
| template<typename Q , typename Less , typename Func > | |
| bool | erase_with (Q const &key, Less pred, Func f) |
Delete the item from the tree with comparing functor pred. More... | |
| guarded_ptr | extract_min () |
| Extracts an item with minimal key from the tree. More... | |
| guarded_ptr | extract_max () |
| Extracts an item with maximal key from the tree. More... | |
| template<typename Q > | |
| guarded_ptr | extract (Q const &key) |
| Extracts an item from the tree. More... | |
| template<typename Q , typename Less > | |
| guarded_ptr | extract_with (Q const &key, Less pred) |
Extracts an item from the tree using pred for searching. More... | |
| template<typename Q > | |
| bool | contains (Q const &key) const |
Checks whether the set contains key. More... | |
| template<typename Q , typename Less > | |
| bool | contains (Q const &key, Less pred) const |
Checks whether the set contains key using pred predicate for searching. More... | |
| template<typename Q , typename Func > | |
| bool | find (Q &key, Func f) const |
Finds the key key. More... | |
| template<typename Q , typename Less , typename Func > | |
| bool | find_with (Q &key, Less pred, Func f) const |
Finds the key key with comparing functor pred. More... | |
| template<typename Q > | |
| guarded_ptr | get (Q const &key) const |
Finds key and returns the item found. More... | |
| template<typename Q , typename Less > | |
| guarded_ptr | get_with (Q const &key, Less pred) const |
Finds key with predicate pred and returns the item found. More... | |
| bool | empty () const |
| Checks if the tree is empty. | |
| void | clear () |
| Clears the tree (thread safe, not atomic) More... | |
| void | unsafe_clear () |
| Clears the tree (not thread safe) More... | |
| size_t | size () const |
| Returns item count in the tree. More... | |
| stat const & | statistics () const |
| Returns const reference to internal statistics. | |
| bool | check_consistency () const |
| Checks internal consistency (not atomic, not thread-safe) More... | |
Static Public Attributes | |
| static constexpr const size_t | c_nHazardPtrCount = 9 |
| Count of hazard pointer required for the algorithm. | |
Protected Attributes | |
| item_counter | m_ItemCounter |
| item counter | |
| stat | m_Stat |
| internal statistics | |
Ellen's et al binary search tree.
EllenBinTree is an unbalanced leaf-oriented binary search tree that implements the set abstract data type. Nodes maintains child pointers but not parent pointers. Every internal node has exactly two children, and all data of type T currently in the tree are stored in the leaves. Internal nodes of the tree are used to direct find() operation along the path to the correct leaf. The keys (of Key type) stored in internal nodes may or may not be in the set. Key type is a subset of T type. There should be exactly defined a key extracting functor for converting object of type T to object of type Key.
Due to extract_min() and extract_max() member functions the EllenBinTree can act as a priority queue. In this case you should provide unique compound key, for example, the priority value plus some uniformly distributed random value.
O(log N) for uniformly distributed random keys, but in the worst case the complexity is O(N).<cds/intrusive/impl/ellen_bintree.h> header file explicitly. There are header file for each GC type:<cds/intrusive/ellen_bintree_hp.h> - for Hazard Pointer GC cds::gc::HP <cds/intrusive/ellen_bintree_dhp.h> - for Dynamic Hazard Pointer GC cds::gc::DHP <cds/intrusive/ellen_bintree_rcu.h> - for RCU (see RCU-based EllenBinTree)Template arguments :
GC - garbage collector, possible types are cds::gc::HP, cds::gc::DHP.Key - key type, a subset of T T - type to be stored in tree's leaf nodes. The type must be based on ellen_bintree::node (for ellen_bintree::base_hook) or it must have a member of type ellen_bintree::node (for ellen_bintree::member_hook).Traits - tree traits, default is ellen_bintree::traits It is possible to declare option-based tree with ellen_bintree::make_traits metafunction instead of Traits template argument.Traits::less, Traits::compare and other predicates using with member fuctions should accept at least parameters of type T and Key in any combination. For example, for Foo struct with std::string key field the appropiate less functor is:
Usage examples see here
|
inline |
Checks internal consistency (not atomic, not thread-safe)
The debugging function to check internal consistency of the tree.
|
inline |
Clears the tree (thread safe, not atomic)
The function unlink all items from the tree. The function is thread safe but not atomic: in multi-threaded environment with parallel insertions this sequence
the assertion could be raised.
For each leaf the disposer will be called after unlinking.
|
inline |
Checks whether the set 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 set contains key using pred predicate for searching.
The function is similar to 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 |
Deletes the item from the tree.
The function searches an item with key equal to key in the tree, unlinks it from the tree, and returns true. If the item with key equal to key is not found the function return false.
Note the Traits::less and/or Traits::compare predicate should accept a parameter of type Q that can be not the same as value_type.
|
inline |
Deletes the item from the tree.
The function searches an item with key equal to key in the tree, call f functor with item found, unlinks it from the tree, and returns true. The disposer specified in Traits class template parameter is called by garbage collector GC asynchronously.
The Func interface is
If the item with key equal to key is not found the function return false.
Note the Traits::less and/or Traits::compare predicate should accept a parameter of type Q that can be not the same as value_type.
|
inline |
Delete the item from the tree with comparing functor pred.
The function is an analog of erase(Q const&) but pred predicate is used for key comparing. Less has the interface like std::less and should meet Predicate requirements. pred must imply the same element order as the comparator used for building the tree.
|
inline |
Delete the item from the tree with comparing functor pred.
The function is an analog of erase(Q const&, Func) but pred predicate is used for key comparing. Less has the interface like std::less and should meet Predicate requirements. pred must imply the same element order as the comparator used for building the tree.
|
inline |
Extracts an item from the tree.
The function searches an item with key equal to key in the tree, unlinks it, and returns a guarded pointer to an item found. If the item is not found the function returns an empty guarded_ptr.
guarded_ptr prevents disposer invocation for returned item, see cds::gc::guarded_ptr for explanation.
guarded_ptr object uses the GC's guard that can be limited resource.
|
inline |
Extracts an item with maximal key from the tree.
The function searches an item with maximal key, unlinks it, and returns a guarded pointer to an item found. If the tree is empty the function returns an empty guarded_ptr.
The returned guarded_ptr prevents disposer invocation for returned item, see cds::gc::guarded_ptr for explanation.
guarded_ptr object uses the GC's guard that can be limited resource.
|
inline |
Extracts an item with minimal key from the tree.
The function searches an item with minimal key, unlinks it, and returns a guarded pointer to an item found. If the tree is empty the function returns an empty guarded pointer.
The returned guarded_ptr prevents disposer invocation for returned item, see cds::gc::guarded_ptr for explanation.
guarded_ptr object uses the GC's guard that can be limited resource.
|
inline |
Extracts an item from the tree using pred for searching.
The function is an analog of extract(Q const&) but pred is used for key compare. Less has the interface like std::less and should meet Predicate requirements. pred must imply the same element order as the comparator used for building the tree.
|
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 can 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 tree 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 the key key with comparing functor pred.
The function is an analog of find(Q&, Func) but pred is used for key comparison. Less functor has the interface like std::less and should meet Predicate requirements. pred must imply the same element order as the comparator used for building the tree.
|
inline |
Finds key and returns the item found.
The function searches the item with key equal to key and returns the item found as guarded_ptr object. The function returns an empty guarded pointer is key is not found.
guarded_ptr prevents disposer invocation for returned item, see cds::gc::guarded_ptr for explanation.
guarded_ptr object uses the GC's guard that can be limited resource.
|
inline |
Finds key with predicate pred and returns the item found.
The function is an analog of get(Q const&) but pred is used for key comparing. Less functor has the interface like std::less and should meet Predicate requirements. pred must imply the same element order as the comparator used for building the tree.
|
inline |
Inserts new node.
The function inserts val in the tree if it does not contain an item with key equal to val.
Returns true if val is placed into the tree, false otherwise.
|
inline |
Inserts new node.
This function is intended for derived non-intrusive containers.
The function allows to split creating of new item into two part:
f functor to initialize value-field of val.The functor signature is:
where val is the item inserted. User-defined functor f should guarantee that during changing val no any other changes could be made on this tree's item by concurrent threads. The user-defined functor is called only if the inserting is success.
|
inline |
Returns item count in the tree.
Only leaf nodes containing user data are counted.
The value returned depends on item counter type provided by Traits template parameter. If it is atomicity::empty_item_counter this function always returns 0. The function is not suitable for checking the tree emptiness, use empty() member function for this purpose.
|
inline |
Unlinks the item val from the tree.
The function searches the item val in the tree and unlink it from the tree if it is found and is equal to val.
Difference between erase and unlink functions: erase finds a key and deletes the item found. unlink finds an item by key and deletes it only if val is a node, i.e. the pointer to item found is equal to &val .
The disposer specified in Traits class template parameter is called by garbage collector GC asynchronously.
The function returns true if success and false otherwise.
|
inline |
Clears the tree (not thread safe)
This function is not thread safe and may be called only when no other thread deals with the tree. The function is used in the tree destructor.
|
inline |
Updates the node.
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 into the set iff bAllowInsert is true. Otherwise, the functor func is called with item found. The functor func signature is:
with arguments:
bNew - true if the item has been inserted, false otherwiseitem - item of the setval - argument val passed into the update() function If new item has been inserted (i.e. bNew is true) then item and val arguments refer to the same thing.The functor can change non-key fields of the item; however, func must guarantee that during changing no any other modifications could be made on this item by concurrent threads.
Returns std::pair<bool, bool> where first is true if operation is successful, i.e. the node has been inserted or updated, second is true if new item has been added or false if the item with key already exists.