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