2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSLIB_INTRUSIVE_MICHAEL_SET_H
32 #define CDSLIB_INTRUSIVE_MICHAEL_SET_H
34 #include <cds/intrusive/details/michael_set_base.h>
35 #include <cds/intrusive/details/iterable_list_base.h>
37 namespace cds { namespace intrusive {
39 /// Michael's hash set
40 /** @ingroup cds_intrusive_map
41 \anchor cds_intrusive_MichaelHashSet_hp
44 - [2002] Maged Michael "High performance dynamic lock-free hash tables and list-based sets"
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.
51 Template parameters are:
52 - \p GC - Garbage collector used. Note the \p GC must be the same as the GC used for \p OrderedList
53 - \p OrderedList - ordered list implementation used as bucket for hash set, possible implementations:
54 \p MichaelList, \p LazyList, \p IterableList.
55 The intrusive ordered list implementation specifies the type \p T stored in the hash-set, the reclamation
56 schema \p GC used by hash-set, the comparison functor for the type \p T and other features specific for
58 - \p Traits - type traits. See \p michael_set::traits for explanation.
59 Instead of defining \p Traits struct you can use option-based syntax with \p michael_set::make_traits metafunction.
61 There are several specializations of \p %MichaelHashSet for each GC. You should include:
62 - <tt><cds/intrusive/michael_set_rcu.h></tt> for \ref cds_intrusive_MichaelHashSet_rcu "RCU type"
63 - <tt><cds/intrusive/michael_set_nogc.h></tt> for \ref cds_intrusive_MichaelHashSet_nogc for append-only set
64 - <tt><cds/intrusive/michael_set.h></tt> for \p gc::HP, \p gc::DHP
68 Some member functions of Michael's hash set accept the key parameter of type \p Q which differs from \p value_type.
69 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
70 the hash values of these keys must be equal.
71 The hash functor \p Traits::hash should accept parameters of both type:
75 std::string key_; // key field
81 size_t operator()( const std::string& s ) const
83 return std::hash( s );
86 size_t operator()( const Foo& f ) const
88 return (*this)( f.key_ );
95 First, you should define ordered list type to use in your hash set:
97 // For gc::HP-based MichaelList implementation
98 #include <cds/intrusive/michael_list_hp.h>
100 // cds::intrusive::MichaelHashSet declaration
101 #include <cds/intrusive/michael_set.h>
103 // Type of hash-set items
104 struct Foo: public cds::intrusive::michael_list::node< cds::gc::HP >
106 std::string key_ ; // key field
107 unsigned val_ ; // value field
108 // ... other value fields
111 // Declare comparator for the item
114 int operator()( const Foo& f1, const Foo& f2 ) const
116 return f1.key_.compare( f2.key_ );
120 // Declare bucket type for Michael's hash set
121 // The bucket type is any ordered list type like MichaelList, LazyList
122 typedef cds::intrusive::MichaelList< cds::gc::HP, Foo,
123 typename cds::intrusive::michael_list::make_traits<
125 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::HP > > >
126 // item comparator option
127 ,cds::opt::compare< FooCmp >
132 Second, you should declare Michael's hash set container:
135 // Declare hash functor
136 // Note, the hash functor accepts parameter type Foo and std::string
138 size_t operator()( const Foo& f ) const
140 return cds::opt::v::hash<std::string>()( f.key_ );
142 size_t operator()( const std::string& f ) const
144 return cds::opt::v::hash<std::string>()( f );
148 // Michael's set typedef
149 typedef cds::intrusive::MichaelHashSet<
152 ,typename cds::intrusive::michael_set::make_traits<
153 cds::opt::hash< FooHash >
158 Now, you can use \p Foo_set in your application.
160 Like other intrusive containers, you may build several containers on single item structure:
162 #include <cds/intrusive/michael_list_hp.h>
163 #include <cds/intrusive/michael_list_dhp.h>
164 #include <cds/intrusive/michael_set.h>
170 // The first key is maintained by gc::HP, second key is maintained by gc::DHP garbage collectors
171 // (I don't know what is needed for, but it is correct)
173 : public cds::intrusive::michael_list::node< cds::gc::HP, tag_key1_idx >
174 , public cds::intrusive::michael_list::node< cds::gc::DHP, tag_key2_idx >
176 std::string key1_ ; // first key field
177 unsigned int key2_ ; // second key field
179 // ... value fields and fields for controlling item's lifetime
182 // Declare comparators for the item
185 int operator()( const Foo& f1, const Foo& f2 ) const { return f1.key1_.compare( f2.key1_ ) ; }
189 bool operator()( const Foo& f1, const Foo& f2 ) const { return f1.key2_ < f2.key1_ ; }
192 // Declare bucket type for Michael's hash set indexed by key1_ field and maintained by gc::HP
193 typedef cds::intrusive::MichaelList< cds::gc::HP, Foo,
194 typename cds::intrusive::michael_list::make_traits<
196 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::HP >, tag_key1_idx > >
197 // item comparator option
198 ,cds::opt::compare< Key1Cmp >
202 // Declare bucket type for Michael's hash set indexed by key2_ field and maintained by gc::DHP
203 typedef cds::intrusive::MichaelList< cds::gc::DHP, Foo,
204 typename cds::intrusive::michael_list::make_traits<
206 cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::DHP >, tag_key2_idx > >
207 // item comparator option
208 ,cds::opt::less< Key2Less >
212 // Declare hash functor
214 size_t operator()( const Foo& f ) const { return cds::opt::v::hash<std::string>()( f.key1_ ) ; }
215 size_t operator()( const std::string& s ) const { return cds::opt::v::hash<std::string>()( s ) ; }
217 inline size_t Key2Hash( const Foo& f ) { return (size_t) f.key2_ ; }
219 // Michael's set indexed by key1_ field
220 typedef cds::intrusive::MichaelHashSet<
223 ,typename cds::intrusive::michael_set::make_traits<
224 cds::opt::hash< Key1Hash >
228 // Michael's set indexed by key2_ field
229 typedef cds::intrusive::MichaelHashSet<
232 ,typename cds::intrusive::michael_set::make_traits<
233 cds::opt::hash< Key2Hash >
241 #ifdef CDS_DOXYGEN_INVOKED
242 class Traits = michael_set::traits
250 typedef GC gc; ///< Garbage collector
251 typedef OrderedList ordered_list; ///< type of ordered list used as a bucket implementation
252 typedef Traits traits; ///< Set traits
254 typedef typename ordered_list::value_type value_type ; ///< type of value to be stored in the set
255 typedef typename ordered_list::key_comparator key_comparator ; ///< key comparing functor
256 typedef typename ordered_list::disposer disposer ; ///< Node disposer functor
257 typedef typename ordered_list::stat stat ; ///< Internal statistics
259 /// Hash functor for \p value_type and all its derivatives that you use
260 typedef typename cds::opt::v::hash_selector< typename traits::hash >::type hash;
261 typedef typename traits::item_counter item_counter; ///< Item counter type
262 typedef typename traits::allocator allocator; ///< Bucket table allocator
264 typedef typename ordered_list::guarded_ptr guarded_ptr; ///< Guarded pointer
266 /// Count of hazard pointer required for the algorithm
267 static CDS_CONSTEXPR const size_t c_nHazardPtrCount = ordered_list::c_nHazardPtrCount;
269 // GC and OrderedList::gc must be the same
270 static_assert(std::is_same<gc, typename ordered_list::gc>::value, "GC and OrderedList::gc must be the same");
272 // atomicity::empty_item_counter is not allowed as a item counter
273 static_assert(!std::is_same<item_counter, atomicity::empty_item_counter>::value,
274 "cds::atomicity::empty_item_counter is not allowed as a item counter");
278 typedef typename ordered_list::template select_stat_wrapper< typename ordered_list::stat > bucket_stat;
280 typedef typename ordered_list::template rebind_traits<
281 cds::opt::item_counter< cds::atomicity::empty_item_counter >
282 , cds::opt::stat< typename bucket_stat::wrapped_stat >
283 >::type internal_bucket_type;
285 typedef typename allocator::template rebind< internal_bucket_type >::other bucket_table_allocator;
287 hash m_HashFunctor; ///< Hash functor
288 size_t const m_nHashBitmask;
289 internal_bucket_type* m_Buckets; ///< bucket table
290 item_counter m_ItemCounter; ///< Item counter
291 typename bucket_stat::stat m_Stat; ///< Internal statistics
295 ///@name Forward iterators
299 The forward iterator for Michael's set is based on \p OrderedList forward iterator and has some features:
300 - it has no post-increment operator
301 - it iterates items in unordered fashion
302 - The iterator cannot be moved across thread boundary because it may contain GC's guard that is thread-private GC data.
304 Iterator thread safety depends on type of \p OrderedList:
305 - for \p MichaelList and \p LazyList: iterator guarantees safety even if you delete the item that iterator points to
306 because that item is guarded by hazard pointer.
307 However, in case of concurrent deleting operations it is no guarantee that you iterate all item in the set.
308 Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread.
309 Use this iterator on the concurrent container for debugging purpose only.
310 - for \p IterableList: iterator is thread-safe. You may use it freely in concurrent environment.
313 typedef michael_set::details::iterator< internal_bucket_type, false > iterator;
315 /// Const forward iterator
317 For iterator's features and requirements see \ref iterator
319 typedef michael_set::details::iterator< internal_bucket_type, true > const_iterator;
321 /// Returns a forward iterator addressing the first element in a set
323 For empty set \code begin() == end() \endcode
327 return iterator( m_Buckets[0].begin(), bucket_begin(), bucket_end() );
330 /// Returns an iterator that addresses the location succeeding the last element in a set
332 Do not use the value returned by <tt>end</tt> function to access any item.
333 The returned value can be used only to control reaching the end of the set.
334 For empty set \code begin() == end() \endcode
338 return iterator( bucket_end()[-1].end(), bucket_end() - 1, bucket_end() );
341 /// Returns a forward const iterator addressing the first element in a set
342 const_iterator begin() const
344 return get_const_begin();
347 /// Returns a forward const iterator addressing the first element in a set
348 const_iterator cbegin() const
350 return get_const_begin();
353 /// Returns an const iterator that addresses the location succeeding the last element in a set
354 const_iterator end() const
356 return get_const_end();
359 /// Returns an const iterator that addresses the location succeeding the last element in a set
360 const_iterator cend() const
362 return get_const_end();
367 /// Initializes hash set
369 The Michael's hash set is an unbounded container, but its hash table is non-expandable.
370 At construction time you should pass estimated maximum item count and a load factor.
371 The load factor is average size of one bucket - a small number between 1 and 10.
372 The bucket is an ordered single-linked list, searching in the bucket has linear complexity <tt>O(nLoadFactor)</tt>.
373 The constructor defines hash table size as rounding <tt>nMaxItemCount / nLoadFactor</tt> up to nearest power of two.
376 size_t nMaxItemCount, ///< estimation of max item count in the hash set
377 size_t nLoadFactor ///< load factor: estimation of max number of items in the bucket. Small integer up to 10.
378 ) : m_nHashBitmask( michael_set::details::init_hash_bitmask( nMaxItemCount, nLoadFactor ))
379 , m_Buckets( bucket_table_allocator().allocate( bucket_count()))
381 for ( auto it = m_Buckets, itEnd = m_Buckets + bucket_count(); it != itEnd; ++it )
382 construct_bucket<bucket_stat>( it );
385 /// Clears hash set object and destroys it
390 for ( auto it = m_Buckets, itEnd = m_Buckets + bucket_count(); it != itEnd; ++it )
391 it->~internal_bucket_type();
392 bucket_table_allocator().deallocate( m_Buckets, bucket_count() );
397 The function inserts \p val in the set if it does not contain
398 an item with key equal to \p val.
400 Returns \p true if \p val is placed into the set, \p false otherwise.
402 bool insert( value_type& val )
404 bool bRet = bucket( val ).insert( val );
412 This function is intended for derived non-intrusive containers.
414 The function allows to split creating of new item into two part:
415 - create item with key only
416 - insert new item into the set
417 - if inserting is success, calls \p f functor to initialize value-field of \p val.
419 The functor signature is:
421 void func( value_type& val );
423 where \p val is the item inserted.
425 The user-defined functor is called only if the inserting is success.
427 @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
428 \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
431 template <typename Func>
432 bool insert( value_type& val, Func f )
434 bool bRet = bucket( val ).insert( val, f );
440 /// Updates the element
442 The operation performs inserting or changing data with lock-free manner.
444 If the item \p val not found in the set, then \p val is inserted iff \p bAllowInsert is \p true.
445 Otherwise, the functor \p func is called with item found.
447 The functor signature depends of the type of \p OrderedList:
449 <b>for \p MichaelList, \p LazyList</b>
452 void operator()( bool bNew, value_type& item, value_type& val );
456 - \p bNew - \p true if the item has been inserted, \p false otherwise
457 - \p item - item of the set
458 - \p val - argument \p val passed into the \p %update() function
459 If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
460 refers to the same thing.
462 The functor may change non-key fields of the \p item.
463 @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
464 \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
467 <b>for \p IterableList</b>
469 void func( value_type& val, value_type * old );
472 - \p val - argument \p val passed into the \p %update() function
473 - \p old - old value that will be retired. If new item has been inserted then \p old is \p nullptr.
475 Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successful,
476 \p second is \p true if new item has been added or \p false if the item with \p key
477 already is in the set.
479 template <typename Func>
480 std::pair<bool, bool> update( value_type& val, Func func, bool bAllowInsert = true )
482 std::pair<bool, bool> bRet = bucket( val ).update( val, func, bAllowInsert );
488 template <typename Func>
489 CDS_DEPRECATED("ensure() is deprecated, use update()")
490 std::pair<bool, bool> ensure( value_type& val, Func func )
492 return update( val, func, true );
496 /// Inserts or updates the node (only for \p IterableList)
498 The operation performs inserting or changing data with lock-free manner.
500 If the item \p val is not found in the set, then \p val is inserted iff \p bAllowInsert is \p true.
501 Otherwise, the current element is changed to \p val, the old element will be retired later
502 by call \p Traits::disposer.
504 Returns std::pair<bool, bool> where \p first is \p true if operation is successful,
505 \p second is \p true if \p val has been added or \p false if the item with that key
508 #ifdef CDS_DOXYGEN_INVOKED
509 std::pair<bool, bool> upsert( value_type& val, bool bAllowInsert = true )
511 template <typename Q>
512 typename std::enable_if<
513 std::is_same< Q, value_type>::value && is_iterable_list< ordered_list >::value,
514 std::pair<bool, bool>
516 upsert( Q& val, bool bAllowInsert = true )
519 std::pair<bool, bool> bRet = bucket( val ).upsert( val, bAllowInsert );
525 /// Unlinks the item \p val from the set
527 The function searches the item \p val in the set and unlink it
528 if it is found and is equal to \p val.
530 The function returns \p true if success and \p false otherwise.
532 bool unlink( value_type& val )
534 bool bRet = bucket( val ).unlink( val );
540 /// Deletes the item from the set
541 /** \anchor cds_intrusive_MichaelHashSet_hp_erase
542 The function searches an item with key equal to \p key in the set,
543 unlinks it, and returns \p true.
544 If the item with key equal to \p key is not found the function return \p false.
546 Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
548 template <typename Q>
549 bool erase( Q const& key )
551 if ( bucket( key ).erase( key )) {
558 /// Deletes the item from the set using \p pred predicate for searching
560 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_erase "erase(Q const&)"
561 but \p pred is used for key comparing.
562 \p Less functor has the interface like \p std::less.
563 \p pred must imply the same element order as the comparator used for building the set.
565 template <typename Q, typename Less>
566 bool erase_with( Q const& key, Less pred )
568 if ( bucket( key ).erase_with( key, pred )) {
575 /// Deletes the item from the set
576 /** \anchor cds_intrusive_MichaelHashSet_hp_erase_func
577 The function searches an item with key equal to \p key in the set,
578 call \p f functor with item found, and unlinks it from the set.
579 The \ref disposer specified in \p OrderedList class template parameter is called
580 by garbage collector \p GC asynchronously.
582 The \p Func interface is
585 void operator()( value_type const& item );
589 If the item with key equal to \p key is not found the function return \p false.
591 Note the hash functor should accept a parameter of type \p Q that can be not the same as \p value_type.
593 template <typename Q, typename Func>
594 bool erase( Q const& key, Func f )
596 if ( bucket( key ).erase( key, f )) {
603 /// Deletes the item from the set using \p pred predicate for searching
605 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_erase_func "erase(Q const&, Func)"
606 but \p pred is used for key comparing.
607 \p Less functor has the interface like \p std::less.
608 \p pred must imply the same element order as the comparator used for building the set.
610 template <typename Q, typename Less, typename Func>
611 bool erase_with( Q const& key, Less pred, Func f )
613 if ( bucket( key ).erase_with( key, pred, f )) {
620 /// Extracts the item with specified \p key
621 /** \anchor cds_intrusive_MichaelHashSet_hp_extract
622 The function searches an item with key equal to \p key,
623 unlinks it from the set, and returns an guarded pointer to the item extracted.
624 If \p key is not found the function returns an empty guarded pointer.
626 Note the compare functor should accept a parameter of type \p Q that may be not the same as \p value_type.
628 The \p disposer specified in \p OrderedList class' template parameter is called automatically
629 by garbage collector \p GC when returned \ref guarded_ptr object will be destroyed or released.
630 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
634 typedef cds::intrusive::MichaelHashSet< your_template_args > michael_set;
638 michael_set::guarded_ptr gp( theSet.extract( 5 ));
643 // Destructor of gp releases internal HP guard
647 template <typename Q>
648 guarded_ptr extract( Q const& key )
650 guarded_ptr gp = bucket( key ).extract( key );
656 /// Extracts the item using compare functor \p pred
658 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_extract "extract(Q const&)"
659 but \p pred predicate is used for key comparing.
661 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
663 \p pred must imply the same element order as the comparator used for building the list.
665 template <typename Q, typename Less>
666 guarded_ptr extract_with( Q const& key, Less pred )
668 guarded_ptr gp = bucket( key ).extract_with( key, pred );
674 /// Finds the key \p key
675 /** \anchor cds_intrusive_MichaelHashSet_hp_find_func
676 The function searches the item with key equal to \p key and calls the functor \p f for item found.
677 The interface of \p Func functor is:
680 void operator()( value_type& item, Q& key );
683 where \p item is the item found, \p key is the <tt>find</tt> function argument.
685 The functor may change non-key fields of \p item. Note that the functor is only guarantee
686 that \p item cannot be disposed during functor is executing.
687 The functor does not serialize simultaneous access to the set \p item. If such access is
688 possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
690 The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
691 may modify both arguments.
693 Note the hash functor specified for class \p Traits template parameter
694 should accept a parameter of type \p Q that can be not the same as \p value_type.
696 The function returns \p true if \p key is found, \p false otherwise.
698 template <typename Q, typename Func>
699 bool find( Q& key, Func f )
701 return bucket( key ).find( key, f );
704 template <typename Q, typename Func>
705 bool find( Q const& key, Func f )
707 return bucket( key ).find( key, f );
711 /// Finds \p key and returns iterator pointed to the item found (only for \p IterableList)
713 If \p key is not found the function returns \p end().
715 @note This function is supported only for the set based on \p IterableList
717 template <typename Q>
718 #ifdef CDS_DOXYGEN_INVOKED
721 typename std::enable_if< std::is_same<Q,Q>::value && is_iterable_list< ordered_list >::value, iterator >::type
725 internal_bucket_type& b = bucket( key );
726 typename internal_bucket_type::iterator it = b.find( key );
729 return iterator( it, &b, bucket_end());
732 template <typename Q>
733 typename std::enable_if< std::is_same<Q, Q>::value && is_iterable_list< ordered_list >::value, iterator >::type
736 internal_bucket_type& b = bucket( key );
737 typename internal_bucket_type::iterator it = b.find( key );
740 return iterator( it, &b, bucket_end() );
745 /// Finds the key \p key using \p pred predicate for searching
747 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_find_func "find(Q&, Func)"
748 but \p pred is used for key comparing.
749 \p Less functor has the interface like \p std::less.
750 \p pred must imply the same element order as the comparator used for building the set.
752 template <typename Q, typename Less, typename Func>
753 bool find_with( Q& key, Less pred, Func f )
755 return bucket( key ).find_with( key, pred, f );
758 template <typename Q, typename Less, typename Func>
759 bool find_with( Q const& key, Less pred, Func f )
761 return bucket( key ).find_with( key, pred, f );
765 /// Finds \p key using \p pred predicate and returns iterator pointed to the item found (only for \p IterableList)
767 The function is an analog of \p find(Q&) but \p pred is used for key comparing.
768 \p Less functor has the interface like \p std::less.
769 \p pred must imply the same element order as the comparator used for building the set.
771 If \p key is not found the function returns \p end().
773 @note This function is supported only for the set based on \p IterableList
775 template <typename Q, typename Less>
776 #ifdef CDS_DOXYGEN_INVOKED
779 typename std::enable_if< std::is_same<Q, Q>::value && is_iterable_list< ordered_list >::value, iterator >::type
781 find_with( Q& key, Less pred )
783 internal_bucket_type& b = bucket( key );
784 typename internal_bucket_type::iterator it = b.find_with( key, pred );
787 return iterator( it, &b, bucket_end() );
790 template <typename Q, typename Less>
791 typename std::enable_if< std::is_same<Q, Q>::value && is_iterable_list< ordered_list >::value, iterator >::type
792 find_with( Q const& key, Less pred )
794 internal_bucket_type& b = bucket( key );
795 typename internal_bucket_type::iterator it = b.find_with( key, pred );
798 return iterator( it, &b, bucket_end() );
802 /// Checks whether the set contains \p key
805 The function searches the item with key equal to \p key
806 and returns \p true if the key is found, and \p false otherwise.
808 Note the hash functor specified for class \p Traits template parameter
809 should accept a parameter of type \p Q that can be not the same as \p value_type.
811 template <typename Q>
812 bool contains( Q const& key )
814 return bucket( key ).contains( key );
817 /// Checks whether the set contains \p key using \p pred predicate for searching
819 The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
820 \p Less functor has the interface like \p std::less.
821 \p Less must imply the same element order as the comparator used for building the set.
823 template <typename Q, typename Less>
824 bool contains( Q const& key, Less pred )
826 return bucket( key ).contains( key, pred );
829 /// Finds the key \p key and return the item found
830 /** \anchor cds_intrusive_MichaelHashSet_hp_get
831 The function searches the item with key equal to \p key
832 and returns the guarded pointer to the item found.
833 If \p key is not found the function returns an empty \p guarded_ptr.
835 @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
839 typedef cds::intrusive::MichaelHashSet< your_template_params > michael_set;
843 michael_set::guarded_ptr gp( theSet.get( 5 ));
844 if ( theSet.get( 5 )) {
848 // Destructor of guarded_ptr releases internal HP guard
852 Note the compare functor specified for \p OrderedList template parameter
853 should accept a parameter of type \p Q that can be not the same as \p value_type.
855 template <typename Q>
856 guarded_ptr get( Q const& key )
858 return bucket( key ).get( key );
861 /// Finds the key \p key and return the item found
863 The function is an analog of \ref cds_intrusive_MichaelHashSet_hp_get "get( Q const&)"
864 but \p pred is used for comparing the keys.
866 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
868 \p pred must imply the same element order as the comparator used for building the set.
870 template <typename Q, typename Less>
871 guarded_ptr get_with( Q const& key, Less pred )
873 return bucket( key ).get_with( key, pred );
876 /// Clears the set (non-atomic)
878 The function unlink all items from the set.
879 The function is not atomic. It cleans up each bucket and then resets the item counter to zero.
880 If there are a thread that performs insertion while \p %clear() is working the result is undefined in general case:
881 \p empty() may return \p true but the set may contain item(s).
882 Therefore, \p %clear() may be used only for debugging purposes.
884 For each item the \p disposer is called after unlinking.
888 for ( size_t i = 0; i < bucket_count(); ++i )
889 m_Buckets[i].clear();
890 m_ItemCounter.reset();
893 /// Checks if the set is empty
895 Emptiness is checked by item counting: if item count is zero then the set is empty.
896 Thus, the correct item counting feature is an important part of Michael's set implementation.
903 /// Returns item count in the set
906 return m_ItemCounter;
909 /// Returns const reference to internal statistics
910 stat const& statistics() const
915 /// Returns the size of hash table
917 Since \p %MichaelHashSet cannot dynamically extend the hash table size,
918 the value returned is an constant depending on object initialization parameters,
919 see \p MichaelHashSet::MichaelHashSet.
921 size_t bucket_count() const
923 return m_nHashBitmask + 1;
928 internal_bucket_type * bucket_begin() const
933 internal_bucket_type * bucket_end() const
935 return m_Buckets + bucket_count();
938 const_iterator get_const_begin() const
940 return const_iterator( m_Buckets[0].cbegin(), bucket_begin(), bucket_end() );
942 const_iterator get_const_end() const
944 return const_iterator( bucket_end()[-1].cend(), bucket_end() - 1, bucket_end() );
947 template <typename Stat>
948 typename std::enable_if< Stat::empty >::type construct_bucket( internal_bucket_type * bucket )
950 new (bucket) internal_bucket_type;
953 template <typename Stat>
954 typename std::enable_if< !Stat::empty >::type construct_bucket( internal_bucket_type * bucket )
956 new (bucket) internal_bucket_type( m_Stat );
959 /// Calculates hash value of \p key
960 template <typename Q>
961 size_t hash_value( const Q& key ) const
963 return m_HashFunctor( key ) & m_nHashBitmask;
966 /// Returns the bucket (ordered list) for \p key
967 template <typename Q>
968 internal_bucket_type& bucket( const Q& key )
970 return m_Buckets[hash_value( key )];
975 }} // namespace cds::intrusive
977 #endif // ifndef CDSLIB_INTRUSIVE_MICHAEL_SET_H