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