3 #ifndef __CDS_CONTAINER_IMPL_ELLEN_BINTREE_MAP_H
4 #define __CDS_CONTAINER_IMPL_ELLEN_BINTREE_MAP_H
7 #include <cds/container/details/ellen_bintree_base.h>
8 #include <cds/intrusive/impl/ellen_bintree.h>
9 #include <cds/container/details/guarded_ptr_cast.h>
11 namespace cds { namespace container {
13 /// Map based on Ellen's et al binary search tree
14 /** @ingroup cds_nonintrusive_map
15 @ingroup cds_nonintrusive_tree
16 @anchor cds_container_EllenBinTreeMap
19 - [2010] F.Ellen, P.Fatourou, E.Ruppert, F.van Breugel "Non-blocking Binary Search Tree"
21 %EllenBinTreeMap is an unbalanced leaf-oriented binary search tree that implements the <i>map</i>
22 abstract data type. Nodes maintains child pointers but not parent pointers.
23 Every internal node has exactly two children, and all data of type <tt>std::pair<Key const, T></tt>
24 currently in the tree are stored in the leaves. Internal nodes of the tree are used to direct \p find
25 operation along the path to the correct leaf. The keys (of \p Key type) stored in internal nodes
26 may or may not be in the map.
27 Unlike \ref cds_container_EllenBinTreeSet "EllenBinTreeSet" keys are not a part of \p T type.
28 The map can be represented as a set containing <tt>std::pair< Key const, T> </tt> values.
30 Due to \p extract_min and \p extract_max member functions the \p %EllenBinTreeMap can act as
31 a <i>priority queue</i>. In this case you should provide unique compound key, for example,
32 the priority value plus some uniformly distributed random value.
34 @warning Recall the tree is <b>unbalanced</b>. The complexity of operations is <tt>O(log N)</tt>
35 for uniformly distributed random keys, but in worst case the complexity is <tt>O(N)</tt>.
37 @note In the current implementation we do not use helping technique described in the original paper.
38 In Hazard Pointer schema helping is too complicated and does not give any observable benefits.
39 Instead of helping, when a thread encounters a concurrent operation it just spins waiting for
40 the operation done. Such solution allows greatly simplify implementation of the tree.
42 <b>Template arguments</b> :
43 - \p GC - safe memory reclamation (i.e. light-weight garbage collector) type, like \p cds::gc::HP, \p cds::gc::DHP
45 - \p T - value type to be stored in tree's leaf nodes.
46 - \p Traits - map traits, default is \p ellen_bintree::traits
47 It is possible to declare option-based tree with \p ellen_bintree::make_map_traits metafunction
48 instead of \p Traits template argument.
50 @note Do not include <tt><cds/container/impl/ellen_bintree_map.h></tt> header file directly.
51 There are header file for each GC type:
52 - <tt><cds/container/ellen_bintree_map_hp.h></tt> - for Hazard Pointer GC cds::gc::HP
53 - <tt><cds/container/ellen_bintree_map_dhp.h></tt> - for Dynamic Hazard Pointer GC cds::gc::DHP
54 - <tt><cds/container/ellen_bintree_map_rcu.h></tt> - for RCU GC
55 (see \ref cds_container_EllenBinTreeMap_rcu "RCU-based EllenBinTreeMap")
61 #ifdef CDS_DOXYGEN_INVOKED
62 class Traits = ellen_bintree::traits
68 #ifdef CDS_DOXYGEN_INVOKED
69 : public cds::intrusive::EllenBinTree< GC, Key, T, Traits >
71 : public ellen_bintree::details::make_ellen_bintree_map< GC, Key, T, Traits >::type
75 typedef ellen_bintree::details::make_ellen_bintree_map< GC, Key, T, Traits > maker;
76 typedef typename maker::type base_class;
79 typedef GC gc; ///< Garbage collector
80 typedef Key key_type; ///< type of a key stored in the map
81 typedef T mapped_type; ///< type of value stored in the map
82 typedef std::pair< key_type const, mapped_type > value_type ; ///< Key-value pair stored in leaf node of the mp
83 typedef Traits traits; ///< Map traits
85 # ifdef CDS_DOXYGEN_INVOKED
86 typedef implementation_defined key_comparator; ///< key compare functor based on \p Traits::compare and \p Traits::less
88 typedef typename maker::intrusive_traits::compare key_comparator;
90 typedef typename base_class::item_counter item_counter; ///< Item counting policy
91 typedef typename base_class::memory_model memory_model; ///< Memory ordering, see \p cds::opt::memory_model
92 typedef typename base_class::node_allocator node_allocator_type; ///< allocator for maintaining internal node
93 typedef typename base_class::stat stat; ///< internal statistics type
94 typedef typename traits::copy_policy copy_policy; ///< key copy policy
95 typedef typename traits::back_off back_off; ///< Back-off strategy
97 typedef typename traits::allocator allocator_type; ///< Allocator for leaf nodes
98 typedef typename base_class::node_allocator node_allocator; ///< Internal node allocator
99 typedef typename base_class::update_desc_allocator update_desc_allocator; ///< Update descriptor allocator
103 typedef typename base_class::value_type leaf_node;
104 typedef typename base_class::internal_node internal_node;
105 typedef typename base_class::update_desc update_desc;
107 typedef typename maker::cxx_leaf_node_allocator cxx_leaf_node_allocator;
109 typedef std::unique_ptr< leaf_node, typename maker::leaf_deallocator > scoped_node_ptr;
114 typedef cds::gc::guarded_ptr< gc, leaf_node, value_type, details::guarded_ptr_cast_set<leaf_node, value_type> > guarded_ptr;
117 /// Default constructor
126 /// Inserts new node with key and default value
128 The function creates a node with \p key and default value, and then inserts the node created into the map.
131 - The \ref key_type should be constructible from a value of type \p K.
132 In trivial case, \p K is equal to \ref key_type.
133 - The \ref mapped_type should be default-constructible.
135 Returns \p true if inserting successful, \p false otherwise.
137 template <typename K>
138 bool insert( K const& key )
140 return insert_key( key, [](value_type&){} );
145 The function creates a node with copy of \p val value
146 and then inserts the node created into the map.
149 - The \p key_type should be constructible from \p key of type \p K.
150 - The \p value_type should be constructible from \p val of type \p V.
152 Returns \p true if \p val is inserted into the map, \p false otherwise.
154 template <typename K, typename V>
155 bool insert( K const& key, V const& val )
157 scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key, val ));
158 if ( base_class::insert( *pNode ))
166 /// Inserts new node and initialize it by a functor
168 This function inserts new node with key \p key and if inserting is successful then it calls
169 \p func functor with signature
172 void operator()( value_type& item );
176 The argument \p item of user-defined functor \p func is the reference
177 to the map's item inserted:
178 - <tt>item.first</tt> is a const reference to item's key that cannot be changed.
179 - <tt>item.second</tt> is a reference to item's value that may be changed.
181 The key_type should be constructible from value of type \p K.
183 The function allows to split creating of new item into two part:
184 - create item from \p key;
185 - insert new item into the map;
186 - if inserting is successful, initialize the value of item by calling \p func functor
188 This can be useful if complete initialization of object of \p value_type is heavyweight and
189 it is preferable that the initialization should be completed only if inserting is successful.
191 template <typename K, typename Func>
192 bool insert_key( const K& key, Func func )
194 scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key ));
195 if ( base_class::insert( *pNode, [&func]( leaf_node& item ) { func( item.m_Value ); } )) {
202 /// For key \p key inserts data of type \p value_type created in-place from \p args
204 Returns \p true if inserting successful, \p false otherwise.
206 template <typename K, typename... Args>
207 bool emplace( K&& key, Args&&... args )
209 scoped_node_ptr pNode( cxx_leaf_node_allocator().New( std::forward<K>(key), std::forward<Args>(args)... ));
210 if ( base_class::insert( *pNode )) {
217 /// Ensures that the \p key exists in the map
219 The operation performs inserting or changing data with lock-free manner.
221 If the \p key not found in the map, then the new item created from \p key
222 is inserted into the map (note that in this case the \ref key_type should be
223 constructible from type \p K).
224 Otherwise, the functor \p func is called with item found.
225 The functor \p Func may be a function with signature:
227 void func( bool bNew, value_type& item );
232 void operator()( bool bNew, value_type& item );
237 - \p bNew - \p true if the item has been inserted, \p false otherwise
238 - \p item - item of the list
240 The functor may change any fields of the \p item.second that is \ref value_type.
242 Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
243 \p second is true if new item has been added or \p false if the item with \p key
244 already is in the list.
246 @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
248 template <typename K, typename Func>
249 std::pair<bool, bool> ensure( K const& key, Func func )
251 scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key ));
252 std::pair<bool, bool> res = base_class::ensure( *pNode,
253 [&func](bool bNew, leaf_node& item, leaf_node const& ){ func( bNew, item.m_Value ); }
255 if ( res.first && res.second )
260 /// Delete \p key from the map
261 /**\anchor cds_nonintrusive_EllenBinTreeMap_erase_val
263 Return \p true if \p key is found and deleted, \p false otherwise
265 template <typename K>
266 bool erase( K const& key )
268 return base_class::erase(key);
271 /// Deletes the item from the map using \p pred predicate for searching
273 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_erase_val "erase(K const&)"
274 but \p pred is used for key comparing.
275 \p Less functor has the interface like \p std::less.
276 \p Less must imply the same element order as the comparator used for building the map.
278 template <typename K, typename Less>
279 bool erase_with( K const& key, Less pred )
282 return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >());
285 /// Delete \p key from the map
286 /** \anchor cds_nonintrusive_EllenBinTreeMap_erase_func
288 The function searches an item with key \p key, calls \p f functor
289 and deletes the item. If \p key is not found, the functor is not called.
291 The functor \p Func interface:
294 void operator()(value_type& item) { ... }
298 Return \p true if key is found and deleted, \p false otherwise
300 template <typename K, typename Func>
301 bool erase( K const& key, Func f )
303 return base_class::erase( key, [&f]( leaf_node& node) { f( node.m_Value ); } );
306 /// Deletes the item from the map using \p pred predicate for searching
308 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_erase_func "erase(K const&, Func)"
309 but \p pred is used for key comparing.
310 \p Less functor has the interface like \p std::less.
311 \p Less must imply the same element order as the comparator used for building the map.
313 template <typename K, typename Less, typename Func>
314 bool erase_with( K const& key, Less pred, Func f )
317 return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >(),
318 [&f]( leaf_node& node) { f( node.m_Value ); } );
321 /// Extracts an item with minimal key from the map
323 If the map is not empty, the function returns \p true, \p result contains a pointer to minimum value.
324 If the map is empty, the function returns \p false, \p result is left unchanged.
326 @note Due the concurrent nature of the map, the function extracts <i>nearly</i> minimum key.
327 It means that the function gets leftmost leaf of the tree and tries to unlink it.
328 During unlinking, a concurrent thread may insert an item with key less than leftmost item's key.
329 So, the function returns the item with minimum key at the moment of tree traversing.
331 The guarded pointer \p result prevents deallocation of returned item,
332 see cds::gc::guarded_ptr for explanation.
333 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
335 bool extract_min( guarded_ptr& result )
337 return base_class::extract_min_( result.guard() );
340 /// Extracts an item with maximal key from the map
342 If the map is not empty, the function returns \p true, \p result contains a pointer to maximal value.
343 If the map is empty, the function returns \p false, \p result is left unchanged.
345 @note Due the concurrent nature of the map, the function extracts <i>nearly</i> maximal key.
346 It means that the function gets rightmost leaf of the tree and tries to unlink it.
347 During unlinking, a concurrent thread may insert an item with key great than leftmost item's key.
348 So, the function returns the item with maximum key at the moment of tree traversing.
350 The guarded pointer \p result prevents deallocation of returned item,
351 see cds::gc::guarded_ptr for explanation.
352 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
354 bool extract_max( guarded_ptr& result )
356 return base_class::extract_max_( result.guard() );
359 /// Extracts an item from the tree
360 /** \anchor cds_nonintrusive_EllenBinTreeMap_extract
361 The function searches an item with key equal to \p key in the tree,
362 unlinks it, and returns pointer to an item found in \p result parameter.
363 If the item is not found the function returns \p false.
365 The guarded pointer \p result prevents deallocation of returned item,
366 see cds::gc::guarded_ptr for explanation.
367 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
369 template <typename Q>
370 bool extract( guarded_ptr& result, Q const& key )
372 return base_class::extract_( result.guard(), key );
375 /// Extracts an item from the map using \p pred for searching
377 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_extract "extract(guarded_ptr&, Q const&)"
378 but \p pred is used for key compare.
379 \p Less has the interface like \p std::less.
380 \p pred must imply the same element order as the comparator used for building the map.
382 template <typename Q, typename Less>
383 bool extract_with( guarded_ptr& result, Q const& key, Less pred )
386 return base_class::extract_with_( result.guard(), key,
387 cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >());
390 /// Find the key \p key
391 /** \anchor cds_nonintrusive_EllenBinTreeMap_find_cfunc
393 The function searches the item with key equal to \p key and calls the functor \p f for item found.
394 The interface of \p Func functor is:
397 void operator()( value_type& item );
400 where \p item is the item found.
402 The functor may change \p item.second.
404 The function returns \p true if \p key is found, \p false otherwise.
406 template <typename K, typename Func>
407 bool find( K const& key, Func f )
409 return base_class::find( key, [&f](leaf_node& item, K const& ) { f( item.m_Value );});
412 /// Finds the key \p val using \p pred predicate for searching
414 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_find_cfunc "find(K const&, Func)"
415 but \p pred is used for key comparing.
416 \p Less functor has the interface like \p std::less.
417 \p Less must imply the same element order as the comparator used for building the map.
419 template <typename K, typename Less, typename Func>
420 bool find_with( K const& key, Less pred, Func f )
423 return base_class::find_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >(),
424 [&f](leaf_node& item, K const& ) { f( item.m_Value );});
427 /// Find the key \p key
428 /** \anchor cds_nonintrusive_EllenBinTreeMap_find_val
430 The function searches the item with key equal to \p key
431 and returns \p true if it is found, and \p false otherwise.
433 template <typename K>
434 bool find( K const& key )
436 return base_class::find( key );
439 /// Finds the key \p val using \p pred predicate for searching
441 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_find_val "find(K const&)"
442 but \p pred is used for key comparing.
443 \p Less functor has the interface like \p std::less.
444 \p Less must imply the same element order as the comparator used for building the map.
446 template <typename K, typename Less>
447 bool find_with( K const& key, Less pred )
450 return base_class::find_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >() );
453 /// Finds \p key and returns the item found
454 /** @anchor cds_nonintrusive_EllenBinTreeMap_get
455 The function searches the item with key equal to \p key and returns the item found in \p result parameter.
456 The function returns \p true if \p key is found, \p false otherwise.
458 The guarded pointer \p result prevents deallocation of returned item,
459 see cds::gc::guarded_ptr for explanation.
460 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
462 template <typename Q>
463 bool get( guarded_ptr& result, Q const& key )
465 return base_class::get_( result.guard(), key );
468 /// Finds \p key with predicate \p pred and returns the item found
470 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_get "get(guarded_ptr&, Q const&)"
471 but \p pred is used for key comparing.
472 \p Less functor has the interface like \p std::less.
473 \p pred must imply the same element order as the comparator used for building the map.
475 template <typename Q, typename Less>
476 bool get_with( guarded_ptr& result, Q const& key, Less pred )
479 return base_class::get_with_( result.guard(), key,
480 cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >() );
483 /// Clears the map (not atomic)
489 /// Checks if the map is empty
491 Emptiness is checked by item counting: if item count is zero then the map is empty.
495 return base_class::empty();
498 /// Returns item count in the set
500 Only leaf nodes containing user data are counted.
502 The value returned depends on item counter type provided by \p Traits template parameter.
503 If it is \p atomicity::empty_item_counter this function always returns 0.
505 The function is not suitable for checking the tree emptiness, use \p empty()
506 member function for this purpose.
510 return base_class::size();
513 /// Returns const reference to internal statistics
514 stat const& statistics() const
516 return base_class::statistics();
519 /// Checks internal consistency (not atomic, not thread-safe)
521 The debugging function to check internal consistency of the tree.
523 bool check_consistency() const
525 return base_class::check_consistency();
529 }} // namespace cds::container
531 #endif //#ifndef __CDS_CONTAINER_IMPL_ELLEN_BINTREE_MAP_H