579da7a99e32a872648778f6527580ae330aba35
[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     protected:
180         //@cond
181 #   ifndef CDS_CXX11_LAMBDA_SUPPORT
182         template <typename Func>
183         struct insert_functor
184         {
185             Func        m_func;
186
187             insert_functor ( Func f )
188                 : m_func(f)
189             {}
190
191             void operator()( leaf_node& node )
192             {
193                 cds::unref(m_func)( node.m_Value );
194             }
195         };
196
197         template <typename Q, typename Func>
198         struct ensure_functor
199         {
200             Func        m_func;
201             Q const&    m_arg;
202
203             ensure_functor( Q const& arg, Func f )
204                 : m_func(f)
205                 , m_arg( arg )
206             {}
207
208             void operator ()( bool bNew, leaf_node& node, leaf_node& )
209             {
210                 cds::unref(m_func)( bNew, node.m_Value, m_arg );
211             }
212         };
213
214         template <typename Func>
215         struct erase_functor
216         {
217             Func        m_func;
218
219             erase_functor( Func f )
220                 : m_func(f)
221             {}
222
223             void operator()( leaf_node const& node )
224             {
225                 cds::unref(m_func)( node.m_Value );
226             }
227         };
228
229         template <typename Func>
230         struct find_functor
231         {
232             Func    m_func;
233
234             find_functor( Func f )
235                 : m_func(f)
236             {}
237
238             template <typename Q>
239             void operator ()( leaf_node& node, Q& val )
240             {
241                 cds::unref(m_func)( node.m_Value, val );
242             }
243         };
244 #endif
245         //@endcond
246
247     public:
248         /// Default constructor
249         EllenBinTreeSet()
250             : base_class()
251         {}
252
253         /// Clears the set
254         ~EllenBinTreeSet()
255         {}
256
257         /// Inserts new node
258         /**
259             The function creates a node with copy of \p val value
260             and then inserts the node created into the set.
261
262             The type \p Q should contain at least the complete key for the node.
263             The object of \ref value_type should be constructible from a value of type \p Q.
264             In trivial case, \p Q is equal to \ref value_type.
265
266             RCU \p synchronize method can be called. RCU should not be locked.
267
268             Returns \p true if \p val is inserted into the set, \p false otherwise.
269         */
270         template <typename Q>
271         bool insert( Q const& val )
272         {
273             scoped_node_ptr sp( cxx_leaf_node_allocator().New( val ));
274             if ( base_class::insert( *sp.get() )) {
275                 sp.release();
276                 return true;
277             }
278             return false;
279         }
280
281         /// Inserts new node
282         /**
283             The function allows to split creating of new item into two part:
284             - create item with key only
285             - insert new item into the set
286             - if inserting is success, calls  \p f functor to initialize value-fields of \p val.
287
288             The functor signature is:
289             \code
290                 void func( value_type& val );
291             \endcode
292             where \p val is the item inserted. User-defined functor \p f should guarantee that during changing
293             \p val no any other changes could be made on this set's item by concurrent threads.
294             The user-defined functor is called only if the inserting is success. It may be passed by reference
295             using <tt>boost::ref</tt>
296
297             RCU \p synchronize method can be called. RCU should not be locked.
298         */
299         template <typename Q, typename Func>
300         bool insert( Q const& val, Func f )
301         {
302             scoped_node_ptr sp( cxx_leaf_node_allocator().New( val ));
303 #       ifdef CDS_CXX11_LAMBDA_SUPPORT
304             if ( base_class::insert( *sp.get(), [&f]( leaf_node& val ) { cds::unref(f)( val.m_Value ); } ))
305 #       else
306             insert_functor<Func> wrapper(f);
307             if ( base_class::insert( *sp, cds::ref(wrapper) ))
308 #       endif
309             {
310                 sp.release();
311                 return true;
312             }
313             return false;
314         }
315
316         /// Ensures that the item exists in the set
317         /**
318             The operation performs inserting or changing data with lock-free manner.
319
320             If the \p val key not found in the set, then the new item created from \p val
321             is inserted into the set. Otherwise, the functor \p func is called with the item found.
322             The functor \p Func should be a function with signature:
323             \code
324                 void func( bool bNew, value_type& item, const Q& val );
325             \endcode
326             or a functor:
327             \code
328                 struct my_functor {
329                     void operator()( bool bNew, value_type& item, const Q& val );
330                 };
331             \endcode
332
333             with arguments:
334             - \p bNew - \p true if the item has been inserted, \p false otherwise
335             - \p item - item of the set
336             - \p val - argument \p key passed into the \p ensure function
337
338             The functor may change non-key fields of the \p item; however, \p func must guarantee
339             that during changing no any other modifications could be made on this item by concurrent threads.
340
341             You may pass \p func argument by reference using <tt>boost::ref</tt>.
342
343             RCU \p synchronize method can be called. RCU should not be locked.
344
345             Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
346             \p second is true if new item has been added or \p false if the item with \p key
347             already is in the set.
348         */
349         template <typename Q, typename Func>
350         std::pair<bool, bool> ensure( const Q& val, Func func )
351         {
352             scoped_node_ptr sp( cxx_leaf_node_allocator().New( val ));
353 #       ifdef CDS_CXX11_LAMBDA_SUPPORT
354             std::pair<bool, bool> bRes = base_class::ensure( *sp,
355                 [&func, &val](bool bNew, leaf_node& node, leaf_node&){ cds::unref(func)( bNew, node.m_Value, val ); });
356 #       else
357             ensure_functor<Q, Func> wrapper( val, func );
358             std::pair<bool, bool> bRes = base_class::ensure( *sp, cds::ref(wrapper));
359 #       endif
360             if ( bRes.first && bRes.second )
361                 sp.release();
362             return bRes;
363         }
364
365         /// Inserts data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
366         /**
367             Returns \p true if inserting successful, \p false otherwise.
368
369             RCU \p synchronize method can be called. RCU should not be locked.
370         */
371         template <typename... Args>
372         bool emplace( Args&&... args )
373         {
374             scoped_node_ptr sp( cxx_leaf_node_allocator().New( std::forward<Args>(args)... ));
375             if ( base_class::insert( *sp.get() )) {
376                 sp.release();
377                 return true;
378             }
379             return false;
380         }
381
382         /// Delete \p key from the set
383         /** \anchor cds_nonintrusive_EllenBinTreeSet_rcu_erase_val
384
385             The item comparator should be able to compare the type \p value_type
386             and the type \p Q.
387
388             RCU \p synchronize method can be called. RCU should not be locked.
389
390             Return \p true if key is found and deleted, \p false otherwise
391         */
392         template <typename Q>
393         bool erase( Q const& key )
394         {
395             return base_class::erase( key );
396         }
397
398         /// Deletes the item from the set using \p pred predicate for searching
399         /**
400             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_erase_val "erase(Q const&)"
401             but \p pred is used for key comparing.
402             \p Less functor has the interface like \p std::less.
403             \p Less must imply the same element order as the comparator used for building the set.
404         */
405         template <typename Q, typename Less>
406         bool erase_with( Q const& key, Less pred )
407         {
408             return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >());
409         }
410
411         /// Delete \p key from the set
412         /** \anchor cds_nonintrusive_EllenBinTreeSet_rcu_erase_func
413
414             The function searches an item with key \p key, calls \p f functor
415             and deletes the item. If \p key is not found, the functor is not called.
416
417             The functor \p Func interface:
418             \code
419             struct extractor {
420                 void operator()(value_type const& val);
421             };
422             \endcode
423             The functor may be passed by reference using <tt>boost:ref</tt>
424
425             Since the key of MichaelHashSet's \p value_type is not explicitly specified,
426             template parameter \p Q defines the key type searching in the list.
427             The list item comparator should be able to compare the type \p T of list item
428             and the type \p Q.
429
430             RCU \p synchronize method can be called. RCU should not be locked.
431
432             Return \p true if key is found and deleted, \p false otherwise
433
434             See also: \ref erase
435         */
436         template <typename Q, typename Func>
437         bool erase( Q const& key, Func f )
438         {
439 #       ifdef CDS_CXX11_LAMBDA_SUPPORT
440             return base_class::erase( key, [&f]( leaf_node const& node) { cds::unref(f)( node.m_Value ); } );
441 #       else
442             erase_functor<Func> wrapper(f);
443             return base_class::erase( key, cds::ref(wrapper));
444 #       endif
445         }
446
447         /// Deletes the item from the set using \p pred predicate for searching
448         /**
449             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_erase_func "erase(Q const&, Func)"
450             but \p pred is used for key comparing.
451             \p Less functor has the interface like \p std::less.
452             \p Less must imply the same element order as the comparator used for building the set.
453         */
454         template <typename Q, typename Less, typename Func>
455         bool erase_with( Q const& key, Less pred, Func f )
456         {
457 #       ifdef CDS_CXX11_LAMBDA_SUPPORT
458             return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >(),
459                 [&f]( leaf_node const& node) { cds::unref(f)( node.m_Value ); } );
460 #       else
461             erase_functor<Func> wrapper(f);
462             return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >(), cds::ref(wrapper));
463 #       endif
464         }
465
466         /// Extracts an item with minimal key from the set
467         /**
468             If the set is not empty, the function returns \p true, \p result contains a pointer to value.
469             If the set is empty, the function returns \p false, \p result is left unchanged.
470
471             @note Due the concurrent nature of the set, the function extracts <i>nearly</i> minimum key.
472             It means that the function gets leftmost leaf of the tree and tries to unlink it.
473             During unlinking, a concurrent thread may insert an item with key less than leftmost item's key.
474             So, the function returns the item with minimum key at the moment of tree traversing.
475
476             RCU \p synchronize method can be called. RCU should NOT be locked.
477             The function does not free the item.
478             The deallocator will be implicitly invoked when \p result object is destroyed or when
479             <tt>result.release()</tt> is called, see cds::urcu::exempt_ptr for explanation.
480             @note Before reusing \p result object you should call its \p release() method.
481         */
482         bool extract_min( exempt_ptr& result )
483         {
484             return base_class::extract_min_( result );
485         }
486
487         /// Extracts an item with maximal key from the set
488         /**
489             If the set is not empty, the function returns \p true, \p result contains a pointer to extracted item.
490             If the set is empty, the function returns \p false, \p result is left unchanged.
491
492             @note Due the concurrent nature of the set, the function extracts <i>nearly</i> maximal key.
493             It means that the function gets rightmost leaf of the tree and tries to unlink it.
494             During unlinking, a concurrent thread may insert an item with key great than leftmost item's key.
495             So, the function returns the item with maximum key at the moment of tree traversing.
496
497             RCU \p synchronize method can be called. RCU should NOT be locked.
498             The function does not free the item.
499             The deallocator will be implicitly invoked when \p result object is destroyed or when
500             <tt>result.release()</tt> is called, see cds::urcu::exempt_ptr for explanation.
501             @note Before reusing \p result object you should call its \p release() method.
502         */
503         bool extract_max( exempt_ptr& result )
504         {
505             return base_class::extract_max_( result );
506         }
507
508         /// Extracts an item from the set
509         /** \anchor cds_nonintrusive_EllenBinTreeSet_rcu_extract
510             The function searches an item with key equal to \p key in the tree,
511             unlinks it, and returns pointer to an item found in \p result parameter.
512             If \p key is not found the function returns \p false.
513
514             RCU \p synchronize method can be called. RCU should NOT be locked.
515             The function does not destroy the item found.
516             The dealloctor will be implicitly invoked when \p result object is destroyed or when
517             <tt>result.release()</tt> is called, see cds::urcu::exempt_ptr for explanation.
518             @note Before reusing \p result object you should call its \p release() method.
519         */
520         template <typename Q>
521         bool extract( exempt_ptr& result, Q const& key )
522         {
523             return base_class::extract_( result, key, typename base_class::node_compare());
524         }
525
526         /// Extracts an item from the set using \p pred for searching
527         /**
528             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_extract "extract(exempt_ptr&, Q const&)"
529             but \p pred is used for key compare.
530             \p Less has the interface like \p std::less and should meet \ref cds_container_EllenBinTreeSet_rcu_less
531             "predicate requirements".
532             \p pred must imply the same element order as the comparator used for building the set.
533         */
534         template <typename Q, typename Less>
535         bool extract_with( exempt_ptr& result,  Q const& val, Less pred )
536         {
537             return base_class::extract_with_( result, val,
538                 cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >() );
539         }
540
541         /// Find the key \p val
542         /**
543             @anchor cds_nonintrusive_EllenBinTreeSet_rcu_find_func
544
545             The function searches the item with key equal to \p val and calls the functor \p f for item found.
546             The interface of \p Func functor is:
547             \code
548             struct functor {
549                 void operator()( value_type& item, Q& val );
550             };
551             \endcode
552             where \p item is the item found, \p val is the <tt>find</tt> function argument.
553
554             You may pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
555
556             The functor may change non-key fields of \p item. Note that the functor is only guarantee
557             that \p item cannot be disposed during functor is executing.
558             The functor does not serialize simultaneous access to the set's \p item. If such access is
559             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
560
561             The \p val argument is non-const since it can be used as \p f functor destination i.e., the functor
562             can modify both arguments.
563
564             Note the hash functor specified for class \p Traits template parameter
565             should accept a parameter of type \p Q that may be not the same as \p value_type.
566
567             The function applies RCU lock internally.
568
569             The function returns \p true if \p val is found, \p false otherwise.
570         */
571         template <typename Q, typename Func>
572         bool find( Q& val, Func f ) const
573         {
574 #       ifdef CDS_CXX11_LAMBDA_SUPPORT
575             return base_class::find( val, [&f]( leaf_node& node, Q& v ) { cds::unref(f)( node.m_Value, v ); });
576 #       else
577             find_functor<Func> wrapper(f);
578             return base_class::find( val, cds::ref(wrapper));
579 #       endif
580         }
581
582         /// Finds the key \p val using \p pred predicate for searching
583         /**
584             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_find_func "find(Q&, Func)"
585             but \p pred is used for key comparing.
586             \p Less functor has the interface like \p std::less.
587             \p Less must imply the same element order as the comparator used for building the set.
588         */
589         template <typename Q, typename Less, typename Func>
590         bool find_with( Q& val, Less pred, Func f ) const
591         {
592 #       ifdef CDS_CXX11_LAMBDA_SUPPORT
593             return base_class::find_with( val, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >(),
594                 [&f]( leaf_node& node, Q& v ) { cds::unref(f)( node.m_Value, v ); } );
595 #       else
596             find_functor<Func> wrapper(f);
597             return base_class::find_with( val, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >(),
598                 cds::ref(wrapper));
599 #       endif
600         }
601
602         /// Find the key \p val
603         /** @anchor cds_nonintrusive_EllenBinTreeSet_rcu_find_cfunc
604
605             The function searches the item with key equal to \p val and calls the functor \p f for item found.
606             The interface of \p Func functor is:
607             \code
608             struct functor {
609                 void operator()( value_type& item, Q const& val );
610             };
611             \endcode
612             where \p item is the item found, \p val is the <tt>find</tt> function argument.
613
614             You may pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
615
616             The functor may change non-key fields of \p item. Note that the functor is only guarantee
617             that \p item cannot be disposed during functor is executing.
618             The functor does not serialize simultaneous access to the set's \p item. If such access is
619             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
620
621             Note the hash functor specified for class \p Traits template parameter
622             should accept a parameter of type \p Q that may be not the same as \p value_type.
623
624             The function applies RCU lock internally.
625
626             The function returns \p true if \p val is found, \p false otherwise.
627         */
628         template <typename Q, typename Func>
629         bool find( Q const& val, Func f ) const
630         {
631 #       ifdef CDS_CXX11_LAMBDA_SUPPORT
632             return base_class::find( val, [&f]( leaf_node& node, Q const& v ) { cds::unref(f)( node.m_Value, v ); });
633 #       else
634             find_functor<Func> wrapper(f);
635             return base_class::find( val, cds::ref(wrapper));
636 #       endif
637         }
638
639         /// Finds the key \p val using \p pred predicate for searching
640         /**
641             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_find_cfunc "find(Q const&, Func)"
642             but \p pred is used for key comparing.
643             \p Less functor has the interface like \p std::less.
644             \p Less must imply the same element order as the comparator used for building the set.
645         */
646         template <typename Q, typename Less, typename Func>
647         bool find_with( Q const& val, Less pred, Func f ) const
648         {
649 #       ifdef CDS_CXX11_LAMBDA_SUPPORT
650             return base_class::find_with( val, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >(),
651                 [&f]( leaf_node& node, Q const& v ) { cds::unref(f)( node.m_Value, v ); } );
652 #       else
653             find_functor<Func> wrapper(f);
654             return base_class::find_with( val, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >(),
655                 cds::ref(wrapper));
656 #       endif
657         }
658
659         /// Find the key \p val
660         /** @anchor cds_nonintrusive_EllenBinTreeSet_rcu_find_val
661
662             The function searches the item with key equal to \p val
663             and returns \p true if it is found, and \p false otherwise.
664
665             Note the hash functor specified for class \p Traits template parameter
666             should accept a parameter of type \p Q that may be not the same as \ref value_type.
667
668             The function applies RCU lock internally.
669         */
670         template <typename Q>
671         bool find( Q const & val ) const
672         {
673             return base_class::find( val );
674         }
675
676         /// Finds the key \p val using \p pred predicate for searching
677         /**
678             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_find_val "find(Q const&)"
679             but \p pred is used for key comparing.
680             \p Less functor has the interface like \p std::less.
681             \p Less must imply the same element order as the comparator used for building the set.
682         */
683         template <typename Q, typename Less>
684         bool find_with( Q const& val, Less pred ) const
685         {
686             return base_class::find_with( val, cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >());
687         }
688
689         /// Finds \p key and return the item found
690         /** \anchor cds_nonintrusive_EllenBinTreeSet_rcu_get
691             The function searches the item with key equal to \p key and returns the pointer to item found.
692             If \p key is not found it returns \p nullptr.
693
694             RCU should be locked before call the function.
695             Returned pointer is valid while RCU is locked.
696         */
697         template <typename Q>
698         value_type * get( Q const& key ) const
699         {
700             leaf_node * pNode = base_class::get( key );
701             return pNode ? &pNode->m_Value : nullptr;
702         }
703
704         /// Finds \p key with \p pred predicate and return the item found
705         /**
706             The function is an analog of \ref cds_nonintrusive_EllenBinTreeSet_rcu_get "get(Q const&)"
707             but \p pred is used for comparing the keys.
708
709             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type
710             and \p Q in any order.
711             \p pred must imply the same element order as the comparator used for building the set.
712         */
713         template <typename Q, typename Less>
714         value_type * get_with( Q const& key, Less pred ) const
715         {
716             leaf_node * pNode = base_class::get_with( key,
717                 cds::details::predicate_wrapper< leaf_node, Less, typename maker::value_accessor >());
718             return pNode ? &pNode->m_Value : nullptr;
719         }
720
721         /// Clears the set (non-atomic)
722         /**
723             The function unlink all items from the tree.
724             The function is not atomic, thus, in multi-threaded environment with parallel insertions
725             this sequence
726             \code
727             set.clear();
728             assert( set.empty() );
729             \endcode
730             the assertion could be raised.
731
732             For each leaf the \ref disposer will be called after unlinking.
733
734             RCU \p synchronize method can be called. RCU should not be locked.
735         */
736         void clear()
737         {
738             base_class::clear();
739         }
740
741         /// Checks if the set is empty
742         bool empty() const
743         {
744             return base_class::empty();
745         }
746
747         /// Returns item count in the set
748         /**
749             Only leaf nodes containing user data are counted.
750
751             The value returned depends on item counter type provided by \p Traits template parameter.
752             If it is atomicity::empty_item_counter this function always returns 0.
753             Therefore, the function is not suitable for checking the tree emptiness, use \ref empty
754             member function for this purpose.
755         */
756         size_t size() const
757         {
758             return base_class::size();
759         }
760
761         /// Returns const reference to internal statistics
762         stat const& statistics() const
763         {
764             return base_class::statistics();
765         }
766
767         /// Checks internal consistency (not atomic, not thread-safe)
768         /**
769             The debugging function to check internal consistency of the tree.
770         */
771         bool check_consistency() const
772         {
773             return base_class::check_consistency();
774         }
775
776     };
777
778 }}  // namespace cds::container
779
780 #endif // #ifndef __CDS_CONTAINER_ELLEN_BINTREE_SET_RCU_H