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