2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CDSLIB_CONTAINER_IMPL_ELLEN_BINTREE_MAP_H
32 #define CDSLIB_CONTAINER_IMPL_ELLEN_BINTREE_MAP_H
34 #include <type_traits>
35 #include <cds/container/details/ellen_bintree_base.h>
36 #include <cds/intrusive/impl/ellen_bintree.h>
37 #include <cds/container/details/guarded_ptr_cast.h>
39 namespace cds { namespace container {
41 /// Map based on Ellen's et al binary search tree
42 /** @ingroup cds_nonintrusive_map
43 @ingroup cds_nonintrusive_tree
44 @anchor cds_container_EllenBinTreeMap
47 - [2010] F.Ellen, P.Fatourou, E.Ruppert, F.van Breugel "Non-blocking Binary Search Tree"
49 %EllenBinTreeMap is an unbalanced leaf-oriented binary search tree that implements the <i>map</i>
50 abstract data type. Nodes maintains child pointers but not parent pointers.
51 Every internal node has exactly two children, and all data of type <tt>std::pair<Key const, T></tt>
52 currently in the tree are stored in the leaves. Internal nodes of the tree are used to direct \p find
53 operation along the path to the correct leaf. The keys (of \p Key type) stored in internal nodes
54 may or may not be in the map.
55 Unlike \ref cds_container_EllenBinTreeSet "EllenBinTreeSet" keys are not a part of \p T type.
56 The map can be represented as a set containing <tt>std::pair< Key const, T> </tt> values.
58 Due to \p extract_min and \p extract_max member functions the \p %EllenBinTreeMap can act as
59 a <i>priority queue</i>. In this case you should provide unique compound key, for example,
60 the priority value plus some uniformly distributed random value.
62 @warning Recall the tree is <b>unbalanced</b>. The complexity of operations is <tt>O(log N)</tt>
63 for uniformly distributed random keys, but in worst case the complexity is <tt>O(N)</tt>.
65 @note In the current implementation we do not use helping technique described in the original paper.
66 In Hazard Pointer schema helping is too complicated and does not give any observable benefits.
67 Instead of helping, when a thread encounters a concurrent operation it just spins waiting for
68 the operation done. Such solution allows greatly simplify implementation of the tree.
70 <b>Template arguments</b> :
71 - \p GC - safe memory reclamation (i.e. light-weight garbage collector) type, like \p cds::gc::HP, \p cds::gc::DHP
73 - \p T - value type to be stored in tree's leaf nodes.
74 - \p Traits - map traits, default is \p ellen_bintree::traits
75 It is possible to declare option-based tree with \p ellen_bintree::make_map_traits metafunction
76 instead of \p Traits template argument.
78 @note Do not include <tt><cds/container/impl/ellen_bintree_map.h></tt> header file directly.
79 There are header file for each GC type:
80 - <tt><cds/container/ellen_bintree_map_hp.h></tt> - for Hazard Pointer GC cds::gc::HP
81 - <tt><cds/container/ellen_bintree_map_dhp.h></tt> - for Dynamic Hazard Pointer GC cds::gc::DHP
82 - <tt><cds/container/ellen_bintree_map_rcu.h></tt> - for RCU GC
83 (see \ref cds_container_EllenBinTreeMap_rcu "RCU-based EllenBinTreeMap")
89 #ifdef CDS_DOXYGEN_INVOKED
90 class Traits = ellen_bintree::traits
96 #ifdef CDS_DOXYGEN_INVOKED
97 : public cds::intrusive::EllenBinTree< GC, Key, T, Traits >
99 : public ellen_bintree::details::make_ellen_bintree_map< GC, Key, T, Traits >::type
103 typedef ellen_bintree::details::make_ellen_bintree_map< GC, Key, T, Traits > maker;
104 typedef typename maker::type base_class;
107 typedef GC gc; ///< Garbage collector
108 typedef Key key_type; ///< type of a key stored in the map
109 typedef T mapped_type; ///< type of value stored in the map
110 typedef std::pair< key_type const, mapped_type > value_type ; ///< Key-value pair stored in leaf node of the mp
111 typedef Traits traits; ///< Map traits
113 # ifdef CDS_DOXYGEN_INVOKED
114 typedef implementation_defined key_comparator; ///< key compare functor based on \p Traits::compare and \p Traits::less
116 typedef typename maker::intrusive_traits::compare key_comparator;
118 typedef typename base_class::item_counter item_counter; ///< Item counting policy
119 typedef typename base_class::memory_model memory_model; ///< Memory ordering, see \p cds::opt::memory_model
120 typedef typename base_class::node_allocator node_allocator_type; ///< allocator for maintaining internal node
121 typedef typename base_class::stat stat; ///< internal statistics type
122 typedef typename traits::copy_policy copy_policy; ///< key copy policy
123 typedef typename traits::back_off back_off; ///< Back-off strategy
125 typedef typename traits::allocator allocator_type; ///< Allocator for leaf nodes
126 typedef typename base_class::node_allocator node_allocator; ///< Internal node allocator
127 typedef typename base_class::update_desc_allocator update_desc_allocator; ///< Update descriptor allocator
131 typedef typename base_class::value_type leaf_node;
132 typedef typename base_class::internal_node internal_node;
133 typedef typename base_class::update_desc update_desc;
135 typedef typename maker::cxx_leaf_node_allocator cxx_leaf_node_allocator;
137 typedef std::unique_ptr< leaf_node, typename maker::leaf_deallocator > scoped_node_ptr;
142 typedef typename gc::template guarded_ptr< leaf_node, value_type, details::guarded_ptr_cast_set<leaf_node, value_type> > guarded_ptr;
145 /// Default constructor
154 /// Inserts new node with key and default value
156 The function creates a node with \p key and default value, and then inserts the node created into the map.
159 - The \ref key_type should be constructible from a value of type \p K.
160 In trivial case, \p K is equal to \ref key_type.
161 - The \ref mapped_type should be default-constructible.
163 Returns \p true if inserting successful, \p false otherwise.
165 template <typename K>
166 bool insert( K const& key )
168 return insert_with( key, [](value_type&){} );
173 The function creates a node with copy of \p val value
174 and then inserts the node created into the map.
177 - The \p key_type should be constructible from \p key of type \p K.
178 - The \p value_type should be constructible from \p val of type \p V.
180 Returns \p true if \p val is inserted into the map, \p false otherwise.
182 template <typename K, typename V>
183 bool insert( K const& key, V const& val )
185 scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key, val ));
186 if ( base_class::insert( *pNode ))
194 /// Inserts new node and initialize it by a functor
196 This function inserts new node with key \p key and if inserting is successful then it calls
197 \p func functor with signature
200 void operator()( value_type& item );
204 The argument \p item of user-defined functor \p func is the reference
205 to the map's item inserted:
206 - <tt>item.first</tt> is a const reference to item's key that cannot be changed.
207 - <tt>item.second</tt> is a reference to item's value that may be changed.
209 The key_type should be constructible from value of type \p K.
211 The function allows to split creating of new item into two part:
212 - create item from \p key;
213 - insert new item into the map;
214 - if inserting is successful, initialize the value of item by calling \p func functor
216 This can be useful if complete initialization of object of \p value_type is heavyweight and
217 it is preferable that the initialization should be completed only if inserting is successful.
219 template <typename K, typename Func>
220 bool insert_with( const K& key, Func func )
222 scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key ));
223 if ( base_class::insert( *pNode, [&func]( leaf_node& item ) { func( item.m_Value ); } )) {
230 /// For key \p key inserts data of type \p value_type created in-place from \p args
232 Returns \p true if inserting successful, \p false otherwise.
234 template <typename K, typename... Args>
235 bool emplace( K&& key, Args&&... args )
237 scoped_node_ptr pNode( cxx_leaf_node_allocator().New( std::forward<K>(key), std::forward<Args>(args)... ));
238 if ( base_class::insert( *pNode )) {
247 The operation performs inserting or changing data with lock-free manner.
249 If the item \p val is not found in the map, then \p val is inserted iff \p bAllowInsert is \p true.
250 Otherwise, the functor \p func is called with item found.
251 The functor \p func signature is:
254 void operator()( bool bNew, value_type& item );
259 - \p bNew - \p true if the item has been inserted, \p false otherwise
260 - \p item - item of the map
262 The functor may change any fields of the \p item.second that is \ref mapped_type;
263 however, \p func must guarantee that during changing no any other modifications
264 could be made on this item by concurrent threads.
266 Returns std::pair<bool, bool> where \p first is \p true if operation is successfull,
267 i.e. the node has been inserted or updated,
268 \p second is \p true if new item has been added or \p false if the item with \p key
271 @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
273 template <typename K, typename Func>
274 std::pair<bool, bool> update( K const& key, Func func, bool bAllowInsert = true )
276 scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key ));
277 std::pair<bool, bool> res = base_class::update( *pNode,
278 [&func](bool bNew, leaf_node& item, leaf_node const& ){ func( bNew, item.m_Value ); },
281 if ( res.first && res.second )
286 template <typename K, typename Func>
287 CDS_DEPRECATED("ensure() is deprecated, use update()")
288 std::pair<bool, bool> ensure( K const& key, Func func )
290 return update( key, func, true );
294 /// Delete \p key from the map
295 /**\anchor cds_nonintrusive_EllenBinTreeMap_erase_val
297 Return \p true if \p key is found and deleted, \p false otherwise
299 template <typename K>
300 bool erase( K const& key )
302 return base_class::erase(key);
305 /// Deletes the item from the map using \p pred predicate for searching
307 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_erase_val "erase(K const&)"
308 but \p pred is used for key comparing.
309 \p Less functor has the interface like \p std::less.
310 \p Less must imply the same element order as the comparator used for building the map.
312 template <typename K, typename Less>
313 bool erase_with( K const& key, Less pred )
316 return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >());
319 /// Delete \p key from the map
320 /** \anchor cds_nonintrusive_EllenBinTreeMap_erase_func
322 The function searches an item with key \p key, calls \p f functor
323 and deletes the item. If \p key is not found, the functor is not called.
325 The functor \p Func interface:
328 void operator()(value_type& item) { ... }
332 Return \p true if key is found and deleted, \p false otherwise
334 template <typename K, typename Func>
335 bool erase( K const& key, Func f )
337 return base_class::erase( key, [&f]( leaf_node& node) { f( node.m_Value ); } );
340 /// Deletes the item from the map using \p pred predicate for searching
342 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_erase_func "erase(K const&, Func)"
343 but \p pred is used for key comparing.
344 \p Less functor has the interface like \p std::less.
345 \p Less must imply the same element order as the comparator used for building the map.
347 template <typename K, typename Less, typename Func>
348 bool erase_with( K const& key, Less pred, Func f )
351 return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >(),
352 [&f]( leaf_node& node) { f( node.m_Value ); } );
355 /// Extracts an item with minimal key from the map
357 If the map is not empty, the function returns an guarded pointer to minimum value.
358 If the map is empty, the function returns an empty \p guarded_ptr.
360 @note Due the concurrent nature of the map, the function extracts <i>nearly</i> minimum key.
361 It means that the function gets leftmost leaf of the tree and tries to unlink it.
362 During unlinking, a concurrent thread may insert an item with key less than leftmost item's key.
363 So, the function returns the item with minimum key at the moment of tree traversing.
365 The guarded pointer prevents deallocation of returned item,
366 see \p cds::gc::guarded_ptr for explanation.
367 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
369 guarded_ptr extract_min()
372 base_class::extract_min_( gp.guard() );
376 /// Extracts an item with maximal key from the map
378 If the map is not empty, the function returns a guarded pointer to maximal value.
379 If the map is empty, the function returns an empty \p guarded_ptr.
381 @note Due the concurrent nature of the map, the function extracts <i>nearly</i> maximal key.
382 It means that the function gets rightmost leaf of the tree and tries to unlink it.
383 During unlinking, a concurrent thread may insert an item with key great than leftmost item's key.
384 So, the function returns the item with maximum key at the moment of tree traversing.
386 The guarded pointer prevents deallocation of returned item,
387 see \p cds::gc::guarded_ptr for explanation.
388 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
390 guarded_ptr extract_max()
393 base_class::extract_max_( gp.guard() );
397 /// Extracts an item from the tree
398 /** \anchor cds_nonintrusive_EllenBinTreeMap_extract
399 The function searches an item with key equal to \p key in the tree,
400 unlinks it, and returns a guarded pointer to an item found.
401 If the item is not found the function returns an empty \p guarded_ptr.
403 The guarded pointer prevents deallocation of returned item,
404 see \p cds::gc::guarded_ptr for explanation.
405 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
407 template <typename Q>
408 guarded_ptr extract( Q const& key )
411 base_class::extract_( gp.guard(), key );
415 /// Extracts an item from the map using \p pred for searching
417 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_extract "extract(Q const&)"
418 but \p pred is used for key compare.
419 \p Less has the interface like \p std::less.
420 \p pred must imply the same element order as the comparator used for building the map.
422 template <typename Q, typename Less>
423 guarded_ptr extract_with( Q const& key, Less pred )
427 base_class::extract_with_( gp.guard(), key,
428 cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >());
432 /// Find the key \p key
433 /** \anchor cds_nonintrusive_EllenBinTreeMap_find_cfunc
435 The function searches the item with key equal to \p key and calls the functor \p f for item found.
436 The interface of \p Func functor is:
439 void operator()( value_type& item );
442 where \p item is the item found.
444 The functor may change \p item.second.
446 The function returns \p true if \p key is found, \p false otherwise.
448 template <typename K, typename Func>
449 bool find( K const& key, Func f )
451 return base_class::find( key, [&f](leaf_node& item, K const& ) { f( item.m_Value );});
454 /// Finds the key \p val using \p pred predicate for searching
456 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_find_cfunc "find(K const&, Func)"
457 but \p pred is used for key comparing.
458 \p Less functor has the interface like \p std::less.
459 \p Less must imply the same element order as the comparator used for building the map.
461 template <typename K, typename Less, typename Func>
462 bool find_with( K const& key, Less pred, Func f )
465 return base_class::find_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >(),
466 [&f](leaf_node& item, K const& ) { f( item.m_Value );});
469 /// Checks whether the map contains \p key
471 The function searches the item with key equal to \p key
472 and returns \p true if it is found, and \p false otherwise.
474 template <typename K>
475 bool contains( K const& key )
477 return base_class::contains( key );
480 template <typename K>
481 CDS_DEPRECATED("deprecated, use contains()")
482 bool find( K const& key )
484 return contains( key );
488 /// Checks whether the map contains \p key using \p pred predicate for searching
490 The function is similar to <tt>contains( key )</tt> but \p pred is used for key comparing.
491 \p Less functor has the interface like \p std::less.
492 \p Less must imply the same element order as the comparator used for building the set.
494 template <typename K, typename Less>
495 bool contains( K const& key, Less pred )
498 return base_class::contains( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >() );
501 template <typename K, typename Less>
502 CDS_DEPRECATED("deprecated, use contains()")
503 bool find_with( K const& key, Less pred )
505 return contains( key, pred );
509 /// Finds \p key and returns the item found
510 /** @anchor cds_nonintrusive_EllenBinTreeMap_get
511 The function searches the item with key equal to \p key and returns the item found as a guarded pointer.
512 If \p key is not foudn the function returns an empty \p guarded_ptr.
514 The guarded pointer prevents deallocation of returned item,
515 see \p cds::gc::guarded_ptr for explanation.
516 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
518 template <typename Q>
519 guarded_ptr get( Q const& key )
522 base_class::get_( gp.guard(), key );
526 /// Finds \p key with predicate \p pred and returns the item found
528 The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_get "get(Q const&)"
529 but \p pred is used for key comparing.
530 \p Less functor has the interface like \p std::less.
531 \p pred must imply the same element order as the comparator used for building the map.
533 template <typename Q, typename Less>
534 guarded_ptr get_with( Q const& key, Less pred )
538 base_class::get_with_( gp.guard(), key,
539 cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >() );
543 /// Clears the map (not atomic)
549 /// Checks if the map is empty
551 Emptiness is checked by item counting: if item count is zero then the map is empty.
555 return base_class::empty();
558 /// Returns item count in the set
560 Only leaf nodes containing user data are counted.
562 The value returned depends on item counter type provided by \p Traits template parameter.
563 If it is \p atomicity::empty_item_counter this function always returns 0.
565 The function is not suitable for checking the tree emptiness, use \p empty()
566 member function for this purpose.
570 return base_class::size();
573 /// Returns const reference to internal statistics
574 stat const& statistics() const
576 return base_class::statistics();
579 /// Checks internal consistency (not atomic, not thread-safe)
581 The debugging function to check internal consistency of the tree.
583 bool check_consistency() const
585 return base_class::check_consistency();
589 }} // namespace cds::container
591 #endif //#ifndef CDSLIB_CONTAINER_IMPL_ELLEN_BINTREE_MAP_H