949f5c09ab8efeb95f01448b1e4848a797448dc7
[libcds.git] / cds / container / ellen_bintree_set_rcu.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_CONTAINER_ELLEN_BINTREE_SET_RCU_H
4 #define __CDS_CONTAINER_ELLEN_BINTREE_SET_RCU_H
5
6 #include <cds/container/ellen_bintree_base.h>
7 #include <cds/intrusive/ellen_bintree_rcu.h>
8
9 namespace cds { namespace container {
10
11     /// Set based on Ellen's et al binary search tree (RCU specialization)
12     /** @ingroup cds_nonintrusive_set
13         @ingroup cds_nonintrusive_tree
14         @anchor cds_container_EllenBinTreeSet_rcu
15
16         Source:
17             - [2010] F.Ellen, P.Fatourou, E.Ruppert, F.van Breugel "Non-blocking Binary Search Tree"
18
19         %EllenBinTreeSet is an unbalanced leaf-oriented binary search tree that implements the <i>set</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 \p T currently in
22         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 set. \p Key type is a subset of \p T type.
25         There should be exactly defined a key extracting functor for converting object of type \p T to
26         object of type \p Key.
27
28         Due to \p extract_min and \p extract_max member functions the \p %EllenBinTreeSet 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, a subset of \p T
42         - \p T - 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_set_traits metafunction
46         instead of \p Traits template argument.
47         Template argument list \p Options of ellen_bintree::make_set_traits metafunction are:
48         - ellen_bintree::key_extractor - key extracting functor, mandatory option. The functor has the following prototype:
49             \code
50                 struct key_extractor {
51                     void operator ()( Key& dest, T const& src );
52                 };
53             \endcode
54             It should initialize \p dest key from \p src data. The functor is used to initialize internal nodes.
55         - opt::compare - key compare functor. No default functor is provided.
56             If the option is not specified, \p %opt::less is used.
57         - opt::less - specifies binary predicate used for key compare. At least \p %opt::compare or \p %opt::less should be defined.
58         - opt::item_counter - the type of item counting feature. Default is \ref atomicity::empty_item_counter that is no item counting.
59         - opt::memory_model - C++ memory ordering model. Can be opt::v::relaxed_ordering (relaxed memory model, the default)
60             or opt::v::sequential_consistent (sequentially consisnent memory model).
61         - opt::allocator - the allocator used for \ref ellen_bintree::node "leaf nodes" which contains data.
62             Default is \ref CDS_DEFAULT_ALLOCATOR.
63         - opt::node_allocator - the allocator used for internal nodes. Default is \ref CDS_DEFAULT_ALLOCATOR.
64         - ellen_bintree::update_desc_allocator - an allocator of \ref ellen_bintree::update_desc "update descriptors",
65             default is \ref CDS_DEFAULT_ALLOCATOR.
66             Note that update descriptor is helping data structure with short lifetime and it is good candidate for pooling.
67             The number of simultaneously existing descriptors is a relatively small number limited the number of threads
68             working with the tree and RCU buffer size.
69             Therefore, a bounded lock-free container like \p cds::container::VyukovMPMCCycleQueue is good choice for the free-list
70             of update descriptors, see cds::memory::vyukov_queue_pool free-list implementation.
71             Also notice that size of update descriptor is not dependent on the type of data
72             stored in the tree so single free-list object can be used for several EllenBinTree-based object.
73         - opt::stat - internal statistics. Available types: ellen_bintree::stat, ellen_bintree::empty_stat (the default)
74         - opt::rcu_check_deadlock - a deadlock checking policy. Default is opt::v::rcu_throw_deadlock
75
76         @note Before including <tt><cds/container/ellen_bintree_set_rcu.h></tt> you should include appropriate RCU header file,
77         see \ref cds_urcu_gc "RCU type" for list of existing RCU class and corresponding header files.
78
79         @anchor cds_container_EllenBinTreeSet_rcu_less
80         <b>Predicate requirements</b>
81
82         opt::less, opt::compare and other predicates using with member fuctions should accept at least parameters
83         of type \p T and \p Key in any combination.
84         For example, for \p Foo struct with \p std::string key field the appropiate \p less functor is:
85         \code
86         struct Foo
87         {
88             std::string m_strKey;
89             ...
90         };
91
92         struct less {
93             bool operator()( Foo const& v1, Foo const& v2 ) const
94             { return v1.m_strKey < v2.m_strKey ; }
95
96             bool operator()( Foo const& v, std::string const& s ) const
97             { return v.m_strKey < s ; }
98
99             bool operator()( std::string const& s, Foo const& v ) const
100             { return s < v.m_strKey ; }
101
102             // Support comparing std::string and char const *
103             bool operator()( std::string const& s, char const * p ) const
104             { return s.compare(p) < 0 ; }
105
106             bool operator()( Foo const& v, char const * p ) const
107             { return v.m_strKey.compare(p) < 0 ; }
108
109             bool operator()( char const * p, std::string const& s ) const
110             { return s.compare(p) > 0; }
111
112             bool operator()( char const * p, Foo const& v ) const
113             { return v.m_strKey.compare(p) > 0; }
114         };
115         \endcode
116
117     */
118     template <
119         class RCU,
120         typename Key,
121         typename T,
122 #ifdef CDS_DOXYGEN_INVOKED
123         class Traits = ellen_bintree::type_traits
124 #else
125         class Traits
126 #endif
127     >
128     class EllenBinTreeSet< cds::urcu::gc<RCU>, Key, T, Traits >
129 #ifdef CDS_DOXYGEN_INVOKED
130         : public cds::intrusive::EllenBinTree< cds::urcu::gc<RCU>, Key, T, Traits >
131 #else
132         : public ellen_bintree::details::make_ellen_bintree_set< cds::urcu::gc<RCU>, Key, T, Traits >::type
133 #endif
134     {
135         //@cond
136         typedef ellen_bintree::details::make_ellen_bintree_set< cds::urcu::gc<RCU>, Key, T, Traits > maker;
137         typedef typename maker::type base_class;
138         //@endcond
139
140     public:
141         typedef cds::urcu::gc<RCU>  gc  ;   ///< RCU Garbage collector
142         typedef Key     key_type        ;   ///< type of a key stored in internal nodes; key is a part of \p value_type
143         typedef T       value_type      ;   ///< type of value stored in the binary tree
144         typedef Traits  options         ;   ///< Traits template parameter
145
146 #   ifdef CDS_DOXYGEN_INVOKED
147         typedef implementation_defined key_comparator  ;    ///< key compare functor based on opt::compare and opt::less option setter.
148 #   else
149         typedef typename maker::intrusive_type_traits::compare   key_comparator;
150 #   endif
151         typedef typename base_class::item_counter           item_counter        ; ///< Item counting policy used
152         typedef typename base_class::memory_model           memory_model        ; ///< Memory ordering. See cds::opt::memory_model option
153         typedef typename base_class::stat                   stat                ; ///< internal statistics type
154         typedef typename base_class::rcu_check_deadlock     rcu_check_deadlock  ; ///< Deadlock checking policy
155         typedef typename options::key_extractor             key_extractor       ; ///< key extracting functor
156
157         typedef typename options::allocator                 allocator_type      ;   ///< Allocator for leaf nodes
158         typedef typename base_class::node_allocator         node_allocator      ;   ///< Internal node allocator
159         typedef typename base_class::update_desc_allocator  update_desc_allocator ; ///< Update descriptor allocator
160
161         static CDS_CONSTEXPR_CONST bool c_bExtractLockExternal = base_class::c_bExtractLockExternal; ///< Group of \p extract_xxx functions do not require external locking
162
163     protected:
164         //@cond
165         typedef typename maker::cxx_leaf_node_allocator cxx_leaf_node_allocator;
166         typedef typename base_class::value_type         leaf_node;
167         typedef typename base_class::internal_node      internal_node;
168         typedef std::unique_ptr< leaf_node, typename maker::intrusive_type_traits::disposer >    scoped_node_ptr;
169         //@endcond
170
171     public:
172         typedef typename gc::scoped_lock    rcu_lock ;  ///< RCU scoped lock
173
174         /// pointer to extracted node
175         typedef cds::urcu::exempt_ptr< gc, leaf_node, value_type, typename maker::intrusive_type_traits::disposer,
176             cds::urcu::details::conventional_exempt_member_cast<leaf_node, value_type>
177         > exempt_ptr;
178
179     public:
180         /// Default constructor
181         EllenBinTreeSet()
182             : base_class()
183         {}
184
185         /// Clears the set
186         ~EllenBinTreeSet()
187         {}
188
189         /// Inserts new node
190         /**
191             The function creates a node with copy of \p val value
192             and then inserts the node created into the set.
193
194             The type \p Q should contain at least the complete key for the node.
195             The object of \ref value_type should be constructible from a value of type \p Q.
196             In trivial case, \p Q is equal to \ref value_type.
197
198             RCU \p synchronize method can be called. RCU should not be locked.
199
200             Returns \p true if \p val is inserted into the set, \p false otherwise.
201         */
202         template <typename Q>
203         bool insert( Q const& val )
204         {
205             scoped_node_ptr sp( cxx_leaf_node_allocator().New( val ));
206             if ( base_class::insert( *sp.get() )) {
207                 sp.release();
208                 return true;
209             }
210             return false;
211         }
212
213         /// Inserts new node
214         /**
215             The function allows to split creating of new item into two part:
216             - create item with key only
217             - insert new item into the set
218             - if inserting is success, calls  \p f functor to initialize value-fields of \p val.
219
220             The functor signature is:
221             \code
222                 void func( value_type& val );
223             \endcode
224             where \p val is the item inserted. User-defined functor \p f should guarantee that during changing
225             \p val no any other changes could be made on this set's item by concurrent threads.
226             The user-defined functor is called only if the inserting is success. It may be passed by reference
227             using <tt>boost::ref</tt>
228
229             RCU \p synchronize method can be called. RCU should not be locked.
230         */
231         template <typename Q, typename Func>
232         bool insert( Q const& val, Func f )
233         {
234             scoped_node_ptr sp( cxx_leaf_node_allocator().New( val ));
235             if ( base_class::insert( *sp.get(), [&f]( leaf_node& val ) { cds::unref(f)( val.m_Value ); } )) {
236                 sp.release();
237                 return true;
238             }
239             return false;
240         }
241
242         /// Ensures that the item exists in the set
243         /**
244             The operation performs inserting or changing data with lock-free manner.
245
246             If the \p val key not found in the set, then the new item created from \p val
247             is inserted into the set. Otherwise, the functor \p func is called with the item found.
248             The functor \p Func should be a function with signature:
249             \code
250                 void func( bool bNew, value_type& item, const Q& val );
251             \endcode
252             or a functor:
253             \code
254                 struct my_functor {
255                     void operator()( bool bNew, value_type& item, const Q& val );
256                 };
257             \endcode
258
259             with arguments:
260             - \p bNew - \p true if the item has been inserted, \p false otherwise
261             - \p item - item of the set
262             - \p val - argument \p key passed into the \p ensure function
263
264             The functor may change non-key fields of the \p item; however, \p func must guarantee
265             that during changing no any other modifications could be made on this item by concurrent threads.
266
267             You may pass \p func argument by reference using <tt>boost::ref</tt>.
268
269             RCU \p synchronize method can be called. RCU should not be locked.
270
271             Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
272             \p second is true if new item has been added or \p false if the item with \p key
273             already is in the set.
274         */
275         template <typename Q, typename Func>
276         std::pair<bool, bool> ensure( const Q& val, Func func )
277         {
278             scoped_node_ptr sp( cxx_leaf_node_allocator().New( val ));
279             std::pair<bool, bool> bRes = base_class::ensure( *sp,
280                 [&func, &val](bool bNew, leaf_node& node, leaf_node&){ cds::unref(func)( bNew, node.m_Value, val ); });
281             if ( bRes.first && bRes.second )
282                 sp.release();
283             return bRes;
284         }
285
286         /// Inserts data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
287         /**
288             Returns \p true if inserting successful, \p false otherwise.
289
290             RCU \p synchronize method can be called. RCU should not be locked.
291         */
292         template <typename... Args>
293         bool emplace( Args&&... args )
294         {
295             scoped_node_ptr sp( cxx_leaf_node_allocator().New( std::forward<Args>(args)... ));
296             if ( base_class::insert( *sp.get() )) {
297                 sp.release();
298                 return true;
299             }
300             return false;
301         }
302
303         /// Delete \p key from the set
304         /** \anchor cds_nonintrusive_EllenBinTreeSet_rcu_erase_val
305
306             The item comparator should be able to compare the type \p value_type
307             and the type \p Q.
308
309             RCU \p synchronize method can be called. RCU should not be locked.
310
311             Return \p true if key is found and deleted, \p false otherwise
312         */
313         template <typename Q>
314         bool erase( Q const& key )
315         {
316             return base_class::erase( key );
317         }
318
319         /// Deletes the item from the set using \p pred predicate for searching
320         /**
321             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_erase_val "erase(Q const&)"
322             but \p pred is used for key comparing.
323             \p Less functor has the interface like \p std::less.
324             \p Less must imply the same element order as the comparator used for building the set.
325         */
326         template <typename Q, typename Less>
327         bool erase_with( Q const& key, Less pred )
328         {
329             return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >());
330         }
331
332         /// Delete \p key from the set
333         /** \anchor cds_nonintrusive_EllenBinTreeSet_rcu_erase_func
334
335             The function searches an item with key \p key, calls \p f functor
336             and deletes the item. If \p key is not found, the functor is not called.
337
338             The functor \p Func interface:
339             \code
340             struct extractor {
341                 void operator()(value_type const& val);
342             };
343             \endcode
344             The functor may be passed by reference using <tt>boost:ref</tt>
345
346             Since the key of MichaelHashSet's \p value_type is not explicitly specified,
347             template parameter \p Q defines the key type searching in the list.
348             The list item comparator should be able to compare the type \p T of list item
349             and the type \p Q.
350
351             RCU \p synchronize method can be called. RCU should not be locked.
352
353             Return \p true if key is found and deleted, \p false otherwise
354
355             See also: \ref erase
356         */
357         template <typename Q, typename Func>
358         bool erase( Q const& key, Func f )
359         {
360             return base_class::erase( key, [&f]( leaf_node const& node) { cds::unref(f)( node.m_Value ); } );
361         }
362
363         /// Deletes the item from the set using \p pred predicate for searching
364         /**
365             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_erase_func "erase(Q const&, Func)"
366             but \p pred is used for key comparing.
367             \p Less functor has the interface like \p std::less.
368             \p Less must imply the same element order as the comparator used for building the set.
369         */
370         template <typename Q, typename Less, typename Func>
371         bool erase_with( Q const& key, Less pred, Func f )
372         {
373             return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >(),
374                 [&f]( leaf_node const& node) { cds::unref(f)( node.m_Value ); } );
375         }
376
377         /// Extracts an item with minimal key from the set
378         /**
379             If the set is not empty, the function returns \p true, \p result contains a pointer to value.
380             If the set is empty, the function returns \p false, \p result is left unchanged.
381
382             @note Due the concurrent nature of the set, the function extracts <i>nearly</i> minimum key.
383             It means that the function gets leftmost leaf of the tree and tries to unlink it.
384             During unlinking, a concurrent thread may insert an item with key less than leftmost item's key.
385             So, the function returns the item with minimum key at the moment of tree traversing.
386
387             RCU \p synchronize method can be called. RCU should NOT be locked.
388             The function does not free the item.
389             The deallocator will be implicitly invoked when \p result object is destroyed or when
390             <tt>result.release()</tt> is called, see cds::urcu::exempt_ptr for explanation.
391             @note Before reusing \p result object you should call its \p release() method.
392         */
393         bool extract_min( exempt_ptr& result )
394         {
395             return base_class::extract_min_( result );
396         }
397
398         /// Extracts an item with maximal key from the set
399         /**
400             If the set is not empty, the function returns \p true, \p result contains a pointer to extracted item.
401             If the set is empty, the function returns \p false, \p result is left unchanged.
402
403             @note Due the concurrent nature of the set, the function extracts <i>nearly</i> maximal key.
404             It means that the function gets rightmost leaf of the tree and tries to unlink it.
405             During unlinking, a concurrent thread may insert an item with key great than leftmost item's key.
406             So, the function returns the item with maximum key at the moment of tree traversing.
407
408             RCU \p synchronize method can be called. RCU should NOT be locked.
409             The function does not free the item.
410             The deallocator will be implicitly invoked when \p result object is destroyed or when
411             <tt>result.release()</tt> is called, see cds::urcu::exempt_ptr for explanation.
412             @note Before reusing \p result object you should call its \p release() method.
413         */
414         bool extract_max( exempt_ptr& result )
415         {
416             return base_class::extract_max_( result );
417         }
418
419         /// Extracts an item from the set
420         /** \anchor cds_nonintrusive_EllenBinTreeSet_rcu_extract
421             The function searches an item with key equal to \p key in the tree,
422             unlinks it, and returns pointer to an item found in \p result parameter.
423             If \p key is not found the function returns \p false.
424
425             RCU \p synchronize method can be called. RCU should NOT be locked.
426             The function does not destroy the item found.
427             The dealloctor will be implicitly invoked when \p result object is destroyed or when
428             <tt>result.release()</tt> is called, see cds::urcu::exempt_ptr for explanation.
429             @note Before reusing \p result object you should call its \p release() method.
430         */
431         template <typename Q>
432         bool extract( exempt_ptr& result, Q const& key )
433         {
434             return base_class::extract_( result, key, typename base_class::node_compare());
435         }
436
437         /// Extracts an item from the set using \p pred for searching
438         /**
439             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_extract "extract(exempt_ptr&, Q const&)"
440             but \p pred is used for key compare.
441             \p Less has the interface like \p std::less and should meet \ref cds_container_EllenBinTreeSet_rcu_less
442             "predicate requirements".
443             \p pred must imply the same element order as the comparator used for building the set.
444         */
445         template <typename Q, typename Less>
446         bool extract_with( exempt_ptr& result,  Q const& val, Less pred )
447         {
448             return base_class::extract_with_( result, val,
449                 cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >() );
450         }
451
452         /// Find the key \p val
453         /**
454             @anchor cds_nonintrusive_EllenBinTreeSet_rcu_find_func
455
456             The function searches the item with key equal to \p val and calls the functor \p f for item found.
457             The interface of \p Func functor is:
458             \code
459             struct functor {
460                 void operator()( value_type& item, Q& val );
461             };
462             \endcode
463             where \p item is the item found, \p val is the <tt>find</tt> function argument.
464
465             You may pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
466
467             The functor may change non-key fields of \p item. Note that the functor is only guarantee
468             that \p item cannot be disposed during functor is executing.
469             The functor does not serialize simultaneous access to the set's \p item. If such access is
470             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
471
472             The \p val argument is non-const since it can be used as \p f functor destination i.e., the functor
473             can modify both arguments.
474
475             Note the hash functor specified for class \p Traits template parameter
476             should accept a parameter of type \p Q that may be not the same as \p value_type.
477
478             The function applies RCU lock internally.
479
480             The function returns \p true if \p val is found, \p false otherwise.
481         */
482         template <typename Q, typename Func>
483         bool find( Q& val, Func f ) const
484         {
485             return base_class::find( val, [&f]( leaf_node& node, Q& v ) { cds::unref(f)( node.m_Value, v ); });
486         }
487
488         /// Finds the key \p val using \p pred predicate for searching
489         /**
490             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_find_func "find(Q&, Func)"
491             but \p pred is used for key comparing.
492             \p Less functor has the interface like \p std::less.
493             \p Less must imply the same element order as the comparator used for building the set.
494         */
495         template <typename Q, typename Less, typename Func>
496         bool find_with( Q& val, Less pred, Func f ) const
497         {
498             return base_class::find_with( val, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >(),
499                 [&f]( leaf_node& node, Q& v ) { cds::unref(f)( node.m_Value, v ); } );
500         }
501
502         /// Find the key \p val
503         /** @anchor cds_nonintrusive_EllenBinTreeSet_rcu_find_cfunc
504
505             The function searches the item with key equal to \p val and calls the functor \p f for item found.
506             The interface of \p Func functor is:
507             \code
508             struct functor {
509                 void operator()( value_type& item, Q const& val );
510             };
511             \endcode
512             where \p item is the item found, \p val is the <tt>find</tt> function argument.
513
514             You may pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
515
516             The functor may change non-key fields of \p item. Note that the functor is only guarantee
517             that \p item cannot be disposed during functor is executing.
518             The functor does not serialize simultaneous access to the set's \p item. If such access is
519             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
520
521             Note the hash functor specified for class \p Traits template parameter
522             should accept a parameter of type \p Q that may be not the same as \p value_type.
523
524             The function applies RCU lock internally.
525
526             The function returns \p true if \p val is found, \p false otherwise.
527         */
528         template <typename Q, typename Func>
529         bool find( Q const& val, Func f ) const
530         {
531             return base_class::find( val, [&f]( leaf_node& node, Q const& v ) { cds::unref(f)( node.m_Value, v ); });
532         }
533
534         /// Finds the key \p val using \p pred predicate for searching
535         /**
536             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_find_cfunc "find(Q const&, Func)"
537             but \p pred is used for key comparing.
538             \p Less functor has the interface like \p std::less.
539             \p Less must imply the same element order as the comparator used for building the set.
540         */
541         template <typename Q, typename Less, typename Func>
542         bool find_with( Q const& val, Less pred, Func f ) const
543         {
544             return base_class::find_with( val, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >(),
545                 [&f]( leaf_node& node, Q const& v ) { cds::unref(f)( node.m_Value, v ); } );
546         }
547
548         /// Find the key \p val
549         /** @anchor cds_nonintrusive_EllenBinTreeSet_rcu_find_val
550
551             The function searches the item with key equal to \p val
552             and returns \p true if it is found, and \p false otherwise.
553
554             Note the hash functor specified for class \p Traits template parameter
555             should accept a parameter of type \p Q that may be not the same as \ref value_type.
556
557             The function applies RCU lock internally.
558         */
559         template <typename Q>
560         bool find( Q const & val ) const
561         {
562             return base_class::find( val );
563         }
564
565         /// Finds the key \p val using \p pred predicate for searching
566         /**
567             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_find_val "find(Q const&)"
568             but \p pred is used for key comparing.
569             \p Less functor has the interface like \p std::less.
570             \p Less must imply the same element order as the comparator used for building the set.
571         */
572         template <typename Q, typename Less>
573         bool find_with( Q const& val, Less pred ) const
574         {
575             return base_class::find_with( val, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >());
576         }
577
578         /// Finds \p key and return the item found
579         /** \anchor cds_nonintrusive_EllenBinTreeSet_rcu_get
580             The function searches the item with key equal to \p key and returns the pointer to item found.
581             If \p key is not found it returns \p nullptr.
582
583             RCU should be locked before call the function.
584             Returned pointer is valid while RCU is locked.
585         */
586         template <typename Q>
587         value_type * get( Q const& key ) const
588         {
589             leaf_node * pNode = base_class::get( key );
590             return pNode ? &pNode->m_Value : nullptr;
591         }
592
593         /// Finds \p key with \p pred predicate and return the item found
594         /**
595             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_get "get(Q const&)"
596             but \p pred is used for comparing the keys.
597
598             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type
599             and \p Q in any order.
600             \p pred must imply the same element order as the comparator used for building the set.
601         */
602         template <typename Q, typename Less>
603         value_type * get_with( Q const& key, Less pred ) const
604         {
605             leaf_node * pNode = base_class::get_with( key,
606                 cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >());
607             return pNode ? &pNode->m_Value : nullptr;
608         }
609
610         /// Clears the set (non-atomic)
611         /**
612             The function unlink all items from the tree.
613             The function is not atomic, thus, in multi-threaded environment with parallel insertions
614             this sequence
615             \code
616             set.clear();
617             assert( set.empty() );
618             \endcode
619             the assertion could be raised.
620
621             For each leaf the \ref disposer will be called after unlinking.
622
623             RCU \p synchronize method can be called. RCU should not be locked.
624         */
625         void clear()
626         {
627             base_class::clear();
628         }
629
630         /// Checks if the set is empty
631         bool empty() const
632         {
633             return base_class::empty();
634         }
635
636         /// Returns item count in the set
637         /**
638             Only leaf nodes containing user data are counted.
639
640             The value returned depends on item counter type provided by \p Traits template parameter.
641             If it is atomicity::empty_item_counter this function always returns 0.
642             Therefore, the function is not suitable for checking the tree emptiness, use \ref empty
643             member function for this purpose.
644         */
645         size_t size() const
646         {
647             return base_class::size();
648         }
649
650         /// Returns const reference to internal statistics
651         stat const& statistics() const
652         {
653             return base_class::statistics();
654         }
655
656         /// Checks internal consistency (not atomic, not thread-safe)
657         /**
658             The debugging function to check internal consistency of the tree.
659         */
660         bool check_consistency() const
661         {
662             return base_class::check_consistency();
663         }
664
665     };
666
667 }}  // namespace cds::container
668
669 #endif // #ifndef __CDS_CONTAINER_ELLEN_BINTREE_SET_RCU_H