Added copyright and license
[libcds.git] / cds / container / impl / ellen_bintree_map.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_ELLEN_BINTREE_MAP_H
32 #define CDSLIB_CONTAINER_IMPL_ELLEN_BINTREE_MAP_H
33
34 #include <type_traits>
35 #include <cds/container/details/ellen_bintree_base.h>
36 #include <cds/intrusive/impl/ellen_bintree.h>
37 #include <cds/container/details/guarded_ptr_cast.h>
38
39 namespace cds { namespace container {
40
41     /// Map based on Ellen's et al binary search tree
42     /** @ingroup cds_nonintrusive_map
43         @ingroup cds_nonintrusive_tree
44         @anchor cds_container_EllenBinTreeMap
45
46         Source:
47             - [2010] F.Ellen, P.Fatourou, E.Ruppert, F.van Breugel "Non-blocking Binary Search Tree"
48
49         %EllenBinTreeMap is an unbalanced leaf-oriented binary search tree that implements the <i>map</i>
50         abstract data type. Nodes maintains child pointers but not parent pointers.
51         Every internal node has exactly two children, and all data of type <tt>std::pair<Key const, T></tt>
52         currently in the tree are stored in the leaves. Internal nodes of the tree are used to direct \p find
53         operation along the path to the correct leaf. The keys (of \p Key type) stored in internal nodes
54         may or may not be in the map.
55         Unlike \ref cds_container_EllenBinTreeSet "EllenBinTreeSet" keys are not a part of \p T type.
56         The map can be represented as a set containing <tt>std::pair< Key const, T> </tt> values.
57
58         Due to \p extract_min and \p extract_max member functions the \p %EllenBinTreeMap can act as
59         a <i>priority queue</i>. In this case you should provide unique compound key, for example,
60         the priority value plus some uniformly distributed random value.
61
62         @warning Recall the tree is <b>unbalanced</b>. The complexity of operations is <tt>O(log N)</tt>
63         for uniformly distributed random keys, but in worst case the complexity is <tt>O(N)</tt>.
64
65         @note In the current implementation we do not use helping technique described in the original paper.
66         In Hazard Pointer schema helping is too complicated and does not give any observable benefits.
67         Instead of helping, when a thread encounters a concurrent operation it just spins waiting for
68         the operation done. Such solution allows greatly simplify implementation of the tree.
69
70         <b>Template arguments</b> :
71         - \p GC - safe memory reclamation (i.e. light-weight garbage collector) type, like \p cds::gc::HP, \p cds::gc::DHP
72         - \p Key - key type
73         - \p T - value type to be stored in tree's leaf nodes.
74         - \p Traits - map traits, default is \p ellen_bintree::traits
75             It is possible to declare option-based tree with \p ellen_bintree::make_map_traits metafunction
76             instead of \p Traits template argument.
77
78         @note Do not include <tt><cds/container/impl/ellen_bintree_map.h></tt> header file directly.
79         There are header file for each GC type:
80         - <tt><cds/container/ellen_bintree_map_hp.h></tt> - for Hazard Pointer GC cds::gc::HP
81         - <tt><cds/container/ellen_bintree_map_dhp.h></tt> - for Dynamic Hazard Pointer GC cds::gc::DHP
82         - <tt><cds/container/ellen_bintree_map_rcu.h></tt> - for RCU GC
83             (see \ref cds_container_EllenBinTreeMap_rcu "RCU-based EllenBinTreeMap")
84     */
85     template <
86         class GC,
87         typename Key,
88         typename T,
89 #ifdef CDS_DOXYGEN_INVOKED
90         class Traits = ellen_bintree::traits
91 #else
92         class Traits
93 #endif
94     >
95     class EllenBinTreeMap
96 #ifdef CDS_DOXYGEN_INVOKED
97         : public cds::intrusive::EllenBinTree< GC, Key, T, Traits >
98 #else
99         : public ellen_bintree::details::make_ellen_bintree_map< GC, Key, T, Traits >::type
100 #endif
101     {
102         //@cond
103         typedef ellen_bintree::details::make_ellen_bintree_map< GC, Key, T, Traits > maker;
104         typedef typename maker::type base_class;
105         //@endcond
106     public:
107         typedef GC      gc;          ///< Garbage collector
108         typedef Key     key_type;    ///< type of a key stored in the map
109         typedef T       mapped_type; ///< type of value stored in the map
110         typedef std::pair< key_type const, mapped_type >    value_type  ;   ///< Key-value pair stored in leaf node of the mp
111         typedef Traits  traits;      ///< Map traits
112
113 #   ifdef CDS_DOXYGEN_INVOKED
114         typedef implementation_defined key_comparator; ///< key compare functor based on \p Traits::compare and \p Traits::less
115 #   else
116         typedef typename maker::intrusive_traits::compare   key_comparator;
117 #   endif
118         typedef typename base_class::item_counter           item_counter; ///< Item counting policy
119         typedef typename base_class::memory_model           memory_model; ///< Memory ordering, see \p cds::opt::memory_model
120         typedef typename base_class::node_allocator         node_allocator_type; ///< allocator for maintaining internal node
121         typedef typename base_class::stat                   stat;         ///< internal statistics type
122         typedef typename traits::copy_policy                copy_policy;  ///< key copy policy
123         typedef typename traits::back_off                   back_off;      ///< Back-off strategy
124
125         typedef typename traits::allocator                  allocator_type;   ///< Allocator for leaf nodes
126         typedef typename base_class::node_allocator         node_allocator;   ///< Internal node allocator
127         typedef typename base_class::update_desc_allocator  update_desc_allocator; ///< Update descriptor allocator
128
129     protected:
130         //@cond
131         typedef typename base_class::value_type         leaf_node;
132         typedef typename base_class::internal_node      internal_node;
133         typedef typename base_class::update_desc        update_desc;
134
135         typedef typename maker::cxx_leaf_node_allocator cxx_leaf_node_allocator;
136
137         typedef std::unique_ptr< leaf_node, typename maker::leaf_deallocator >    scoped_node_ptr;
138         //@endcond
139
140     public:
141         /// Guarded pointer
142         typedef typename gc::template guarded_ptr< leaf_node, value_type, details::guarded_ptr_cast_set<leaf_node, value_type> > guarded_ptr;
143
144     public:
145         /// Default constructor
146         EllenBinTreeMap()
147             : base_class()
148         {}
149
150         /// Clears the map
151         ~EllenBinTreeMap()
152         {}
153
154         /// Inserts new node with key and default value
155         /**
156             The function creates a node with \p key and default value, and then inserts the node created into the map.
157
158             Preconditions:
159             - The \ref key_type should be constructible from a value of type \p K.
160                 In trivial case, \p K is equal to \ref key_type.
161             - The \ref mapped_type should be default-constructible.
162
163             Returns \p true if inserting successful, \p false otherwise.
164         */
165         template <typename K>
166         bool insert( K const& key )
167         {
168             return insert_with( key, [](value_type&){} );
169         }
170
171         /// Inserts new node
172         /**
173             The function creates a node with copy of \p val value
174             and then inserts the node created into the map.
175
176             Preconditions:
177             - The \p key_type should be constructible from \p key of type \p K.
178             - The \p value_type should be constructible from \p val of type \p V.
179
180             Returns \p true if \p val is inserted into the map, \p false otherwise.
181         */
182         template <typename K, typename V>
183         bool insert( K const& key, V const& val )
184         {
185             scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key, val ));
186             if ( base_class::insert( *pNode ))
187             {
188                 pNode.release();
189                 return true;
190             }
191             return false;
192         }
193
194         /// Inserts new node and initialize it by a functor
195         /**
196             This function inserts new node with key \p key and if inserting is successful then it calls
197             \p func functor with signature
198             \code
199                 struct functor {
200                     void operator()( value_type& item );
201                 };
202             \endcode
203
204             The argument \p item of user-defined functor \p func is the reference
205             to the map's item inserted:
206                 - <tt>item.first</tt> is a const reference to item's key that cannot be changed.
207                 - <tt>item.second</tt> is a reference to item's value that may be changed.
208
209             The key_type should be constructible from value of type \p K.
210
211             The function allows to split creating of new item into two part:
212             - create item from \p key;
213             - insert new item into the map;
214             - if inserting is successful, initialize the value of item by calling \p func functor
215
216             This can be useful if complete initialization of object of \p value_type is heavyweight and
217             it is preferable that the initialization should be completed only if inserting is successful.
218         */
219         template <typename K, typename Func>
220         bool insert_with( const K& key, Func func )
221         {
222             scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key ));
223             if ( base_class::insert( *pNode, [&func]( leaf_node& item ) { func( item.m_Value ); } )) {
224                 pNode.release();
225                 return true;
226             }
227             return false;
228         }
229
230         /// For key \p key inserts data of type \p value_type created in-place from \p args
231         /**
232             Returns \p true if inserting successful, \p false otherwise.
233         */
234         template <typename K, typename... Args>
235         bool emplace( K&& key, Args&&... args )
236         {
237             scoped_node_ptr pNode( cxx_leaf_node_allocator().New( std::forward<K>(key), std::forward<Args>(args)... ));
238             if ( base_class::insert( *pNode )) {
239                 pNode.release();
240                 return true;
241             }
242             return false;
243         }
244
245         /// Updates the node
246         /**
247             The operation performs inserting or changing data with lock-free manner.
248
249             If the item \p val is not found in the map, then \p val is inserted iff \p bAllowInsert is \p true.
250             Otherwise, the functor \p func is called with item found.
251             The functor \p func signature is:
252             \code
253                 struct my_functor {
254                     void operator()( bool bNew, value_type& item );
255                 };
256             \endcode
257
258             with arguments:
259             - \p bNew - \p true if the item has been inserted, \p false otherwise
260             - \p item - item of the map
261
262             The functor may change any fields of the \p item.second that is \ref mapped_type;
263             however, \p func must guarantee that during changing no any other modifications
264             could be made on this item by concurrent threads.
265
266             Returns std::pair<bool, bool> where \p first is \p true if operation is successfull,
267             i.e. the node has been inserted or updated,
268             \p second is \p true if new item has been added or \p false if the item with \p key
269             already exists.
270
271             @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
272         */
273         template <typename K, typename Func>
274         std::pair<bool, bool> update( K const& key, Func func, bool bAllowInsert = true )
275         {
276             scoped_node_ptr pNode( cxx_leaf_node_allocator().New( key ));
277             std::pair<bool, bool> res = base_class::update( *pNode,
278                 [&func](bool bNew, leaf_node& item, leaf_node const& ){ func( bNew, item.m_Value ); },
279                 bAllowInsert
280             );
281             if ( res.first && res.second )
282                 pNode.release();
283             return res;
284         }
285         //@cond
286         template <typename K, typename Func>
287         CDS_DEPRECATED("ensure() is deprecated, use update()")
288         std::pair<bool, bool> ensure( K const& key, Func func )
289         {
290             return update( key, func, true );
291         }
292         //@endcond
293
294         /// Delete \p key from the map
295         /**\anchor cds_nonintrusive_EllenBinTreeMap_erase_val
296
297             Return \p true if \p key is found and deleted, \p false otherwise
298         */
299         template <typename K>
300         bool erase( K const& key )
301         {
302             return base_class::erase(key);
303         }
304
305         /// Deletes the item from the map using \p pred predicate for searching
306         /**
307             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_erase_val "erase(K const&)"
308             but \p pred is used for key comparing.
309             \p Less functor has the interface like \p std::less.
310             \p Less must imply the same element order as the comparator used for building the map.
311         */
312         template <typename K, typename Less>
313         bool erase_with( K const& key, Less pred )
314         {
315             CDS_UNUSED( pred );
316             return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >());
317         }
318
319         /// Delete \p key from the map
320         /** \anchor cds_nonintrusive_EllenBinTreeMap_erase_func
321
322             The function searches an item with key \p key, calls \p f functor
323             and deletes the item. If \p key is not found, the functor is not called.
324
325             The functor \p Func interface:
326             \code
327             struct extractor {
328                 void operator()(value_type& item) { ... }
329             };
330             \endcode
331
332             Return \p true if key is found and deleted, \p false otherwise
333         */
334         template <typename K, typename Func>
335         bool erase( K const& key, Func f )
336         {
337             return base_class::erase( key, [&f]( leaf_node& node) { f( node.m_Value ); } );
338         }
339
340         /// Deletes the item from the map using \p pred predicate for searching
341         /**
342             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_erase_func "erase(K const&, Func)"
343             but \p pred is used for key comparing.
344             \p Less functor has the interface like \p std::less.
345             \p Less must imply the same element order as the comparator used for building the map.
346         */
347         template <typename K, typename Less, typename Func>
348         bool erase_with( K const& key, Less pred, Func f )
349         {
350             CDS_UNUSED( pred );
351             return base_class::erase_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >(),
352                 [&f]( leaf_node& node) { f( node.m_Value ); } );
353         }
354
355         /// Extracts an item with minimal key from the map
356         /**
357             If the map is not empty, the function returns an guarded pointer to minimum value.
358             If the map is empty, the function returns an empty \p guarded_ptr.
359
360             @note Due the concurrent nature of the map, the function extracts <i>nearly</i> minimum key.
361             It means that the function gets leftmost leaf of the tree and tries to unlink it.
362             During unlinking, a concurrent thread may insert an item with key less than leftmost item's key.
363             So, the function returns the item with minimum key at the moment of tree traversing.
364
365             The guarded pointer prevents deallocation of returned item,
366             see \p cds::gc::guarded_ptr for explanation.
367             @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
368         */
369         guarded_ptr extract_min()
370         {
371             guarded_ptr gp;
372             base_class::extract_min_( gp.guard() );
373             return gp;
374         }
375
376         /// Extracts an item with maximal key from the map
377         /**
378             If the map is not empty, the function returns a guarded pointer to maximal value.
379             If the map is empty, the function returns an empty \p guarded_ptr.
380
381             @note Due the concurrent nature of the map, the function extracts <i>nearly</i> maximal key.
382             It means that the function gets rightmost leaf of the tree and tries to unlink it.
383             During unlinking, a concurrent thread may insert an item with key great than leftmost item's key.
384             So, the function returns the item with maximum key at the moment of tree traversing.
385
386             The guarded pointer prevents deallocation of returned item,
387             see \p cds::gc::guarded_ptr for explanation.
388             @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
389         */
390         guarded_ptr extract_max()
391         {
392             guarded_ptr gp;
393             base_class::extract_max_( gp.guard() );
394             return gp;
395         }
396
397         /// Extracts an item from the tree
398         /** \anchor cds_nonintrusive_EllenBinTreeMap_extract
399             The function searches an item with key equal to \p key in the tree,
400             unlinks it, and returns a guarded pointer to an item found.
401             If the item  is not found the function returns an empty \p guarded_ptr.
402
403             The guarded pointer prevents deallocation of returned item,
404             see \p cds::gc::guarded_ptr for explanation.
405             @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
406         */
407         template <typename Q>
408         guarded_ptr extract( Q const& key )
409         {
410             guarded_ptr gp;
411             base_class::extract_( gp.guard(), key );
412             return gp;
413         }
414
415         /// Extracts an item from the map using \p pred for searching
416         /**
417             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_extract "extract(Q const&)"
418             but \p pred is used for key compare.
419             \p Less has the interface like \p std::less.
420             \p pred must imply the same element order as the comparator used for building the map.
421         */
422         template <typename Q, typename Less>
423         guarded_ptr extract_with( Q const& key, Less pred )
424         {
425             CDS_UNUSED( pred );
426             guarded_ptr gp;
427             base_class::extract_with_( gp.guard(), key,
428                 cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >());
429             return gp;
430         }
431
432         /// Find the key \p key
433         /** \anchor cds_nonintrusive_EllenBinTreeMap_find_cfunc
434
435             The function searches the item with key equal to \p key and calls the functor \p f for item found.
436             The interface of \p Func functor is:
437             \code
438             struct functor {
439                 void operator()( value_type& item );
440             };
441             \endcode
442             where \p item is the item found.
443
444             The functor may change \p item.second.
445
446             The function returns \p true if \p key is found, \p false otherwise.
447         */
448         template <typename K, typename Func>
449         bool find( K const& key, Func f )
450         {
451             return base_class::find( key, [&f](leaf_node& item, K const& ) { f( item.m_Value );});
452         }
453
454         /// Finds the key \p val using \p pred predicate for searching
455         /**
456             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_find_cfunc "find(K const&, Func)"
457             but \p pred is used for key comparing.
458             \p Less functor has the interface like \p std::less.
459             \p Less must imply the same element order as the comparator used for building the map.
460         */
461         template <typename K, typename Less, typename Func>
462         bool find_with( K const& key, Less pred, Func f )
463         {
464             CDS_UNUSED( pred );
465             return base_class::find_with( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >(),
466                 [&f](leaf_node& item, K const& ) { f( item.m_Value );});
467         }
468
469         /// Checks whether the map contains \p key
470         /**
471             The function searches the item with key equal to \p key
472             and returns \p true if it is found, and \p false otherwise.
473         */
474         template <typename K>
475         bool contains( K const& key )
476         {
477             return base_class::contains( key );
478         }
479         //@cond
480         template <typename K>
481         CDS_DEPRECATED("deprecated, use contains()")
482         bool find( K const& key )
483         {
484             return contains( key );
485         }
486         //@endcond
487
488         /// Checks whether the map contains \p key using \p pred predicate for searching
489         /**
490             The function is similar to <tt>contains( key )</tt> but \p pred is used for key comparing.
491             \p Less functor has the interface like \p std::less.
492             \p Less must imply the same element order as the comparator used for building the set.
493         */
494         template <typename K, typename Less>
495         bool contains( K const& key, Less pred )
496         {
497             CDS_UNUSED( pred );
498             return base_class::contains( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >() );
499         }
500         //@cond
501         template <typename K, typename Less>
502         CDS_DEPRECATED("deprecated, use contains()")
503         bool find_with( K const& key, Less pred )
504         {
505             return contains( key, pred );
506         }
507         //@endcond
508
509         /// Finds \p key and returns the item found
510         /** @anchor cds_nonintrusive_EllenBinTreeMap_get
511             The function searches the item with key equal to \p key and returns the item found as a guarded pointer.
512             If \p key is not foudn the function returns an empty \p guarded_ptr.
513
514             The guarded pointer prevents deallocation of returned item,
515             see \p cds::gc::guarded_ptr for explanation.
516             @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
517         */
518         template <typename Q>
519         guarded_ptr get( Q const& key )
520         {
521             guarded_ptr gp;
522             base_class::get_( gp.guard(), key );
523             return gp;
524         }
525
526         /// Finds \p key with predicate \p pred and returns the item found
527         /**
528             The function is an analog of \ref cds_nonintrusive_EllenBinTreeMap_get "get(Q const&)"
529             but \p pred is used for key comparing.
530             \p Less functor has the interface like \p std::less.
531             \p pred must imply the same element order as the comparator used for building the map.
532         */
533         template <typename Q, typename Less>
534         guarded_ptr get_with( Q const& key, Less pred )
535         {
536             CDS_UNUSED( pred );
537             guarded_ptr gp;
538             base_class::get_with_( gp.guard(), key,
539                 cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >() );
540             return gp;
541         }
542
543         /// Clears the map (not atomic)
544         void clear()
545         {
546             base_class::clear();
547         }
548
549         /// Checks if the map is empty
550         /**
551             Emptiness is checked by item counting: if item count is zero then the map is empty.
552         */
553         bool empty() const
554         {
555             return base_class::empty();
556         }
557
558         /// Returns item count in the set
559         /**
560             Only leaf nodes containing user data are counted.
561
562             The value returned depends on item counter type provided by \p Traits template parameter.
563             If it is \p atomicity::empty_item_counter this function always returns 0.
564
565             The function is not suitable for checking the tree emptiness, use \p empty()
566             member function for this purpose.
567         */
568         size_t size() const
569         {
570             return base_class::size();
571         }
572
573         /// Returns const reference to internal statistics
574         stat const& statistics() const
575         {
576             return base_class::statistics();
577         }
578
579         /// Checks internal consistency (not atomic, not thread-safe)
580         /**
581             The debugging function to check internal consistency of the tree.
582         */
583         bool check_consistency() const
584         {
585             return base_class::check_consistency();
586         }
587
588     };
589 }} // namespace cds::container
590
591 #endif //#ifndef CDSLIB_CONTAINER_IMPL_ELLEN_BINTREE_MAP_H