Added Hazard Pointer count
[libcds.git] / cds / container / impl / michael_list.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
29 */
30
31 #ifndef CDSLIB_CONTAINER_IMPL_MICHAEL_LIST_H
32 #define CDSLIB_CONTAINER_IMPL_MICHAEL_LIST_H
33
34 #include <memory>
35 #include <cds/container/details/guarded_ptr_cast.h>
36
37 namespace cds { namespace container {
38
39     /// Michael's ordered list
40     /** @ingroup cds_nonintrusive_list
41         \anchor cds_nonintrusive_MichaelList_gc
42
43         Usually, ordered single-linked list is used as a building block for the hash table implementation.
44         The complexity of searching is <tt>O(N)</tt>, where \p N is the item count in the list, not in the
45         hash table.
46
47         Source:
48         - [2002] Maged Michael "High performance dynamic lock-free hash tables and list-based sets"
49
50         This class is non-intrusive version of cds::intrusive::MichaelList class
51
52         Template arguments:
53         - \p GC - garbage collector used
54         - \p T - type stored in the list. The type must be default- and copy-constructible.
55         - \p Traits - type traits, default is \p michael_list::traits
56
57         Unlike standard container, this implementation does not divide type \p T into key and value part and
58         may be used as a main building block for hash set algorithms.
59         The key is a function (or a part) of type \p T, and this function is specified by <tt>Traits::compare</tt> functor
60         or <tt>Traits::less</tt> predicate
61
62         MichaelKVList is a key-value version of Michael's non-intrusive list that is closer to the C++ std library approach.
63
64         It is possible to declare option-based list with cds::container::michael_list::make_traits metafunction istead of \p Traits template
65         argument. For example, the following traits-based declaration of gc::HP Michael's list
66         \code
67         #include <cds/container/michael_list_hp.h>
68         // Declare comparator for the item
69         struct my_compare {
70             int operator ()( int i1, int i2 )
71             {
72                 return i1 - i2;
73             }
74         };
75
76         // Declare traits
77         struct my_traits: public cds::container::michael_list::traits
78         {
79             typedef my_compare compare;
80         };
81
82         // Declare traits-based list
83         typedef cds::container::MichaelList< cds::gc::HP, int, my_traits >     traits_based_list;
84         \endcode
85
86         is equivalent for the following option-based list
87         \code
88         #include <cds/container/michael_list_hp.h>
89
90         // my_compare is the same
91
92         // Declare option-based list
93         typedef cds::container::MichaelList< cds::gc::HP, int,
94             typename cds::container::michael_list::make_traits<
95                 cds::container::opt::compare< my_compare >     // item comparator option
96             >::type
97         >     option_based_list;
98         \endcode
99
100         \par Usage
101         There are different specializations of this template for each garbage collecting schema used.
102         You should include appropriate .h-file depending on GC you are using:
103         - for gc::HP: \code #include <cds/container/michael_list_hp.h> \endcode
104         - for gc::DHP: \code #include <cds/container/michael_list_dhp.h> \endcode
105         - for \ref cds_urcu_desc "RCU": \code #include <cds/container/michael_list_rcu.h> \endcode
106         - for gc::nogc: \code #include <cds/container/michael_list_nogc.h> \endcode
107     */
108     template <
109         typename GC,
110         typename T,
111 #ifdef CDS_DOXYGEN_INVOKED
112         typename Traits = michael_list::traits
113 #else
114         typename Traits
115 #endif
116     >
117     class MichaelList:
118 #ifdef CDS_DOXYGEN_INVOKED
119         protected intrusive::MichaelList< GC, T, Traits >
120 #else
121         protected details::make_michael_list< GC, T, Traits >::type
122 #endif
123     {
124         //@cond
125         typedef details::make_michael_list< GC, T, Traits > maker;
126         typedef typename maker::type base_class;
127         //@endcond
128
129     public:
130         typedef T value_type;   ///< Type of value stored in the list
131         typedef Traits traits;  ///< List traits
132
133         typedef typename base_class::gc             gc;             ///< Garbage collector used
134         typedef typename base_class::back_off       back_off;       ///< Back-off strategy used
135         typedef typename maker::allocator_type      allocator_type; ///< Allocator type used for allocate/deallocate the nodes
136         typedef typename base_class::item_counter   item_counter;   ///< Item counting policy used
137         typedef typename maker::key_comparator      key_comparator; ///< key comparison functor
138         typedef typename base_class::memory_model   memory_model;   ///< Memory ordering. See \p cds::opt::memory_model option
139
140         static CDS_CONSTEXPR const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount; ///< Count of hazard pointer required for the algorithm
141
142     protected:
143         //@cond
144         typedef typename base_class::value_type      node_type;
145         typedef typename maker::cxx_allocator        cxx_allocator;
146         typedef typename maker::node_deallocator     node_deallocator;
147         typedef typename maker::intrusive_traits::compare intrusive_key_comparator;
148
149         typedef typename base_class::atomic_node_ptr head_type;
150         //@endcond
151
152     public:
153         /// Guarded pointer
154         typedef typename gc::template guarded_ptr< node_type, value_type, details::guarded_ptr_cast_set<node_type, value_type> > guarded_ptr;
155
156     private:
157         //@cond
158         static value_type& node_to_value( node_type& n )
159         {
160             return n.m_Value;
161         }
162         static value_type const& node_to_value( node_type const& n )
163         {
164             return n.m_Value;
165         }
166         //@endcond
167
168     protected:
169         //@cond
170         template <typename Q>
171         static node_type * alloc_node( Q const& v )
172         {
173             return cxx_allocator().New( v );
174         }
175
176         template <typename... Args>
177         static node_type * alloc_node( Args&&... args )
178         {
179             return cxx_allocator().MoveNew( std::forward<Args>(args)... );
180         }
181
182         static void free_node( node_type * pNode )
183         {
184             cxx_allocator().Delete( pNode );
185         }
186
187         struct node_disposer {
188             void operator()( node_type * pNode )
189             {
190                 free_node( pNode );
191             }
192         };
193         typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
194
195         head_type& head()
196         {
197             return base_class::m_pHead;
198         }
199
200         head_type const& head() const
201         {
202             return base_class::m_pHead;
203         }
204         //@endcond
205
206     protected:
207         //@cond
208         template <bool IsConst>
209         class iterator_type: protected base_class::template iterator_type<IsConst>
210         {
211             typedef typename base_class::template iterator_type<IsConst>    iterator_base;
212
213             iterator_type( head_type const& pNode )
214                 : iterator_base( pNode )
215             {}
216
217             friend class MichaelList;
218
219         public:
220             typedef typename cds::details::make_const_type<value_type, IsConst>::pointer   value_ptr;
221             typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
222
223             iterator_type()
224             {}
225
226             iterator_type( iterator_type const& src )
227                 : iterator_base( src )
228             {}
229
230             value_ptr operator ->() const
231             {
232                 typename iterator_base::value_ptr p = iterator_base::operator ->();
233                 return p ? &(p->m_Value) : nullptr;
234             }
235
236             value_ref operator *() const
237             {
238                 return (iterator_base::operator *()).m_Value;
239             }
240
241             /// Pre-increment
242             iterator_type& operator ++()
243             {
244                 iterator_base::operator ++();
245                 return *this;
246             }
247
248             template <bool C>
249             bool operator ==(iterator_type<C> const& i ) const
250             {
251                 return iterator_base::operator ==(i);
252             }
253             template <bool C>
254             bool operator !=(iterator_type<C> const& i ) const
255             {
256                 return iterator_base::operator !=(i);
257             }
258         };
259         //@endcond
260
261     public:
262         /// Forward iterator
263         /**
264             The forward iterator for Michael's list has some features:
265             - it has no post-increment operator
266             - to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator.
267               For some GC (\p gc::HP), a guard is limited resource per thread, so an exception (or assertion) "no free guard"
268               may be thrown if a limit of guard count per thread is exceeded.
269             - The iterator cannot be moved across thread boundary since it contains GC's guard that is thread-private GC data.
270             - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
271               deleting operations it is no guarantee that you iterate all item in the list.
272
273             @warning Use this iterator on the concurrent container for debugging purpose only.
274         */
275         typedef iterator_type<false>    iterator;
276
277         /// Const forward iterator
278         /**
279             For iterator's features and requirements see \ref iterator
280         */
281         typedef iterator_type<true>     const_iterator;
282
283     ///@name Forward iterators (only for debugging purpose)
284     //@{
285         /// Returns a forward iterator addressing the first element in a list
286         /**
287             For empty list \code begin() == end() \endcode
288         */
289         iterator begin()
290         {
291             return iterator( head() );
292         }
293
294         /// Returns an iterator that addresses the location succeeding the last element in a list
295         /**
296             Do not use the value returned by <tt>end</tt> function to access any item.
297             Internally, <tt>end</tt> returning value equals to \p nullptr.
298
299             The returned value can be used only to control reaching the end of the list.
300             For empty list \code begin() == end() \endcode
301         */
302         iterator end()
303         {
304             return iterator();
305         }
306
307         /// Returns a forward const iterator addressing the first element in a list
308         const_iterator begin() const
309         {
310             return const_iterator( head() );
311         }
312
313         /// Returns a forward const iterator addressing the first element in a list
314         const_iterator cbegin() const
315         {
316             return const_iterator( head() );
317         }
318
319         /// Returns an const iterator that addresses the location succeeding the last element in a list
320         const_iterator end() const
321         {
322             return const_iterator();
323         }
324
325         /// Returns an const iterator that addresses the location succeeding the last element in a list
326         const_iterator cend() const
327         {
328             return const_iterator();
329         }
330     //@}
331
332     public:
333         /// Default constructor
334         /**
335             Initialize empty list
336         */
337         MichaelList()
338         {}
339
340         /// List destructor
341         /**
342             Clears the list
343         */
344         ~MichaelList()
345         {
346             clear();
347         }
348
349         /// Inserts new node
350         /**
351             The function creates a node with copy of \p val value
352             and then inserts the node created into the list.
353
354             The type \p Q should contain least the complete key of the node.
355             The object of \ref value_type should be constructible from \p val of type \p Q.
356             In trivial case, \p Q is equal to \ref value_type.
357
358             Returns \p true if inserting successful, \p false otherwise.
359         */
360         template <typename Q>
361         bool insert( Q const& val )
362         {
363             return insert_at( head(), val );
364         }
365
366         /// Inserts new node
367         /**
368             This function inserts new node with default-constructed value and then it calls
369             \p func functor with signature
370             \code void func( value_type& itemValue ) ;\endcode
371
372             The argument \p itemValue of user-defined functor \p func is the reference
373             to the list's item inserted. User-defined functor \p func should guarantee that during changing
374             item's value no any other changes could be made on this list's item by concurrent threads.
375             The user-defined functor is called only if inserting is success.
376
377             The type \p Q should contain the complete key of the node.
378             The object of \p value_type should be constructible from \p key of type \p Q.
379
380             The function allows to split creating of new item into two part:
381             - create item from \p key with initializing key-fields only;
382             - insert new item into the list;
383             - if inserting is successful, initialize non-key fields of item by calling \p func functor
384
385             The method can be useful if complete initialization of object of \p value_type is heavyweight and
386             it is preferable that the initialization should be completed only if inserting is successful.
387
388             @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
389         */
390         template <typename Q, typename Func>
391         bool insert( Q const& key, Func func )
392         {
393             return insert_at( head(), key, func );
394         }
395
396         /// Updates data by \p key
397         /**
398             The operation performs inserting or replacing the element with lock-free manner.
399
400             If the \p key not found in the list, then the new item created from \p key
401             will be inserted iff \p bAllowInsert is \p true.
402             Otherwise, if \p key is found, the functor \p func is called with item found.
403
404             The functor \p Func signature is:
405             \code
406                 struct my_functor {
407                     void operator()( bool bNew, value_type& item, Q const& val );
408                 };
409             \endcode
410
411             with arguments:
412             - \p bNew - \p true if the item has been inserted, \p false otherwise
413             - \p item - item of the list
414             - \p val - argument \p key passed into the \p %update() function
415
416             The functor may change non-key fields of the \p item; however, \p func must guarantee
417             that during changing no any other modifications could be made on this item by concurrent threads.
418
419             Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
420             \p second is true if new item has been added or \p false if the item with \p key
421             already exists.
422
423             @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
424         */
425         template <typename Q, typename Func>
426         std::pair<bool, bool> update( Q const& key, Func func, bool bAllowInsert = true )
427         {
428             return update_at( head(), key, func, bAllowInsert );
429         }
430         //@cond
431         template <typename Q, typename Func>
432         CDS_DEPRECATED("ensure() is deprecated, use update()")
433         std::pair<bool, bool> ensure( Q const& key, Func func )
434         {
435             return update( key, func );
436         }
437         //@endcond
438
439         /// Inserts data of type \p value_type constructed with <tt>std::forward<Args>(args)...</tt>
440         /**
441             Returns \p true if inserting successful, \p false otherwise.
442         */
443         template <typename... Args>
444         bool emplace( Args&&... args )
445         {
446             return emplace_at( head(), std::forward<Args>(args)... );
447         }
448
449         /// Delete \p key from the list
450         /** \anchor cds_nonintrusive_MichealList_hp_erase_val
451             Since the key of MichaelList's item type \p value_type is not explicitly specified,
452             template parameter \p Q sould contain the complete key to search in the list.
453             The list item comparator should be able to compare the type \p value_type
454             and the type \p Q.
455
456             Return \p true if key is found and deleted, \p false otherwise
457         */
458         template <typename Q>
459         bool erase( Q const& key )
460         {
461             return erase_at( head(), key, intrusive_key_comparator(), [](value_type const&){} );
462         }
463
464         /// Deletes the item from the list using \p pred predicate for searching
465         /**
466             The function is an analog of \ref cds_nonintrusive_MichealList_hp_erase_val "erase(Q const&)"
467             but \p pred is used for key comparing.
468             \p Less functor has the interface like \p std::less.
469             \p pred must imply the same element order as the comparator used for building the list.
470         */
471         template <typename Q, typename Less>
472         bool erase_with( Q const& key, Less pred )
473         {
474             CDS_UNUSED( pred );
475             return erase_at( head(), key, typename maker::template less_wrapper<Less>::type(), [](value_type const&){} );
476         }
477
478         /// Deletes \p key from the list
479         /** \anchor cds_nonintrusive_MichaelList_hp_erase_func
480             The function searches an item with key \p key, calls \p f functor with item found
481             and deletes it. If \p key is not found, the functor is not called.
482
483             The functor \p Func interface:
484             \code
485             struct extractor {
486                 void operator()(const value_type& val) { ... }
487             };
488             \endcode
489
490             Since the key of MichaelList's item type \p value_type is not explicitly specified,
491             template parameter \p Q should contain the complete key to search in the list.
492             The list item comparator should be able to compare the type \p value_type of list item
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, typename Func>
498         bool erase( Q const& key, Func f )
499         {
500             return erase_at( head(), key, intrusive_key_comparator(), f );
501         }
502
503         /// Deletes the item from the list using \p pred predicate for searching
504         /**
505             The function is an analog of \ref cds_nonintrusive_MichaelList_hp_erase_func "erase(Q const&, Func)"
506             but \p pred is used for key comparing.
507             \p Less functor has the interface like \p std::less.
508             \p pred must imply the same element order as the comparator used for building the list.
509         */
510         template <typename Q, typename Less, typename Func>
511         bool erase_with( Q const& key, Less pred, Func f )
512         {
513             CDS_UNUSED( pred );
514             return erase_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
515         }
516
517         /// Extracts the item from the list with specified \p key
518         /** \anchor cds_nonintrusive_MichaelList_hp_extract
519             The function searches an item with key equal to \p key,
520             unlinks it from the list, and returns it as \p guarded_ptr.
521             If \p key is not found the function returns an empty guarded pointer.
522
523             Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
524
525             @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
526
527             Usage:
528             \code
529             typedef cds::container::MichaelList< cds::gc::HP, foo, my_traits >  ord_list;
530             ord_list theList;
531             // ...
532             {
533                 ord_list::guarded_ptr gp(theList.extract( 5 ));
534                 if ( gp ) {
535                     // Deal with gp
536                     // ...
537                 }
538                 // Destructor of gp releases internal HP guard and frees the item
539             }
540             \endcode
541         */
542         template <typename Q>
543         guarded_ptr extract( Q const& key )
544         {
545             guarded_ptr gp;
546             extract_at( head(), gp.guard(), key, intrusive_key_comparator() );
547             return gp;
548         }
549
550         /// Extracts the item from the list with comparing functor \p pred
551         /**
552             The function is an analog of \ref cds_nonintrusive_MichaelList_hp_extract "extract(Q const&)"
553             but \p pred predicate is used for key comparing.
554
555             \p Less functor has the semantics like \p std::less but it should accept arguments of type \p value_type and \p Q
556             in any order.
557             \p pred must imply the same element order as the comparator used for building the list.
558         */
559         template <typename Q, typename Less>
560         guarded_ptr extract_with( Q const& key, Less pred )
561         {
562             CDS_UNUSED( pred );
563             guarded_ptr gp;
564             extract_at( head(), gp.guard(), key, typename maker::template less_wrapper<Less>::type() );
565             return gp;
566         }
567
568         /// Checks whether the list contains \p key
569         /**
570             The function searches the item with key equal to \p key
571             and returns \p true if it is found, and \p false otherwise.
572         */
573         template <typename Q>
574         bool contains( Q const& key )
575         {
576             return find_at( head(), key, intrusive_key_comparator() );
577         }
578         //@cond
579         template <typename Q>
580         CDS_DEPRECATED("deprecated, use contains()")
581         bool find( Q const& key )
582         {
583             return contains( key );
584         }
585         //@endcond
586
587         /// Checks whether the list contains \p key using \p pred predicate for searching
588         /**
589             The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
590             \p Less functor has the interface like \p std::less.
591             \p pred must imply the same element order as the comparator used for building the list.
592         */
593         template <typename Q, typename Less>
594         bool contains( Q const& key, Less pred )
595         {
596             CDS_UNUSED( pred );
597             return find_at( head(), key, typename maker::template less_wrapper<Less>::type() );
598         }
599         //@cond
600         template <typename Q, typename Less>
601         CDS_DEPRECATED("deprecated, use contains()")
602         bool find_with( Q const& key, Less pred )
603         {
604             return contains( key, pred );
605         }
606         //@endcond
607
608         /// Finds \p key and perform an action with it
609         /** \anchor cds_nonintrusive_MichaelList_hp_find_func
610             The function searches an item with key equal to \p key and calls the functor \p f for the item found.
611             The interface of \p Func functor is:
612             \code
613             struct functor {
614                 void operator()( value_type& item, Q& key );
615             };
616             \endcode
617             where \p item is the item found, \p key is the <tt>find</tt> function argument.
618
619             The functor may change non-key fields of \p item. Note that the function is only guarantee
620             that \p item cannot be deleted during functor is executing.
621             The function does not serialize simultaneous access to the list \p item. If such access is
622             possible you must provide your own synchronization schema to exclude unsafe item modifications.
623
624             The function returns \p true if \p key is found, \p false otherwise.
625         */
626         template <typename Q, typename Func>
627         bool find( Q& key, Func f )
628         {
629             return find_at( head(), key, intrusive_key_comparator(), f );
630         }
631         //@cond
632         template <typename Q, typename Func>
633         bool find( Q const& key, Func f )
634         {
635             return find_at( head(), key, intrusive_key_comparator(), f );
636         }
637         //@endcond
638
639         /// Finds \p key using \p pred predicate for searching
640         /**
641             The function is an analog of \ref cds_nonintrusive_MichaelList_hp_find_func "find(Q&, Func)"
642             but \p pred is used for key comparing.
643             \p Less functor has the interface like \p std::less.
644             \p pred must imply the same element order as the comparator used for building the list.
645         */
646         template <typename Q, typename Less, typename Func>
647         bool find_with( Q& key, Less pred, Func f )
648         {
649             CDS_UNUSED( pred );
650             return find_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
651         }
652         //@cond
653         template <typename Q, typename Less, typename Func>
654         bool find_with( Q const& key, Less pred, Func f )
655         {
656             CDS_UNUSED( pred );
657             return find_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
658         }
659         //@endcond
660
661         /// Finds \p key and return the item found
662         /** \anchor cds_nonintrusive_MichaelList_hp_get
663             The function searches the item with key equal to \p key
664             and returns it as \p guarded_ptr.
665             If \p key is not found the function returns an empty guarded pointer.
666
667             @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
668
669             Usage:
670             \code
671             typedef cds::container::MichaelList< cds::gc::HP, foo, my_traits >  ord_list;
672             ord_list theList;
673             // ...
674             {
675                 ord_list::guarded_ptr gp(theList.get( 5 ));
676                 if ( gp ) {
677                     // Deal with gp
678                     //...
679                 }
680                 // Destructor of guarded_ptr releases internal HP guard and frees the item
681             }
682             \endcode
683
684             Note the compare functor specified for class \p Traits template parameter
685             should accept a parameter of type \p Q that can be not the same as \p value_type.
686         */
687         template <typename Q>
688         guarded_ptr get( Q const& key )
689         {
690             guarded_ptr gp;
691             get_at( head(), gp.guard(), key, intrusive_key_comparator() );
692             return gp;
693         }
694
695         /// Finds \p key and return the item found
696         /**
697             The function is an analog of \ref cds_nonintrusive_MichaelList_hp_get "get( Q const&)"
698             but \p pred is used for comparing the keys.
699
700             \p Less functor has the semantics like \p std::less but should accept arguments of type \p value_type and \p Q
701             in any order.
702             \p pred must imply the same element order as the comparator used for building the list.
703         */
704         template <typename Q, typename Less>
705         guarded_ptr get_with( Q const& key, Less pred )
706         {
707             CDS_UNUSED( pred );
708             guarded_ptr gp;
709             get_at( head(), gp.guard(), key, typename maker::template less_wrapper<Less>::type() );
710             return gp;
711         }
712
713         /// Check if the list is empty
714         bool empty() const
715         {
716             return base_class::empty();
717         }
718
719         /// Returns list's item count
720         /**
721             The value returned depends on item counter provided by \p Traits. For \p atomicity::empty_item_counter,
722             this function always returns 0.
723
724             @note Even if you use real item counter and it returns 0, this fact is not mean that the list
725             is empty. To check list emptyness use \p empty() method.
726         */
727         size_t size() const
728         {
729             return base_class::size();
730         }
731
732         /// Clears the list
733         void clear()
734         {
735             base_class::clear();
736         }
737
738     protected:
739         //@cond
740         bool insert_node_at( head_type& refHead, node_type * pNode )
741         {
742             assert( pNode );
743             scoped_node_ptr p(pNode);
744             if ( base_class::insert_at( refHead, *pNode )) {
745                 p.release();
746                 return true;
747             }
748
749             return false;
750         }
751
752         template <typename Q>
753         bool insert_at( head_type& refHead, Q const& val )
754         {
755             return insert_node_at( refHead, alloc_node( val ));
756         }
757
758         template <typename Q, typename Func>
759         bool insert_at( head_type& refHead, Q const& key, Func f )
760         {
761             scoped_node_ptr pNode( alloc_node( key ));
762
763             if ( base_class::insert_at( refHead, *pNode, [&f]( node_type& node ) { f( node_to_value(node) ); } )) {
764                 pNode.release();
765                 return true;
766             }
767             return false;
768         }
769
770         template <typename... Args>
771         bool emplace_at( head_type& refHead, Args&&... args )
772         {
773             return insert_node_at( refHead, alloc_node( std::forward<Args>(args) ... ));
774         }
775
776         template <typename Q, typename Compare, typename Func>
777         bool erase_at( head_type& refHead, Q const& key, Compare cmp, Func f )
778         {
779             return base_class::erase_at( refHead, key, cmp, [&f](node_type const& node){ f( node_to_value(node) ); } );
780         }
781
782         template <typename Q, typename Compare>
783         bool extract_at( head_type& refHead, typename guarded_ptr::native_guard& guard, Q const& key, Compare cmp )
784         {
785             return base_class::extract_at( refHead, guard, key, cmp );
786         }
787
788         template <typename Q, typename Func>
789         std::pair<bool, bool> update_at( head_type& refHead, Q const& key, Func f, bool bAllowInsert )
790         {
791             scoped_node_ptr pNode( alloc_node( key ));
792
793             std::pair<bool, bool> ret = base_class::update_at( refHead, *pNode,
794                 [&f, &key](bool bNew, node_type& node, node_type&){ f( bNew, node_to_value(node), key );},
795                 bAllowInsert );
796             if ( ret.first && ret.second )
797                 pNode.release();
798
799             return ret;
800         }
801
802         template <typename Q, typename Compare>
803         bool find_at( head_type& refHead, Q const& key, Compare cmp )
804         {
805             return base_class::find_at( refHead, key, cmp );
806         }
807
808         template <typename Q, typename Compare, typename Func>
809         bool find_at( head_type& refHead, Q& val, Compare cmp, Func f )
810         {
811             return base_class::find_at( refHead, val, cmp, [&f](node_type& node, Q& v){ f( node_to_value(node), v ); });
812         }
813
814         template <typename Q, typename Compare>
815         bool get_at( head_type& refHead, typename guarded_ptr::native_guard& guard, Q const& key, Compare cmp )
816         {
817             return base_class::get_at( refHead, guard, key, cmp );
818         }
819
820         //@endcond
821     };
822
823 }}  // namespace cds::container
824
825 #endif  // #ifndef CDSLIB_CONTAINER_IMPL_MICHAEL_LIST_H