00b0b646dc313293e0666616acfe1eb5c33b9211
[libcds.git] / cds / container / ellen_bintree_map_rcu.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_CONTAINER_ELLEN_BINTREE_MAP_RCU_H
4 #define __CDS_CONTAINER_ELLEN_BINTREE_MAP_RCU_H
5
6 #include <cds/container/details/ellen_bintree_base.h>
7 #include <cds/intrusive/ellen_bintree_rcu.h>
8
9 namespace cds { namespace container {
10
11     /// Map based on Ellen's et al binary search tree (RCU specialization)
12     /** @ingroup cds_nonintrusive_map
13         @ingroup cds_nonintrusive_tree
14         @anchor cds_container_EllenBinTreeMap_rcu
15
16         Source:
17             - [2010] F.Ellen, P.Fatourou, E.Ruppert, F.van Breugel "Non-blocking Binary Search Tree"
18
19         %EllenBinTreeMap is an unbalanced leaf-oriented binary search tree that implements the <i>map</i>
20         abstract data type. Nodes maintains child pointers but not parent pointers.
21         Every internal node has exactly two children, and all data of type <tt>std::pair<Key const, T></tt>
22         currently in the tree are stored in the leaves. Internal nodes of the tree are used to direct \p find
23         operation along the path to the correct leaf. The keys (of \p Key type) stored in internal nodes
24         may or may not be in the map.
25         Unlike \ref cds_container_EllenBinTreeSet_rcu "EllenBinTreeSet" keys are not a part of \p T type.
26         The map can be represented as a set containing <tt>std::pair< Key const, T> </tt> values.
27
28         Due to \p extract_min and \p extract_max member functions the \p %EllenBinTreeMap can act as
29         a <i>priority queue</i>. In this case you should provide unique compound key, for example,
30         the priority value plus some uniformly distributed random value.
31
32         @warning Recall the tree is <b>unbalanced</b>. The complexity of operations is <tt>O(log N)</tt>
33         for uniformly distributed random keys, but in worst case the complexity is <tt>O(N)</tt>.
34
35         @note In the current implementation we do not use helping technique described in original paper.
36         So, the current implementation is near to fine-grained lock-based tree.
37         Helping will be implemented in future release
38
39         <b>Template arguments</b> :
40         - \p RCU - one of \ref cds_urcu_gc "RCU type"
41         - \p Key - key type
42         - \p T - value type to be stored in tree's leaf nodes.
43         - \p Traits - type traits. See ellen_bintree::type_traits for explanation.
44
45         It is possible to declare option-based tree with ellen_bintree::make_map_traits metafunction
46         instead of \p Traits template argument.
47         Template argument list \p Options of ellen_bintree::make_map_traits metafunction are:
48         - opt::compare - key compare functor. No default functor is provided.
49             If the option is not specified, \p %opt::less is used.
50         - opt::less - specifies binary predicate used for key compare. At least \p %opt::compare or \p %opt::less should be defined.
51         - opt::item_counter - the type of item counting feature. Default is \ref atomicity::empty_item_counter that is no item counting.
52         - opt::memory_model - C++ memory ordering model. Can be opt::v::relaxed_ordering (relaxed memory model, the default)
53             or opt::v::sequential_consistent (sequentially consisnent memory model).
54         - opt::allocator - the allocator used for \ref ellen_bintree::map_node "leaf nodes" which contains data.
55             Default is \ref CDS_DEFAULT_ALLOCATOR.
56         - opt::node_allocator - the allocator used for \ref ellen_bintree::internal_node "internal nodes".
57             Default is \ref CDS_DEFAULT_ALLOCATOR.
58         - ellen_bintree::update_desc_allocator - an allocator of \ref ellen_bintree::update_desc "update descriptors",
59             default is \ref CDS_DEFAULT_ALLOCATOR.
60             Note that update descriptor is helping data structure with short lifetime and it is good candidate for pooling.
61             The number of simultaneously existing descriptors is a relatively small number limited the number of threads
62             working with the tree and RCU buffer size.
63             Therefore, a bounded lock-free container like \p cds::container::VyukovMPMCCycleQueue is good choice for the free-list
64             of update descriptors, see cds::memory::vyukov_queue_pool free-list implementation.
65             Also notice that size of update descriptor is not dependent on the type of data
66             stored in the tree so single free-list object can be used for several EllenBinTree-based object.
67         - opt::stat - internal statistics. Available types: ellen_bintree::stat, ellen_bintree::empty_stat (the default)
68         - opt::rcu_check_deadlock - a deadlock checking policy. Default is opt::v::rcu_throw_deadlock
69         - opt::copy_policy - key copy policy defines a functor to copy leaf node's key to internal node.
70             By default, assignment operator is used.
71             The copy functor interface is:
72             \code
73             struct copy_functor {
74                 void operator()( Key& dest, Key const& src );
75             };
76             \endcode
77
78         @note Before including <tt><cds/container/ellen_bintree_map_rcu.h></tt> you should include appropriate RCU header file,
79         see \ref cds_urcu_gc "RCU type" for list of existing RCU class and corresponding header files.
80     */
81     template <
82         class RCU,
83         typename Key,
84         typename T,
85 #ifdef CDS_DOXYGEN_INVOKED
86         class Traits = ellen_bintree::type_traits
87 #else
88         class Traits
89 #endif
90     >
91     class EllenBinTreeMap< cds::urcu::gc<RCU>, Key, T, Traits >
92 #ifdef CDS_DOXYGEN_INVOKED
93         : public cds::intrusive::EllenBinTree< cds::urcu::gc<RCU>, Key, T, Traits >
94 #else
95         : public ellen_bintree::details::make_ellen_bintree_map< cds::urcu::gc<RCU>, Key, T, Traits >::type
96 #endif
97     {
98         //@cond
99         typedef ellen_bintree::details::make_ellen_bintree_map< cds::urcu::gc<RCU>, Key, T, Traits > maker;
100         typedef typename maker::type base_class;
101         //@endcond
102     public:
103         typedef cds::urcu::gc<RCU>  gc  ;   ///< RCU Garbage collector
104         typedef Key     key_type        ;   ///< type of a key stored in the map
105         typedef T       mapped_type      ;  ///< type of value stored in the map
106         typedef std::pair< key_type const, mapped_type >    value_type  ;   ///< Key-value pair stored in leaf node of the mp
107         typedef Traits  options         ;   ///< Traits template parameter
108
109 #   ifdef CDS_DOXYGEN_INVOKED
110         typedef implementation_defined key_comparator  ;    ///< key compare functor based on opt::compare and opt::less option setter.
111 #   else
112         typedef typename maker::intrusive_type_traits::compare   key_comparator;
113 #   endif
114         typedef typename base_class::item_counter           item_counter        ; ///< Item counting policy used
115         typedef typename base_class::memory_model           memory_model        ; ///< Memory ordering. See cds::opt::memory_model option
116         typedef typename base_class::node_allocator         node_allocator_type ; ///< allocator for maintaining internal node
117         typedef typename base_class::stat                   stat                ; ///< internal statistics type
118         typedef typename base_class::rcu_check_deadlock     rcu_check_deadlock  ; ///< Deadlock checking policy
119         typedef typename options::copy_policy               copy_policy         ; ///< key copy policy
120
121         typedef typename options::allocator                 allocator_type      ;   ///< Allocator for leaf nodes
122         typedef typename base_class::node_allocator         node_allocator      ;   ///< Internal node allocator
123         typedef typename base_class::update_desc_allocator  update_desc_allocator ; ///< Update descriptor allocator
124
125         static CDS_CONSTEXPR const bool c_bExtractLockExternal = base_class::c_bExtractLockExternal; ///< Group of \p extract_xxx functions do not require external locking
126
127     protected:
128         //@cond
129         typedef typename base_class::value_type         leaf_node;
130         typedef typename base_class::internal_node      internal_node;
131         typedef typename base_class::update_desc        update_desc;
132
133         typedef typename maker::cxx_leaf_node_allocator cxx_leaf_node_allocator;
134
135         typedef std::unique_ptr< leaf_node, typename maker::leaf_deallocator >    scoped_node_ptr;
136         //@endcond
137
138     public:
139         typedef typename gc::scoped_lock    rcu_lock ;  ///< RCU scoped lock
140
141         /// pointer to extracted node
142         typedef cds::urcu::exempt_ptr< gc, leaf_node, value_type, typename maker::intrusive_type_traits::disposer,
143             cds::urcu::details::conventional_exempt_member_cast<leaf_node, value_type>
144         > exempt_ptr;
145
146     public:
147         /// Default constructor
148         EllenBinTreeMap()
149             : base_class()
150         {}
151
152         /// Clears the map
153         ~EllenBinTreeMap()
154         {}
155
156         /// Inserts new node with key and default value
157         /**
158             The function creates a node with \p key and default value, and then inserts the node created into the map.
159
160             Preconditions:
161             - The \ref key_type should be constructible from a value of type \p K.
162                 In trivial case, \p K is equal to \ref key_type.
163             - The \ref mapped_type should be default-constructible.
164
165             RCU \p synchronize method can be called. RCU should not be locked.
166
167             Returns \p true if inserting successful, \p false otherwise.
168         */
169         template <typename K>
170         bool insert( K const& key )
171         {
172             return insert_key( key, [](value_type&){} );
173         }
174
175         /// Inserts new node
176         /**
177             The function creates a node with copy of \p val value
178             and then inserts the node created into the map.
179
180             Preconditions:
181             - The \ref key_type should be constructible from \p key of type \p K.
182             - The \ref value_type should be constructible from \p val of type \p V.
183
184             RCU \p synchronize method can be called. RCU should not be locked.
185
186             Returns \p true if \p val is inserted into the map, \p false otherwise.
187         */
188         template <typename K, typename V>
189         bool insert( K const& key, V const& val )
190         {
191             scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key, val ));
192             if ( base_class::insert( *pNode ))
193             {
194                 pNode.release();
195                 return true;
196             }
197             return false;
198         }
199
200         /// Inserts new node and initialize it by a functor
201         /**
202             This function inserts new node with key \p key and if inserting is successful then it calls
203             \p func functor with signature
204             \code
205                 struct functor {
206                     void operator()( value_type& item );
207                 };
208             \endcode
209
210             The argument \p item of user-defined functor \p func is the reference
211             to the map's item inserted:
212                 - <tt>item.first</tt> is a const reference to item's key that cannot be changed.
213                 - <tt>item.second</tt> is a reference to item's value that may be changed.
214
215             The user-defined functor can be passed by reference using \p std::ref
216             and it is called only if inserting is successful.
217
218             The key_type should be constructible from value of type \p K.
219
220             The function allows to split creating of new item into two part:
221             - create item from \p key;
222             - insert new item into the map;
223             - if inserting is successful, initialize the value of item by calling \p func functor
224
225             This can be useful if complete initialization of object of \p value_type is heavyweight and
226             it is preferable that the initialization should be completed only if inserting is successful.
227
228             RCU \p synchronize method can be called. RCU should not be locked.
229         */
230         template <typename K, typename Func>
231         bool insert_key( const K& key, Func func )
232         {
233             scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key ));
234             if ( base_class::insert( *pNode, [&func]( leaf_node& item ) { func( item.m_Value ); } )) {
235                 pNode.release();
236                 return true;
237             }
238             return false;
239         }
240
241         /// For key \p key inserts data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
242         /**
243             Returns \p true if inserting successful, \p false otherwise.
244
245             RCU \p synchronize method can be called. RCU should not be locked.
246         */
247         template <typename K, typename... Args>
248         bool emplace( K&& key, Args&&... args )
249         {
250             scoped_node_ptr pNode( cxx_leaf_node_allocator().New( std::forward<K>(key), std::forward<Args>(args)... ));
251             if ( base_class::insert( *pNode )) {
252                 pNode.release();
253                 return true;
254             }
255             return false;
256         }
257
258         /// Ensures that the \p key exists in the map
259         /**
260             The operation performs inserting or changing data with lock-free manner.
261
262             If the \p key not found in the map, then the new item created from \p key
263             is inserted into the map (note that in this case the \ref key_type should be
264             constructible from type \p K).
265             Otherwise, the functor \p func is called with item found.
266             The functor \p Func may be a function with signature:
267             \code
268                 void func( bool bNew, value_type& item );
269             \endcode
270             or a functor:
271             \code
272                 struct my_functor {
273                     void operator()( bool bNew, value_type& item );
274                 };
275             \endcode
276
277             with arguments:
278             - \p bNew - \p true if the item has been inserted, \p false otherwise
279             - \p item - item of the list
280
281             The functor may change any fields of the \p item.second that is \ref value_type.
282
283             You may pass \p func argument by reference using \p std::ref
284
285             RCU \p synchronize method can be called. RCU should not be locked.
286
287             Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
288             \p second is true if new item has been added or \p false if the item with \p key
289             already is in the list.
290         */
291         template <typename K, typename Func>
292         std::pair<bool, bool> ensure( K const& key, Func func )
293         {
294             scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key ));
295             std::pair<bool, bool> res = base_class::ensure( *pNode,
296                 [&func](bool bNew, leaf_node& item, leaf_node const& ){ func( bNew, item.m_Value ); }
297             );
298             if ( res.first && res.second )
299                 pNode.release();
300             return res;
301         }
302
303         /// Delete \p key from the map
304         /**\anchor cds_nonintrusive_EllenBinTreeMap_rcu_erase_val
305
306             RCU \p synchronize method can be called. RCU should not be locked.
307
308             Return \p true if \p key is found and deleted, \p false otherwise
309         */
310         template <typename K>
311         bool erase( K const& key )
312         {
313             return base_class::erase(key);
314         }
315
316         /// Deletes the item from the map using \p pred predicate for searching
317         /**
318             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_rcu_erase_val "erase(K const&)"
319             but \p pred is used for key comparing.
320             \p Less functor has the interface like \p std::less.
321             \p Less must imply the same element order as the comparator used for building the map.
322         */
323         template <typename K, typename Less>
324         bool erase_with( K const& key, Less pred )
325         {
326             return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >());
327         }
328
329         /// Delete \p key from the map
330         /** \anchor cds_nonintrusive_EllenBinTreeMap_rcu_erase_func
331
332             The function searches an item with key \p key, calls \p f functor
333             and deletes the item. If \p key is not found, the functor is not called.
334
335             The functor \p Func interface:
336             \code
337             struct extractor {
338                 void operator()(value_type& item) { ... }
339             };
340             \endcode
341             The functor may be passed by reference using <tt>boost:ref</tt>
342
343             RCU \p synchronize method can be called. RCU should not be locked.
344
345             Return \p true if key is found and deleted, \p false otherwise
346         */
347         template <typename K, typename Func>
348         bool erase( K const& key, Func f )
349         {
350             return base_class::erase( key, [&f]( leaf_node& node) { f( node.m_Value ); } );
351         }
352
353         /// Deletes the item from the map using \p pred predicate for searching
354         /**
355             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_rcu_erase_func "erase(K const&, Func)"
356             but \p pred is used for key comparing.
357             \p Less functor has the interface like \p std::less.
358             \p Less must imply the same element order as the comparator used for building the map.
359         */
360         template <typename K, typename Less, typename Func>
361         bool erase_with( K const& key, Less pred, Func f )
362         {
363             return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >(),
364                 [&f]( leaf_node& node) { f( node.m_Value ); } );
365         }
366
367         /// Extracts an item with minimal key from the map
368         /**
369             If the map is not empty, the function returns \p true, \p result contains a pointer to value.
370             If the map is empty, the function returns \p false, \p result is left unchanged.
371
372             @note Due the concurrent nature of the map, the function extracts <i>nearly</i> minimum key.
373             It means that the function gets leftmost leaf of the tree and tries to unlink it.
374             During unlinking, a concurrent thread may insert an item with key less than leftmost item's key.
375             So, the function returns the item with minimum key at the moment of tree traversing.
376
377             RCU \p synchronize method can be called. RCU should NOT be locked.
378             The function does not free the item.
379             The deallocator will be implicitly invoked when \p result object is destroyed or when
380             <tt>result.release()</tt> is called, see cds::urcu::exempt_ptr for explanation.
381             @note Before reusing \p result object you should call its \p release() method.
382         */
383         bool extract_min( exempt_ptr& result )
384         {
385             return base_class::extract_min_( result );
386         }
387
388         /// Extracts an item with maximal key from the map
389         /**
390             If the map is not empty, the function returns \p true, \p result contains a pointer to extracted item.
391             If the map is empty, the function returns \p false, \p result is left unchanged.
392
393             @note Due the concurrent nature of the map, the function extracts <i>nearly</i> maximal key.
394             It means that the function gets rightmost leaf of the tree and tries to unlink it.
395             During unlinking, a concurrent thread may insert an item with key great than leftmost item's key.
396             So, the function returns the item with maximum key at the moment of tree traversing.
397
398             RCU \p synchronize method can be called. RCU should NOT be locked.
399             The function does not free the item.
400             The deallocator will be implicitly invoked when \p result object is destroyed or when
401             <tt>result.release()</tt> is called, see cds::urcu::exempt_ptr for explanation.
402             @note Before reusing \p result object you should call its \p release() method.
403         */
404         bool extract_max( exempt_ptr& result )
405         {
406             return base_class::extract_max_( result );
407         }
408
409         /// Extracts an item from the map
410         /** \anchor cds_nonintrusive_EllenBinTreeMap_rcu_extract
411             The function searches an item with key equal to \p key in the tree,
412             unlinks it, and returns pointer to an item found in \p result parameter.
413             If \p key is not found the function returns \p false.
414
415             RCU \p synchronize method can be called. RCU should NOT be locked.
416             The function does not destroy the item found.
417             The dealloctor will be implicitly invoked when \p result object is destroyed or when
418             <tt>result.release()</tt> is called, see cds::urcu::exempt_ptr for explanation.
419             @note Before reusing \p result object you should call its \p release() method.
420         */
421         template <typename Q>
422         bool extract( exempt_ptr& result, Q const& key )
423         {
424             return base_class::extract_( result, key, typename base_class::node_compare());
425         }
426
427         /// Extracts an item from the map using \p pred for searching
428         /**
429             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_rcu_extract "extract(exempt_ptr&, Q const&)"
430             but \p pred is used for key compare.
431             \p Less has the interface like \p std::less and should meet \ref cds_container_EllenBinTreeSet_rcu_less
432             "predicate requirements".
433             \p pred must imply the same element order as the comparator used for building the map.
434         */
435         template <typename Q, typename Less>
436         bool extract_with( exempt_ptr& result,  Q const& val, Less pred )
437         {
438             return base_class::extract_with_( result, val,
439                 cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >() );
440         }
441
442         /// Find the key \p key
443         /** \anchor cds_nonintrusive_EllenBinTreeMap_rcu_find_cfunc
444
445             The function searches the item with key equal to \p key and calls the functor \p f for item found.
446             The interface of \p Func functor is:
447             \code
448             struct functor {
449                 void operator()( value_type& item );
450             };
451             \endcode
452             where \p item is the item found.
453
454             You can pass \p f argument by reference using \p std::ref
455
456             The functor may change \p item.second.
457
458             The function applies RCU lock internally.
459
460             The function returns \p true if \p key is found, \p false otherwise.
461         */
462         template <typename K, typename Func>
463         bool find( K const& key, Func f )
464         {
465             return base_class::find( key, [&f](leaf_node& item, K const& ) { f( item.m_Value );});
466         }
467
468         /// Finds the key \p val using \p pred predicate for searching
469         /**
470             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_rcu_find_cfunc "find(K const&, Func)"
471             but \p pred is used for key comparing.
472             \p Less functor has the interface like \p std::less.
473             \p Less must imply the same element order as the comparator used for building the map.
474         */
475         template <typename K, typename Less, typename Func>
476         bool find_with( K const& key, Less pred, Func f )
477         {
478             return base_class::find_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >(),
479                 [&f](leaf_node& item, K const& ) { f( item.m_Value );});
480         }
481
482         /// Find the key \p key
483         /** \anchor cds_nonintrusive_EllenBinTreeMap_rcu_find_val
484
485             The function searches the item with key equal to \p key
486             and returns \p true if it is found, and \p false otherwise.
487
488             The function applies RCU lock internally.
489         */
490         template <typename K>
491         bool find( K const& key )
492         {
493             return base_class::find( key );
494         }
495
496         /// Finds the key \p val using \p pred predicate for searching
497         /**
498             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_rcu_find_val "find(K const&)"
499             but \p pred is used for key comparing.
500             \p Less functor has the interface like \p std::less.
501             \p Less must imply the same element order as the comparator used for building the map.
502         */
503         template <typename K, typename Less>
504         bool find_with( K const& key, Less pred )
505         {
506             return base_class::find_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >() );
507         }
508
509         /// Finds \p key and return the item found
510         /** \anchor cds_nonintrusive_EllenBinTreeMap_rcu_get
511             The function searches the item with key equal to \p key and returns the pointer to item found.
512             If \p key is not found it returns \p nullptr.
513
514             RCU should be locked before call the function.
515             Returned pointer is valid while RCU is locked.
516         */
517         template <typename Q>
518         value_type * get( Q const& key ) const
519         {
520             leaf_node * pNode = base_class::get( key );
521             return pNode ? &pNode->m_Value : nullptr;
522         }
523
524         /// Finds \p key with \p pred predicate and return the item found
525         /**
526             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_rcu_get "get(Q const&)"
527             but \p pred is used for comparing the keys.
528
529             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type
530             and \p Q in any order.
531             \p pred must imply the same element order as the comparator used for building the map.
532         */
533         template <typename Q, typename Less>
534         value_type * get_with( Q const& key, Less pred ) const
535         {
536             leaf_node * pNode = base_class::get_with( key,
537                 cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >());
538             return pNode ? &pNode->m_Value : nullptr;
539         }
540
541         /// Clears the map
542         void clear()
543         {
544             base_class::clear();
545         }
546
547         /// Checks if the map is empty
548         /**
549             Emptiness is checked by item counting: if item count is zero then the map is empty.
550         */
551         bool empty() const
552         {
553             return base_class::empty();
554         }
555
556         /// Returns item count in the map
557         size_t size() const
558         {
559             return base_class::size();
560         }
561
562         /// Returns const reference to internal statistics
563         stat const& statistics() const
564         {
565             return base_class::statistics();
566         }
567
568         /// Checks internal consistency (not atomic, not thread-safe)
569         /**
570             The debugging function to check internal consistency of the tree.
571         */
572         bool check_consistency() const
573         {
574             return base_class::check_consistency();
575         }
576
577     };
578 }} // namespace cds::container
579
580 #endif //#ifndef __CDS_CONTAINER_ELLEN_BINTREE_MAP_RCU_H