SplitListMap, SplitListSet:
[libcds.git] / cds / container / split_list_set.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_CONTAINER_SPLIT_LIST_SET_H
4 #define CDSLIB_CONTAINER_SPLIT_LIST_SET_H
5
6 #include <cds/intrusive/split_list.h>
7 #include <cds/container/details/make_split_list_set.h>
8
9 namespace cds { namespace container {
10
11     /// Split-ordered list set
12     /** @ingroup cds_nonintrusive_set
13         \anchor cds_nonintrusive_SplitListSet_hp
14
15         Hash table implementation based on split-ordered list algorithm discovered by Ori Shalev and Nir Shavit, see
16         - [2003] Ori Shalev, Nir Shavit "Split-Ordered Lists - Lock-free Resizable Hash Tables"
17         - [2008] Nir Shavit "The Art of Multiprocessor Programming"
18
19         See \p intrusive::SplitListSet for a brief description of the split-list algorithm.
20
21         Template parameters:
22         - \p GC - Garbage collector used
23         - \p T - type to be stored in the split-list.
24         - \p Traits - type traits, default is \p split_list::traits. Instead of declaring \p split_list::traits -based
25             struct you may apply option-based notation with \p split_list::make_traits metafunction.
26
27         There are the specializations:
28         - for \ref cds_urcu_desc "RCU" - declared in <tt>cd/container/split_list_set_rcu.h</tt>,
29             see \ref cds_nonintrusive_SplitListSet_rcu "SplitListSet<RCU>".
30         - for \ref cds::gc::nogc declared in <tt>cds/container/split_list_set_nogc.h</tt>,
31             see \ref cds_nonintrusive_SplitListSet_nogc "SplitListSet<gc::nogc>".
32
33         \par Usage
34
35         You should decide what garbage collector you want, and what ordered list you want to use as a base. Split-ordered list
36         is original data structure based on an ordered list.
37
38         Suppose, you want construct split-list set based on \p gc::DHP GC
39         and \p LazyList as ordered list implementation. So, you beginning your program with following include:
40         \code
41         #include <cds/container/lazy_list_dhp.h>
42         #include <cds/container/split_list_set.h>
43
44         namespace cc = cds::container;
45
46         // The data belonged to split-ordered list
47         sturuct foo {
48             int     nKey;   // key field
49             std::string strValue    ;   // value field
50         };
51         \endcode
52         The inclusion order is important: first, include header for ordered-list implementation (for this example, <tt>cds/container/lazy_list_dhp.h</tt>),
53         then the header for split-list set <tt>cds/container/split_list_set.h</tt>.
54
55         Now, you should declare traits for split-list set. The main parts of traits are a hash functor for the set and a comparing functor for ordered list.
56         Note that we define several function in <tt>foo_hash</tt> and <tt>foo_less</tt> functors for different argument types since we want call our \p %SplitListSet
57         object by the key of type <tt>int</tt> and by the value of type <tt>foo</tt>.
58
59         The second attention: instead of using \p %LazyList in \p %SplitListSet traits we use a tag \p cds::contaner::lazy_list_tag for the lazy list.
60         The split-list requires significant support from underlying ordered list class and it is not good idea to dive you
61         into deep implementation details of split-list and ordered list interrelations. The tag paradigm simplifies split-list interface.
62
63         \code
64         // foo hash functor
65         struct foo_hash {
66             size_t operator()( int key ) const { return std::hash( key ) ; }
67             size_t operator()( foo const& item ) const { return std::hash( item.nKey ) ; }
68         };
69
70         // foo comparator
71         struct foo_less {
72             bool operator()(int i, foo const& f ) const { return i < f.nKey ; }
73             bool operator()(foo const& f, int i ) const { return f.nKey < i ; }
74             bool operator()(foo const& f1, foo const& f2) const { return f1.nKey < f2.nKey; }
75         };
76
77         // SplitListSet traits
78         struct foo_set_traits: public cc::split_list::traits
79         {
80             typedef cc::lazy_list_tag   ordered_list; // what type of ordered list we want to use
81             typedef foo_hash            hash;         // hash functor for our data stored in split-list set
82
83             // Type traits for our LazyList class
84             struct ordered_list_traits: public cc::lazy_list::traits
85             {
86                 typedef foo_less less   ;   // use our foo_less as comparator to order list nodes
87             };
88         };
89         \endcode
90
91         Now you are ready to declare our set class based on \p %SplitListSet:
92         \code
93         typedef cc::SplitListSet< cds::gc::DHP, foo, foo_set_traits > foo_set;
94         \endcode
95
96         You may use the modern option-based declaration instead of classic traits-based one:
97         \code
98         typedef cc::SplitListSet<
99             cs::gc::DHP             // GC used
100             ,foo                    // type of data stored
101             ,cc::split_list::make_traits<      // metafunction to build split-list traits
102                 cc::split_list::ordered_list<cc::lazy_list_tag>  // tag for underlying ordered list implementation
103                 ,cc::opt::hash< foo_hash >               // hash functor
104                 ,cc::split_list::ordered_list_traits<    // ordered list traits desired
105                     cc::lazy_list::make_traits<          // metafunction to build lazy list traits
106                         cc::opt::less< foo_less >        // less-based compare functor
107                     >::type
108                 >
109             >::type
110         >  foo_set;
111         \endcode
112         In case of option-based declaration using split_list::make_traits metafunction
113         the struct \p foo_set_traits is not required.
114
115         Now, the set of type \p foo_set is ready to use in your program.
116
117         Note that in this example we show only mandatory \p traits parts, optional ones is the default and they are inherited
118         from \p cds::container::split_list::traits.
119         There are many other options for deep tuning the split-list and ordered-list containers.
120     */
121     template <
122         class GC,
123         class T,
124 #ifdef CDS_DOXYGEN_INVOKED
125         class Traits = split_list::traits
126 #else
127         class Traits
128 #endif
129     >
130     class SplitListSet:
131 #ifdef CDS_DOXYGEN_INVOKED
132         protected intrusive::SplitListSet<GC, typename Traits::ordered_list, Traits>
133 #else
134         protected details::make_split_list_set< GC, T, typename Traits::ordered_list, split_list::details::wrap_set_traits<T, Traits> >::type
135 #endif
136     {
137     protected:
138         //@cond
139         typedef details::make_split_list_set< GC, T, typename Traits::ordered_list, split_list::details::wrap_set_traits<T, Traits> > maker;
140         typedef typename maker::type  base_class;
141         //@endcond
142
143     public:
144         typedef GC      gc;         ///< Garbage collector
145         typedef T       value_type; ///< Type of vlue to be stored in split-list
146         typedef Traits  traits;     ///< \p Traits template argument
147         typedef typename maker::ordered_list ordered_list; ///< Underlying ordered list class
148         typedef typename base_class::key_comparator key_comparator; ///< key compare functor
149
150         /// Hash functor for \p %value_type and all its derivatives that you use
151         typedef typename base_class::hash         hash;
152         typedef typename base_class::item_counter item_counter; ///< Item counter type
153         typedef typename base_class::stat         stat; ///< Internal statistics
154
155         //@cond
156         typedef cds::container::split_list::implementation_tag implementation_tag;
157         //@endcond
158
159     protected:
160         //@cond
161         typedef typename maker::cxx_node_allocator    cxx_node_allocator;
162         typedef typename maker::node_type             node_type;
163         //@endcond
164
165     public:
166         /// Guarded pointer
167         typedef typename gc::template guarded_ptr< node_type, value_type, details::guarded_ptr_cast_set<node_type, value_type> > guarded_ptr;
168
169     protected:
170         //@cond
171         template <typename Q>
172         static node_type * alloc_node(Q const& v )
173         {
174             return cxx_node_allocator().New( v );
175         }
176
177         template <typename... Args>
178         static node_type * alloc_node( Args&&... args )
179         {
180             return cxx_node_allocator().MoveNew( std::forward<Args>( args )... );
181         }
182
183         static void free_node( node_type * pNode )
184         {
185             cxx_node_allocator().Delete( pNode );
186         }
187
188         template <typename Q, typename Func>
189         bool find_( Q& val, Func f )
190         {
191             return base_class::find( val, [&f]( node_type& item, Q& val ) { f(item.m_Value, val) ; } );
192         }
193
194         template <typename Q, typename Less, typename Func>
195         bool find_with_( Q& val, Less pred, Func f )
196         {
197             CDS_UNUSED( pred );
198             return base_class::find_with( val, typename maker::template predicate_wrapper<Less>::type(),
199                 [&f]( node_type& item, Q& val ) { f(item.m_Value, val) ; } );
200         }
201
202         struct node_disposer {
203             void operator()( node_type * pNode )
204             {
205                 free_node( pNode );
206             }
207         };
208         typedef std::unique_ptr< node_type, node_disposer >     scoped_node_ptr;
209
210         bool insert_node( node_type * pNode )
211         {
212             assert( pNode != nullptr );
213             scoped_node_ptr p(pNode);
214
215             if ( base_class::insert( *pNode ) ) {
216                 p.release();
217                 return true;
218             }
219             return false;
220         }
221
222         //@endcond
223
224     protected:
225         /// Forward iterator
226         /**
227             \p IsConst - constness boolean flag
228
229             The forward iterator for a split-list has the following features:
230             - it has no post-increment operator
231             - it depends on underlying ordered list iterator
232             - The iterator object cannot be moved across thread boundary since it contains GC's guard that is thread-private GC data.
233             - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
234               deleting operations it is no guarantee that you iterate all item in the split-list.
235
236             Therefore, the use of iterators in concurrent environment is not good idea. Use it for debug purpose only.
237         */
238         template <bool IsConst>
239         class iterator_type: protected base_class::template iterator_type<IsConst>
240         {
241             //@cond
242             typedef typename base_class::template iterator_type<IsConst> iterator_base_class;
243             friend class SplitListSet;
244             //@endcond
245         public:
246             /// Value pointer type (const for const iterator)
247             typedef typename cds::details::make_const_type<value_type, IsConst>::pointer   value_ptr;
248             /// Value reference type (const for const iterator)
249             typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
250
251         public:
252             /// Default ctor
253             iterator_type()
254             {}
255
256             /// Copy ctor
257             iterator_type( iterator_type const& src )
258                 : iterator_base_class( src )
259             {}
260
261         protected:
262             //@cond
263             explicit iterator_type( iterator_base_class const& src )
264                 : iterator_base_class( src )
265             {}
266             //@endcond
267
268         public:
269             /// Dereference operator
270             value_ptr operator ->() const
271             {
272                 return &(iterator_base_class::operator->()->m_Value);
273             }
274
275             /// Dereference operator
276             value_ref operator *() const
277             {
278                 return iterator_base_class::operator*().m_Value;
279             }
280
281             /// Pre-increment
282             iterator_type& operator ++()
283             {
284                 iterator_base_class::operator++();
285                 return *this;
286             }
287
288             /// Assignment operator
289             iterator_type& operator = (iterator_type const& src)
290             {
291                 iterator_base_class::operator=(src);
292                 return *this;
293             }
294
295             /// Equality operator
296             template <bool C>
297             bool operator ==(iterator_type<C> const& i ) const
298             {
299                 return iterator_base_class::operator==(i);
300             }
301
302             /// Equality operator
303             template <bool C>
304             bool operator !=(iterator_type<C> const& i ) const
305             {
306                 return iterator_base_class::operator!=(i);
307             }
308         };
309
310     public:
311         /// Initializes split-ordered list of default capacity
312         /**
313             The default capacity is defined in bucket table constructor.
314             See \p intrusive::split_list::expandable_bucket_table, \p intrusive::split_list::static_bucket_table
315             which selects by \p split_list::dynamic_bucket_table option.
316         */
317         SplitListSet()
318             : base_class()
319         {}
320
321         /// Initializes split-ordered list
322         SplitListSet(
323             size_t nItemCount           ///< estimated average of item count
324             , size_t nLoadFactor = 1    ///< the load factor - average item count per bucket. Small integer up to 8, default is 1.
325             )
326             : base_class( nItemCount, nLoadFactor )
327         {}
328
329     public:
330         /// Forward iterator
331         typedef iterator_type<false>  iterator;
332
333         /// Const forward iterator
334         typedef iterator_type<true>    const_iterator;
335
336         /// Returns a forward iterator addressing the first element in a set
337         /**
338             For empty set \code begin() == end() \endcode
339         */
340         iterator begin()
341         {
342             return iterator( base_class::begin() );
343         }
344
345         /// Returns an iterator that addresses the location succeeding the last element in a set
346         /**
347             Do not use the value returned by <tt>end</tt> function to access any item.
348             The returned value can be used only to control reaching the end of the set.
349             For empty set \code begin() == end() \endcode
350         */
351         iterator end()
352         {
353             return iterator( base_class::end() );
354         }
355
356         /// Returns a forward const iterator addressing the first element in a set
357         const_iterator begin() const
358         {
359             return cbegin();
360         }
361         /// Returns a forward const iterator addressing the first element in a set
362         const_iterator cbegin() const
363         {
364             return const_iterator( base_class::cbegin() );
365         }
366
367         /// Returns an const iterator that addresses the location succeeding the last element in a set
368         const_iterator end() const
369         {
370             return cend();
371         }
372         /// Returns an const iterator that addresses the location succeeding the last element in a set
373         const_iterator cend() const
374         {
375             return const_iterator( base_class::cend() );
376         }
377
378     public:
379         /// Inserts new node
380         /**
381             The function creates a node with copy of \p val value
382             and then inserts the node created into the set.
383
384             The type \p Q should contain as minimum the complete key for the node.
385             The object of \ref value_type should be constructible from a value of type \p Q.
386             In trivial case, \p Q is equal to \ref value_type.
387
388             Returns \p true if \p val is inserted into the set, \p false otherwise.
389         */
390         template <typename Q>
391         bool insert( Q const& val )
392         {
393             return insert_node( alloc_node( val ) );
394         }
395
396         /// Inserts new node
397         /**
398             The function allows to split creating of new item into two part:
399             - create item with key only
400             - insert new item into the set
401             - if inserting is success, calls  \p f functor to initialize value-field of \p val.
402
403             The functor signature is:
404             \code
405                 void func( value_type& val );
406             \endcode
407             where \p val is the item inserted.
408
409             The user-defined functor is called only if the inserting is success.
410
411             @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
412             \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
413             synchronization.
414         */
415         template <typename Q, typename Func>
416         bool insert( Q const& val, Func f )
417         {
418             scoped_node_ptr pNode( alloc_node( val ));
419
420             if ( base_class::insert( *pNode, [&f](node_type& node) { f( node.m_Value ) ; } )) {
421                 pNode.release();
422                 return true;
423             }
424             return false;
425         }
426
427         /// Inserts data of type \p value_type created from \p args
428         /**
429             Returns \p true if inserting successful, \p false otherwise.
430         */
431         template <typename... Args>
432         bool emplace( Args&&... args )
433         {
434             return insert_node( alloc_node( std::forward<Args>(args)...));
435         }
436
437         /// Updates the node
438         /**
439             The operation performs inserting or changing data with lock-free manner.
440
441             If \p key is not found in the set, then \p key is inserted iff \p bAllowInsert is \p true.
442             Otherwise, the functor \p func is called with item found.
443
444             The functor signature is:
445             \code
446                 struct my_functor {
447                     void operator()( bool bNew, value_type& item, const Q& val );
448                 };
449             \endcode
450
451             with arguments:
452             - \p bNew - \p true if the item has been inserted, \p false otherwise
453             - \p item - item of the set
454             - \p val - argument \p val passed into the \p %update() function
455
456             The functor may change non-key fields of the \p item.
457
458             Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
459             \p second is true if new item has been added or \p false if the item with \p key
460             already is in the map.
461
462             @warning For \ref cds_intrusive_MichaelList_hp "MichaelList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
463             \ref cds_intrusive_LazyList_hp "LazyList" provides exclusive access to inserted item and does not require any node-level
464             synchronization.
465         */
466         template <typename Q, typename Func>
467         std::pair<bool, bool> update( Q const& val, Func func, bool bAllowInsert = true )
468         {
469             scoped_node_ptr pNode( alloc_node( val ));
470
471             std::pair<bool, bool> bRet = base_class::update( *pNode,
472                 [&func, &val]( bool bNew, node_type& item,  node_type const& /*val*/ ) {
473                     func( bNew, item.m_Value, val );
474                 }, bAllowInsert );
475
476             if ( bRet.first && bRet.second )
477                 pNode.release();
478             return bRet;
479         }
480         //@cond
481         // Deprecated, use update()
482         template <typename Q, typename Func>
483         std::pair<bool, bool> ensure( Q const& val, Func func )
484         {
485             return update( val, func, true );
486         }
487         //@endcond
488
489         /// Deletes \p key from the set
490         /** \anchor cds_nonintrusive_SplitListSet_erase_val
491
492             The item comparator should be able to compare the values of type \p value_type
493             and the type \p Q.
494
495             Return \p true if key is found and deleted, \p false otherwise
496         */
497         template <typename Q>
498         bool erase( Q const& key )
499         {
500             return base_class::erase( key );
501         }
502
503         /// Deletes the item from the set using \p pred predicate for searching
504         /**
505             The function is an analog of \ref cds_nonintrusive_SplitListSet_erase_val "erase(Q const&)"
506             but \p pred is used for key comparing.
507             \p Less functor has the interface like \p std::less.
508             \p Less must imply the same element order as the comparator used for building the set.
509         */
510         template <typename Q, typename Less>
511         bool erase_with( Q const& key, Less pred )
512         {
513             CDS_UNUSED( pred );
514             return base_class::erase_with( key, typename maker::template predicate_wrapper<Less>::type() );
515         }
516
517         /// Deletes \p key from the set
518         /** \anchor cds_nonintrusive_SplitListSet_erase_func
519
520             The function searches an item with key \p key, calls \p f functor
521             and deletes the item. If \p key is not found, the functor is not called.
522
523             The functor \p Func interface:
524             \code
525             struct extractor {
526                 void operator()(value_type const& val);
527             };
528             \endcode
529
530             Since the key of split-list \p value_type is not explicitly specified,
531             template parameter \p Q defines the key type searching in the list.
532             The list item comparator should be able to compare the values of the type \p value_type
533             and the type \p Q.
534
535             Return \p true if key is found and deleted, \p false otherwise
536         */
537         template <typename Q, typename Func>
538         bool erase( Q const& key, Func f )
539         {
540             return base_class::erase( key, [&f](node_type& node) { f( node.m_Value ); } );
541         }
542
543         /// Deletes the item from the set using \p pred predicate for searching
544         /**
545             The function is an analog of \ref cds_nonintrusive_SplitListSet_erase_func "erase(Q const&, Func)"
546             but \p pred is used for key comparing.
547             \p Less functor has the interface like \p std::less.
548             \p Less must imply the same element order as the comparator used for building the set.
549         */
550         template <typename Q, typename Less, typename Func>
551         bool erase_with( Q const& key, Less pred, Func f )
552         {
553             CDS_UNUSED( pred );
554             return base_class::erase_with( key, typename maker::template predicate_wrapper<Less>::type(),
555                 [&f](node_type& node) { f( node.m_Value ); } );
556         }
557
558         /// Extracts the item with specified \p key
559         /** \anchor cds_nonintrusive_SplitListSet_hp_extract
560             The function searches an item with key equal to \p key,
561             unlinks it from the set, and returns it as \p guarded_ptr.
562             If \p key is not found the function returns an empty guarded pointer.
563
564             Note the compare functor should accept a parameter of type \p Q that may be not the same as \p value_type.
565
566             The extracted item is freed automatically when returned \p guarded_ptr object will be destroyed or released.
567             @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
568
569             Usage:
570             \code
571             typedef cds::container::SplitListSet< your_template_args > splitlist_set;
572             splitlist_set theSet;
573             // ...
574             {
575                 splitlist_set::guarded_ptr gp(theSet.extract( 5 ));
576                 if ( gp ) {
577                     // Deal with gp
578                     // ...
579                 }
580                 // Destructor of gp releases internal HP guard
581             }
582             \endcode
583         */
584         template <typename Q>
585         guarded_ptr extract( Q const& key )
586         {
587             guarded_ptr gp;
588             extract_( gp.guard(), key );
589             return gp;
590         }
591
592         /// Extracts the item using compare functor \p pred
593         /**
594             The function is an analog of \ref cds_nonintrusive_SplitListSet_hp_extract "extract(Q const&)"
595             but \p pred predicate is used for key comparing.
596
597             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
598             in any order.
599             \p pred must imply the same element order as the comparator used for building the set.
600         */
601         template <typename Q, typename Less>
602         guarded_ptr extract_with( Q const& key, Less pred )
603         {
604             guarded_ptr gp;
605             extract_with_( gp.guard(), key, pred );
606             return gp;
607         }
608
609         /// Finds the key \p key
610         /** \anchor cds_nonintrusive_SplitListSet_find_func
611
612             The function searches the item with key equal to \p key and calls the functor \p f for item found.
613             The interface of \p Func functor is:
614             \code
615             struct functor {
616                 void operator()( value_type& item, Q& key );
617             };
618             \endcode
619             where \p item is the item found, \p key is the <tt>find</tt> function argument.
620
621             The functor may change non-key fields of \p item. Note that the functor is only guarantee
622             that \p item cannot be disposed during functor is executing.
623             The functor does not serialize simultaneous access to the set's \p item. If such access is
624             possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
625
626             The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
627             may modify both arguments.
628
629             Note the hash functor specified for class \p Traits template parameter
630             should accept a parameter of type \p Q that can be not the same as \p value_type.
631
632             The function returns \p true if \p key is found, \p false otherwise.
633         */
634         template <typename Q, typename Func>
635         bool find( Q& key, Func f )
636         {
637             return find_( key, f );
638         }
639         //@cond
640         template <typename Q, typename Func>
641         bool find( Q const& key, Func f )
642         {
643             return find_( key, f );
644         }
645         //@endcond
646
647         /// Finds the key \p key using \p pred predicate for searching
648         /**
649             The function is an analog of \ref cds_nonintrusive_SplitListSet_find_func "find(Q&, Func)"
650             but \p pred is used for key comparing.
651             \p Less functor has the interface like \p std::less.
652             \p Less must imply the same element order as the comparator used for building the set.
653         */
654         template <typename Q, typename Less, typename Func>
655         bool find_with( Q& key, Less pred, Func f )
656         {
657             return find_with_( key, pred, f );
658         }
659         //@cond
660         template <typename Q, typename Less, typename Func>
661         bool find_with( Q const& key, Less pred, Func f )
662         {
663             return find_with_( key, pred, f );
664         }
665         //@endcond
666
667         /// Checks whether the set contains \p key
668         /**
669             The function searches the item with key equal to \p key
670             and returns \p true if it is found, and \p false otherwise.
671
672             Note the hash functor specified for class \p Traits template parameter
673             should accept a parameter of type \p Q that can be not the same as \p value_type.
674             Otherwise, you may use \p contains( Q const&, Less pred ) functions with explicit predicate for key comparing.
675         */
676         template <typename Q>
677         bool contains( Q const& key )
678         {
679             return base_class::contains( key );
680         }
681         //@cond
682         // Deprecated, use contains()
683         template <typename Q>
684         bool find( Q const& key )
685         {
686             return contains( key );
687         }
688         //@endcond
689
690         /// Checks whether the map contains \p key using \p pred predicate for searching
691         /**
692             The function is similar to <tt>contains( key )</tt> but \p pred is used for key comparing.
693             \p Less functor has the interface like \p std::less.
694             \p Less must imply the same element order as the comparator used for building the map.
695         */
696         template <typename Q, typename Less>
697         bool contains( Q const& key, Less pred )
698         {
699             CDS_UNUSED( pred );
700             return base_class::contains( key, typename maker::template predicate_wrapper<Less>::type() );
701         }
702         //@cond
703         // Deprecated, use contains()
704         template <typename Q, typename Less>
705         bool find_with( Q const& key, Less pred )
706         {
707             return contains( key, pred );
708         }
709         //@endcond
710
711         /// Finds the key \p key and return the item found
712         /** \anchor cds_nonintrusive_SplitListSet_hp_get
713             The function searches the item with key equal to \p key
714             and returns the item found as \p guarded_ptr.
715             If \p key is not found the function returns an empty guarded pointer.
716
717             @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
718
719             Usage:
720             \code
721             typedef cds::container::SplitListSet< your_template_params >  splitlist_set;
722             splitlist_set theSet;
723             // ...
724             {
725                 splitlist_set::guarded_ptr gp(theSet.get( 5 ));
726                 if ( gp ) {
727                     // Deal with gp
728                     //...
729                 }
730                 // Destructor of guarded_ptr releases internal HP guard
731             }
732             \endcode
733
734             Note the compare functor specified for split-list set
735             should accept a parameter of type \p Q that can be not the same as \p value_type.
736         */
737         template <typename Q>
738         guarded_ptr get( Q const& key )
739         {
740             guarded_ptr gp;
741             get_( gp.guard(), key );
742             return gp;
743         }
744
745         /// Finds \p key and return the item found
746         /**
747             The function is an analog of \ref cds_nonintrusive_SplitListSet_hp_get "get( Q const&)"
748             but \p pred is used for comparing the keys.
749
750             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
751             in any order.
752             \p pred must imply the same element order as the comparator used for building the set.
753         */
754         template <typename Q, typename Less>
755         guarded_ptr get_with( Q const& key, Less pred )
756         {
757             guarded_ptr gp;
758             get_with_( gp.guard(), key, pred );
759             return gp;
760         }
761
762         /// Clears the set (not atomic)
763         void clear()
764         {
765             base_class::clear();
766         }
767
768         /// Checks if the set is empty
769         /**
770             Emptiness is checked by item counting: if item count is zero then assume that the set is empty.
771             Thus, the correct item counting feature is an important part of split-list set implementation.
772         */
773         bool empty() const
774         {
775             return base_class::empty();
776         }
777
778         /// Returns item count in the set
779         size_t size() const
780         {
781             return base_class::size();
782         }
783
784         /// Returns internal statistics
785         stat const& statistics() const
786         {
787             return base_class::statistics();
788         }
789
790     protected:
791         //@cond
792         using base_class::extract_;
793         using base_class::get_;
794
795         template <typename Q, typename Less>
796         bool extract_with_( typename guarded_ptr::native_guard& guard, Q const& key, Less pred )
797         {
798             CDS_UNUSED( pred );
799             return base_class::extract_with_( guard, key, typename maker::template predicate_wrapper<Less>::type() );
800         }
801
802         template <typename Q, typename Less>
803         bool get_with_( typename guarded_ptr::native_guard& guard, Q const& key, Less pred )
804         {
805             CDS_UNUSED( pred );
806             return base_class::get_with_( guard, key, typename maker::template predicate_wrapper<Less>::type() );
807         }
808
809         //@endcond
810
811     };
812
813
814 }}  // namespace cds::container
815
816 #endif // #ifndef CDSLIB_CONTAINER_SPLIT_LIST_SET_H