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