Refactored Set_DelOdd MT-test
[libcds.git] / cds / intrusive / michael_set.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_INTRUSIVE_MICHAEL_SET_H
4 #define CDSLIB_INTRUSIVE_MICHAEL_SET_H
5
6 #include <cds/intrusive/details/michael_set_base.h>
7 #include <cds/details/allocator.h>
8
9 namespace cds { namespace intrusive {
10
11     /// Michael's hash set
12     /** @ingroup cds_intrusive_map
13         \anchor cds_intrusive_MichaelHashSet_hp
14
15         Source:
16             - [2002] Maged Michael "High performance dynamic lock-free hash tables and list-based sets"
17
18         Michael's hash table algorithm is based on lock-free ordered list and it is very simple.
19         The main structure is an array \p T of size \p M. Each element in \p T is basically a pointer
20         to a hash bucket, implemented as a singly linked list. The array of buckets cannot be dynamically expanded.
21         However, each bucket may contain unbounded number of items.
22
23         Template parameters are:
24         - \p GC - Garbage collector used. Note the \p GC must be the same as the GC used for \p OrderedList
25         - \p OrderedList - ordered list implementation used as bucket for hash set, for example, \p MichaelList, \p LazyList.
26             The intrusive ordered list implementation specifies the type \p T stored in the hash-set, the reclamation
27             schema \p GC used by hash-set, the comparison functor for the type \p T and other features specific for
28             the ordered list.
29         - \p Traits - type traits. See \p michael_set::traits for explanation.
30             Instead of defining \p Traits struct you can use option-based syntax with \p michael_set::make_traits metafunction.
31
32         There are several specializations of \p %MichaelHashSet for each GC. You should include:
33         - <tt><cds/intrusive/michael_set_rcu.h></tt> for \ref cds_intrusive_MichaelHashSet_rcu "RCU type"
34         - <tt><cds/intrusive/michael_set_nogc.h></tt> for \ref cds_intrusive_MichaelHashSet_nogc for append-only set
35         - <tt><cds/intrusive/michael_set.h></tt> for \p gc::HP, \p gc::DHP
36
37         <b>Hash functor</b>
38
39         Some member functions of Michael's hash set accept the key parameter of type \p Q which differs from \p value_type.
40         It is expected that type \p Q contains full key of \p value_type, and for equal keys of type \p Q and \p value_type
41         the hash values of these keys must be equal.
42         The hash functor \p Traits::hash should accept parameters of both type:
43         \code
44         // Our node type
45         struct Foo {
46             std::string     key_; // key field
47             // ... other fields
48         };
49
50         // Hash functor
51         struct fooHash {
52             size_t operator()( const std::string& s ) const
53             {
54                 return std::hash( s );
55             }
56
57             size_t operator()( const Foo& f ) const
58             {
59                 return (*this)( f.key_ );
60             }
61         };
62         \endcode
63
64         <b>How to use</b>
65
66         First, you should define ordered list type to use in your hash set:
67         \code
68         // For gc::HP-based MichaelList implementation
69         #include <cds/intrusive/michael_list_hp.h>
70
71         // cds::intrusive::MichaelHashSet declaration
72         #include <cds/intrusive/michael_set.h>
73
74         // Type of hash-set items
75         struct Foo: public cds::intrusive::michael_list::node< cds::gc::HP >
76         {
77             std::string     key_    ;   // key field
78             unsigned        val_    ;   // value field
79             // ...  other value fields
80         };
81
82         // Declare comparator for the item
83         struct FooCmp
84         {
85             int operator()( const Foo& f1, const Foo& f2 ) const
86             {
87                 return f1.key_.compare( f2.key_ );
88             }
89         };
90
91         // Declare bucket type for Michael's hash set
92         // The bucket type is any ordered list type like MichaelList, LazyList
93         typedef cds::intrusive::MichaelList< cds::gc::HP, Foo,
94             typename cds::intrusive::michael_list::make_traits<
95                 // hook option
96                 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::HP > > >
97                 // item comparator option
98                 ,cds::opt::compare< FooCmp >
99             >::type
100         >  Foo_bucket;
101         \endcode
102
103         Second, you should declare Michael's hash set container:
104         \code
105
106         // Declare hash functor
107         // Note, the hash functor accepts parameter type Foo and std::string
108         struct FooHash {
109             size_t operator()( const Foo& f ) const
110             {
111                 return cds::opt::v::hash<std::string>()( f.key_ );
112             }
113             size_t operator()( const std::string& f ) const
114             {
115                 return cds::opt::v::hash<std::string>()( f );
116             }
117         };
118
119         // Michael's set typedef
120         typedef cds::intrusive::MichaelHashSet<
121             cds::gc::HP
122             ,Foo_bucket
123             ,typename cds::intrusive::michael_set::make_traits<
124                 cds::opt::hash< FooHash >
125             >::type
126         > Foo_set;
127         \endcode
128
129         Now, you can use \p Foo_set in your application.
130
131         Like other intrusive containers, you may build several containers on single item structure:
132         \code
133         #include <cds/intrusive/michael_list_hp.h>
134         #include <cds/intrusive/michael_list_dhp.h>
135         #include <cds/intrusive/michael_set.h>
136
137         struct tag_key1_idx;
138         struct tag_key2_idx;
139
140         // Your two-key data
141         // The first key is maintained by gc::HP, second key is maintained by gc::DHP garbage collectors
142         // (I don't know what is needed for, but it is correct)
143         struct Foo
144             : public cds::intrusive::michael_list::node< cds::gc::HP, tag_key1_idx >
145             , public cds::intrusive::michael_list::node< cds::gc::DHP, tag_key2_idx >
146         {
147             std::string     key1_   ;   // first key field
148             unsigned int    key2_   ;   // second key field
149
150             // ... value fields and fields for controlling item's lifetime
151         };
152
153         // Declare comparators for the item
154         struct Key1Cmp
155         {
156             int operator()( const Foo& f1, const Foo& f2 ) const { return f1.key1_.compare( f2.key1_ ) ; }
157         };
158         struct Key2Less
159         {
160             bool operator()( const Foo& f1, const Foo& f2 ) const { return f1.key2_ < f2.key1_ ; }
161         };
162
163         // Declare bucket type for Michael's hash set indexed by key1_ field and maintained by gc::HP
164         typedef cds::intrusive::MichaelList< cds::gc::HP, Foo,
165             typename cds::intrusive::michael_list::make_traits<
166                 // hook option
167                 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::HP >, tag_key1_idx > >
168                 // item comparator option
169                 ,cds::opt::compare< Key1Cmp >
170             >::type
171         >  Key1_bucket;
172
173         // Declare bucket type for Michael's hash set indexed by key2_ field and maintained by gc::DHP
174         typedef cds::intrusive::MichaelList< cds::gc::DHP, Foo,
175             typename cds::intrusive::michael_list::make_traits<
176                 // hook option
177                 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::DHP >, tag_key2_idx > >
178                 // item comparator option
179                 ,cds::opt::less< Key2Less >
180             >::type
181         >  Key2_bucket;
182
183         // Declare hash functor
184         struct Key1Hash {
185             size_t operator()( const Foo& f ) const { return cds::opt::v::hash<std::string>()( f.key1_ ) ; }
186             size_t operator()( const std::string& s ) const { return cds::opt::v::hash<std::string>()( s ) ; }
187         };
188         inline size_t Key2Hash( const Foo& f ) { return (size_t) f.key2_  ; }
189
190         // Michael's set indexed by key1_ field
191         typedef cds::intrusive::MichaelHashSet<
192             cds::gc::HP
193             ,Key1_bucket
194             ,typename cds::intrusive::michael_set::make_traits<
195                 cds::opt::hash< Key1Hash >
196             >::type
197         > key1_set;
198
199         // Michael's set indexed by key2_ field
200         typedef cds::intrusive::MichaelHashSet<
201             cds::gc::DHP
202             ,Key2_bucket
203             ,typename cds::intrusive::michael_set::make_traits<
204                 cds::opt::hash< Key2Hash >
205             >::type
206         > key2_set;
207         \endcode
208     */
209     template <
210         class GC,
211         class OrderedList,
212 #ifdef CDS_DOXYGEN_INVOKED
213         class Traits = michael_set::traits
214 #else
215         class Traits
216 #endif
217     >
218     class MichaelHashSet
219     {
220     public:
221         typedef GC           gc;            ///< Garbage collector
222         typedef OrderedList  ordered_list;  ///< type of ordered list used as a bucket implementation
223         typedef ordered_list bucket_type;   ///< bucket type
224         typedef Traits       traits;       ///< Set traits
225
226         typedef typename ordered_list::value_type       value_type      ;   ///< type of value to be stored in the set
227         typedef typename ordered_list::key_comparator   key_comparator  ;   ///< key comparing functor
228         typedef typename ordered_list::disposer         disposer        ;   ///< Node disposer functor
229
230         /// Hash functor for \p value_type and all its derivatives that you use
231         typedef typename cds::opt::v::hash_selector< typename traits::hash >::type hash;
232         typedef typename traits::item_counter item_counter;   ///< Item counter type
233
234         typedef typename ordered_list::guarded_ptr guarded_ptr; ///< Guarded pointer
235
236         /// Bucket table allocator
237         typedef cds::details::Allocator< bucket_type, typename traits::allocator > bucket_table_allocator;
238
239         //@cond
240         typedef cds::intrusive::michael_set::implementation_tag implementation_tag;
241         //@endcond
242
243     protected:
244         item_counter    m_ItemCounter;   ///< Item counter
245         hash            m_HashFunctor;   ///< Hash functor
246         bucket_type *   m_Buckets;      ///< bucket table
247
248     private:
249         //@cond
250         const size_t    m_nHashBitmask;
251         //@endcond
252
253     protected:
254         //@cond
255         /// Calculates hash value of \p key
256         template <typename Q>
257         size_t hash_value( const Q& key ) const
258         {
259             return m_HashFunctor( key ) & m_nHashBitmask;
260         }
261
262         /// Returns the bucket (ordered list) for \p key
263         template <typename Q>
264         bucket_type&    bucket( const Q& key )
265         {
266             return m_Buckets[ hash_value( key ) ];
267         }
268         //@endcond
269
270     public:
271         /// Forward iterator
272         /**
273             The forward iterator for Michael's set is based on \p OrderedList forward iterator and has some features:
274             - it has no post-increment operator
275             - it iterates items in unordered fashion
276             - The iterator cannot be moved across thread boundary since it may contain GC's guard that is thread-private GC data.
277             - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
278               deleting operations it is no guarantee that you iterate all item in the set.
279
280             Therefore, the use of iterators in concurrent environment is not good idea. Use the iterator for the concurrent container
281             for debug purpose only.
282         */
283         typedef michael_set::details::iterator< bucket_type, false >    iterator;
284
285         /// Const forward iterator
286         /**
287             For iterator's features and requirements see \ref iterator
288         */
289         typedef michael_set::details::iterator< bucket_type, true >     const_iterator;
290
291         /// Returns a forward iterator addressing the first element in a set
292         /**
293             For empty set \code begin() == end() \endcode
294         */
295         iterator begin()
296         {
297             return iterator( m_Buckets[0].begin(), m_Buckets, m_Buckets + bucket_count() );
298         }
299
300         /// Returns an iterator that addresses the location succeeding the last element in a set
301         /**
302             Do not use the value returned by <tt>end</tt> function to access any item.
303             The returned value can be used only to control reaching the end of the set.
304             For empty set \code begin() == end() \endcode
305         */
306         iterator end()
307         {
308             return iterator( m_Buckets[bucket_count() - 1].end(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
309         }
310
311         /// Returns a forward const iterator addressing the first element in a set
312         //@{
313         const_iterator begin() const
314         {
315             return get_const_begin();
316         }
317         const_iterator cbegin() const
318         {
319             return get_const_begin();
320         }
321         //@}
322
323         /// Returns an const iterator that addresses the location succeeding the last element in a set
324         //@{
325         const_iterator end() const
326         {
327             return get_const_end();
328         }
329         const_iterator cend() const
330         {
331             return get_const_end();
332         }
333         //@}
334
335     private:
336         //@cond
337         const_iterator get_const_begin() const
338         {
339             return const_iterator( m_Buckets[0].cbegin(), m_Buckets, m_Buckets + bucket_count() );
340         }
341         const_iterator get_const_end() const
342         {
343             return const_iterator( m_Buckets[bucket_count() - 1].cend(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
344         }
345         //@endcond
346
347     public:
348         /// Initializes hash set
349         /** @anchor cds_intrusive_MichaelHashSet_hp_ctor
350             The Michael's hash set is an unbounded container, but its hash table is non-expandable.
351             At construction time you should pass estimated maximum item count and a load factor.
352             The load factor is average size of one bucket - a small number between 1 and 10.
353             The bucket is an ordered single-linked list, searching in the bucket has linear complexity <tt>O(nLoadFactor)</tt>.
354             The constructor defines hash table size as rounding <tt>nMaxItemCount / nLoadFactor</tt> up to nearest power of two.
355         */
356         MichaelHashSet(
357             size_t nMaxItemCount,   ///< estimation of max item count in the hash set
358             size_t nLoadFactor      ///< load factor: estimation of max number of items in the bucket. Small integer up to 10.
359         ) : m_nHashBitmask( michael_set::details::init_hash_bitmask( nMaxItemCount, nLoadFactor ))
360         {
361             // GC and OrderedList::gc must be the same
362             static_assert( std::is_same<gc, typename bucket_type::gc>::value, "GC and OrderedList::gc must be the same");
363
364             // atomicity::empty_item_counter is not allowed as a item counter
365             static_assert( !std::is_same<item_counter, atomicity::empty_item_counter>::value,
366                            "cds::atomicity::empty_item_counter is not allowed as a item counter");
367
368             m_Buckets = bucket_table_allocator().NewArray( bucket_count() );
369         }
370
371         /// Clears hash set object and destroys it
372         ~MichaelHashSet()
373         {
374             clear();
375             bucket_table_allocator().Delete( m_Buckets, bucket_count() );
376         }
377
378         /// Inserts new node
379         /**
380             The function inserts \p val in the set if it does not contain
381             an item with key equal to \p val.
382
383             Returns \p true if \p val is placed into the set, \p false otherwise.
384         */
385         bool insert( value_type& val )
386         {
387             bool bRet = bucket( val ).insert( val );
388             if ( bRet )
389                 ++m_ItemCounter;
390             return bRet;
391         }
392
393         /// Inserts new node
394         /**
395             This function is intended for derived non-intrusive containers.
396
397             The function allows to split creating of new item into two part:
398             - create item with key only
399             - insert new item into the set
400             - if inserting is success, calls  \p f functor to initialize value-field of \p val.
401
402             The functor signature is:
403             \code
404                 void func( value_type& val );
405             \endcode
406             where \p val is the item inserted.
407
408             The user-defined functor is called only if the inserting is success.
409
410             @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
411             \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
412             synchronization.
413         */
414         template <typename Func>
415         bool insert( value_type& val, Func f )
416         {
417             bool bRet = bucket( val ).insert( val, f );
418             if ( bRet )
419                 ++m_ItemCounter;
420             return bRet;
421         }
422
423         /// Updates the element
424         /**
425             The operation performs inserting or changing data with lock-free manner.
426
427             If the item \p val not found in the set, then \p val is inserted iff \p bAllowInsert is \p true.
428             Otherwise, the functor \p func is called with item found.
429             The functor signature is:
430             \code
431                 struct functor {
432                     void operator()( bool bNew, value_type& item, value_type& val );
433                 };
434             \endcode
435             with arguments:
436             - \p bNew - \p true if the item has been inserted, \p false otherwise
437             - \p item - item of the set
438             - \p val - argument \p val passed into the \p %update() function
439             If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
440             refers to the same thing.
441
442             The functor may change non-key fields of the \p item.
443
444             Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successfull,
445             \p second is \p true if new item has been added or \p false if the item with \p key
446             already is in the set.
447
448             @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
449             \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
450             synchronization.
451         */
452         template <typename Func>
453         std::pair<bool, bool> update( value_type& val, Func func, bool bAllowInsert = true )
454         {
455             std::pair<bool, bool> bRet = bucket( val ).update( val, func, bAllowInsert );
456             if ( bRet.second )
457                 ++m_ItemCounter;
458             return bRet;
459         }
460         //@cond
461         template <typename Func>
462         CDS_DEPRECATED("ensure() is deprecated, use update()")
463         std::pair<bool, bool> ensure( value_type& val, Func func )
464         {
465             return update( val, func, true );
466         }
467         //@endcond
468
469         /// Unlinks the item \p val from the set
470         /**
471             The function searches the item \p val in the set and unlink it
472             if it is found and is equal to \p val.
473
474             The function returns \p true if success and \p false otherwise.
475         */
476         bool unlink( value_type& val )
477         {
478             bool bRet = bucket( val ).unlink( val );
479             if ( bRet )
480                 --m_ItemCounter;
481             return bRet;
482         }
483
484         /// Deletes the item from the set
485         /** \anchor cds_intrusive_MichaelHashSet_hp_erase
486             The function searches an item with key equal to \p key in the set,
487             unlinks it, and returns \p true.
488             If the item with key equal to \p key is not found the function return \p false.
489
490             Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
491         */
492         template <typename Q>
493         bool erase( Q const& key )
494         {
495             if ( bucket( key ).erase( key )) {
496                 --m_ItemCounter;
497                 return true;
498             }
499             return false;
500         }
501
502         /// Deletes the item from the set using \p pred predicate for searching
503         /**
504             The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_erase "erase(Q const&)"
505             but \p pred is used for key comparing.
506             \p Less functor has the interface like \p std::less.
507             \p pred must imply the same element order as the comparator used for building the set.
508         */
509         template <typename Q, typename Less>
510         bool erase_with( Q const& key, Less pred )
511         {
512             if ( bucket( key ).erase_with( key, pred )) {
513                 --m_ItemCounter;
514                 return true;
515             }
516             return false;
517         }
518
519         /// Deletes the item from the set
520         /** \anchor cds_intrusive_MichaelHashSet_hp_erase_func
521             The function searches an item with key equal to \p key in the set,
522             call \p f functor with item found, and unlinks it from the set.
523             The \ref disposer specified in \p OrderedList class template parameter is called
524             by garbage collector \p GC asynchronously.
525
526             The \p Func interface is
527             \code
528             struct functor {
529                 void operator()( value_type const& item );
530             };
531             \endcode
532
533             If the item with key equal to \p key is not found the function return \p false.
534
535             Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
536         */
537         template <typename Q, typename Func>
538         bool erase( const Q& key, Func f )
539         {
540             if ( bucket( key ).erase( key, f )) {
541                 --m_ItemCounter;
542                 return true;
543             }
544             return false;
545         }
546
547         /// Deletes the item from the set using \p pred predicate for searching
548         /**
549             The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_erase_func "erase(Q const&, Func)"
550             but \p pred is used for key comparing.
551             \p Less functor has the interface like \p std::less.
552             \p pred must imply the same element order as the comparator used for building the set.
553         */
554         template <typename Q, typename Less, typename Func>
555         bool erase_with( const Q& key, Less pred, Func f )
556         {
557             if ( bucket( key ).erase_with( key, pred, f )) {
558                 --m_ItemCounter;
559                 return true;
560             }
561             return false;
562         }
563
564         /// Extracts the item with specified \p key
565         /** \anchor cds_intrusive_MichaelHashSet_hp_extract
566             The function searches an item with key equal to \p key,
567             unlinks it from the set, and returns an guarded pointer to the item extracted.
568             If \p key is not found the function returns an empty guarded pointer.
569
570             Note the compare functor should accept a parameter of type \p Q that may be not the same as \p value_type.
571
572             The \p disposer specified in \p OrderedList class' template parameter is called automatically
573             by garbage collector \p GC when returned \ref guarded_ptr object will be destroyed or released.
574             @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
575
576             Usage:
577             \code
578             typedef cds::intrusive::MichaelHashSet< your_template_args > michael_set;
579             michael_set theSet;
580             // ...
581             {
582                 michael_set::guarded_ptr gp( theSet.extract( 5 ));
583                 if ( gp ) {
584                     // Deal with gp
585                     // ...
586                 }
587                 // Destructor of gp releases internal HP guard
588             }
589             \endcode
590         */
591         template <typename Q>
592         guarded_ptr extract( Q const& key )
593         {
594             guarded_ptr gp = bucket( key ).extract( key );
595             if ( gp )
596                 --m_ItemCounter;
597             return gp;
598         }
599
600         /// Extracts the item using compare functor \p pred
601         /**
602             The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_extract "extract(Q const&)"
603             but \p pred predicate is used for key comparing.
604
605             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
606             in any order.
607             \p pred must imply the same element order as the comparator used for building the list.
608         */
609         template <typename Q, typename Less>
610         guarded_ptr extract_with( Q const& key, Less pred )
611         {
612             guarded_ptr gp = bucket( key ).extract_with( key, pred );
613             if ( gp )
614                 --m_ItemCounter;
615             return gp;
616         }
617
618         /// Finds the key \p key
619         /** \anchor cds_intrusive_MichaelHashSet_hp_find_func
620             The function searches the item with key equal to \p key and calls the functor \p f for item found.
621             The interface of \p Func functor is:
622             \code
623             struct functor {
624                 void operator()( value_type& item, Q& key );
625             };
626             \endcode
627             where \p item is the item found, \p key is the <tt>find</tt> function argument.
628
629             The functor may change non-key fields of \p item. Note that the functor is only guarantee
630             that \p item cannot be disposed during functor is executing.
631             The functor does not serialize simultaneous access to the set \p item. If such access is
632             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
633
634             The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
635             may modify both arguments.
636
637             Note the hash functor specified for class \p Traits template parameter
638             should accept a parameter of type \p Q that can be not the same as \p value_type.
639
640             The function returns \p true if \p key is found, \p false otherwise.
641         */
642         template <typename Q, typename Func>
643         bool find( Q& key, Func f )
644         {
645             return bucket( key ).find( key, f );
646         }
647         //@cond
648         template <typename Q, typename Func>
649         bool find( Q const& key, Func f )
650         {
651             return bucket( key ).find( key, f );
652         }
653         //@endcond
654
655         /// Finds the key \p key using \p pred predicate for searching
656         /**
657             The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_find_func "find(Q&, Func)"
658             but \p pred is used for key comparing.
659             \p Less functor has the interface like \p std::less.
660             \p pred must imply the same element order as the comparator used for building the set.
661         */
662         template <typename Q, typename Less, typename Func>
663         bool find_with( Q& key, Less pred, Func f )
664         {
665             return bucket( key ).find_with( key, pred, f );
666         }
667         //@cond
668         template <typename Q, typename Less, typename Func>
669         bool find_with( Q const& key, Less pred, Func f )
670         {
671             return bucket( key ).find_with( key, pred, f );
672         }
673         //@endcond
674
675         /// Checks whether the set contains \p key
676         /** 
677             The function searches the item with key equal to \p key
678             and returns \p true if the key is found, and \p false otherwise.
679
680             Note the hash functor specified for class \p Traits template parameter
681             should accept a parameter of type \p Q that can be not the same as \p value_type.
682         */
683         template <typename Q>
684         bool contains( Q const& key )
685         {
686             return bucket( key ).contains( key );
687         }
688         //@cond
689         template <typename Q>
690         CDS_DEPRECATED("use contains()")
691         bool find( Q const& key )
692         {
693             return contains( key );
694         }
695         //@endcond
696
697         /// Checks whether the set contains \p key using \p pred predicate for searching
698         /**
699             The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
700             \p Less functor has the interface like \p std::less.
701             \p Less must imply the same element order as the comparator used for building the set.
702         */
703         template <typename Q, typename Less>
704         bool contains( Q const& key, Less pred )
705         {
706             return bucket( key ).contains( key, pred );
707         }
708         //@cond
709         template <typename Q, typename Less>
710         CDS_DEPRECATED("use contains()")
711         bool find_with( Q const& key, Less pred )
712         {
713             return contains( key, pred );
714         }
715         //@endcond
716
717         /// Finds the key \p key and return the item found
718         /** \anchor cds_intrusive_MichaelHashSet_hp_get
719             The function searches the item with key equal to \p key
720             and returns the guarded pointer to the item found.
721             If \p key is not found the function returns an empty \p guarded_ptr.
722
723             @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
724
725             Usage:
726             \code
727             typedef cds::intrusive::MichaelHashSet< your_template_params >  michael_set;
728             michael_set theSet;
729             // ...
730             {
731                 michael_set::guarded_ptr gp( theSet.get( 5 ));
732                 if ( theSet.get( 5 )) {
733                     // Deal with gp
734                     //...
735                 }
736                 // Destructor of guarded_ptr releases internal HP guard
737             }
738             \endcode
739
740             Note the compare functor specified for \p OrderedList template parameter
741             should accept a parameter of type \p Q that can be not the same as \p value_type.
742         */
743         template <typename Q>
744         guarded_ptr get( Q const& key )
745         {
746             return bucket( key ).get( key );
747         }
748
749         /// Finds the key \p key and return the item found
750         /**
751             The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_get "get( Q const&)"
752             but \p pred is used for comparing the keys.
753
754             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
755             in any order.
756             \p pred must imply the same element order as the comparator used for building the set.
757         */
758         template <typename Q, typename Less>
759         guarded_ptr get_with( Q const& key, Less pred )
760         {
761             return bucket( key ).get_with( key, pred );
762         }
763
764         /// Clears the set (non-atomic)
765         /**
766             The function unlink all items from the set.
767             The function is not atomic. It cleans up each bucket and then resets the item counter to zero.
768             If there are a thread that performs insertion while \p %clear() is working the result is undefined in general case:
769             \p empty() may return \p true but the set may contain item(s).
770             Therefore, \p %clear() may be used only for debugging purposes.
771
772             For each item the \p disposer is called after unlinking.
773         */
774         void clear()
775         {
776             for ( size_t i = 0; i < bucket_count(); ++i )
777                 m_Buckets[i].clear();
778             m_ItemCounter.reset();
779         }
780
781         /// Checks if the set is empty
782         /**
783             Emptiness is checked by item counting: if item count is zero then the set is empty.
784             Thus, the correct item counting feature is an important part of Michael's set implementation.
785         */
786         bool empty() const
787         {
788             return size() == 0;
789         }
790
791         /// Returns item count in the set
792         size_t size() const
793         {
794             return m_ItemCounter;
795         }
796
797         /// Returns the size of hash table
798         /**
799             Since \p %MichaelHashSet cannot dynamically extend the hash table size,
800             the value returned is an constant depending on object initialization parameters,
801             see \p MichaelHashSet::MichaelHashSet.
802         */
803         size_t bucket_count() const
804         {
805             return m_nHashBitmask + 1;
806         }
807     };
808
809 }}  // namespace cds::intrusive
810
811 #endif // ifndef CDSLIB_INTRUSIVE_MICHAEL_SET_H