Added copyright and license
[libcds.git] / cds / container / skip_list_set_rcu.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
29 */
30
31 #ifndef CDSLIB_CONTAINER_SKIP_LIST_SET_RCU_H
32 #define CDSLIB_CONTAINER_SKIP_LIST_SET_RCU_H
33
34 #include <cds/intrusive/skip_list_rcu.h>
35 #include <cds/container/details/make_skip_list_set.h>
36
37 namespace cds { namespace container {
38
39     /// Lock-free skip-list set (template specialization for \ref cds_urcu_desc "RCU")
40     /** @ingroup cds_nonintrusive_set
41         \anchor cds_nonintrusive_SkipListSet_rcu
42
43         The implementation of well-known probabilistic data structure called skip-list
44         invented by W.Pugh in his papers:
45             - [1989] W.Pugh Skip Lists: A Probabilistic Alternative to Balanced Trees
46             - [1990] W.Pugh A Skip List Cookbook
47
48         A skip-list is a probabilistic data structure that provides expected logarithmic
49         time search without the need of rebalance. The skip-list is a collection of sorted
50         linked list. Nodes are ordered by key. Each node is linked into a subset of the lists.
51         Each list has a level, ranging from 0 to 32. The bottom-level list contains
52         all the nodes, and each higher-level list is a sublist of the lower-level lists.
53         Each node is created with a random top level (with a random height), and belongs
54         to all lists up to that level. The probability that a node has the height 1 is 1/2.
55         The probability that a node has the height N is 1/2 ** N (more precisely,
56         the distribution depends on an random generator provided, but our generators
57         have this property).
58
59         The lock-free variant of skip-list is implemented according to book
60             - [2008] M.Herlihy, N.Shavit "The Art of Multiprocessor Programming",
61                 chapter 14.4 "A Lock-Free Concurrent Skiplist"
62
63         Template arguments:
64         - \p RCU - one of \ref cds_urcu_gc "RCU type".
65         - \p T - type to be stored in the list.
66         - \p Traits - set traits, default is skip_list::traits for explanation.
67
68         It is possible to declare option-based list with cds::container::skip_list::make_traits metafunction istead of \p Traits template
69         argument.
70         Template argument list \p Options of cds::container::skip_list::make_traits metafunction are:
71         - opt::compare - key comparison functor. No default functor is provided.
72             If the option is not specified, the opt::less is used.
73         - opt::less - specifies binary predicate used for key comparison. Default is \p std::less<T>.
74         - opt::item_counter - the type of item counting feature. Default is \ref atomicity::empty_item_counter that is no item counting.
75         - opt::memory_model - C++ memory ordering model. Can be opt::v::relaxed_ordering (relaxed memory model, the default)
76             or opt::v::sequential_consistent (sequentially consisnent memory model).
77         - skip_list::random_level_generator - random level generator. Can be skip_list::xorshift, skip_list::turbo_pascal or
78             user-provided one. See skip_list::random_level_generator option description for explanation.
79             Default is \p %skip_list::turbo_pascal.
80         - opt::allocator - allocator for skip-list node. Default is \ref CDS_DEFAULT_ALLOCATOR.
81         - opt::back_off - back-off strategy used. If the option is not specified, the cds::backoff::Default is used.
82         - opt::stat - internal statistics. Available types: skip_list::stat, skip_list::empty_stat (the default)
83         - opt::rcu_check_deadlock - a deadlock checking policy. Default is opt::v::rcu_throw_deadlock
84
85         @note Before including <tt><cds/container/skip_list_set_rcu.h></tt> you should include appropriate RCU header file,
86         see \ref cds_urcu_gc "RCU type" for list of existing RCU class and corresponding header files.
87
88         <b>Iterators</b>
89
90         The class supports a forward iterator (\ref iterator and \ref const_iterator).
91         The iteration is ordered.
92
93         You may iterate over skip-list set items only under RCU lock.
94         Only in this case the iterator is thread-safe since
95         while RCU is locked any set's item cannot be reclaimed.
96
97         The requirement of RCU lock during iterating means that deletion of the elements (i.e. \ref erase)
98         is not possible.
99
100         @warning The iterator object cannot be passed between threads
101
102         Example how to use skip-list set iterators:
103         \code
104         // First, you should include the header for RCU type you have chosen
105         #include <cds/urcu/general_buffered.h>
106         #include <cds/container/skip_list_set_rcu.h>
107
108         typedef cds::urcu::gc< cds::urcu::general_buffered<> > rcu_type;
109
110         struct Foo {
111             // ...
112         };
113
114         // Traits for your skip-list.
115         // At least, you should define cds::opt::less or cds::opt::compare for Foo struct
116         struct my_traits: public cds::continer::skip_list::traits
117         {
118             // ...
119         };
120         typedef cds::container::SkipListSet< rcu_type, Foo, my_traits > my_skiplist_set;
121
122         my_skiplist_set theSet;
123
124         // ...
125
126         // Begin iteration
127         {
128             // Apply RCU locking manually
129             typename rcu_type::scoped_lock sl;
130
131             for ( auto it = theList.begin(); it != theList.end(); ++it ) {
132                 // ...
133             }
134
135             // rcu_type::scoped_lock destructor releases RCU lock implicitly
136         }
137         \endcode
138
139         \warning Due to concurrent nature of skip-list set it is not guarantee that you can iterate
140         all elements in the set: any concurrent deletion can exclude the element
141         pointed by the iterator from the set, and your iteration can be terminated
142         before end of the set. Therefore, such iteration is more suitable for debugging purposes
143
144         The iterator class supports the following minimalistic interface:
145         \code
146         struct iterator {
147             // Default ctor
148             iterator();
149
150             // Copy ctor
151             iterator( iterator const& s);
152
153             value_type * operator ->() const;
154             value_type& operator *() const;
155
156             // Pre-increment
157             iterator& operator ++();
158
159             // Copy assignment
160             iterator& operator = (const iterator& src);
161
162             bool operator ==(iterator const& i ) const;
163             bool operator !=(iterator const& i ) const;
164         };
165         \endcode
166         Note, the iterator object returned by \ref end, \p cend member functions points to \p nullptr and should not be dereferenced.
167     */
168     template <
169         typename RCU,
170         typename T,
171 #ifdef CDS_DOXYGEN_INVOKED
172         typename Traits = skip_list::traits
173 #else
174         typename Traits
175 #endif
176     >
177     class SkipListSet< cds::urcu::gc< RCU >, T, Traits >:
178 #ifdef CDS_DOXYGEN_INVOKED
179         protected intrusive::SkipListSet< cds::urcu::gc< RCU >, T, Traits >
180 #else
181         protected details::make_skip_list_set< cds::urcu::gc< RCU >, T, Traits >::type
182 #endif
183     {
184         //@cond
185         typedef details::make_skip_list_set< cds::urcu::gc< RCU >, T, Traits >    maker;
186         typedef typename maker::type base_class;
187         //@endcond
188     public:
189         typedef typename base_class::gc gc  ; ///< Garbage collector used
190         typedef T       value_type  ;   ///< Value type stored in the set
191         typedef Traits  traits     ;   ///< Options specified
192
193         typedef typename base_class::back_off       back_off        ;   ///< Back-off strategy used
194         typedef typename traits::allocator         allocator_type  ;   ///< Allocator type used for allocate/deallocate the skip-list nodes
195         typedef typename base_class::item_counter   item_counter    ;   ///< Item counting policy used
196         typedef typename maker::key_comparator      key_comparator  ;   ///< key compare functor
197         typedef typename base_class::memory_model   memory_model    ;   ///< Memory ordering. See cds::opt::memory_model option
198         typedef typename traits::random_level_generator random_level_generator ; ///< random level generator
199         typedef typename traits::stat              stat            ;   ///< internal statistics type
200         typedef typename traits::rcu_check_deadlock    rcu_check_deadlock ; ///< Deadlock checking policy
201
202     protected:
203         //@cond
204         typedef typename maker::node_type           node_type;
205         typedef typename maker::node_allocator      node_allocator;
206
207         typedef std::unique_ptr< node_type, typename maker::node_deallocator >    scoped_node_ptr;
208         //@endcond
209
210     public:
211         typedef typename base_class::rcu_lock  rcu_lock;   ///< RCU scoped lock
212         /// Group of \p extract_xxx functions do not require external locking
213         static CDS_CONSTEXPR const bool c_bExtractLockExternal = base_class::c_bExtractLockExternal;
214
215         /// pointer to extracted node
216         using exempt_ptr = cds::urcu::exempt_ptr< gc, node_type, value_type, typename maker::intrusive_traits::disposer >;
217
218     private:
219         //@cond
220         struct raw_ptr_converter
221         {
222             value_type * operator()( node_type * p ) const
223             {
224                return p ? &p->m_Value : nullptr;
225             }
226
227             value_type& operator()( node_type& n ) const
228             {
229                 return n.m_Value;
230             }
231
232             value_type const& operator()( node_type const& n ) const
233             {
234                 return n.m_Value;
235             }
236         };
237         //@endcond
238
239     public:
240         /// Result of \p get(), \p get_with() functions - pointer to the node found
241         typedef cds::urcu::raw_ptr_adaptor< value_type, typename base_class::raw_ptr, raw_ptr_converter > raw_ptr;
242
243     protected:
244         //@cond
245         unsigned int random_level()
246         {
247             return base_class::random_level();
248         }
249         //@endcond
250
251     public:
252         /// Default ctor
253         SkipListSet()
254             : base_class()
255         {}
256
257         /// Destructor destroys the set object
258         ~SkipListSet()
259         {}
260
261     public:
262         /// Iterator type
263         typedef skip_list::details::iterator< typename base_class::iterator >  iterator;
264
265         /// Const iterator type
266         typedef skip_list::details::iterator< typename base_class::const_iterator >   const_iterator;
267
268         /// Returns a forward iterator addressing the first element in a set
269         iterator begin()
270         {
271             return iterator( base_class::begin() );
272         }
273
274         /// Returns a forward const iterator addressing the first element in a set
275         //@{
276         const_iterator begin() const
277         {
278             return const_iterator( base_class::begin() );
279         }
280         const_iterator cbegin() const
281         {
282             return const_iterator( base_class::cbegin() );
283         }
284         //@}
285
286         /// Returns a forward iterator that addresses the location succeeding the last element in a set.
287         iterator end()
288         {
289             return iterator( base_class::end() );
290         }
291
292         /// Returns a forward const iterator that addresses the location succeeding the last element in a set.
293         //@{
294         const_iterator end() const
295         {
296             return const_iterator( base_class::end() );
297         }
298         const_iterator cend() const
299         {
300             return const_iterator( base_class::cend() );
301         }
302         //@}
303
304     public:
305         /// Inserts new node
306         /**
307             The function creates a node with copy of \p val value
308             and then inserts the node created into the set.
309
310             The type \p Q should contain as minimum the complete key for the node.
311             The object of \ref value_type should be constructible from a value of type \p Q.
312             In trivial case, \p Q is equal to \ref value_type.
313
314             RCU \p synchronize method can be called. RCU should not be locked.
315
316             Returns \p true if \p val is inserted into the set, \p false otherwise.
317         */
318         template <typename Q>
319         bool insert( Q const& val )
320         {
321             scoped_node_ptr sp( node_allocator().New( random_level(), val ));
322             if ( base_class::insert( *sp.get() )) {
323                 sp.release();
324                 return true;
325             }
326             return false;
327         }
328
329         /// Inserts new node
330         /**
331             The function allows to split creating of new item into two part:
332             - create item with key only
333             - insert new item into the set
334             - if inserting is success, calls  \p f functor to initialize value-fields of \p val.
335
336             The functor signature is:
337             \code
338                 void func( value_type& val );
339             \endcode
340             where \p val is the item inserted. User-defined functor \p f should guarantee that during changing
341             \p val no any other changes could be made on this set's item by concurrent threads.
342             The user-defined functor is called only if the inserting is success.
343
344             RCU \p synchronize method can be called. RCU should not be locked.
345         */
346         template <typename Q, typename Func>
347         bool insert( Q const& val, Func f )
348         {
349             scoped_node_ptr sp( node_allocator().New( random_level(), val ));
350             if ( base_class::insert( *sp.get(), [&f]( node_type& val ) { f( val.m_Value ); } )) {
351                 sp.release();
352                 return true;
353             }
354             return false;
355         }
356
357         /// Updates the item
358         /**
359             The operation performs inserting or changing data with lock-free manner.
360
361             If \p val not found in the set, then the new item created from \p val
362             is inserted into the set iff \p bInsert is \p true.
363             Otherwise, the functor \p func is called with the item found.
364             The functor \p Func signature:
365             \code
366                 struct my_functor {
367                     void operator()( bool bNew, value_type& item, const Q& val );
368                 };
369             \endcode
370             where:
371             - \p bNew - \p true if the item has been inserted, \p false otherwise
372             - \p item - an item of the set
373             - \p val - argument \p val passed into the \p %update() function
374
375             The functor may change non-key fields of the \p item; however, \p func must guarantee
376             that during changing no any other modifications could be made on this item by concurrent threads.
377
378             RCU \p synchronize method can be called. RCU should not be locked.
379
380             Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
381             \p second is true if new item has been added or \p false if the item with \p key
382             already exists.
383         */
384         template <typename Q, typename Func>
385         std::pair<bool, bool> update( const Q& val, Func func, bool bInsert = true )
386         {
387             scoped_node_ptr sp( node_allocator().New( random_level(), val ));
388             std::pair<bool, bool> bRes = base_class::update( *sp,
389                 [&func, &val](bool bNew, node_type& node, node_type&){ func( bNew, node.m_Value, val );}, bInsert );
390             if ( bRes.first && bRes.second )
391                 sp.release();
392             return bRes;
393         }
394         //@cond
395         template <typename Q, typename Func>
396         CDS_DEPRECATED("ensure() is deprecated, use update()")
397         std::pair<bool, bool> ensure( const Q& val, Func func )
398         {
399             return update( val, func, true );
400         }
401         //@endcond
402
403         /// Inserts data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
404         /**
405             Returns \p true if inserting successful, \p false otherwise.
406
407             RCU \p synchronize method can be called. RCU should not be locked.
408         */
409         template <typename... Args>
410         bool emplace( Args&&... args )
411         {
412             scoped_node_ptr sp( node_allocator().New( random_level(), std::forward<Args>(args)... ));
413             if ( base_class::insert( *sp.get() )) {
414                 sp.release();
415                 return true;
416             }
417             return false;
418         }
419
420         /// Delete \p key from the set
421         /** \anchor cds_nonintrusive_SkipListSet_rcu_erase_val
422
423             The item comparator should be able to compare the type \p value_type
424             and the type \p Q.
425
426             RCU \p synchronize method can be called. RCU should not be locked.
427
428             Return \p true if key is found and deleted, \p false otherwise
429         */
430         template <typename Q>
431         bool erase( Q const& key )
432         {
433             return base_class::erase( key );
434         }
435
436         /// Deletes the item from the set using \p pred predicate for searching
437         /**
438             The function is an analog of \ref cds_nonintrusive_SkipListSet_rcu_erase_val "erase(Q const&)"
439             but \p pred is used for key comparing.
440             \p Less functor has the interface like \p std::less.
441             \p Less must imply the same element order as the comparator used for building the set.
442         */
443         template <typename Q, typename Less>
444         bool erase_with( Q const& key, Less pred )
445         {
446             CDS_UNUSED( pred );
447             return base_class::erase_with( key, cds::details::predicate_wrapper< node_type, Less, typename maker::value_accessor >());
448         }
449
450         /// Delete \p key from the set
451         /** \anchor cds_nonintrusive_SkipListSet_rcu_erase_func
452
453             The function searches an item with key \p key, calls \p f functor
454             and deletes the item. If \p key is not found, the functor is not called.
455
456             The functor \p Func interface:
457             \code
458             struct extractor {
459                 void operator()(value_type const& val);
460             };
461             \endcode
462
463             Since the key of MichaelHashSet's \p value_type is not explicitly specified,
464             template parameter \p Q defines the key type searching in the list.
465             The list item comparator should be able to compare the type \p T of list item
466             and the type \p Q.
467
468             RCU \p synchronize method can be called. RCU should not be locked.
469
470             Return \p true if key is found and deleted, \p false otherwise
471
472             See also: \ref erase
473         */
474         template <typename Q, typename Func>
475         bool erase( Q const& key, Func f )
476         {
477             return base_class::erase( key, [&f]( node_type const& node) { f( node.m_Value ); } );
478         }
479
480         /// Deletes the item from the set using \p pred predicate for searching
481         /**
482             The function is an analog of \ref cds_nonintrusive_SkipListSet_rcu_erase_func "erase(Q const&, Func)"
483             but \p pred is used for key comparing.
484             \p Less functor has the interface like \p std::less.
485             \p Less must imply the same element order as the comparator used for building the set.
486         */
487         template <typename Q, typename Less, typename Func>
488         bool erase_with( Q const& key, Less pred, Func f )
489         {
490             CDS_UNUSED( pred );
491             return base_class::erase_with( key, cds::details::predicate_wrapper< node_type, Less, typename maker::value_accessor >(),
492                 [&f]( node_type const& node) { f( node.m_Value ); } );
493         }
494
495         /// Extracts the item from the set with specified \p key
496         /** \anchor cds_nonintrusive_SkipListSet_rcu_extract
497             The function searches an item with key equal to \p key in the set,
498             unlinks it from the set, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
499             If the item is not found the function returns an empty \p exempt_ptr
500
501             Note the compare functor from \p Traits class' template argument
502             should accept a parameter of type \p Q that can be not the same as \p value_type.
503
504             RCU \p synchronize method can be called. RCU should NOT be locked.
505
506             The function does not free the item found.
507             The item will be implicitly freed when the returned object is destroyed or when
508             its \p release() member function is called.
509         */
510         template <typename Q>
511         exempt_ptr extract( Q const& key )
512         {
513             return exempt_ptr( base_class::do_extract( key ));
514         }
515
516         /// Extracts the item from the set with comparing functor \p pred
517         /**
518             The function is an analog of \p extract(Q const&) but \p pred predicate is used for key comparing.
519             \p Less has the semantics like \p std::less.
520             \p pred must imply the same element order as the comparator used for building the set.
521         */
522         template <typename Q, typename Less>
523         exempt_ptr extract_with( Q const& key, Less pred )
524         {
525             CDS_UNUSED( pred );
526             return exempt_ptr( base_class::do_extract_with( key, cds::details::predicate_wrapper< node_type, Less, typename maker::value_accessor >()));
527         }
528
529         /// Extracts an item with minimal key from the set
530         /**
531             The function searches an item with minimal key, unlinks it,
532             and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item.
533             If the skip-list is empty the function returns an empty \p exempt_ptr.
534
535             RCU \p synchronize method can be called. RCU should NOT be locked.
536
537             The function does not free the item found.
538             The item will be implicitly freed when the returned object is destroyed or when
539             its \p release() member function is called.
540         */
541         exempt_ptr extract_min()
542         {
543             return exempt_ptr( base_class::do_extract_min());
544         }
545
546         /// Extracts an item with maximal key from the set
547         /**
548             The function searches an item with maximal key, unlinks it from the set,
549             and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item.
550             If the skip-list is empty the function returns an empty \p exempt_ptr.
551
552             RCU \p synchronize method can be called. RCU should NOT be locked.
553
554             The function does not free the item found.
555             The item will be implicitly freed when the returned object is destroyed or when
556             its \p release() member function is called.
557         */
558         exempt_ptr extract_max()
559         {
560             return exempt_ptr( base_class::do_extract_max());
561         }
562
563         /// Find the key \p val
564         /**
565             @anchor cds_nonintrusive_SkipListSet_rcu_find_func
566
567             The function searches the item with key equal to \p val and calls the functor \p f for item found.
568             The interface of \p Func functor is:
569             \code
570             struct functor {
571                 void operator()( value_type& item, Q& val );
572             };
573             \endcode
574             where \p item is the item found, \p val is the <tt>find</tt> function argument.
575
576             The functor may change non-key fields of \p item. Note that the functor is only guarantee
577             that \p item cannot be disposed during functor is executing.
578             The functor does not serialize simultaneous access to the set's \p item. If such access is
579             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
580
581             The \p val argument is non-const since it can be used as \p f functor destination i.e., the functor
582             can modify both arguments.
583
584             Note the hash functor specified for class \p Traits template parameter
585             should accept a parameter of type \p Q that may be not the same as \p value_type.
586
587             The function applies RCU lock internally.
588
589             The function returns \p true if \p val is found, \p false otherwise.
590         */
591         template <typename Q, typename Func>
592         bool find( Q& val, Func f )
593         {
594             return base_class::find( val, [&f]( node_type& node, Q& v ) { f( node.m_Value, v ); });
595         }
596         //@cond
597         template <typename Q, typename Func>
598         bool find( Q const& val, Func f )
599         {
600             return base_class::find( val, [&f]( node_type& node, Q& v ) { f( node.m_Value, v ); } );
601         }
602         //@endcond
603
604         /// Finds the key \p val using \p pred predicate for searching
605         /**
606             The function is an analog of \ref cds_nonintrusive_SkipListSet_rcu_find_func "find(Q&, Func)"
607             but \p pred is used for key comparing.
608             \p Less functor has the interface like \p std::less.
609             \p Less must imply the same element order as the comparator used for building the set.
610         */
611         template <typename Q, typename Less, typename Func>
612         bool find_with( Q& val, Less pred, Func f )
613         {
614             CDS_UNUSED( pred );
615             return base_class::find_with( val, cds::details::predicate_wrapper< node_type, Less, typename maker::value_accessor >(),
616                 [&f]( node_type& node, Q& v ) { f( node.m_Value, v ); } );
617         }
618         //@cond
619         template <typename Q, typename Less, typename Func>
620         bool find_with( Q const& val, Less pred, Func f )
621         {
622             CDS_UNUSED( pred );
623             return base_class::find_with( val, cds::details::predicate_wrapper< node_type, Less, typename maker::value_accessor >(),
624                                           [&f]( node_type& node, Q& v ) { f( node.m_Value, v ); } );
625         }
626         //@endcond
627
628         /// Checks whether the set contains \p key
629         /**
630             The function searches the item with key equal to \p key
631             and returns \p true if it is found, and \p false otherwise.
632
633             The function applies RCU lock internally.
634         */
635         template <typename Q>
636         bool contains( Q const & key )
637         {
638             return base_class::contains( key );
639         }
640         //@cond
641         template <typename Q>
642         CDS_DEPRECATED("deprecated, use contains()")
643         bool find( Q const & key )
644         {
645             return contains( key );
646         }
647         //@endcond
648
649         /// Checks whether the set contains \p key using \p pred predicate for searching
650         /**
651             The function is similar to <tt>contains( key )</tt> but \p pred is used for key comparing.
652             \p Less functor has the interface like \p std::less.
653             \p Less must imply the same element order as the comparator used for building the set.
654         */
655         template <typename Q, typename Less>
656         bool contains( Q const& key, Less pred )
657         {
658             CDS_UNUSED( pred );
659             return base_class::contains( key, cds::details::predicate_wrapper< node_type, Less, typename maker::value_accessor >());
660         }
661         //@cond
662         template <typename Q, typename Less>
663         CDS_DEPRECATED("deprecated, use contains()")
664         bool find_with( Q const& key, Less pred )
665         {
666             return contains( key, pred );
667         }
668         //@endcond
669
670         /// Finds \p key and return the item found
671         /** \anchor cds_nonintrusive_SkipListSet_rcu_get
672             The function searches the item with key equal to \p key and returns a \p raw_ptr object pointed to item found.
673             If \p key is not found it returns empty \p raw_ptr.
674
675             Note the compare functor in \p Traits class' template argument
676             should accept a parameter of type \p Q that can be not the same as \p value_type.
677
678             RCU should be locked before call of this function.
679             Returned item is valid only while RCU is locked:
680             \code
681             typedef cds::container::SkipListSet< cds::urcu::gc< cds::urcu::general_buffered<> >, foo, my_traits > skip_list;
682             skip_list theList;
683             // ...
684             typename skip_list::raw_ptr pVal;
685             {
686                 // Lock RCU
687                 skip_list::rcu_lock lock;
688
689                 pVal = theList.get( 5 );
690                 if ( pVal ) {
691                     // Deal with pVal
692                     //...
693                 }
694             }
695             // You can manually release pVal after RCU-locked section
696             pVal.release();
697             \endcode
698         */
699         template <typename Q>
700         raw_ptr get( Q const& key )
701         {
702             return raw_ptr( base_class::get( key ));
703         }
704
705         /// Finds the key \p val and return the item found
706         /**
707             The function is an analog of \ref cds_nonintrusive_SkipListSet_rcu_get "get(Q const&)"
708             but \p pred is used for comparing the keys.
709
710             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
711             in any order.
712             \p pred must imply the same element order as the comparator used for building the set.
713         */
714         template <typename Q, typename Less>
715         raw_ptr get_with( Q const& val, Less pred )
716         {
717             CDS_UNUSED( pred );
718             return raw_ptr( base_class::get_with( val, cds::details::predicate_wrapper< node_type, Less, typename maker::value_accessor >() ));
719         }
720
721         /// Clears the set (non-atomic).
722         /**
723             The function deletes all items from the set.
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 item the \ref disposer provided by \p Traits template parameter will be called.
733         */
734         void clear()
735         {
736             base_class::clear();
737         }
738
739         /// Checks if the set is empty
740         bool empty() const
741         {
742             return base_class::empty();
743         }
744
745         /// Returns item count in the set
746         /**
747             The value returned depends on item counter type provided by \p Traits template parameter.
748             If it is atomicity::empty_item_counter this function always returns 0.
749             Therefore, the function is not suitable for checking the set emptiness, use \ref empty
750             member function for this purpose.
751         */
752         size_t size() const
753         {
754             return base_class::size();
755         }
756
757         /// Returns const reference to internal statistics
758         stat const& statistics() const
759         {
760             return base_class::statistics();
761         }
762     };
763
764 }} // namespace cds::container
765
766 #endif // #ifndef CDSLIB_CONTAINER_SKIP_LIST_SET_RCU_H