Remove CDS_CXX11_LAMBDA_SUPPORT macro and a lot of emulating code
[libcds.git] / cds / container / lazy_list_impl.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_CONTAINER_LAZY_LIST_IMPL_H
4 #define __CDS_CONTAINER_LAZY_LIST_IMPL_H
5
6 #include <memory>
7 #include <cds/container/details/guarded_ptr_cast.h>
8
9 namespace cds { namespace container {
10
11     /// Lazy ordered list
12     /** @ingroup cds_nonintrusive_list
13         \anchor cds_nonintrusive_LazyList_gc
14
15         Usually, ordered single-linked list is used as a building block for the hash table implementation.
16         The complexity of searching is <tt>O(N)</tt>.
17
18         Source:
19         - [2005] Steve Heller, Maurice Herlihy, Victor Luchangco, Mark Moir, William N. Scherer III, and Nir Shavit
20         "A Lazy Concurrent List-Based Set Algorithm"
21
22         The lazy list is based on an optimistic locking scheme for inserts and removes,
23         eliminating the need to use the equivalent of an atomically markable
24         reference. It also has a novel wait-free membership \p find operation
25         that does not need to perform cleanup operations and is more efficient.
26
27         It is non-intrusive version of cds::intrusive::LazyList class
28
29         Template arguments:
30         - \p GC - garbage collector used
31         - \p T - type stored in the list. The type must be default- and copy-constructible.
32         - \p Traits - type traits, default is lazy_list::type_traits
33
34         Unlike standard container, this implementation does not divide type \p T into key and value part and
35         may be used as main building block for hash set algorithms.
36
37         The key is a function (or a part) of type \p T, and this function is specified by <tt> Traits::compare </tt> functor
38         or <tt> Traits::less </tt> predicate
39
40         LazyKVList is a key-value version of lazy non-intrusive list that is closer to the C++ std library approach.
41
42         It is possible to declare option-based list with cds::container::lazy_list::make_traits metafunction istead of \p Traits template
43         argument. For example, the following traits-based declaration of gc::HP lazy list
44         \code
45         #include <cds/container/lazy_list_hp.h>
46         // Declare comparator for the item
47         struct my_compare {
48             int operator ()( int i1, int i2 )
49             {
50                 return i1 - i2;
51             }
52         };
53
54         // Declare type_traits
55         struct my_traits: public cds::container::lazy_list::type_traits
56         {
57             typedef my_compare compare;
58         };
59
60         // Declare traits-based list
61         typedef cds::container::LazyList< cds::gc::HP, int, my_traits >     traits_based_list;
62         \endcode
63
64         is equivalent for the following option-based list
65         \code
66         #include <cds/container/lazy_list_hp.h>
67
68         // my_compare is the same
69
70         // Declare option-based list
71         typedef cds::container::LazyList< cds::gc::HP, int,
72             typename cds::container::lazy_list::make_traits<
73                 cds::container::opt::compare< my_compare >     // item comparator option
74             >::type
75         >     option_based_list;
76         \endcode
77
78         Template argument list \p Options of cds::container::lazy_list::make_traits metafunction are:
79         - opt::lock_type - lock type for per-node locking. Default is cds::lock::Spin. Note that <b>each</b> node
80             of the list has member of type \p lock_type, therefore, heavy-weighted locking primitive is not
81             acceptable as candidate for \p lock_type.
82         - opt::compare - key compare functor. No default functor is provided.
83             If the option is not specified, the opt::less is used.
84         - opt::less - specifies binary predicate used for key compare. Default is \p std::less<T>.
85         - opt::back_off - back-off strategy used. If the option is not specified, the cds::backoff::empty is used.
86         - opt::item_counter - the type of item counting feature. Default is \ref atomicity::empty_item_counter that is no item counting.
87         - opt::allocator - the allocator used for creating and freeing list's item. Default is \ref CDS_DEFAULT_ALLOCATOR macro.
88         - opt::memory_model - C++ memory ordering model. Can be opt::v::relaxed_ordering (relaxed memory model, the default)
89             or opt::v::sequential_consistent (sequentially consisnent memory model).
90
91         \par Usage
92         There are different specializations of this template for each garbage collecting schema used.
93         You should include appropriate .h-file depending on GC you are using:
94         - for gc::HP: \code #include <cds/container/lazy_list_hp.h> \endcode
95         - for gc::PTB: \code #include <cds/container/lazy_list_ptb.h> \endcode
96         - for gc::HRC: \code #include <cds/container/lazy_list_hrc.h> \endcode
97         - for \ref cds_urcu_desc "RCU": \code #include <cds/container/lazy_list_rcu.h> \endcode
98         - for gc::nogc: \code #include <cds/container/lazy_list_nogc.h> \endcode
99     */
100     template <
101         typename GC,
102         typename T,
103 #ifdef CDS_DOXYGEN_INVOKED
104         typename Traits = lazy_list::type_traits
105 #else
106         typename Traits
107 #endif
108     >
109     class LazyList:
110 #ifdef CDS_DOXYGEN_INVOKED
111         protected intrusive::LazyList< GC, T, Traits >
112 #else
113         protected details::make_lazy_list< GC, T, Traits >::type
114 #endif
115     {
116         //@cond
117         typedef details::make_lazy_list< GC, T, Traits > options;
118         typedef typename options::type  base_class;
119         //@endcond
120
121     public:
122         typedef T                                   value_type      ;   ///< Type of value stored in the list
123         typedef typename base_class::gc             gc              ;   ///< Garbage collector used
124         typedef typename base_class::back_off       back_off        ;   ///< Back-off strategy used
125         typedef typename options::allocator_type    allocator_type  ;   ///< Allocator type used for allocate/deallocate the nodes
126         typedef typename base_class::item_counter   item_counter    ;   ///< Item counting policy used
127         typedef typename options::key_comparator    key_comparator  ;   ///< key comparison functor
128         typedef typename base_class::memory_model   memory_model    ;   ///< Memory ordering. See cds::opt::memory_model option
129
130     protected:
131         //@cond
132         typedef typename base_class::value_type     node_type;
133         typedef typename options::cxx_allocator     cxx_allocator;
134         typedef typename options::node_deallocator  node_deallocator;
135         typedef typename options::type_traits::compare  intrusive_key_comparator;
136
137         typedef typename base_class::node_type      head_type;
138         //@endcond
139
140     public:
141         /// Guarded pointer
142         typedef cds::gc::guarded_ptr< gc, node_type, value_type, details::guarded_ptr_cast_set<node_type, value_type> > guarded_ptr;
143
144     private:
145         //@cond
146         static value_type& node_to_value( node_type& n )
147         {
148             return n.m_Value;
149         }
150         static value_type const& node_to_value( node_type const& n )
151         {
152             return n.m_Value;
153         }
154         //@endcond
155
156     protected:
157         //@cond
158         template <typename Q>
159         static node_type * alloc_node( Q const& v )
160         {
161             return cxx_allocator().New( v );
162         }
163
164         template <typename... Args>
165         static node_type * alloc_node( Args&&... args )
166         {
167             return cxx_allocator().MoveNew( std::forward<Args>(args)... );
168         }
169
170         static void free_node( node_type * pNode )
171         {
172             cxx_allocator().Delete( pNode );
173         }
174
175         struct node_disposer {
176             void operator()( node_type * pNode )
177             {
178                 free_node( pNode );
179             }
180         };
181         typedef std::unique_ptr< node_type, node_disposer >     scoped_node_ptr;
182
183         head_type& head()
184         {
185             return *base_class::head();
186         }
187
188         head_type const& head() const
189         {
190             return *base_class::head();
191         }
192
193         head_type& tail()
194         {
195             return *base_class::tail();
196         }
197
198         head_type const&  tail() const
199         {
200             return *base_class::tail();
201         }
202         //@endcond
203
204     protected:
205                 //@cond
206         template <bool IsConst>
207         class iterator_type: protected base_class::template iterator_type<IsConst>
208         {
209             typedef typename base_class::template iterator_type<IsConst>    iterator_base;
210
211             iterator_type( head_type const& pNode )
212                 : iterator_base( const_cast<head_type *>( &pNode ))
213             {}
214
215             iterator_type( head_type const * pNode )
216                 : iterator_base( const_cast<head_type *>( pNode ))
217             {}
218
219             friend class LazyList;
220
221         public:
222             typedef typename cds::details::make_const_type<value_type, IsConst>::pointer   value_ptr;
223             typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
224
225             iterator_type()
226             {}
227
228             iterator_type( const iterator_type& src )
229                 : iterator_base( src )
230             {}
231
232             value_ptr operator ->() const
233             {
234                 typename iterator_base::value_ptr p = iterator_base::operator ->();
235                 return p ? &(p->m_Value) : nullptr;
236             }
237
238             value_ref operator *() const
239             {
240                 return (iterator_base::operator *()).m_Value;
241             }
242
243             /// Pre-increment
244             iterator_type& operator ++()
245             {
246                 iterator_base::operator ++();
247                 return *this;
248             }
249
250             template <bool C>
251             bool operator ==(iterator_type<C> const& i ) const
252             {
253                 return iterator_base::operator ==(i);
254             }
255             template <bool C>
256             bool operator !=(iterator_type<C> const& i ) const
257             {
258                 return iterator_base::operator !=(i);
259             }
260         };
261         //@endcond
262
263     public:
264         /// Forward iterator
265         /**
266             The forward iterator for lazy list has some features:
267             - it has no post-increment operator
268             - to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator.
269               For some GC (gc::HP, gc::HRC), a guard is limited resource per thread, so an exception (or assertion) "no free guard"
270               may be thrown if a limit of guard count per thread is exceeded.
271             - The iterator cannot be moved across thread boundary since it contains GC's guard that is thread-private GC data.
272             - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
273               deleting operations it is no guarantee that you iterate all item in the list.
274
275             Therefore, the use of iterators in concurrent environment is not good idea. Use the iterator on the concurrent container
276             for debug purpose only.
277         */
278         typedef iterator_type<false>    iterator;
279
280         /// Const forward iterator
281         /**
282             For iterator's features and requirements see \ref iterator
283         */
284         typedef iterator_type<true>     const_iterator;
285
286         /// Returns a forward iterator addressing the first element in a list
287         /**
288             For empty list \code begin() == end() \endcode
289         */
290         iterator begin()
291         {
292             iterator it( head() );
293             ++it        ;   // skip dummy head node
294             return it;
295         }
296
297         /// Returns an iterator that addresses the location succeeding the last element in a list
298         /**
299             Do not use the value returned by <tt>end</tt> function to access any item.
300
301             The returned value can be used only to control reaching the end of the list.
302             For empty list \code begin() == end() \endcode
303         */
304         iterator end()
305         {
306             return iterator( tail() );
307         }
308
309         /// Returns a forward const iterator addressing the first element in a list
310         //@{
311         const_iterator begin() const
312         {
313             const_iterator it( head() );
314             ++it        ;   // skip dummy head node
315             return it;
316         }
317         const_iterator cbegin()
318         {
319             const_iterator it( head() );
320             ++it        ;   // skip dummy head node
321             return it;
322         }
323         //@}
324
325         /// Returns an const iterator that addresses the location succeeding the last element in a list
326         //@{
327         const_iterator end() const
328         {
329             return const_iterator( tail() );
330         }
331         const_iterator cend()
332         {
333             return const_iterator( tail() );
334         }
335         //@}
336
337     public:
338         /// Default constructor
339         /**
340             Initializes empty list
341         */
342         LazyList()
343         {}
344
345         /// List desctructor
346         /**
347             Clears the list
348         */
349         ~LazyList()
350         {
351             clear();
352         }
353
354         /// Inserts new node
355         /**
356             The function creates a node with copy of \p val value
357             and then inserts the node created into the list.
358
359             The type \p Q should contain as minimum the complete key of the node.
360             The object of \ref value_type should be constructible from \p val of type \p Q.
361             In trivial case, \p Q is equal to \ref value_type.
362
363             Returns \p true if inserting successful, \p false otherwise.
364         */
365         template <typename Q>
366         bool insert( Q const& val )
367         {
368             return insert_at( head(), val );
369         }
370
371         /// Inserts new node
372         /**
373             This function inserts new node with default-constructed value and then it calls
374             \p func functor with signature
375             \code void func( value_type& itemValue ) ;\endcode
376
377             The argument \p itemValue of user-defined functor \p func is the reference
378             to the list's item inserted. User-defined functor \p func should guarantee that during changing
379             item's value no any other changes could be made on this list's item by concurrent threads.
380             The user-defined functor can be passed by reference using <tt>boost::ref</tt>
381             and it is called only if the inserting is success.
382
383             The type \p Q should contain the complete key of the node.
384             The object of \ref value_type should be constructible from \p key of type \p Q.
385
386             The function allows to split creating of new item into two part:
387             - create item from \p key with initializing key-fields only;
388             - insert new item into the list;
389             - if inserting is successful, initialize non-key fields of item by calling \p f functor
390
391             This can be useful if complete initialization of object of \p value_type is heavyweight and
392             it is preferable that the initialization should be completed only if inserting is successful.
393         */
394         template <typename Q, typename Func>
395         bool insert( Q const& key, Func func )
396         {
397             return insert_at( head(), key, func );
398         }
399
400         /// Inserts data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
401         /**
402             Returns \p true if inserting successful, \p false otherwise.
403         */
404         template <typename... Args>
405         bool emplace( Args&&... args )
406         {
407             return emplace_at( head(), std::forward<Args>(args)... );
408         }
409
410         /// Ensures that the \p key exists in the list
411         /**
412             The operation performs inserting or changing data with lock-free manner.
413
414             If the \p key not found in the list, then the new item created from \p key
415             is inserted into the list. Otherwise, the functor \p func is called with the item found.
416             The functor \p Func should be a function with signature:
417             \code
418                 void func( bool bNew, value_type& item, const Q& val );
419             \endcode
420             or a functor:
421             \code
422                 struct my_functor {
423                     void operator()( bool bNew, value_type& item, const Q& val );
424                 };
425             \endcode
426
427             with arguments:
428             - \p bNew - \p true if the item has been inserted, \p false otherwise
429             - \p item - item of the list
430             - \p val - argument \p key passed into the \p ensure function
431
432             The functor may change non-key fields of the \p item; however, \p func must guarantee
433             that during changing no any other modifications could be made on this item by concurrent threads.
434
435             You may pass \p func argument by reference using <tt>boost::ref</tt>.
436
437             Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
438             \p second is true if new item has been added or \p false if the item with \p key
439             already is in the list.
440         */
441         template <typename Q, typename Func>
442         std::pair<bool, bool> ensure( Q const& key, Func f )
443         {
444             return ensure_at( head(), key, f );
445         }
446
447         /// Deletes \p key from the list
448         /** \anchor cds_nonintrusive_LazyList_hp_erase_val
449             Since the key of LazyList's item type \p T is not explicitly specified,
450             template parameter \p Q defines the key type searching in the list.
451             The list item comparator should be able to compare the type \p T of list item
452             and the type \p Q.
453
454             Return \p true if key is found and deleted, \p false otherwise
455         */
456         template <typename Q>
457         bool erase( Q const& key )
458         {
459             return erase_at( head(), key, intrusive_key_comparator(), [](value_type const&){} );
460         }
461
462         /// Deletes the item from the list using \p pred predicate for searching
463         /**
464             The function is an analog of \ref cds_nonintrusive_LazyList_hp_erase_val "erase(Q const&)"
465             but \p pred is used for key comparing.
466             \p Less functor has the interface like \p std::less.
467             \p pred must imply the same element order as the comparator used for building the list.
468         */
469         template <typename Q, typename Less>
470         bool erase_with( Q const& key, Less pred )
471         {
472             return erase_at( head(), key, typename options::template less_wrapper<Less>::type(), [](value_type const&){} );
473         }
474
475         /// Deletes \p key from the list
476         /** \anchor cds_nonintrusive_LazyList_hp_erase_func
477             The function searches an item with key \p key, calls \p f functor with item found
478             and deletes the item. If \p key is not found, the functor is not called.
479
480             The functor \p Func interface:
481             \code
482             struct extractor {
483                 void operator()(const value_type& val) { ... }
484             };
485             \endcode
486             The functor may be passed by reference with <tt>boost:ref</tt>
487
488             Since the key of LazyList's item type \p T is not explicitly specified,
489             template parameter \p Q defines the key type searching in the list.
490             The list item comparator should be able to compare the type \p T of list item
491             and the type \p Q.
492
493             Return \p true if key is found and deleted, \p false otherwise
494
495             See also: \ref erase
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_LazyList_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             return erase_at( head(), key, typename options::template less_wrapper<Less>::type(), f );
514         }
515
516         /// Extracts the item from the list with specified \p key
517         /** \anchor cds_nonintrusive_LazyList_hp_extract
518             The function searches an item with key equal to \p key,
519             unlinks it from the list, and returns it in \p dest parameter.
520             If the item with key equal to \p key is not found the function returns \p false.
521
522             Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
523
524             @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
525
526             Usage:
527             \code
528             typedef cds::container::LazyList< cds::gc::HP, foo, my_traits >  ord_list;
529             ord_list theList;
530             // ...
531             {
532                 ord_list::guarded_ptr gp;
533                 theList.extract( gp, 5 );
534                 // Deal with gp
535                 // ...
536
537                 // Destructor of gp releases internal HP guard and frees the item
538             }
539             \endcode
540         */
541         template <typename Q>
542         bool extract( guarded_ptr& dest, Q const& key )
543         {
544             return extract_at( head(), dest.guard(), key, intrusive_key_comparator() );
545         }
546
547         /// Extracts the item from the list with comparing functor \p pred
548         /**
549             The function is an analog of \ref cds_nonintrusive_LazyList_hp_extract "extract(guarded_ptr&, Q const&)"
550             but \p pred predicate is used for key comparing.
551
552             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
553             in any order.
554             \p pred must imply the same element order as the comparator used for building the list.
555         */
556         template <typename Q, typename Less>
557         bool extract_with( guarded_ptr& dest, Q const& key, Less pred )
558         {
559             return extract_at( head(), dest.guard(), key, typename options::template less_wrapper<Less>::type() );
560         }
561
562         /// Finds the key \p key
563         /** \anchor cds_nonintrusive_LazyList_hp_find_val
564             The function searches the item with key equal to \p key
565             and returns \p true if it is found, and \p false otherwise
566         */
567         template <typename Q>
568         bool find( Q const& key )
569         {
570             return find_at( head(), key, intrusive_key_comparator() );
571         }
572
573         /// Finds the key \p val using \p pred predicate for searching
574         /**
575             The function is an analog of \ref cds_nonintrusive_LazyList_hp_find_val "find(Q const&)"
576             but \p pred is used for key comparing.
577             \p Less functor has the interface like \p std::less.
578             \p pred must imply the same element order as the comparator used for building the list.
579         */
580         template <typename Q, typename Less>
581         bool find_with( Q const& key, Less pred )
582         {
583             return find_at( head(), key, typename options::template less_wrapper<Less>::type() );
584         }
585
586         /// Finds the key \p val and performs an action with it
587         /** \anchor cds_nonintrusive_LazyList_hp_find_func
588             The function searches an item with key equal to \p val and calls the functor \p f for the item found.
589             The interface of \p Func functor is:
590             \code
591             struct functor {
592                 void operator()( value_type& item, Q& val );
593             };
594             \endcode
595             where \p item is the item found, \p val is the <tt>find</tt> function argument.
596
597             You may pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
598
599             The functor may change non-key fields of \p item. Note that the function is only guarantee
600             that \p item cannot be deleted during functor is executing.
601             The function does not serialize simultaneous access to the list \p item. If such access is
602             possible you must provide your own synchronization schema to exclude unsafe item modifications.
603
604             The \p val argument is non-const since it can be used as \p f functor destination i.e., the functor
605             may modify both arguments.
606
607             The function returns \p true if \p val is found, \p false otherwise.
608         */
609         template <typename Q, typename Func>
610         bool find( Q& val, Func f )
611         {
612             return find_at( head(), val, intrusive_key_comparator(), f );
613         }
614
615         /// Finds the key \p val using \p pred predicate for searching
616         /**
617             The function is an analog of \ref cds_nonintrusive_LazyList_hp_find_func "find(Q&, Func)"
618             but \p pred is used for key comparing.
619             \p Less functor has the interface like \p std::less.
620             \p pred must imply the same element order as the comparator used for building the list.
621         */
622         template <typename Q, typename Less, typename Func>
623         bool find_with( Q& val, Less pred, Func f )
624         {
625             return find_at( head(), val, typename options::template less_wrapper<Less>::type(), f );
626         }
627
628         /// Finds the key \p val and performs an action with it
629         /** \anchor cds_nonintrusive_LazyList_hp_find_cfunc
630             The function searches an item with key equal to \p val and calls the functor \p f for the item found.
631             The interface of \p Func functor is:
632             \code
633             struct functor {
634                 void operator()( value_type& item, Q const& val );
635             };
636             \endcode
637             where \p item is the item found, \p val is the <tt>find</tt> function argument.
638
639             You may pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
640
641             The function does not serialize simultaneous access to the list \p item. If such access is
642             possible you must provide your own synchronization schema to exclude unsafe item modifications.
643
644             The function returns \p true if \p val is found, \p false otherwise.
645         */
646         template <typename Q, typename Func>
647         bool find( Q const& val, Func f )
648         {
649             return find_at( head(), val, intrusive_key_comparator(), f );
650         }
651
652         /// Finds the key \p val using \p pred predicate for searching
653         /**
654             The function is an analog of \ref cds_nonintrusive_LazyList_hp_find_cfunc "find(Q&, Func)"
655             but \p pred is used for key comparing.
656             \p Less functor has the interface like \p std::less.
657             \p pred must imply the same element order as the comparator used for building the list.
658         */
659         template <typename Q, typename Less, typename Func>
660         bool find_with( Q const& val, Less pred, Func f )
661         {
662             return find_at( head(), val, typename options::template less_wrapper<Less>::type(), f );
663         }
664
665         /// Finds the key \p val and return the item found
666         /** \anchor cds_nonintrusive_LazyList_hp_get
667             The function searches the item with key equal to \p val
668             and assigns the item found to guarded pointer \p ptr.
669             The function returns \p true if \p val is found, and \p false otherwise.
670             If \p val is not found the \p ptr parameter is not changed.
671
672             @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
673
674             Usage:
675             \code
676             typedef cds::container::LazyList< cds::gc::HP, foo, my_traits >  ord_list;
677             ord_list theList;
678             // ...
679             {
680                 ord_list::guarded_ptr gp;
681                 if ( theList.get( gp, 5 )) {
682                     // Deal with gp
683                     //...
684                 }
685                 // Destructor of guarded_ptr releases internal HP guard and frees the item
686             }
687             \endcode
688
689             Note the compare functor specified for class \p Traits template parameter
690             should accept a parameter of type \p Q that can be not the same as \p value_type.
691         */
692         template <typename Q>
693         bool get( guarded_ptr& ptr, Q const& val )
694         {
695             return get_at( head(), ptr.guard(), val, intrusive_key_comparator() );
696         }
697
698         /// Finds the key \p val and return the item found
699         /**
700             The function is an analog of \ref cds_nonintrusive_LazyList_hp_get "get( guarded_ptr& ptr, Q const&)"
701             but \p pred is used for comparing the keys.
702
703             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
704             in any order.
705             \p pred must imply the same element order as the comparator used for building the list.
706         */
707         template <typename Q, typename Less>
708         bool get_with( guarded_ptr& ptr, Q const& val, Less pred )
709         {
710             return get_at( head(), ptr.guard(), val, typename options::template less_wrapper<Less>::type() );
711         }
712
713         /// Checks 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 opt::item_counter option. For atomicity::empty_item_counter,
722             this function always returns 0.
723
724             <b>Warning</b>: 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 \ref empty() method.
726         */
727         size_t size() const
728         {
729             return base_class::size();
730         }
731
732         /// Clears the list
733         /**
734             Post-condition: the list is empty
735         */
736         void clear()
737         {
738             base_class::clear();
739         }
740
741     protected:
742         //@cond
743         bool insert_node_at( head_type& refHead, node_type * pNode )
744         {
745             assert( pNode != nullptr );
746             scoped_node_ptr p( pNode );
747
748             if ( base_class::insert_at( &refHead, *pNode )) {
749                 p.release();
750                 return true;
751             }
752
753             return false;
754         }
755
756         template <typename Q>
757         bool insert_at( head_type& refHead, const Q& val )
758         {
759             return insert_node_at( refHead, alloc_node( val ));
760         }
761
762         template <typename... Args>
763         bool emplace_at( head_type& refHead, Args&&... args )
764         {
765             return insert_node_at( refHead, alloc_node( std::forward<Args>(args)... ));
766         }
767
768         template <typename Q, typename Func>
769         bool insert_at( head_type& refHead, const Q& key, Func f )
770         {
771             scoped_node_ptr pNode( alloc_node( key ));
772
773             if ( base_class::insert_at( &refHead, *pNode, [&f](node_type& node){ cds::unref(f)( node_to_value(node) ); } )) {
774                 pNode.release();
775                 return true;
776             }
777             return false;
778         }
779
780         template <typename Q, typename Compare, typename Func>
781         bool erase_at( head_type& refHead, const Q& key, Compare cmp, Func f )
782         {
783             return base_class::erase_at( &refHead, key, cmp, [&f](node_type const& node){ cds::unref(f)( node_to_value(node) ); } );
784         }
785
786         template <typename Q, typename Compare>
787         bool extract_at( head_type& refHead, typename gc::Guard& dest, Q const& key, Compare cmp )
788         {
789             return base_class::extract_at( &refHead, dest, key, cmp );
790         }
791
792         template <typename Q, typename Func>
793         std::pair<bool, bool> ensure_at( head_type& refHead, const Q& key, Func f )
794         {
795             scoped_node_ptr pNode( alloc_node( key ));
796
797             std::pair<bool, bool> ret = base_class::ensure_at( &refHead, *pNode,
798                 [&f, &key](bool bNew, node_type& node, node_type&){cds::unref(f)( bNew, node_to_value(node), key ); });
799             if ( ret.first && ret.second )
800                 pNode.release();
801
802             return ret;
803         }
804
805         template <typename Q, typename Compare>
806         bool find_at( head_type& refHead, Q const& key, Compare cmp )
807         {
808             return base_class::find_at( &refHead, key, cmp );
809         }
810
811         template <typename Q, typename Compare, typename Func>
812         bool find_at( head_type& refHead, Q& val, Compare cmp, Func f )
813         {
814             return base_class::find_at( &refHead, val, cmp, [&f](node_type& node, Q& val){ cds::unref(f)( node_to_value(node), val ); });
815         }
816
817         template <typename Q, typename Compare>
818         bool get_at( head_type& refHead, typename gc::Guard& guard, Q const& key, Compare cmp )
819         {
820             return base_class::get_at( &refHead, guard, key, cmp );
821         }
822
823         //@endcond
824     };
825
826 }} // namespace cds::container
827
828 #endif // #ifndef __CDS_CONTAINER_LAZY_LIST_IMPL_H