Docfix
[libcds.git] / cds / container / michael_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_MICHAEL_SET_RCU_H
32 #define CDSLIB_CONTAINER_MICHAEL_SET_RCU_H
33
34 #include <cds/container/details/michael_set_base.h>
35 #include <cds/details/allocator.h>
36
37 namespace cds { namespace container {
38
39     /// Michael's hash set (template specialization for \ref cds_urcu_desc "RCU")
40     /** @ingroup cds_nonintrusive_set
41         \anchor cds_nonintrusive_MichaelHashSet_rcu
42
43         Source:
44             - [2002] Maged Michael "High performance dynamic lock-free hash tables and list-based sets"
45
46         Michael's hash table algorithm is based on lock-free ordered list and it is very simple.
47         The main structure is an array \p T of size \p M. Each element in \p T is basically a pointer
48         to a hash bucket, implemented as a singly linked list. The array of buckets cannot be dynamically expanded.
49         However, each bucket may contain unbounded number of items.
50
51         Template parameters are:
52         - \p RCU - one of \ref cds_urcu_gc "RCU type"
53         - \p OrderedList - ordered list implementation used as the bucket for hash set, for example,
54             \ref cds_nonintrusive_MichaelList_rcu "MichaelList".
55             The ordered list implementation specifies the type \p T stored in the hash-set,
56             the comparison functor for the type \p T and other features specific for
57             the ordered list.
58         - \p Traits - set traits, default is michael_set::traits.
59             Instead of defining \p Traits struct you may use option-based syntax with michael_set::make_traits metafunction.
60
61         About hash functor see \ref cds_nonintrusive_MichaelHashSet_hash_functor "MichaelSet hash functor".
62
63         <b>How to use</b>
64
65         Suppose, we have the following type \p Foo that we want to store in your \p %MichaelHashSet:
66         \code
67         struct Foo {
68             int     nKey    ;   // key field
69             int     nVal    ;   // value field
70         };
71         \endcode
72
73         To use \p %MichaelHashSet for \p Foo values, you should first choose suitable ordered list class
74         that will be used as a bucket for the set. We will cds::urcu::general_buffered<> RCU type and
75         MichaelList as a bucket type.
76         You should include RCU-related header file (<tt>cds/urcu/general_buffered.h</tt> in this example)
77         before including <tt>cds/container/michael_set_rcu.h</tt>.
78         Also, for ordered list we should develop a comparator for our \p Foo struct.
79         \code
80         #include <cds/urcu/general_buffered.h>
81         #include <cds/container/michael_list_rcu.h>
82         #include <cds/container/michael_set_rcu.h>
83
84         namespace cc = cds::container;
85
86         // Foo comparator
87         struct Foo_cmp {
88             int operator ()(Foo const& v1, Foo const& v2 ) const
89             {
90                 if ( std::less( v1.nKey, v2.nKey ))
91                     return -1;
92                 return std::less(v2.nKey, v1.nKey) ? 1 : 0;
93             }
94         };
95
96         // Ordered list
97         typedef cc::MichaelList< cds::urcu::gc< cds::urcu::general_buffered<> >, Foo,
98             typename cc::michael_list::make_traits<
99                 cc::opt::compare< Foo_cmp >     // item comparator option
100             >::type
101         > bucket_list;
102
103         // Hash functor for Foo
104         struct foo_hash {
105             size_t operator ()( int i ) const
106             {
107                 return std::hash( i );
108             }
109             size_t operator()( Foo const& i ) const
110             {
111                 return std::hash( i.nKey );
112             }
113         };
114
115         // Declare the set
116         // Note that \p RCU template parameter of ordered list must be equal \p RCU for the set.
117         typedef cc::MichaelHashSet< cds::urcu::gc< cds::urcu::general_buffered<> >, bucket_list,
118             cc::michael_set::make_traits<
119                 cc::opt::hash< foo_hash >
120             >::type
121         > foo_set;
122
123         foo_set fooSet;
124         \endcode
125     */
126     template <
127         class RCU,
128         class OrderedList,
129 #ifdef CDS_DOXYGEN_INVOKED
130         class Traits = michael_set::traits
131 #else
132         class Traits
133 #endif
134     >
135     class MichaelHashSet< cds::urcu::gc< RCU >, OrderedList, Traits >
136     {
137     public:
138         typedef cds::urcu::gc< RCU > gc; ///< RCU used as garbage collector
139         typedef OrderedList bucket_type; ///< type of ordered list to be used as a bucket implementation
140         typedef Traits      traits;      ///< Set traits
141
142         typedef typename bucket_type::value_type        value_type;     ///< type of value to be stored in the list
143         typedef typename bucket_type::key_comparator    key_comparator; ///< key comparing functor
144
145         /// Hash functor for \ref value_type and all its derivatives that you use
146         typedef typename cds::opt::v::hash_selector< typename traits::hash >::type hash;
147         typedef typename traits::item_counter item_counter;   ///< Item counter type
148
149         /// Bucket table allocator
150         typedef cds::details::Allocator< bucket_type, typename traits::allocator >  bucket_table_allocator;
151
152         typedef typename bucket_type::rcu_lock   rcu_lock;   ///< RCU scoped lock
153         typedef typename bucket_type::exempt_ptr exempt_ptr; ///< pointer to extracted node
154         typedef typename bucket_type::raw_ptr    raw_ptr;    ///< Return type of \p get() member function and its derivatives
155         /// Group of \p extract_xxx functions require external locking if underlying ordered list requires that
156         static CDS_CONSTEXPR const bool c_bExtractLockExternal = bucket_type::c_bExtractLockExternal;
157
158     protected:
159         item_counter    m_ItemCounter; ///< Item counter
160         hash            m_HashFunctor; ///< Hash functor
161         bucket_type *   m_Buckets;     ///< bucket table
162
163     private:
164         //@cond
165         const size_t    m_nHashBitmask;
166         //@endcond
167
168     protected:
169         //@cond
170         /// Calculates hash value of \p key
171         template <typename Q>
172         size_t hash_value( Q const& key ) const
173         {
174             return m_HashFunctor( key ) & m_nHashBitmask;
175         }
176
177         /// Returns the bucket (ordered list) for \p key
178         template <typename Q>
179         bucket_type&    bucket( Q const& key )
180         {
181             return m_Buckets[ hash_value( key ) ];
182         }
183         template <typename Q>
184         bucket_type const&    bucket( Q const& key ) const
185         {
186             return m_Buckets[ hash_value( key ) ];
187         }
188         //@endcond
189     public:
190     ///@name Forward iterators (thread-safe under RCU lock)
191     //@{
192         /// Forward iterator
193         /**
194             The forward iterator for Michael's set is based on \p OrderedList forward iterator and has some features:
195             - it has no post-increment operator
196             - it iterates items in unordered fashion
197
198             You may safely use iterators in multi-threaded environment only under RCU lock.
199             Otherwise, a crash is possible if another thread deletes the element the iterator points to.
200
201             The iterator interface:
202             \code
203             class iterator {
204             public:
205                 // Default constructor
206                 iterator();
207
208                 // Copy construtor
209                 iterator( iterator const& src );
210
211                 // Dereference operator
212                 value_type * operator ->() const;
213
214                 // Dereference operator
215                 value_type& operator *() const;
216
217                 // Preincrement operator
218                 iterator& operator ++();
219
220                 // Assignment operator
221                 iterator& operator = (iterator const& src);
222
223                 // Equality operators
224                 bool operator ==(iterator const& i ) const;
225                 bool operator !=(iterator const& i ) const;
226             };
227             \endcode
228         */
229         typedef michael_set::details::iterator< bucket_type, false >    iterator;
230
231         /// Const forward iterator
232         typedef michael_set::details::iterator< bucket_type, true >     const_iterator;
233
234         /// Returns a forward iterator addressing the first element in a set
235         /**
236             For empty set \code begin() == end() \endcode
237         */
238         iterator begin()
239         {
240             return iterator( m_Buckets[0].begin(), m_Buckets, m_Buckets + bucket_count() );
241         }
242
243         /// Returns an iterator that addresses the location succeeding the last element in a set
244         /**
245             Do not use the value returned by <tt>end</tt> function to access any item.
246             The returned value can be used only to control reaching the end of the set.
247             For empty set \code begin() == end() \endcode
248         */
249         iterator end()
250         {
251             return iterator( m_Buckets[bucket_count() - 1].end(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
252         }
253
254         /// Returns a forward const iterator addressing the first element in a set
255         const_iterator begin() const
256         {
257             return get_const_begin();
258         }
259
260         /// Returns a forward const iterator addressing the first element in a set
261         const_iterator cbegin() const
262         {
263             return get_const_begin();
264         }
265
266         /// Returns an const iterator that addresses the location succeeding the last element in a set
267         const_iterator end() const
268         {
269             return get_const_end();
270         }
271
272         /// Returns an const iterator that addresses the location succeeding the last element in a set
273         const_iterator cend() const
274         {
275             return get_const_end();
276         }
277     //@}
278
279     private:
280         //@cond
281         const_iterator get_const_begin() const
282         {
283             return const_iterator( const_cast<bucket_type const&>(m_Buckets[0]).begin(), m_Buckets, m_Buckets + bucket_count() );
284         }
285         const_iterator get_const_end() const
286         {
287             return const_iterator( const_cast<bucket_type const&>(m_Buckets[bucket_count() - 1]).end(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
288         }
289         //@endcond
290
291     public:
292         /// Initialize hash set
293         /**
294             The Michael's hash set is non-expandable container. You should point the average count of items \p nMaxItemCount
295             when you create an object.
296             \p nLoadFactor parameter defines average count of items per bucket and it should be small number between 1 and 10.
297             Remember, since the bucket implementation is an ordered list, searching in the bucket is linear [<tt>O(nLoadFactor)</tt>].
298
299             The ctor defines hash table size as rounding <tt>nMaxItemCount / nLoadFactor</tt> up to nearest power of two.
300         */
301         MichaelHashSet(
302             size_t nMaxItemCount,   ///< estimation of max item count in the hash set
303             size_t nLoadFactor      ///< load factor: estimation of max number of items in the bucket
304         ) : m_nHashBitmask( michael_set::details::init_hash_bitmask( nMaxItemCount, nLoadFactor ))
305         {
306             // GC and OrderedList::gc must be the same
307             static_assert( std::is_same<gc, typename bucket_type::gc>::value, "GC and OrderedList::gc must be the same");
308
309             // atomicity::empty_item_counter is not allowed as a item counter
310             static_assert( !std::is_same<item_counter, atomicity::empty_item_counter>::value,
311                            "atomicity::empty_item_counter is not allowed as a item counter");
312
313             m_Buckets = bucket_table_allocator().NewArray( bucket_count() );
314         }
315
316         /// Clears hash set and destroys it
317         ~MichaelHashSet()
318         {
319             clear();
320             bucket_table_allocator().Delete( m_Buckets, bucket_count() );
321         }
322
323         /// Inserts new node
324         /**
325             The function creates a node with copy of \p val value
326             and then inserts the node created into the set.
327
328             The type \p Q should contain as minimum the complete key for the node.
329             The object of \ref value_type should be constructible from a value of type \p Q.
330             In trivial case, \p Q is equal to \ref value_type.
331
332             The function applies RCU lock internally.
333
334             Returns \p true if \p val is inserted into the set, \p false otherwise.
335         */
336         template <typename Q>
337         bool insert( Q const& val )
338         {
339             const bool bRet = bucket( val ).insert( val );
340             if ( bRet )
341                 ++m_ItemCounter;
342             return bRet;
343         }
344
345         /// Inserts new node
346         /**
347             The function allows to split creating of new item into two part:
348             - create item with key only
349             - insert new item into the set
350             - if inserting is success, calls  \p f functor to initialize value-fields of \p val.
351
352             The functor signature is:
353             \code
354                 void func( value_type& val );
355             \endcode
356             where \p val is the item inserted.
357             The user-defined functor is called only if the inserting is success.
358
359             The function applies RCU lock internally.
360
361             @warning For \ref cds_nonintrusive_MichaelList_rcu "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
362             \ref cds_nonintrusive_LazyList_rcu "LazyList" provides exclusive access to inserted item and does not require any node-level
363             synchronization.
364             */
365         template <typename Q, typename Func>
366         bool insert( Q const& val, Func f )
367         {
368             const bool bRet = bucket( val ).insert( val, f );
369             if ( bRet )
370                 ++m_ItemCounter;
371             return bRet;
372         }
373
374         /// Ensures that the item exists in the set
375         /**
376             The operation performs inserting or changing data with lock-free manner.
377
378             If the \p val key not found in the set, then the new item created from \p val
379             is inserted into the set. Otherwise, the functor \p func is called with the item found.
380             The functor \p Func signature is:
381             \code
382                 struct my_functor {
383                     void operator()( bool bNew, value_type& item, const Q& val );
384                 };
385             \endcode
386
387             with arguments:
388             - \p bNew - \p true if the item has been inserted, \p false otherwise
389             - \p item - item of the set
390             - \p val - argument \p key passed into the \p ensure function
391
392             The functor may change non-key fields of the \p item.
393
394             The function applies RCU lock internally.
395
396             Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
397             \p second is true if new item has been added or \p false if the item with \p key
398             already is in the set.
399
400             @warning For \ref cds_nonintrusive_MichaelList_rcu "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
401             \ref cds_nonintrusive_LazyList_rcu "LazyList" provides exclusive access to inserted item and does not require any node-level
402             synchronization.
403         */
404         /// Updates the element
405         /**
406             The operation performs inserting or changing data with lock-free manner.
407
408             If the item \p val not found in the set, then \p val is inserted iff \p bAllowInsert is \p true.
409             Otherwise, the functor \p func is called with item found.
410             The functor signature is:
411             \code
412                 struct functor {
413                     void operator()( bool bNew, value_type& item, Q const& val );
414                 };
415             \endcode
416             with arguments:
417             - \p bNew - \p true if the item has been inserted, \p false otherwise
418             - \p item - item of the set
419             - \p val - argument \p val passed into the \p %update() function
420
421             The functor may change non-key fields of the \p item.
422
423             The function applies RCU lock internally.
424
425             Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successfull,
426             \p second is \p true if new item has been added or \p false if the item with \p key
427             already is in the set.
428
429             @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
430             \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
431             synchronization.
432         */
433         template <typename Q, typename Func>
434         std::pair<bool, bool> update( const Q& val, Func func, bool bAllowInsert = true )
435         {
436             std::pair<bool, bool> bRet = bucket( val ).update( val, func, bAllowInsert );
437             if ( bRet.second )
438                 ++m_ItemCounter;
439             return bRet;
440         }//@cond
441         template <typename Q, typename Func>
442         CDS_DEPRECATED("ensure() is deprecated, use update()")
443         std::pair<bool, bool> ensure( const Q& val, Func func )
444         {
445             return update( val, func, true );
446         }
447         //@endcond
448
449         /// Inserts data of type \p value_type created from \p args
450         /**
451             Returns \p true if inserting successful, \p false otherwise.
452
453             The function applies RCU lock internally.
454         */
455         template <typename... Args>
456         bool emplace( Args&&... args )
457         {
458             bool bRet = bucket( value_type(std::forward<Args>(args)...) ).emplace( std::forward<Args>(args)... );
459             if ( bRet )
460                 ++m_ItemCounter;
461             return bRet;
462         }
463
464         /// Deletes \p key from the set
465         /** \anchor cds_nonintrusive_MichealSet_rcu_erase_val
466
467             Since the key of MichaelHashSet's item type \p value_type is not explicitly specified,
468             template parameter \p Q defines the key type searching in the list.
469             The set item comparator should be able to compare the type \p value_type
470             and the type \p Q.
471
472             RCU \p synchronize method can be called. RCU should not be locked.
473
474             Return \p true if key is found and deleted, \p false otherwise
475         */
476         template <typename Q>
477         bool erase( Q const& key )
478         {
479             const bool bRet = bucket( key ).erase( key );
480             if ( bRet )
481                 --m_ItemCounter;
482             return bRet;
483         }
484
485         /// Deletes the item from the set using \p pred predicate for searching
486         /**
487             The function is an analog of \ref cds_nonintrusive_MichealSet_rcu_erase_val "erase(Q const&)"
488             but \p pred is used for key comparing.
489             \p Less functor has the interface like \p std::less.
490             \p Less must imply the same element order as the comparator used for building the set.
491         */
492         template <typename Q, typename Less>
493         bool erase_with( Q const& key, Less pred )
494         {
495             const bool bRet = bucket( key ).erase_with( key, pred );
496             if ( bRet )
497                 --m_ItemCounter;
498             return bRet;
499         }
500
501         /// Deletes \p key from the set
502         /** \anchor cds_nonintrusive_MichealSet_rcu_erase_func
503
504             The function searches an item with key \p key, calls \p f functor
505             and deletes the item. If \p key is not found, the functor is not called.
506
507             The functor \p Func interface:
508             \code
509             struct extractor {
510                 void operator()(value_type const& val);
511             };
512             \endcode
513
514             Since the key of %MichaelHashSet's \p value_type is not explicitly specified,
515             template parameter \p Q defines the key type searching in the list.
516             The list item comparator should be able to compare the type \p T of list item
517             and the type \p Q.
518
519             RCU \p synchronize method can be called. RCU should not be locked.
520
521             Return \p true if key is found and deleted, \p false otherwise
522         */
523         template <typename Q, typename Func>
524         bool erase( Q const& key, Func f )
525         {
526             const bool bRet = bucket( key ).erase( key, f );
527             if ( bRet )
528                 --m_ItemCounter;
529             return bRet;
530         }
531
532         /// Deletes the item from the set using \p pred predicate for searching
533         /**
534             The function is an analog of \ref cds_nonintrusive_MichealSet_rcu_erase_func "erase(Q const&, Func)"
535             but \p pred is used for key comparing.
536             \p Less functor has the interface like \p std::less.
537             \p Less must imply the same element order as the comparator used for building the set.
538         */
539         template <typename Q, typename Less, typename Func>
540         bool erase_with( Q const& key, Less pred, Func f )
541         {
542             const bool bRet = bucket( key ).erase_with( key, pred, f );
543             if ( bRet )
544                 --m_ItemCounter;
545             return bRet;
546         }
547
548         /// Extracts an item from the set
549         /** \anchor cds_nonintrusive_MichaelHashSet_rcu_extract
550             The function searches an item with key equal to \p key in the set,
551             unlinks it from the set, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
552             If the item with the key equal to \p key is not found the function return an empty \p exempt_ptr.
553
554             The function just excludes the item from the set and returns a pointer to item found.
555             Depends on \p bucket_type you should or should not lock RCU before calling of this function:
556             - for the set based on \ref cds_nonintrusive_MichaelList_rcu "MichaelList" RCU should not be locked
557             - for the set based on \ref cds_nonintrusive_LazyList_rcu "LazyList" RCU should be locked
558             See ordered list implementation for details.
559
560             \code
561             #include <cds/urcu/general_buffered.h>
562             #include <cds/container/michael_list_rcu.h>
563             #include <cds/container/michael_set_rcu.h>
564
565             typedef cds::urcu::gc< general_buffered<> > rcu;
566             typedef cds::container::MichaelList< rcu, Foo > rcu_michael_list;
567             typedef cds::container::MichaelHashSet< rcu, rcu_michael_list, foo_traits > rcu_michael_set;
568
569             rcu_michael_set theSet;
570             // ...
571
572             typename rcu_michael_set::exempt_ptr p;
573
574             // For MichaelList we should not lock RCU
575
576             // Note that you must not delete the item found inside the RCU lock
577             p = theSet.extract( 10 );
578             if ( p ) {
579                 // do something with p
580                 ...
581             }
582
583             // We may safely release p here
584             // release() passes the pointer to RCU reclamation cycle
585             p.release();
586             \endcode
587         */
588         template <typename Q>
589         exempt_ptr extract( Q const& key )
590         {
591             exempt_ptr p = bucket( key ).extract( key );
592             if ( p )
593                 --m_ItemCounter;
594             return p;
595         }
596
597         /// Extracts an item from the set using \p pred predicate for searching
598         /**
599             The function is an analog of \p extract(Q const&) but \p pred is used for key comparing.
600             \p Less functor has the interface like \p std::less.
601             \p pred must imply the same element order as the comparator used for building the set.
602         */
603         template <typename Q, typename Less>
604         exempt_ptr extract_with( Q const& key, Less pred )
605         {
606             exempt_ptr p = bucket( key ).extract_with( key, pred );
607             if ( p )
608                 --m_ItemCounter;
609             return p;
610         }
611
612         /// Finds the key \p key
613         /** \anchor cds_nonintrusive_MichealSet_rcu_find_func
614
615             The function searches the item with key equal to \p key and calls the functor \p f for item found.
616             The interface of \p Func functor is:
617             \code
618             struct functor {
619                 void operator()( value_type& item, Q& key );
620             };
621             \endcode
622             where \p item is the item found, \p key is the <tt>find</tt> function argument.
623
624             The functor may change non-key fields of \p item. Note that the functor is only guarantee
625             that \p item cannot be disposed during functor is executing.
626             The functor does not serialize simultaneous access to the set's \p item. If such access is
627             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
628
629             The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
630             can modify both arguments.
631
632             Note the hash functor specified for class \p Traits template parameter
633             should accept a parameter of type \p Q that may be not the same as \p value_type.
634
635             The function applies RCU lock internally.
636
637             The function returns \p true if \p key is found, \p false otherwise.
638         */
639         template <typename Q, typename Func>
640         bool find( Q& key, Func f )
641         {
642             return bucket( key ).find( key, f );
643         }
644         //@cond
645         template <typename Q, typename Func>
646         bool find( Q const& key, Func f )
647         {
648             return bucket( key ).find( key, f );
649         }
650         //@endcond
651
652         /// Finds the key \p key using \p pred predicate for searching
653         /**
654             The function is an analog of \ref cds_nonintrusive_MichealSet_rcu_find_func "find(Q&, Func)"
655             but \p pred is used for key comparing.
656             \p Less functor has the interface like \p std::less.
657             \p Less must imply the same element order as the comparator used for building the set.
658         */
659         template <typename Q, typename Less, typename Func>
660         bool find_with( Q& key, Less pred, Func f )
661         {
662             return bucket( key ).find_with( key, pred, f );
663         }
664         //@cond
665         template <typename Q, typename Less, typename Func>
666         bool find_with( Q const& key, Less pred, Func f )
667         {
668             return bucket( key ).find_with( key, pred, f );
669         }
670         //@endcond
671
672         /// Checks whether the set contains \p key
673         /**
674
675             The function searches the item with key equal to \p key
676             and returns \p true if the key is found, and \p false otherwise.
677
678             Note the hash functor specified for class \p Traits template parameter
679             should accept a parameter of type \p Q that can be not the same as \p value_type.
680         */
681         template <typename Q>
682         bool contains( Q const& key )
683         {
684             return bucket( key ).contains( key );
685         }
686         //@cond
687         template <typename Q>
688         CDS_DEPRECATED("use contains()")
689         bool find( Q const& key )
690         {
691             return contains( key );
692         }
693         //@endcond
694
695         /// Checks whether the set contains \p key using \p pred predicate for searching
696         /**
697             The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
698             \p Less functor has the interface like \p std::less.
699             \p Less must imply the same element order as the comparator used for building the set.
700         */
701         template <typename Q, typename Less>
702         bool contains( Q const& key, Less pred )
703         {
704             return bucket( key ).contains( key, pred );
705         }
706         //@cond
707         template <typename Q, typename Less>
708         CDS_DEPRECATED("use contains()")
709         bool find_with( Q const& key, Less pred )
710         {
711             return contains( key, pred );
712         }
713         //@endcond
714
715         /// Finds the key \p key and return the item found
716         /** \anchor cds_nonintrusive_MichaelHashSet_rcu_get
717             The function searches the item with key equal to \p key and returns the pointer to item found.
718             If \p key is not found it returns \p nullptr.
719             Note the type of returned value depends on underlying \p bucket_type.
720             For details, see documentation of ordered list you use.
721
722             Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
723
724             RCU should be locked before call of this function.
725             Returned item is valid only while RCU is locked:
726             \code
727             typedef cds::container::MichaelHashSet< your_template_parameters > hash_set;
728             hash_set theSet;
729             typename hash_set::raw_ptr gp;
730             // ...
731             {
732                 // Lock RCU
733                 hash_set::rcu_lock lock;
734
735                 gp = theSet.get( 5 );
736                 if ( gp ) {
737                     // Deal with pVal
738                     //...
739                 }
740                 // Unlock RCU by rcu_lock destructor
741                 // gp can be reclaimed at any time after RCU has been unlocked
742             }
743             \endcode
744         */
745         template <typename Q>
746         raw_ptr get( Q const& key )
747         {
748             return bucket( key ).get( key );
749         }
750
751         /// Finds the key \p key and return the item found
752         /**
753             The function is an analog of \ref cds_nonintrusive_MichaelHashSet_rcu_get "get(Q const&)"
754             but \p pred is used for comparing the keys.
755
756             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
757             in any order.
758             \p pred must imply the same element order as the comparator used for building the set.
759         */
760         template <typename Q, typename Less>
761         raw_ptr get_with( Q const& key, Less pred )
762         {
763             return bucket( key ).get_with( key, pred );
764         }
765
766         /// Clears the set (not atomic)
767         void clear()
768         {
769             for ( size_t i = 0; i < bucket_count(); ++i )
770                 m_Buckets[i].clear();
771             m_ItemCounter.reset();
772         }
773
774         /// Checks if the set is empty
775         /**
776             Emptiness is checked by item counting: if item count is zero then the set is empty.
777             Thus, the correct item counting feature is an important part of Michael's set implementation.
778         */
779         bool empty() const
780         {
781             return size() == 0;
782         }
783
784         /// Returns item count in the set
785         size_t size() const
786         {
787             return m_ItemCounter;
788         }
789
790         /// Returns the size of hash table
791         /**
792             Since \p %MichaelHashSet cannot dynamically extend the hash table size,
793             the value returned is an constant depending on object initialization parameters;
794             see MichaelHashSet::MichaelHashSet for explanation.
795         */
796         size_t bucket_count() const
797         {
798             return m_nHashBitmask + 1;
799         }
800     };
801
802 }} // namespace cds::container
803
804 #endif // ifndef CDSLIB_CONTAINER_MICHAEL_SET_RCU_H