Tuning stress-tests for GCCFarm
[libcds.git] / cds / container / striped_map.h
index ce9a2325328b1cb264985577db9f60057a3f59c0..e2e412669d7ba2770df85ba5f8cc7c8bd0dc6a12 100644 (file)
@@ -1,7 +1,35 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
 
-#ifndef __CDS_CONTAINER_STRIPED_MAP_H
-#define __CDS_CONTAINER_STRIPED_MAP_H
+    (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_STRIPED_MAP_H
+#define CDSLIB_CONTAINER_STRIPED_MAP_H
 
 #include <type_traits>
 #include <cds/container/striped_set.h>
@@ -68,19 +96,19 @@ namespace cds { namespace container {
         among \p Options template arguments.
 
         The \p Options are:
-            - \p opt::mutex_policy - concurrent access policy.
-                Available policies: \p intrusive::striped_set::striping, \p intrusive::striped_set::refinable.
-                Default is %striped_set::striping.
-            - \p opt::hash - hash functor. Default option value see <tt>opt::v::hash_selector<opt::none> </tt>
+            - \p cds::opt::mutex_policy - concurrent access policy.
+                Available policies: \p striped_set::striping, \p striped_set::refinable.
+                Default is \p %striped_set::striping.
+            - \p cds::opt::hash - hash functor. Default option value see <tt>opt::v::hash_selector<opt::none> </tt>
                 which selects default hash functor for your compiler.
-            - \p opt::compare - key comparison functor. No default functor is provided.
+            - \p cds::opt::compare - key comparison functor. No default functor is provided.
                 If the option is not specified, the \p %opt::less is used.
-            - \p opt::less - specifies binary predicate used for key comparison. Default is \p std::less<T>.
-            - \p opt::item_counter - item counter type. Default is \p atomicity::item_counter since some operation on the counter is performed
-                without locks. Note that item counting is an essential part of the map algorithm, so dummy counter 
+            - \p cds::opt::less - specifies binary predicate used for key comparison. Default is \p std::less<T>.
+            - \p cds::opt::item_counter - item counter type. Default is \p atomicity::item_counter since some operation on the counter is performed
+                without locks. Note that item counting is an essential part of the map algorithm, so dummy counter
                 like as \p atomicity::empty_item_counter is not suitable.
-            - \p opt::allocator - the allocator type using for memory allocation of bucket table and lock array. Default is \ref CDS_DEFAULT_ALLOCATOR.
-            - \p opt::resizing_policy - the resizing policy that is a functor that decides when to resize the hash map.
+            - \p cds::opt::allocator - the allocator type using for memory allocation of bucket table and lock array. Default is \ref CDS_DEFAULT_ALLOCATOR.
+            - \p cds::opt::resizing_policy - the resizing policy that is a functor that decides when to resize the hash map.
                 Default option value depends on bucket container type:
                     for sequential containers like \p std::list, \p std::vector the resizing policy is <tt>striped_set::load_factor_resizing<4> </tt>;
                     for other type of containers like \p std::map, \p std::unordered_map the resizing policy is \p striped_set::no_resizing.
@@ -90,7 +118,7 @@ namespace cds { namespace container {
                 significantly improve performance.
                 For other, non-sequential types of \p Container (like a \p std::map)
                 the resizing policy is not so important.
-            - \p opt::copy_policy - the copy policy which is used to copy items from the old map to the new one when resizing.
+            - \p cds::opt::copy_policy - the copy policy which is used to copy items from the old map to the new one when resizing.
                 The policy can be optionally used in adapted bucket container for performance reasons of resizing.
                 The detail of copy algorithm depends on type of bucket container and explains below.
 
@@ -213,7 +241,7 @@ namespace cds { namespace container {
                         #include <cds/container/striped_map/boost_map.h>
                         #include <cds/container/striped_hash_map.h>
                         typedef cds::container::StripedMap<
-                            boost::container::map< Key, T, std::pair< const Key, T> >
+                            boost::container::map< Key, T, std::less<Key> >
                         > striped_map;
                     \endcode
                     </td>
@@ -228,7 +256,7 @@ namespace cds { namespace container {
                         #include <cds/container/striped_hash_map.h>
                         typedef cds::container::StripedMap<
                             boost::container::flat_map< Key, T,
-                                std::less< std::pair< const Key, T> >
+                                std::less< std::less<Key> >
                             >
                         > striped_map;
                     \endcode
@@ -258,7 +286,7 @@ namespace cds { namespace container {
             There are two possibility:
             - either your \p MyBestContainer class has native support of bucket's interface;
                 in this case, you can use default <tt>striped_set::adapt</tt> metafunction;
-            - or your \p MyBestContainer class does not support bucket's interface, which means, that you should develop a specialization
+            - or your \p MyBestContainer class does not support bucket's interface; it means you should develop a specialization
                 <tt>cds::container::striped_set::adapt<MyBestContainer> </tt> metafunction providing necessary interface.
 
             The <tt>striped_set::adapt< Container, Options... ></tt> metafunction has two template argument:
@@ -267,7 +295,7 @@ namespace cds { namespace container {
                 any option from \p Options for its internal use. For example, a \p compare option can be passed to \p adapt
                 metafunction via \p Options argument of \p %StripedMap declaration.
 
-            See striped_set::adapt metafunction for the description of interface that the bucket container must provide
+            See \p striped_set::adapt metafunction for the description of interface that the bucket container must provide
             to be \p %StripedMap compatible.
 
         <b>Copy policy</b>
@@ -311,7 +339,7 @@ namespace cds { namespace container {
                                 std::list<std::pair<const Key, T> >::iterator itInsert,
                                 std::list<std::pair<const Key, T> >::iterator itWhat )
                             {
-                                std::pair<Key, T> newVal( itWhat->first, T() );
+                                std::pair<Key, T> newVal( itWhat->first, T());
                                 std::swap( list.insert( itInsert, newVal )->second, itWhat->second );
                             }
                         } \endcode
@@ -323,7 +351,7 @@ namespace cds { namespace container {
                                 std::list<std::pair<const Key, T> >::iterator itInsert,
                                 std::list<std::pair<const Key, T> >::iterator itWhat )
                             {
-                                list.insert( itInsert, std::move( *itWhat ) );
+                                list.insert( itInsert, std::move( *itWhat ));
                             }
                         } \endcode
                     </td>
@@ -350,7 +378,7 @@ namespace cds { namespace container {
                             {
                                 std::swap(
                                     map.insert(
-                                        std::map::value_type( itWhat->first, T() ) ).first->second
+                                        std::map::value_type( itWhat->first, T())).first->second
                                         , itWhat->second
                                 ));
                             }
@@ -386,7 +414,7 @@ namespace cds { namespace container {
                                 bc::slist<std::pair<const Key, T> >::iterator itInsert,
                                 bc::slist<std::pair<const Key, T> >::iterator itWhat )
                             {
-                                std::pair<Key, T> newVal( itWhat->first, T() );
+                                std::pair<Key, T> newVal( itWhat->first, T());
                                 std::swap( list.insert( itInsert, newVal )->second, itWhat->second );
                             }
                         } \endcode
@@ -398,7 +426,7 @@ namespace cds { namespace container {
                                 bc::slist<std::pair<const Key, T> >::iterator itInsert,
                                 bc::slist<std::pair<const Key, T> >::iterator itWhat )
                             {
-                                list.insert_after( itInsert, std::move( *itWhat ) );
+                                list.insert_after( itInsert, std::move( *itWhat ));
                             }
                         } \endcode
                     </td>
@@ -538,7 +566,7 @@ template <class Container, typename... Options>
         StripedMap(
             size_t nCapacity    ///< Initial size of bucket table and lock array. Must be power of two, the minimum is 16.
             ,resizing_policy&& resizingPolicy  ///< Resizing policy
-            ) : base_class( nCapacity, std::forward<resizing_policy>(resizingPolicy) )
+            ) : base_class( nCapacity, std::forward<resizing_policy>(resizingPolicy))
         {}
 
         /// Destructor destroys internal data
@@ -560,7 +588,7 @@ template <class Container, typename... Options>
         template <typename K>
         bool insert( K const& key )
         {
-            return insert_key( key, [](value_type&){} );
+            return insert_with( key, [](value_type&){} );
         }
 
         /// Inserts new node
@@ -577,7 +605,7 @@ template <class Container, typename... Options>
         template <typename K, typename V>
         bool insert( K const& key, V const& val )
         {
-            return insert_key( key, [&val](value_type& item) { item.second = val ; } );
+            return insert_with( key, [&val](value_type& item) { item.second = val ; } );
         }
 
         /// Inserts new node and initialize it by a functor
@@ -606,7 +634,7 @@ template <class Container, typename... Options>
             it is preferable that the initialization should be completed only if inserting is successful.
         */
         template <typename K, typename Func>
-        bool insert_key( const K& key, Func func )
+        bool insert_with( const K& key, Func func )
         {
             return base_class::insert( key, func );
         }
@@ -636,37 +664,29 @@ template <class Container, typename... Options>
             return bOk;
         }
 
-        /// Ensures that the \p key exists in the map
+        /// Updates the node
         /**
             The operation performs inserting or changing data with lock-free manner.
 
-            If the \p key not found in the map, then the new item created from \p key
-            is inserted into the map (note that in this case the \p key_type should be
-            constructible from type \p K).
+            If \p key is not found in the map, then \p key is inserted iff \p bAllowInsert is \p true.
             Otherwise, the functor \p func is called with item found.
-            The functor \p Func may be a function with signature:
-            \code
-                void func( bool bNew, value_type& item );
-            \endcode
-            or a functor:
+
+            The functor signature is:
             \code
                 struct my_functor {
                     void operator()( bool bNew, value_type& item );
                 };
             \endcode
-
             with arguments:
             - \p bNew - \p true if the item has been inserted, \p false otherwise
-            - \p item - item of the list
+            - \p item - item of the map
 
-            The functor may change any fields of the \p item.second that is \p mapped_type.
-
-            Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
+            Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successful,
             \p second is true if new item has been added or \p false if the item with \p key
-            already is in the list.
+            already is in the map.
         */
         template <typename K, typename Func>
-        std::pair<bool, bool> ensure( K const& key, Func func )
+        std::pair<bool, bool> update( K const& key, Func func, bool bAllowInsert = true )
         {
             std::pair<bool, bool> result;
             bool bResize;
@@ -676,7 +696,7 @@ template <class Container, typename... Options>
                 scoped_cell_lock sl( base_class::m_MutexPolicy, nHash );
                 pBucket = base_class::bucket( nHash );
 
-                result = pBucket->ensure( key, func );
+                result = pBucket->update( key, func, bAllowInsert );
                 bResize = result.first && result.second && base_class::m_ResizingPolicy( ++base_class::m_ItemCounter, *this, *pBucket );
             }
 
@@ -684,6 +704,14 @@ template <class Container, typename... Options>
                 base_class::resize();
             return result;
         }
+        //@cond
+        template <typename K, typename Func>
+        CDS_DEPRECATED("ensure() is deprecated, use update() instead")
+        std::pair<bool, bool> ensure( K const& key, Func func )
+        {
+            return update( key, func, true );
+        }
+        //@endcond
 
         /// Delete \p key from the map
         /** \anchor cds_nonintrusive_StripedMap_erase
@@ -796,36 +824,51 @@ template <class Container, typename... Options>
                 [&f]( value_type& pair, K const& ) mutable { f(pair); } );
         }
 
-        /// Find the key \p key
-        /** \anchor cds_nonintrusive_StripedMap_find_val
-
+        /// Checks whether the map contains \p key
+        /**
             The function searches the item with key equal to \p key
             and returns \p true if it is found, and \p false otherwise.
         */
         template <typename K>
+        bool contains( K const& key )
+        {
+            return base_class::contains( key );
+        }
+        //@cond
+        template <typename K>
+        CDS_DEPRECATED("use contains()")
         bool find( K const& key )
         {
-            return base_class::find( key );
+            return contains( key );
         }
+        //@endcond
 
-        /// Find the key \p val using \p pred predicate
+        /// Checks whether the set contains \p key using \p pred predicate for searching
         /**
-            The function is an analog of \ref cds_nonintrusive_StripedMap_find_val "find(K const&)"
-            but \p pred is used for key comparing
-            \p Less has the interface like \p std::less.
-            \p pred must imply the same element order as the comparator used for building the set.
+            The function is similar to <tt>contains( key )</tt> but \p pred is used for key comparing.
+            \p Less functor has the interface like \p std::less.
+            \p Less must imply the same element order as the comparator used for building the set.
 
             @note This function is enabled if the compiler supports C++11
             default template arguments for function template <b>and</b> the underlying container
-            supports \p %find_with feature.
+            supports \p %contains() feature.
         */
         template <typename K, typename Less
             ,typename Bucket = bucket_type, typename = typename std::enable_if< Bucket::has_find_with >::type >
-        bool find_with( K const& key, Less pred )
+        bool contains( K const& key, Less pred )
         {
             CDS_UNUSED( pred );
-            return base_class::find_with( key, cds::details::predicate_wrapper< value_type, Less, key_accessor >() );
+            return base_class::contains( key, cds::details::predicate_wrapper< value_type, Less, key_accessor >());
+        }
+        //@cond
+        template <typename K, typename Less
+            ,typename Bucket = bucket_type, typename = typename std::enable_if< Bucket::has_find_with >::type >
+        CDS_DEPRECATED("use contains()")
+        bool find_with( K const& key, Less pred )
+        {
+            return contains( key, pred );
         }
+        //@endcond
 
         /// Clears the map
         void clear()
@@ -881,4 +924,4 @@ template <class Container, typename... Options>
 
 }}  // namespace cds::container
 
-#endif // #ifndef __CDS_CONTAINER_STRIPED_MAP_H
+#endif // #ifndef CDSLIB_CONTAINER_STRIPED_MAP_H