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