2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSLIB_CONTAINER_LAZY_KVLIST_NOGC_H
32 #define CDSLIB_CONTAINER_LAZY_KVLIST_NOGC_H
35 #include <cds/container/details/lazy_list_base.h>
36 #include <cds/intrusive/lazy_list_nogc.h>
37 #include <cds/container/details/make_lazy_kvlist.h>
39 namespace cds { namespace container {
41 /// Lazy ordered list (key-value pair, template specialization for gc::nogc)
42 /** @ingroup cds_nonintrusive_list
43 @anchor cds_nonintrusive_LazyKVList_nogc
45 This specialization is append-only list when no item
46 reclamation may be performed. The class does not support deleting of list's item.
48 See @ref cds_nonintrusive_LazyList_gc "cds::container::LazyList<cds::gc::nogc, T, Traits>"
53 #ifdef CDS_DOXYGEN_INVOKED
54 typename Traits = lazy_list::traits
59 class LazyKVList<gc::nogc, Key, Value, Traits>:
60 #ifdef CDS_DOXYGEN_INVOKED
61 protected intrusive::LazyList< gc::nogc, implementation_defined, Traits >
63 protected details::make_lazy_kvlist< cds::gc::nogc, Key, Value, Traits >::type
67 typedef details::make_lazy_kvlist< cds::gc::nogc, Key, Value, Traits > maker;
68 typedef typename maker::type base_class;
72 typedef Traits traits; ///< List traits
73 typedef cds::gc::nogc gc; ///< Garbage collector
74 #ifdef CDS_DOXYGEN_INVOKED
75 typedef Key key_type ; ///< Key type
76 typedef Value mapped_type ; ///< Type of value stored in the list
77 typedef std::pair<key_type const, mapped_type> value_type ; ///< key/value pair stored in the list
79 typedef typename maker::key_type key_type;
80 typedef typename maker::mapped_type mapped_type;
81 typedef typename maker::value_type value_type;
83 typedef typename base_class::back_off back_off; ///< Back-off strategy used
84 typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes
85 typedef typename base_class::item_counter item_counter; ///< Item counting policy used
86 typedef typename maker::key_comparator key_comparator; ///< key comparison functor
87 typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
88 typedef typename base_class::stat stat; ///< Internal statistics
89 static CDS_CONSTEXPR bool const c_bSort = base_class::c_bSort; ///< List type: ordered (\p true) or unordered (\p false)
92 // Rebind traits (split-list support)
93 template <typename... Options>
94 struct rebind_traits {
97 , key_type, mapped_type
98 , typename cds::opt::make_options< traits, Options...>::type
103 template <typename Stat>
104 using select_stat_wrapper = typename base_class::template select_stat_wrapper< Stat >;
109 typedef typename base_class::value_type node_type;
110 typedef typename maker::cxx_allocator cxx_allocator;
111 typedef typename maker::node_deallocator node_deallocator;
112 typedef typename base_class::key_comparator intrusive_key_comparator;
113 typedef typename base_class::node_type head_type;
118 template <typename K>
119 static node_type * alloc_node(const K& key)
121 return cxx_allocator().New( key );
124 template <typename K, typename V>
125 static node_type * alloc_node( const K& key, const V& val )
127 return cxx_allocator().New( key, val );
130 template <typename... Args>
131 static node_type * alloc_node( Args&&... args )
133 return cxx_allocator().MoveNew( std::forward<Args>(args)... );
136 static void free_node( node_type * pNode )
138 cxx_allocator().Delete( pNode );
141 struct node_disposer {
142 void operator()( node_type * pNode )
147 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
151 return base_class::m_Head;
154 head_type const& head() const
156 return base_class::m_Head;
161 return base_class::m_Tail;
164 head_type const& tail() const
166 return base_class::m_Tail;
172 template <bool IsConst>
173 class iterator_type: protected base_class::template iterator_type<IsConst>
175 typedef typename base_class::template iterator_type<IsConst> iterator_base;
177 iterator_type( head_type const& refNode )
178 : iterator_base( const_cast<head_type *>( &refNode ))
181 explicit iterator_type( const iterator_base& it )
182 : iterator_base( it )
185 friend class LazyKVList;
188 explicit iterator_type( node_type& pNode )
189 : iterator_base( &pNode )
193 typedef typename cds::details::make_const_type<mapped_type, IsConst>::reference value_ref;
194 typedef typename cds::details::make_const_type<mapped_type, IsConst>::pointer value_ptr;
196 typedef typename cds::details::make_const_type<value_type, IsConst>::reference pair_ref;
197 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer pair_ptr;
203 iterator_type( const iterator_type& src )
204 : iterator_base( src )
207 key_type const& key() const
209 typename iterator_base::value_ptr p = iterator_base::operator ->();
210 assert( p != nullptr );
211 return p->m_Data.first;
214 value_ref val() const
216 typename iterator_base::value_ptr p = iterator_base::operator ->();
217 assert( p != nullptr );
218 return p->m_Data.second;
221 pair_ptr operator ->() const
223 typename iterator_base::value_ptr p = iterator_base::operator ->();
224 return p ? &(p->m_Data) : nullptr;
227 pair_ref operator *() const
229 typename iterator_base::value_ref p = iterator_base::operator *();
234 iterator_type& operator ++()
236 iterator_base::operator ++();
241 iterator_type operator ++(int)
243 return iterator_base::operator ++(0);
247 bool operator ==(iterator_type<C> const& i ) const
249 return iterator_base::operator ==(i);
252 bool operator !=(iterator_type<C> const& i ) const
254 return iterator_base::operator !=(i);
260 ///@name Forward iterators
264 The forward iterator is safe: you may use it in multi-threaded enviromnent without any synchronization.
266 The forward iterator for lazy list based on \p gc::nogc has pre- and post-increment operators.
268 The iterator interface to access item data:
269 - <tt> operator -> </tt> - returns a pointer to \p value_type
270 - <tt> operator *</tt> - returns a reference (a const reference for \p const_iterator) to \p value_type
271 - <tt> const key_type& key() </tt> - returns a key reference for iterator
272 - <tt> mapped_type& val() </tt> - retuns a value reference for iterator (const reference for \p const_iterator)
274 For both functions the iterator should not be equal to \p end()
276 typedef iterator_type<false> iterator;
278 /// Const forward iterator
280 For iterator's features and requirements see \ref iterator
282 typedef iterator_type<true> const_iterator;
284 /// Returns a forward iterator addressing the first element in a list
286 For empty list \code begin() == end() \endcode
290 iterator it( head());
291 ++it ; // skip dummy head
295 /// Returns an iterator that addresses the location succeeding the last element in a list
297 Do not use the value returned by <tt>end</tt> function to access any item.
298 Internally, <tt>end</tt> returning value equals to nullptr.
300 The returned value can be used only to control reaching the end of the list.
301 For empty list \code begin() == end() \endcode
305 return iterator( tail());
308 /// Returns a forward const iterator addressing the first element in a list
309 const_iterator begin() const
311 const_iterator it( head());
312 ++it ; // skip dummy head
315 /// Returns a forward const iterator addressing the first element in a list
316 const_iterator cbegin() const
318 const_iterator it( head());
319 ++it ; // skip dummy head
323 /// Returns an const iterator that addresses the location succeeding the last element in a list
324 const_iterator end() const
326 return const_iterator( tail());
328 /// Returns an const iterator that addresses the location succeeding the last element in a list
329 const_iterator cend() const
331 return const_iterator( tail());
337 iterator node_to_iterator( node_type * pNode )
340 return iterator( *pNode );
346 /// Default constructor
351 template <typename Stat, typename = std::enable_if<std::is_same<stat, lazy_list::wrapped_stat<Stat>>::value >>
352 explicit LazyKVList( Stat& st )
357 /// Desctructor clears the list
363 /// Inserts new node with key and default value
365 The function creates a node with \p key and default value, and then inserts the node created into the list.
368 - The \ref key_type should be constructible from value of type \p K.
369 In trivial case, \p K is equal to \ref key_type.
370 - The \ref mapped_type should be default-constructible.
372 Returns an iterator pointed to inserted value, or \p end() if inserting is failed
374 template <typename K>
375 iterator insert( const K& key )
377 return node_to_iterator( insert_at( head(), key ));
380 /// Inserts new node with a key and a value
382 The function creates a node with \p key and value \p val, and then inserts the node created into the list.
385 - The \ref key_type should be constructible from \p key of type \p K.
386 - The \ref mapped_type should be constructible from \p val of type \p V.
388 Returns an iterator pointed to inserted value, or \p end() if inserting is failed
390 template <typename K, typename V>
391 iterator insert( const K& key, const V& val )
393 // We cannot use insert with functor here
394 // because we cannot lock inserted node for updating
395 // Therefore, we use separate function
396 return node_to_iterator( insert_at( head(), key, val ));
399 /// Inserts new node and initialize it by a functor
401 This function inserts new node with key \p key and if inserting is successful then it calls
402 \p func functor with signature
403 \code void func( value_type& item ) ; endcode
407 void operator()( value_type& item );
411 The argument \p item of user-defined functor \p func is the reference
412 to the list's item inserted. <tt>item.second</tt> is a reference to item's value that may be changed.
413 The user-defined functor is called only if the inserting is successful.
415 The key_type should be constructible from value of type \p K.
417 The function allows to split creating of new item into two part:
418 - create item from \p key;
419 - insert new item into the list;
420 - if inserting is successful, initialize the value of item by calling \p f functor
422 This can be useful if complete initialization of object of \p mapped_type is heavyweight and
423 it is preferable that the initialization should be completed only if inserting is successful.
425 Returns an iterator pointed to inserted value, or \p end() if inserting is failed
427 template <typename K, typename Func>
428 iterator insert_with( const K& key, Func func )
430 return node_to_iterator( insert_with_at( head(), key, func ));
435 If \p key is not in the list and \p bAllowInsert is \p true,
437 the function inserts a new item.
438 Otherwise, the function returns an iterator pointing to the item found.
440 Returns <tt> std::pair<iterator, bool> </tt> where \p first is an iterator pointing to
441 item found or inserted, \p second is true if new item has been added or \p false if the item
442 already is in the list.
444 template <typename K>
445 std::pair<iterator, bool> update( const K& key, bool bAllowInsert = true )
447 std::pair< node_type *, bool > ret = update_at( head(), key, bAllowInsert );
448 return std::make_pair( node_to_iterator( ret.first ), ret.second );
451 template <typename K>
452 CDS_DEPRECATED("ensure() is deprecated, use update()")
453 std::pair<iterator, bool> ensure( const K& key )
455 return update( key, true );
459 /// Inserts data of type \ref mapped_type constructed with <tt>std::forward<Args>(args)...</tt>
461 Returns an iterator pointed to inserted value, or \p end() if inserting is failed
463 template <typename... Args>
464 iterator emplace( Args&&... args )
466 return node_to_iterator( emplace_at( head(), std::forward<Args>(args)... ));
469 /// Checks whether the list contains \p key
471 The function searches the item with key equal to \p key
472 and returns an iterator pointed to item found if the key is found,
473 and \ref end() otherwise
475 template <typename Q>
476 iterator contains( Q const& key )
478 return node_to_iterator( find_at( head(), key, intrusive_key_comparator()));
481 template <typename Q>
482 CDS_DEPRECATED("deprecated, use contains()")
483 iterator find( Q const& key )
485 return contains( key );
489 /// Checks whether the map contains \p key using \p pred predicate for searching (ordered list version)
491 The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
492 \p Less functor has the interface like \p std::less.
493 \p Less must imply the same element order as the comparator used for building the list.
495 template <typename Q, typename Less, bool Sort = c_bSort>
496 typename std::enable_if<Sort, iterator>::type contains( Q const& key, Less pred )
499 return node_to_iterator( find_at( head(), key, typename maker::template less_wrapper<Less>::type()));
502 template <typename Q, typename Less, bool Sort = c_bSort>
503 CDS_DEPRECATED("deprecated, use contains()")
504 typename std::enable_if<Sort, iterator>::type find_with( Q const& key, Less pred )
506 return contains( key, pred );
510 /// Finds the key \p val using \p equal predicate for searching (unordered list version)
512 The function is an analog of <tt>contains( key )</tt> but \p equal is used for key comparing.
513 \p Equal functor has the interface like \p std::equal_to.
515 template <typename Q, typename Equal, bool Sort = c_bSort>
516 typename std::enable_if<!Sort, iterator>::type contains( Q const& key, Equal equal )
519 return node_to_iterator( find_at( head(), key, typename maker::template equal_to_wrapper<Equal>::type()));
522 template <typename Q, typename Equal, bool Sort = c_bSort>
523 CDS_DEPRECATED("deprecated, use contains()")
524 typename std::enable_if<!Sort, iterator>::type find_with( Q const& key, Equal equal )
526 return contains( key, equal );
530 /// Check if the list is empty
533 return base_class::empty();
536 /// Returns list's item count
538 The value returned depends on opt::item_counter option. For atomicity::empty_item_counter,
539 this function always returns 0.
541 @note Even if you use real item counter and it returns 0, this fact is not mean that the list
542 is empty. To check list emptyness use \ref empty() method.
546 return base_class::size();
549 /// Returns const reference to internal statistics
550 stat const& statistics() const
552 return base_class::statistics();
557 Post-condition: the list is empty
566 node_type * insert_node_at( head_type& refHead, node_type * pNode )
568 assert( pNode != nullptr );
569 scoped_node_ptr p( pNode );
570 if ( base_class::insert_at( &refHead, *p ))
576 template <typename K>
577 node_type * insert_at( head_type& refHead, const K& key )
579 return insert_node_at( refHead, alloc_node( key ));
582 template <typename K, typename V>
583 node_type * insert_at( head_type& refHead, const K& key, const V& val )
585 return insert_node_at( refHead, alloc_node( key, val ));
588 template <typename K, typename Func>
589 node_type * insert_with_at( head_type& refHead, const K& key, Func f )
591 scoped_node_ptr pNode( alloc_node( key ));
593 if ( base_class::insert_at( &refHead, *pNode )) {
595 return pNode.release();
602 template <typename K>
603 std::pair< node_type *, bool > update_at( head_type& refHead, const K& key, bool bAllowInsert )
605 scoped_node_ptr pNode( alloc_node( key ));
606 node_type * pItemFound = nullptr;
608 std::pair<bool, bool> ret = base_class::update_at( &refHead, *pNode,
609 [&pItemFound](bool, node_type& item, node_type&){ pItemFound = &item; },
615 return std::make_pair( pItemFound, ret.second );
618 template <typename... Args>
619 node_type * emplace_at( head_type& refHead, Args&&... args )
621 return insert_node_at( refHead, alloc_node( std::forward<Args>(args)... ));
624 template <typename K, typename Compare>
625 node_type * find_at( head_type& refHead, const K& key, Compare cmp )
627 return base_class::find_at( &refHead, key, cmp );
631 template <typename K, typenam Compare, typename Func>
632 bool find_at( head_type& refHead, K& key, Compare cmp, Func f )
634 return base_class::find_at( &refHead, key, cmp, [&f]( node_type& node, K const& ){ f( node.m_Data ); });
640 }} // namespace cds::container
642 #endif // #ifndef CDSLIB_CONTAINER_LAZY_KVLIST_NOGC_H