Updated copyright
[libcds.git] / cds / container / impl / ellen_bintree_map.h
index b3a2feefb9c0e86901ea863f09c55c105cdd8879..e9dd938cb910379e70c5296ebc1597778af1c4ec 100644 (file)
@@ -1,4 +1,32 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
+
+    (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
+
+    Source code repo: http://github.com/khizmax/libcds/
+    Download: http://sourceforge.net/projects/libcds/files/
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice, this
+      list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above copyright notice,
+      this list of conditions and the following disclaimer in the documentation
+      and/or other materials provided with the distribution.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
 
 #ifndef CDSLIB_CONTAINER_IMPL_ELLEN_BINTREE_MAP_H
 #define CDSLIB_CONTAINER_IMPL_ELLEN_BINTREE_MAP_H
@@ -32,7 +60,7 @@ namespace cds { namespace container {
         the priority value plus some uniformly distributed random value.
 
         @warning Recall the tree is <b>unbalanced</b>. The complexity of operations is <tt>O(log N)</tt>
-        for uniformly distributed random keys, but in worst case the complexity is <tt>O(N)</tt>.
+        for uniformly distributed random keys, but in the worst case the complexity is <tt>O(N)</tt>.
 
         @note In the current implementation we do not use helping technique described in the original paper.
         In Hazard Pointer schema helping is too complicated and does not give any observable benefits.
@@ -41,7 +69,7 @@ namespace cds { namespace container {
 
         <b>Template arguments</b> :
         - \p GC - safe memory reclamation (i.e. light-weight garbage collector) type, like \p cds::gc::HP, \p cds::gc::DHP
-        - \p Key - key type
+        - \p Key - key type. Should be default-constructible
         - \p T - value type to be stored in tree's leaf nodes.
         - \p Traits - map traits, default is \p ellen_bintree::traits
             It is possible to declare option-based tree with \p ellen_bintree::make_map_traits metafunction
@@ -82,6 +110,8 @@ namespace cds { namespace container {
         typedef std::pair< key_type const, mapped_type >    value_type  ;   ///< Key-value pair stored in leaf node of the mp
         typedef Traits  traits;      ///< Map traits
 
+        static_assert( std::is_default_constructible<key_type>::value, "Key should be default constructible type");
+
 #   ifdef CDS_DOXYGEN_INVOKED
         typedef implementation_defined key_comparator; ///< key compare functor based on \p Traits::compare and \p Traits::less
 #   else
@@ -206,7 +236,7 @@ namespace cds { namespace container {
         template <typename K, typename... Args>
         bool emplace( K&& key, Args&&... args )
         {
-            scoped_node_ptr pNode( cxx_leaf_node_allocator().New( std::forward<K>(key), std::forward<Args>(args)... ));
+            scoped_node_ptr pNode( cxx_leaf_node_allocator().MoveNew( key_type( std::forward<K>(key)), mapped_type( std::forward<Args>(args)... )));
             if ( base_class::insert( *pNode )) {
                 pNode.release();
                 return true;
@@ -235,7 +265,7 @@ namespace cds { namespace container {
             however, \p func must guarantee that during changing no any other modifications
             could be made on this item by concurrent threads.
 
-            Returns std::pair<bool, bool> where \p first is \p true if operation is successfull,
+            Returns std::pair<bool, bool> where \p first is \p true if operation is successful,
             i.e. the node has been inserted or updated,
             \p second is \p true if new item has been added or \p false if the item with \p key
             already exists.
@@ -340,9 +370,7 @@ namespace cds { namespace container {
         */
         guarded_ptr extract_min()
         {
-            guarded_ptr gp;
-            base_class::extract_min_( gp.guard() );
-            return gp;
+            return guarded_ptr( base_class::extract_min_());
         }
 
         /// Extracts an item with maximal key from the map
@@ -361,9 +389,7 @@ namespace cds { namespace container {
         */
         guarded_ptr extract_max()
         {
-            guarded_ptr gp;
-            base_class::extract_max_( gp.guard() );
-            return gp;
+            return guarded_ptr( base_class::extract_max_());
         }
 
         /// Extracts an item from the tree
@@ -379,9 +405,7 @@ namespace cds { namespace container {
         template <typename Q>
         guarded_ptr extract( Q const& key )
         {
-            guarded_ptr gp;
-            base_class::extract_( gp.guard(), key );
-            return gp;
+            return guarded_ptr( base_class::extract_( key ));
         }
 
         /// Extracts an item from the map using \p pred for searching
@@ -395,10 +419,8 @@ namespace cds { namespace container {
         guarded_ptr extract_with( Q const& key, Less pred )
         {
             CDS_UNUSED( pred );
-            guarded_ptr gp;
-            base_class::extract_with_( gp.guard(), key,
-                cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >());
-            return gp;
+            return guarded_ptr( base_class::extract_with_( key,
+                cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >()));
         }
 
         /// Find the key \p key
@@ -467,7 +489,7 @@ namespace cds { namespace container {
         bool contains( K const& key, Less pred )
         {
             CDS_UNUSED( pred );
-            return base_class::contains( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >() );
+            return base_class::contains( key, cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >());
         }
         //@cond
         template <typename K, typename Less>
@@ -490,9 +512,7 @@ namespace cds { namespace container {
         template <typename Q>
         guarded_ptr get( Q const& key )
         {
-            guarded_ptr gp;
-            base_class::get_( gp.guard(), key );
-            return gp;
+            return guarded_ptr( base_class::get_( key ));
         }
 
         /// Finds \p key with predicate \p pred and returns the item found
@@ -506,10 +526,8 @@ namespace cds { namespace container {
         guarded_ptr get_with( Q const& key, Less pred )
         {
             CDS_UNUSED( pred );
-            guarded_ptr gp;
-            base_class::get_with_( gp.guard(), key,
-                cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >() );
-            return gp;
+            return guarded_ptr( base_class::get_with_( key,
+                cds::details::predicate_wrapper< leaf_node, Less, typename maker::key_accessor >()));
         }
 
         /// Clears the map (not atomic)