9157f02dbe6dbbee52e946aa1940c8f4deb64aa6
[libcds.git] / cds / intrusive / 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_INTRUSIVE_MICHAEL_SET_RCU_H
32 #define CDSLIB_INTRUSIVE_MICHAEL_SET_RCU_H
33
34 #include <cds/intrusive/details/michael_set_base.h>
35 #include <cds/details/allocator.h>
36
37 namespace cds { namespace intrusive {
38
39     /// Michael's hash set, \ref cds_urcu_desc "RCU" specialization
40     /** @ingroup cds_intrusive_map
41         \anchor cds_intrusive_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 bucket for hash set, for example, MichaelList, LazyList.
54             The intrusive ordered list implementation specifies the type \p T stored in the hash-set, the reclamation
55             schema \p GC used by hash-set, the comparison functor for the type \p T and other features specific for
56             the ordered list.
57         - \p Traits - type traits, default is \p michael_set::traits.
58             Instead of defining \p Traits struct you can use option-based syntax with \p michael_set::make_traits metafunction.
59
60         \par Usage
61             Before including <tt><cds/intrusive/michael_set_rcu.h></tt> you should include appropriate RCU header file,
62             see \ref cds_urcu_gc "RCU type" for list of existing RCU class and corresponding header files.
63             For example, for \ref cds_urcu_general_buffered_gc "general-purpose buffered RCU" you should include:
64             \code
65             #include <cds/urcu/general_buffered.h>
66             #include <cds/intrusive/michael_list_rcu.h>
67             #include <cds/intrusive/michael_set_rcu.h>
68
69             struct Foo { ... };
70             // Hash functor for struct Foo
71             struct foo_hash {
72                 size_t operator()( Foo const& foo ) const { return ... }
73             };
74
75             // Now, you can declare Michael's list for type Foo and default traits:
76             typedef cds::intrusive::MichaelList<cds::urcu::gc< general_buffered<> >, Foo > rcu_michael_list;
77
78             // Declare Michael's set with MichaelList as bucket type
79             typedef cds::intrusive::MichaelSet<
80                 cds::urcu::gc< general_buffered<> >,
81                 rcu_michael_list,
82                 cds::intrusive::michael_set::make_traits<
83                     cds::opt::::hash< foo_hash >
84                 >::type
85             > rcu_michael_set;
86
87             // Declares hash set for 1000000 items with load factor 2
88             rcu_michael_set theSet( 1000000, 2 );
89
90             // Now you can use theSet object in many threads without any synchronization.
91             \endcode
92     */
93     template <
94         class RCU,
95         class OrderedList,
96 #ifdef CDS_DOXYGEN_INVOKED
97         class Traits = michael_set::traits
98 #else
99         class Traits
100 #endif
101     >
102     class MichaelHashSet< cds::urcu::gc< RCU >, OrderedList, Traits >
103     {
104     public:
105         typedef cds::urcu::gc< RCU > gc;   ///< RCU schema
106         typedef OrderedList bucket_type;   ///< type of ordered list used as a bucket implementation
107         typedef Traits traits;             ///< Set traits
108
109         typedef typename bucket_type::value_type        value_type      ;   ///< type of value stored in the list
110         typedef typename bucket_type::key_comparator    key_comparator  ;   ///< key comparing functor
111         typedef typename bucket_type::disposer          disposer        ;   ///< Node disposer functor
112
113         /// Hash functor for \ref value_type and all its derivatives that you use
114         typedef typename cds::opt::v::hash_selector< typename traits::hash >::type hash;
115         typedef typename traits::item_counter item_counter;   ///< Item counter type
116
117         /// Bucket table allocator
118         typedef cds::details::Allocator< bucket_type, typename traits::allocator >  bucket_table_allocator;
119
120         typedef typename bucket_type::rcu_lock    rcu_lock;   ///< RCU scoped lock
121         typedef typename bucket_type::exempt_ptr  exempt_ptr; ///< pointer to extracted node
122         typedef typename bucket_type::raw_ptr     raw_ptr;    ///< Return type of \p get() member function and its derivatives
123         /// Group of \p extract_xxx functions require external locking if underlying ordered list requires that
124         static CDS_CONSTEXPR const bool c_bExtractLockExternal = bucket_type::c_bExtractLockExternal;
125
126     protected:
127         item_counter    m_ItemCounter;   ///< Item counter
128         hash            m_HashFunctor;   ///< Hash functor
129         bucket_type *   m_Buckets;       ///< bucket table
130
131     private:
132         //@cond
133         const size_t m_nHashBitmask;
134         //@endcond
135
136     protected:
137         //@cond
138         /// Calculates hash value of \p key
139         template <typename Q>
140         size_t hash_value( Q const& key ) const
141         {
142             return m_HashFunctor( key ) & m_nHashBitmask;
143         }
144
145         /// Returns the bucket (ordered list) for \p key
146         template <typename Q>
147         bucket_type& bucket( Q const& key )
148         {
149             return m_Buckets[ hash_value( key ) ];
150         }
151         template <typename Q>
152         bucket_type const& bucket( Q const& key ) const
153         {
154             return m_Buckets[ hash_value( key ) ];
155         }
156         //@endcond
157
158     public:
159     ///@name Forward iterators (thread-safe under RCU lock)
160     //@{
161         /// Forward iterator
162         /**
163             The forward iterator for Michael's set is based on \p OrderedList forward iterator and has some features:
164             - it has no post-increment operator
165             - it iterates items in unordered fashion
166
167             You may safely use iterators in multi-threaded environment only under RCU lock.
168             Otherwise, a crash is possible if another thread deletes the element the iterator points to.
169         */
170         typedef michael_set::details::iterator< bucket_type, false >    iterator;
171
172         /// Const forward iterator
173         /**
174             For iterator's features and requirements see \ref iterator
175         */
176         typedef michael_set::details::iterator< bucket_type, true >     const_iterator;
177
178         /// Returns a forward iterator addressing the first element in a set
179         /**
180             For empty set \code begin() == end() \endcode
181         */
182         iterator begin()
183         {
184             return iterator( m_Buckets[0].begin(), m_Buckets, m_Buckets + bucket_count() );
185         }
186
187         /// Returns an iterator that addresses the location succeeding the last element in a set
188         /**
189             Do not use the value returned by <tt>end</tt> function to access any item.
190             The returned value can be used only to control reaching the end of the set.
191             For empty set \code begin() == end() \endcode
192         */
193         iterator end()
194         {
195             return iterator( m_Buckets[bucket_count() - 1].end(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
196         }
197
198         /// Returns a forward const iterator addressing the first element in a set
199         const_iterator begin() const
200         {
201             return cbegin();
202         }
203
204         /// Returns a forward const iterator addressing the first element in a set
205         const_iterator cbegin() const
206         {
207             return const_iterator( m_Buckets[0].cbegin(), m_Buckets, m_Buckets + bucket_count() );
208         }
209
210         /// Returns an const iterator that addresses the location succeeding the last element in a set
211         const_iterator end() const
212         {
213             return cend();
214         }
215
216         /// Returns an const iterator that addresses the location succeeding the last element in a set
217         const_iterator cend() const
218         {
219             return const_iterator( m_Buckets[bucket_count() - 1].cend(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() );
220         }
221     //@}
222
223     public:
224         /// Initialize hash set
225         /**
226             The Michael's hash set is an unbounded container, but its hash table is non-expandable.
227             At construction time you should pass estimated maximum item count and a load factor.
228             The load factor is average size of one bucket - a small number between 1 and 10.
229             The bucket is an ordered single-linked list, the complexity of searching in the bucket is linear <tt>O(nLoadFactor)</tt>.
230             The constructor defines hash table size as rounding <tt>nMaxItemCount / nLoadFactor</tt> up to nearest power of two.
231         */
232         MichaelHashSet(
233             size_t nMaxItemCount,   ///< estimation of max item count in the hash set
234             size_t nLoadFactor      ///< load factor: average size of the bucket
235         ) : m_nHashBitmask( michael_set::details::init_hash_bitmask( nMaxItemCount, nLoadFactor ))
236         {
237             // GC and OrderedList::gc must be the same
238             static_assert( std::is_same<gc, typename bucket_type::gc>::value, "GC and OrderedList::gc must be the same");
239
240             // atomicity::empty_item_counter is not allowed as a item counter
241             static_assert( !std::is_same<item_counter, atomicity::empty_item_counter>::value,
242                 "atomicity::empty_item_counter is not allowed as a item counter");
243
244             m_Buckets = bucket_table_allocator().NewArray( bucket_count() );
245         }
246
247         /// Clear hash set and destroy it
248         ~MichaelHashSet()
249         {
250             clear();
251             bucket_table_allocator().Delete( m_Buckets, bucket_count() );
252         }
253
254         /// Inserts new node
255         /**
256             The function inserts \p val in the set if it does not contain
257             an item with key equal to \p val.
258
259             Returns \p true if \p val is placed into the set, \p false otherwise.
260         */
261         bool insert( value_type& val )
262         {
263             bool bRet = bucket( val ).insert( val );
264             if ( bRet )
265                 ++m_ItemCounter;
266             return bRet;
267         }
268
269         /// Inserts new node
270         /**
271             This function is intended for derived non-intrusive containers.
272
273             The function allows to split creating of new item into two part:
274             - create item with key only
275             - insert new item into the set
276             - if inserting is success, calls  \p f functor to initialize value-field of \p val.
277
278             The functor signature is:
279             \code
280                 void func( value_type& val );
281             \endcode
282             where \p val is the item inserted.
283             The user-defined functor is called only if the inserting is success.
284
285             @warning For \ref cds_intrusive_MichaelList_rcu "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
286             \ref cds_intrusive_LazyList_rcu "LazyList" provides exclusive access to inserted item and does not require any node-level
287             synchronization.
288         */
289         template <typename Func>
290         bool insert( value_type& val, Func f )
291         {
292             bool bRet = bucket( val ).insert( val, f );
293             if ( bRet )
294                 ++m_ItemCounter;
295             return bRet;
296         }
297
298         /// Updates the element
299         /**
300             The operation performs inserting or changing data with lock-free manner.
301
302             If the item \p val not found in the set, then \p val is inserted iff \p bAllowInsert is \p true.
303             Otherwise, the functor \p func is called with item found.
304             The functor signature is:
305             \code
306                 struct functor {
307                     void operator()( bool bNew, value_type& item, value_type& val );
308                 };
309             \endcode
310             with arguments:
311             - \p bNew - \p true if the item has been inserted, \p false otherwise
312             - \p item - item of the set
313             - \p val - argument \p val passed into the \p %update() function
314             If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
315             refers to the same thing.
316
317             The functor may change non-key fields of the \p item.
318
319             Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successfull,
320             \p second is \p true if new item has been added or \p false if the item with \p key
321             already is in the set.
322
323             @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
324             \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
325             synchronization.
326         */
327         template <typename Func>
328         std::pair<bool, bool> update( value_type& val, Func func, bool bAllowInsert = true )
329         {
330             std::pair<bool, bool> bRet = bucket( val ).update( val, func, bAllowInsert );
331             if ( bRet.second )
332                 ++m_ItemCounter;
333             return bRet;
334         }
335         //@cond
336         template <typename Func>
337         CDS_DEPRECATED("ensure() is deprecated, use update()")
338         std::pair<bool, bool> ensure( value_type& val, Func func )
339         {
340             return update( val, func, true );
341         }
342         //@endcond
343
344         /// Unlinks the item \p val from the set
345         /**
346             The function searches the item \p val in the set and unlink it from the set
347             if it is found and is equal to \p val.
348
349             The function returns \p true if success and \p false otherwise.
350         */
351         bool unlink( value_type& val )
352         {
353             bool bRet = bucket( val ).unlink( val );
354             if ( bRet )
355                 --m_ItemCounter;
356             return bRet;
357         }
358
359         /// Deletes the item from the set
360         /** \anchor cds_intrusive_MichaelHashSet_rcu_erase
361             The function searches an item with key equal to \p key in the set,
362             unlinks it from the set, and returns \p true.
363             If the item with key equal to \p key is not found the function return \p false.
364
365             Note the hash functor should accept a parameter of type \p Q that may be not the same as \p value_type.
366         */
367         template <typename Q>
368         bool erase( Q const& key )
369         {
370             if ( bucket( key ).erase( key )) {
371                 --m_ItemCounter;
372                 return true;
373             }
374             return false;
375         }
376
377         /// Deletes the item from the set using \p pred predicate for searching
378         /**
379             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_erase "erase(Q const&)"
380             but \p pred is used for key comparing.
381             \p Less functor has the interface like \p std::less.
382             \p pred must imply the same element order as the comparator used for building the set.
383         */
384         template <typename Q, typename Less>
385         bool erase_with( Q const& key, Less pred )
386         {
387             if ( bucket( key ).erase_with( key, pred )) {
388                 --m_ItemCounter;
389                 return true;
390             }
391             return false;
392         }
393
394         /// Deletes the item from the set
395         /** \anchor cds_intrusive_MichaelHashSet_rcu_erase_func
396             The function searches an item with key equal to \p key in the set,
397             call \p f functor with item found, and unlinks it from the set.
398             The \ref disposer specified in \p OrderedList class template parameter is called
399             by garbage collector \p GC asynchronously.
400
401             The \p Func interface is
402             \code
403             struct functor {
404                 void operator()( value_type const& item );
405             };
406             \endcode
407
408             If the item with key equal to \p key is not found the function return \p false.
409
410             Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
411         */
412         template <typename Q, typename Func>
413         bool erase( const Q& key, Func f )
414         {
415             if ( bucket( key ).erase( key, f )) {
416                 --m_ItemCounter;
417                 return true;
418             }
419             return false;
420         }
421
422         /// Deletes the item from the set using \p pred predicate for searching
423         /**
424             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_erase_func "erase(Q const&)"
425             but \p pred is used for key comparing.
426             \p Less functor has the interface like \p std::less.
427             \p pred must imply the same element order as the comparator used for building the set.
428         */
429         template <typename Q, typename Less, typename Func>
430         bool erase_with( const Q& key, Less pred, Func f )
431         {
432             if ( bucket( key ).erase_with( key, pred, f )) {
433                 --m_ItemCounter;
434                 return true;
435             }
436             return false;
437         }
438
439         /// Extracts an item from the set
440         /** \anchor cds_intrusive_MichaelHashSet_rcu_extract
441             The function searches an item with key equal to \p key in the set,
442             unlinks it from the set, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
443             If the item with the key equal to \p key is not found the function returns an empty \p exempt_ptr.
444
445             Depends on \p bucket_type you should or should not lock RCU before calling of this function:
446             - for the set based on \ref cds_intrusive_MichaelList_rcu "MichaelList" RCU should not be locked
447             - for the set based on \ref cds_intrusive_LazyList_rcu "LazyList" RCU should be locked
448
449             See ordered list implementation for details.
450
451             \code
452             #include <cds/urcu/general_buffered.h>
453             #include <cds/intrusive/michael_list_rcu.h>
454             #include <cds/intrusive/michael_set_rcu.h>
455
456             typedef cds::urcu::gc< general_buffered<> > rcu;
457             typedef cds::intrusive::MichaelList< rcu, Foo > rcu_michael_list;
458             typedef cds::intrusive::MichaelHashSet< rcu, rcu_michael_list, foo_traits > rcu_michael_set;
459
460             rcu_michael_set theSet;
461             // ...
462
463             typename rcu_michael_set::exempt_ptr p;
464
465             // For MichaelList we should not lock RCU
466
467             // Now, you can apply extract function
468             // Note that you must not delete the item found inside the RCU lock
469             p = theSet.extract( 10 )
470             if ( p ) {
471                 // do something with p
472                 ...
473             }
474
475             // We may safely release p here
476             // release() passes the pointer to RCU reclamation cycle:
477             // it invokes RCU retire_ptr function with the disposer you provided for rcu_michael_list.
478             p.release();
479             \endcode
480         */
481         template <typename Q>
482         exempt_ptr extract( Q const& key )
483         {
484             exempt_ptr p( bucket( key ).extract( key ) );
485             if ( p )
486                 --m_ItemCounter;
487             return p;
488         }
489
490         /// Extracts an item from the set using \p pred predicate for searching
491         /**
492             The function is an analog of \p extract(Q const&) but \p pred is used for key comparing.
493             \p Less functor has the interface like \p std::less.
494             \p pred must imply the same element order as the comparator used for building the set.
495         */
496         template <typename Q, typename Less>
497         exempt_ptr extract_with( Q const& key, Less pred )
498         {
499             exempt_ptr p( bucket( key ).extract_with( key, pred ) );
500             if ( p )
501                 --m_ItemCounter;
502             return p;
503         }
504
505         /// Checks whether the set contains \p key
506         /**
507
508             The function searches the item with key equal to \p key
509             and returns \p true if the key is found, and \p false otherwise.
510
511             Note the hash functor specified for class \p Traits template parameter
512             should accept a parameter of type \p Q that can be not the same as \p value_type.
513         */
514         template <typename Q>
515         bool contains( Q const& key )
516         {
517             return bucket( key ).contains( key );
518         }
519         //@cond
520         template <typename Q>
521         CDS_DEPRECATED("use contains()")
522         bool find( Q const& key )
523         {
524             return contains( key );
525         }
526         //@endcond
527
528         /// Checks whether the set contains \p key using \p pred predicate for searching
529         /**
530             The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
531             \p Less functor has the interface like \p std::less.
532             \p Less must imply the same element order as the comparator used for building the set.
533         */
534         template <typename Q, typename Less>
535         bool contains( Q const& key, Less pred )
536         {
537             return bucket( key ).contains( key, pred );
538         }
539         //@cond
540         template <typename Q, typename Less>
541         CDS_DEPRECATED("use contains()")
542         bool find_with( Q const& key, Less pred )
543         {
544             return contains( key, pred );
545         }
546         //@endcond
547
548         /// Find the key \p key
549         /** \anchor cds_intrusive_MichaelHashSet_rcu_find_func
550             The function searches the item with key equal to \p key and calls the functor \p f for item found.
551             The interface of \p Func functor is:
552             \code
553             struct functor {
554                 void operator()( value_type& item, Q& key );
555             };
556             \endcode
557             where \p item is the item found, \p key is the <tt>find</tt> function argument.
558
559             The functor can change non-key fields of \p item.
560             The functor does not serialize simultaneous access to the set \p item. If such access is
561             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
562
563             The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
564             can modify both arguments.
565
566             Note the hash functor specified for class \p Traits template parameter
567             should accept a parameter of type \p Q that can be not the same as \p value_type.
568
569             The function returns \p true if \p key is found, \p false otherwise.
570         */
571         template <typename Q, typename Func>
572         bool find( Q& key, Func f )
573         {
574             return bucket( key ).find( key, f );
575         }
576         //@cond
577         template <typename Q, typename Func>
578         bool find( Q const& key, Func f )
579         {
580             return bucket( key ).find( key, f );
581         }
582         //@endcond
583
584         /// Finds the key \p key using \p pred predicate for searching
585         /**
586             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_find_func "find(Q&, Func)"
587             but \p pred is used for key comparing.
588             \p Less functor has the interface like \p std::less.
589             \p pred must imply the same element order as the comparator used for building the set.
590         */
591         template <typename Q, typename Less, typename Func>
592         bool find_with( Q& key, Less pred, Func f )
593         {
594             return bucket( key ).find_with( key, pred, f );
595         }
596         //@cond
597         template <typename Q, typename Less, typename Func>
598         bool find_with( Q const& key, Less pred, Func f )
599         {
600             return bucket( key ).find_with( key, pred, f );
601         }
602         //@endcond
603
604         /// Finds the key \p key and return the item found
605         /** \anchor cds_intrusive_MichaelHashSet_rcu_get
606             The function searches the item with key equal to \p key and returns the pointer to item found.
607             If \p key is not found it returns \p nullptr.
608             Note the type of returned value depends on underlying \p bucket_type.
609             For details, see documentation of ordered list you use.
610
611             Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
612
613             RCU should be locked before call of this function.
614             Returned item is valid only while RCU is locked:
615             \code
616             typedef cds::intrusive::MichaelHashSet< your_template_parameters > hash_set;
617             hash_set theSet;
618             // ...
619             // Result of get() call
620             typename hash_set::raw_ptr ptr;
621             {
622                 // Lock RCU
623                 hash_set::rcu_lock lock;
624
625                 ptr = theSet.get( 5 );
626                 if ( ptr ) {
627                     // Deal with ptr
628                     //...
629                 }
630                 // Unlock RCU by rcu_lock destructor
631                 // ptr can be reclaimed by disposer at any time after RCU has been unlocked
632             }
633             \endcode
634         */
635         template <typename Q>
636         raw_ptr get( Q const& key )
637         {
638             return bucket( key ).get( key );
639         }
640
641         /// Finds the key \p key and return the item found
642         /**
643             The function is an analog of \ref cds_intrusive_MichaelHashSet_rcu_get "get(Q const&)"
644             but \p pred is used for comparing the keys.
645
646             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
647             in any order.
648             \p pred must imply the same element order as the comparator used for building the set.
649         */
650         template <typename Q, typename Less>
651         raw_ptr get_with( Q const& key, Less pred )
652         {
653             return bucket( key ).get_with( key, pred );
654         }
655
656         /// Clears the set (non-atomic)
657         /**
658             The function unlink all items from the set.
659             The function is not atomic. It cleans up each bucket and then resets the item counter to zero.
660             If there are a thread that performs insertion while \p clear is working the result is undefined in general case:
661             <tt> empty() </tt> may return \p true but the set may contain item(s).
662             Therefore, \p clear may be used only for debugging purposes.
663
664             For each item the \p disposer is called after unlinking.
665         */
666         void clear()
667         {
668             for ( size_t i = 0; i < bucket_count(); ++i )
669                 m_Buckets[i].clear();
670             m_ItemCounter.reset();
671         }
672
673
674         /// Checks if the set is empty
675         /**
676             Emptiness is checked by item counting: if item count is zero then the set is empty.
677             Thus, the correct item counting feature is an important part of Michael's set implementation.
678         */
679         bool empty() const
680         {
681             return size() == 0;
682         }
683
684         /// Returns item count in the set
685         size_t size() const
686         {
687             return m_ItemCounter;
688         }
689
690         /// Returns the size of hash table
691         /**
692             Since %MichaelHashSet cannot dynamically extend the hash table size,
693             the value returned is an constant depending on object initialization parameters;
694             see \ref cds_intrusive_MichaelHashSet_hp "MichaelHashSet" for explanation.
695         */
696         size_t bucket_count() const
697         {
698             return m_nHashBitmask + 1;
699         }
700
701     };
702
703 }} // namespace cds::intrusive
704
705 #endif // #ifndef CDSLIB_INTRUSIVE_MICHAEL_SET_NOGC_H
706