Fixed explicit ctor stuff
[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 for 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 instead 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         typedef typename base_class::stat         stat;           ///< Internal statistics
140
141         static CDS_CONSTEXPR const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount; ///< Count of hazard pointer required for the algorithm
142
143     protected:
144         //@cond
145         typedef typename base_class::value_type   node_type;
146         typedef typename maker::cxx_allocator     cxx_allocator;
147         typedef typename maker::node_deallocator  node_deallocator;
148         typedef typename maker::intrusive_traits::compare  intrusive_key_comparator;
149
150         typedef typename base_class::atomic_node_ptr head_type;
151         //@endcond
152
153     public:
154         /// Guarded pointer
155         typedef typename gc::template guarded_ptr< node_type, value_type, details::guarded_ptr_cast_map<node_type, value_type> > guarded_ptr;
156
157     protected:
158         //@cond
159         template <typename K>
160         static node_type * alloc_node(const K& key)
161         {
162             return cxx_allocator().New( key );
163         }
164
165         template <typename K, typename V>
166         static node_type * alloc_node( const K& key, const V& val )
167         {
168             return cxx_allocator().New( key, val );
169         }
170
171         template <typename K, typename... Args>
172         static node_type * alloc_node( K&& key, Args&&... args )
173         {
174             return cxx_allocator().MoveNew( std::forward<K>(key), std::forward<Args>(args)...);
175         }
176
177         static void free_node( node_type * pNode )
178         {
179             cxx_allocator().Delete( pNode );
180         }
181
182         struct node_disposer {
183             void operator()( node_type * pNode )
184             {
185                 free_node( pNode );
186             }
187         };
188         typedef std::unique_ptr< node_type, node_disposer >     scoped_node_ptr;
189
190         head_type& head()
191         {
192             return base_class::m_pHead;
193         }
194
195         head_type const& head() const
196         {
197             return base_class::m_pHead;
198         }
199         //@endcond
200
201     protected:
202         //@cond
203         template <bool IsConst>
204         class iterator_type: protected base_class::template iterator_type<IsConst>
205         {
206             typedef typename base_class::template iterator_type<IsConst>    iterator_base;
207
208             iterator_type( head_type const& pNode )
209                 : iterator_base( pNode )
210             {}
211
212             friend class MichaelKVList;
213
214         public:
215             typedef typename cds::details::make_const_type<mapped_type, IsConst>::reference  value_ref;
216             typedef typename cds::details::make_const_type<mapped_type, IsConst>::pointer    value_ptr;
217
218             typedef typename cds::details::make_const_type<value_type,  IsConst>::reference  pair_ref;
219             typedef typename cds::details::make_const_type<value_type,  IsConst>::pointer    pair_ptr;
220
221             iterator_type()
222             {}
223
224             iterator_type( iterator_type const& src )
225                 : iterator_base( src )
226             {}
227
228             key_type const& key() const
229             {
230                 typename iterator_base::value_ptr p = iterator_base::operator ->();
231                 assert( p != nullptr );
232                 return p->m_Data.first;
233             }
234
235             pair_ptr operator ->() const
236             {
237                 typename iterator_base::value_ptr p = iterator_base::operator ->();
238                 return p ? &(p->m_Data) : nullptr;
239             }
240
241             pair_ref operator *() const
242             {
243                 typename iterator_base::value_ref p = iterator_base::operator *();
244                 return p.m_Data;
245             }
246
247             value_ref val() const
248             {
249                 typename iterator_base::value_ptr p = iterator_base::operator ->();
250                 assert( p != nullptr );
251                 return p->m_Data.second;
252             }
253
254             /// Pre-increment
255             iterator_type& operator ++()
256             {
257                 iterator_base::operator ++();
258                 return *this;
259             }
260
261             template <bool C>
262             bool operator ==(iterator_type<C> const& i ) const
263             {
264                 return iterator_base::operator ==(i);
265             }
266             template <bool C>
267             bool operator !=(iterator_type<C> const& i ) const
268             {
269                 return iterator_base::operator !=(i);
270             }
271         };
272         //@endcond
273
274     public:
275         /// Forward iterator
276         /**
277             The forward iterator for Michael's list has some features:
278             - it has no post-increment operator
279             - to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator.
280               For some GC (\p gc::HP), a guard is limited resource per thread, so an exception (or assertion) "no free guard"
281               may be thrown if a limit of guard count per thread is exceeded.
282             - The iterator cannot be moved across thread boundary since it contains GC's guard that is thread-private GC data.
283             - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
284               deleting operations it is no guarantee that you iterate all item in the list.
285
286             @warning Use this iterator on the concurrent container for debugging purpose only.
287
288             The iterator interface to access item data:
289             - <tt> operator -> </tt> - returns a pointer to \ref value_type for iterator
290             - <tt> operator *</tt> - returns a reference (a const reference for \p const_iterator) to \ref value_type for iterator
291             - <tt> const key_type& key() </tt> - returns a key reference for iterator
292             - <tt> mapped_type& val() </tt> - retuns a value reference for iterator (const reference for \p const_iterator)
293
294             For both functions the iterator should not be equal to <tt> end() </tt>
295         */
296         typedef iterator_type<false>    iterator;
297
298         /// Const forward iterator
299         /**
300             For iterator's features and requirements see \ref iterator
301         */
302         typedef iterator_type<true>     const_iterator;
303
304     ///@name Forward iterators (only for debugging purpose)
305     //@{
306         /// Returns a forward iterator addressing the first element in a list
307         /**
308             For empty list \code begin() == end() \endcode
309         */
310         iterator begin()
311         {
312             return iterator( head() );
313         }
314
315         /// Returns an iterator that addresses the location succeeding the last element in a list
316         /**
317             Do not use the value returned by <tt>end</tt> function to access any item.
318             Internally, <tt>end</tt> returning value equals to \p nullptr.
319
320             The returned value can be used only to control reaching the end of the list.
321             For empty list \code begin() == end() \endcode
322         */
323         iterator end()
324         {
325             return iterator();
326         }
327
328         /// Returns a forward const iterator addressing the first element in a list
329         const_iterator begin() const
330         {
331             return const_iterator( head() );
332         }
333
334         /// Returns a forward const iterator addressing the first element in a list
335         const_iterator cbegin() const
336         {
337             return const_iterator( head() );
338         }
339
340         /// Returns an const iterator that addresses the location succeeding the last element in a list
341         const_iterator end() const
342         {
343             return const_iterator();
344         }
345
346         /// Returns an const iterator that addresses the location succeeding the last element in a list
347         const_iterator cend() const
348         {
349             return const_iterator();
350         }
351     //@}
352
353     public:
354         /// Default constructor
355         /**
356             Initializes empty list
357         */
358         MichaelKVList()
359         {}
360
361         //@cond
362         template <typename Stat, typename = std::enable_if<std::is_same<stat, michael_list::wrapped_stat<Stat>>::value >>
363         explicit MichaelKVList( Stat& st )
364             : base_class( st )
365         {}
366         //@endcond
367
368         /// List destructor
369         /**
370             Clears the list
371         */
372         ~MichaelKVList()
373         {
374             clear();
375         }
376
377         /// Inserts new node with key and default value
378         /**
379             The function creates a node with \p key and default value, and then inserts the node created into the list.
380
381             Preconditions:
382             - The \p key_type should be constructible from value of type \p K.
383                 In trivial case, \p K is equal to \p key_type.
384             - The \p mapped_type should be default-constructible.
385
386             Returns \p true if inserting successful, \p false otherwise.
387         */
388         template <typename K>
389         bool insert( const K& key )
390         {
391             return insert_at( head(), key );
392         }
393
394         /// Inserts new node with a key and a value
395         /**
396             The function creates a node with \p key and value \p val, and then inserts the node created into the list.
397
398             Preconditions:
399             - The \p key_type should be constructible from \p key of type \p K.
400             - The \p mapped_type should be constructible from \p val of type \p V.
401
402             Returns \p true if inserting successful, \p false otherwise.
403         */
404         template <typename K, typename V>
405         bool insert( const K& key, const V& val )
406         {
407             // We cannot use insert with functor here
408             // because we cannot lock inserted node for updating
409             // Therefore, we use separate function
410             return insert_at( head(), key, val );
411         }
412
413         /// Inserts new node and initialize it by a functor
414         /**
415             This function inserts new node with key \p key and if inserting is successful then it calls
416             \p func functor with signature
417             \code
418                 struct functor {
419                     void operator()( value_type& item );
420                 };
421             \endcode
422
423             The argument \p item of user-defined functor \p func is the reference
424             to the item inserted. <tt>item.second</tt> is a reference to item's value that may be changed.
425             User-defined functor \p func should guarantee that during changing item's value no any other changes
426             could be made on this list's item by concurrent threads.
427             The user-defined functor is called only if inserting is successful.
428
429             The \p key_type should be constructible from value of type \p K.
430
431             The function allows to split creating of new item into two part:
432             - create a new item from \p key;
433             - insert the new item into the list;
434             - if inserting is successful, initialize the value of item by calling \p func functor
435
436             This can be useful if complete initialization of object of \p mapped_type is heavyweight and
437             it is preferable that the initialization should be completed only if inserting is successful.
438
439             @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
440         */
441         template <typename K, typename Func>
442         bool insert_with( const K& key, Func func )
443         {
444             return insert_with_at( head(), key, func );
445         }
446
447         /// Updates data by \p key
448         /**
449             The operation performs inserting or replacing the element with lock-free manner.
450
451             If the \p key not found in the list, then the new item created from \p key
452             will be inserted iff \p bAllowInsert is \p true.
453             (note that in this case the \ref key_type should be constructible from type \p K).
454             Otherwise, if \p key is found, the functor \p func is called with item found.
455
456             The functor \p Func signature is:
457             \code
458                 struct my_functor {
459                     void operator()( bool bNew, value_type& item );
460                 };
461             \endcode
462             with arguments:
463             - \p bNew - \p true if the item has been inserted, \p false otherwise
464             - \p item - the item found or inserted
465
466             The functor may change any fields of the \p item.second of \p mapped_type;
467             however, \p func must guarantee that during changing no any other modifications
468             could be made on this item by concurrent threads.
469
470             Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successful,
471             \p second is true if new item has been added or \p false if the item with \p key
472             already exists.
473
474             @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
475         */
476         template <typename K, typename Func>
477         std::pair<bool, bool> update( K const& key, Func f, bool bAllowInsert = true )
478         {
479             return update_at( head(), key, f, bAllowInsert );
480         }
481         //@cond
482         template <typename K, typename Func>
483         CDS_DEPRECATED("ensure() is deprecated, use update()")
484         std::pair<bool, bool> ensure( K const& key, Func f )
485         {
486             return update( key, f, true );
487         }
488         //@endcond
489
490         /// Inserts a new node using move semantics
491         /**
492             \p key_type field of new item is constructed from \p key argument,
493             \p mapped_type field is done from \p args.
494
495             Returns \p true if inserting successful, \p false otherwise.
496         */
497         template <typename K, typename... Args>
498         bool emplace( K&& key, Args&&... args )
499         {
500             return emplace_at( head(), std::forward<K>(key), std::forward<Args>(args)... );
501         }
502
503         /// Deletes \p key from the list
504         /** \anchor cds_nonintrusive_MichaelKVList_hp_erase_val
505
506             Returns \p true if \p key is found and has been deleted, \p false otherwise
507         */
508         template <typename K>
509         bool erase( K const& key )
510         {
511             return erase_at( head(), key, intrusive_key_comparator() );
512         }
513
514         /// Deletes the item from the list using \p pred predicate for searching
515         /**
516             The function is an analog of \ref cds_nonintrusive_MichaelKVList_hp_erase_val "erase(K const&)"
517             but \p pred is used for key comparing.
518             \p Less functor has the interface like \p std::less.
519             \p pred must imply the same element order as the comparator used for building the list.
520         */
521         template <typename K, typename Less>
522         bool erase_with( K const& key, Less pred )
523         {
524             CDS_UNUSED( pred );
525             return erase_at( head(), key, typename maker::template less_wrapper<Less>::type() );
526         }
527
528         /// Deletes \p key from the list
529         /** \anchor cds_nonintrusive_MichaelKVList_hp_erase_func
530             The function searches an item with key \p key, calls \p f functor
531             and deletes the item. If \p key is not found, the functor is not called.
532
533             The functor \p Func interface:
534             \code
535             struct extractor {
536                 void operator()(value_type& val) { ... }
537             };
538             \endcode
539
540             Return \p true if key is found and deleted, \p false otherwise
541
542             See also: \ref erase
543         */
544         template <typename K, typename Func>
545         bool erase( K const& key, Func f )
546         {
547             return erase_at( head(), key, intrusive_key_comparator(), f );
548         }
549
550         /// Deletes the item from the list using \p pred predicate for searching
551         /**
552             The function is an analog of \ref cds_nonintrusive_MichaelKVList_hp_erase_func "erase(K const&, Func)"
553             but \p pred is used for key comparing.
554             \p Less functor has the interface like \p std::less.
555             \p pred must imply the same element order as the comparator used for building the list.
556         */
557         template <typename K, typename Less, typename Func>
558         bool erase_with( K const& key, Less pred, Func f )
559         {
560             CDS_UNUSED( pred );
561             return erase_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
562         }
563
564         /// Extracts the item from the list with specified \p key
565         /** \anchor cds_nonintrusive_MichaelKVList_hp_extract
566             The function searches an item with key equal to \p key,
567             unlinks it from the list, and returns it as \p guarded_ptr.
568             If \p key is not found the function returns an empty guarded pointer.
569
570             Note the compare functor should accept a parameter of type \p K that can be not the same as \p key_type.
571
572             The \p disposer specified in \p Traits class template parameter is called automatically
573             by garbage collector \p GC specified in class' template parameters when returned \p guarded_ptr object
574             will be destroyed or released.
575             @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
576
577             Usage:
578             \code
579             typedef cds::container::MichaelKVList< cds::gc::HP, int, foo, my_traits >  ord_list;
580             ord_list theList;
581             // ...
582             {
583                 ord_list::guarded_ptr gp(theList.extract( 5 ));
584                 if ( gp ) {
585                     // Deal with gp
586                     // ...
587                 }
588                 // Destructor of gp releases internal HP guard
589             }
590             \endcode
591         */
592         template <typename K>
593         guarded_ptr extract( K const& key )
594         {
595             guarded_ptr gp;
596             extract_at( head(), gp.guard(), key, intrusive_key_comparator() );
597             return gp;
598         }
599
600         /// Extracts the item from the list with comparing functor \p pred
601         /**
602             The function is an analog of \ref cds_nonintrusive_MichaelKVList_hp_extract "extract(K const&)"
603             but \p pred predicate is used for key comparing.
604
605             \p Less functor has the semantics like \p std::less but should take arguments of type \ref key_type and \p K
606             in any order.
607             \p pred must imply the same element order as the comparator used for building the list.
608         */
609         template <typename K, typename Less>
610         guarded_ptr extract_with( K const& key, Less pred )
611         {
612             CDS_UNUSED( pred );
613             guarded_ptr gp;
614             extract_at( head(), gp.guard(), key, typename maker::template less_wrapper<Less>::type() );
615             return gp;
616         }
617
618         /// Checks whether the list contains \p key
619         /**
620             The function searches the item with key equal to \p key
621             and returns \p true if it is found, and \p false otherwise.
622         */
623         template <typename Q>
624         bool contains( Q const& key )
625         {
626             return find_at( head(), key, intrusive_key_comparator() );
627         }
628         //@cond
629         template <typename Q>
630         CDS_DEPRECATED("deprecated, use contains()")
631         bool find( Q const& key )
632         {
633             return contains( key );
634         }
635         //@endcond
636
637         /// Checks whether the map contains \p key using \p pred predicate for searching
638         /**
639             The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
640             \p Less functor has the interface like \p std::less.
641             \p Less must imply the same element order as the comparator used for building the list.
642         */
643         template <typename Q, typename Less>
644         bool contains( Q const& key, Less pred )
645         {
646             CDS_UNUSED( pred );
647             return find_at( head(), key, typename maker::template less_wrapper<Less>::type() );
648         }
649         //@cond
650         template <typename Q, typename Less>
651         CDS_DEPRECATED("deprecated, use contains()")
652         bool find_with( Q const& key, Less pred )
653         {
654             CDS_UNUSED( pred );
655             return contains( key, pred );
656         }
657         //@endcond
658
659         /// Finds the key \p key and performs an action with it
660         /** \anchor cds_nonintrusive_MichaelKVList_hp_find_func
661             The function searches an item with key equal to \p key and calls the functor \p f for the item found.
662             The interface of \p Func functor is:
663             \code
664             struct functor {
665                 void operator()( value_type& item );
666             };
667             \endcode
668             where \p item is the item found.
669
670             The functor may change <tt>item.second</tt> that is reference to value of node.
671             Note that the function is only guarantee that \p item cannot be deleted during functor is executing.
672             The function does not serialize simultaneous access to the list \p item. If such access is
673             possible you must provide your own synchronization schema to exclude unsafe item modifications.
674
675             The function returns \p true if \p key is found, \p false otherwise.
676         */
677         template <typename Q, typename Func>
678         bool find( Q const& key, Func f )
679         {
680             return find_at( head(), key, intrusive_key_comparator(), f );
681         }
682
683         /// Finds the key \p val using \p pred predicate for searching
684         /**
685             The function is an analog of \ref cds_nonintrusive_MichaelKVList_hp_find_func "find(Q&, Func)"
686             but \p pred is used for key comparing.
687             \p Less functor has the interface like \p std::less.
688             \p pred must imply the same element order as the comparator used for building the list.
689         */
690         template <typename Q, typename Less, typename Func>
691         bool find_with( Q const& key, Less pred, Func f )
692         {
693             CDS_UNUSED( pred );
694             return find_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
695         }
696
697         /// Finds the \p key and return the item found
698         /** \anchor cds_nonintrusive_MichaelKVList_hp_get
699             The function searches the item with key equal to \p key
700             and returns it as \p guarded_ptr.
701             If \p key is not found the function returns an empty guarded pointer.
702
703             @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
704
705             Usage:
706             \code
707             typedef cds::container::MichaelKVList< cds::gc::HP, int, foo, my_traits >  ord_list;
708             ord_list theList;
709             // ...
710             {
711                 ord_list::guarded_ptr gp(theList.get( 5 ));
712                 if ( gp ) {
713                     // Deal with gp
714                     //...
715                 }
716                 // Destructor of guarded_ptr releases internal HP guard
717             }
718             \endcode
719
720             Note the compare functor specified for class \p Traits template parameter
721             should accept a parameter of type \p K that can be not the same as \p key_type.
722         */
723         template <typename K>
724         guarded_ptr get( K const& key )
725         {
726             guarded_ptr gp;
727             get_at( head(), gp.guard(), key, intrusive_key_comparator() );
728             return gp;
729         }
730
731         /// Finds the \p key and return the item found
732         /**
733             The function is an analog of \ref cds_nonintrusive_MichaelKVList_hp_get "get( guarded_ptr& ptr, K const&)"
734             but \p pred is used for comparing the keys.
735
736             \p Less functor has the semantics like \p std::less but should take arguments of type \ref key_type and \p K
737             in any order.
738             \p pred must imply the same element order as the comparator used for building the list.
739         */
740         template <typename K, typename Less>
741         guarded_ptr get_with( K const& key, Less pred )
742         {
743             CDS_UNUSED( pred );
744             guarded_ptr gp;
745             get_at( head(), gp.guard(), key, typename maker::template less_wrapper<Less>::type() );
746             return gp;
747         }
748
749         /// Checks if the list is empty
750         bool empty() const
751         {
752             return base_class::empty();
753         }
754
755         /// Returns list's item count
756         /**
757             The value returned depends on item counter provided by \p Traits. For \p atomicity::empty_item_counter,
758             this function always returns 0.
759
760             @note Even if you use real item counter and it returns 0, this fact is not mean that the list
761             is empty. To check list emptyness use \p empty() method.
762         */
763         size_t size() const
764         {
765             return base_class::size();
766         }
767
768         /// Clears the list
769         void clear()
770         {
771             base_class::clear();
772         }
773
774         /// Returns const reference to internal statistics
775         stat const& statistics() const
776         {
777             return base_class::statistics();
778         }
779
780     protected:
781         //@cond
782         bool insert_node_at( head_type& refHead, node_type * pNode )
783         {
784             assert( pNode != nullptr );
785             scoped_node_ptr p( pNode );
786             if ( base_class::insert_at( refHead, *pNode )) {
787                 p.release();
788                 return true;
789             }
790             return false;
791         }
792
793         template <typename K>
794         bool insert_at( head_type& refHead, const K& key )
795         {
796             return insert_node_at( refHead, alloc_node( key ));
797         }
798
799         template <typename K, typename V>
800         bool insert_at( head_type& refHead, const K& key, const V& val )
801         {
802             return insert_node_at( refHead, alloc_node( key, val ));
803         }
804
805         template <typename K, typename Func>
806         bool insert_with_at( head_type& refHead, const K& key, Func f )
807         {
808             scoped_node_ptr pNode( alloc_node( key ));
809
810             if ( base_class::insert_at( refHead, *pNode, [&f](node_type& node){ f( node.m_Data ); })) {
811                 pNode.release();
812                 return true;
813             }
814             return false;
815         }
816
817         template <typename K, typename... Args>
818         bool emplace_at( head_type& refHead, K&& key, Args&&... args )
819         {
820             return insert_node_at( refHead, alloc_node( std::forward<K>(key), std::forward<Args>(args)... ));
821         }
822
823         template <typename K, typename Func>
824         std::pair<bool, bool> update_at( head_type& refHead, const K& key, Func f, bool bAllowInsert )
825         {
826             scoped_node_ptr pNode( alloc_node( key ));
827
828             std::pair<bool, bool> ret = base_class::update_at( refHead, *pNode,
829                 [&f]( bool bNew, node_type& node, node_type& ){ f( bNew, node.m_Data ); },
830                 bAllowInsert );
831             if ( ret.first && ret.second )
832                 pNode.release();
833
834             return ret;
835         }
836
837         template <typename K, typename Compare>
838         bool erase_at( head_type& refHead, K const& key, Compare cmp )
839         {
840             return base_class::erase_at( refHead, key, cmp );
841         }
842
843         template <typename K, typename Compare, typename Func>
844         bool erase_at( head_type& refHead, K const& key, Compare cmp, Func f )
845         {
846             return base_class::erase_at( refHead, key, cmp, [&f]( node_type const & node ){ f( const_cast<value_type&>(node.m_Data)); });
847         }
848         template <typename K, typename Compare>
849         bool extract_at( head_type& refHead, typename guarded_ptr::native_guard& guard, K const& key, Compare cmp )
850         {
851             return base_class::extract_at( refHead, guard, key, cmp );
852         }
853
854         template <typename K, typename Compare>
855         bool find_at( head_type& refHead, K const& key, Compare cmp )
856         {
857             return base_class::find_at( refHead, key, cmp );
858         }
859
860         template <typename K, typename Compare, typename Func>
861         bool find_at( head_type& refHead, K& key, Compare cmp, Func f )
862         {
863             return base_class::find_at( refHead, key, cmp, [&f](node_type& node, K const&){ f( node.m_Data ); });
864         }
865
866         template <typename K, typename Compare>
867         bool get_at( head_type& refHead, typename guarded_ptr::native_guard& guard, K const& key, Compare cmp )
868         {
869             return base_class::get_at( refHead, guard, key, cmp );
870         }
871
872         //@endcond
873     };
874
875 }}  // namespace cds::container
876
877 #endif  // #ifndef CDSLIB_CONTAINER_IMPL_MICHAEL_KVLIST_H