Fixed typo
[libcds.git] / cds / container / striped_set.h
index c18284587342d314eb64dba8d37f8999970c1fbb..ae2466676d69e78fd83d522079376840464f7a4a 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_STRIPED_SET_H
 #define CDSLIB_CONTAINER_STRIPED_SET_H
@@ -323,7 +351,7 @@ namespace cds { namespace container {
                         struct swap_item {
                             void operator()( std::list<T>& list, std::list<T>::iterator itInsert, std::list<T>::iterator itWhat )
                             {
-                                std::swap( *list.insert( itInsert, T() ), *itWhat );
+                                std::swap( *list.insert( itInsert, T()), *itWhat );
                             }
                         } \endcode
 
@@ -331,7 +359,7 @@ namespace cds { namespace container {
                         struct move_item {
                             void operator()( std::list<T>& list, std::list<T>::iterator itInsert, std::list<T>::iterator itWhat )
                             {
-                                list.insert( itInsert, std::move( *itWhat ) );
+                                list.insert( itInsert, std::move( *itWhat ));
                             }
                         } \endcode
                     </td>
@@ -375,7 +403,7 @@ namespace cds { namespace container {
                         struct swap_item {
                             void operator()( bc::slist<T>& list, bc::slist<T>::iterator itInsert, bc::slist<T>::iterator itWhat )
                             {
-                                std::swap( *list.insert_after( itInsert, T() ), *itWhat );
+                                std::swap( *list.insert_after( itInsert, T()), *itWhat );
                             }
                         } \endcode
 
@@ -383,7 +411,7 @@ namespace cds { namespace container {
                         struct move_item {
                             void operator()( bc::slist<T>& list, bc::slist<T>::iterator itInsert, bc::slist<T>::iterator itWhat )
                             {
-                                list.insert_after( itInsert, std::move( *itWhat ) );
+                                list.insert_after( itInsert, std::move( *itWhat ));
                             }
                         } \endcode
                     </td>
@@ -492,13 +520,15 @@ namespace cds { namespace container {
     public:
         /// Default ctor. The initial capacity is 16.
         StripedSet()
-        : base_class()
+            : base_class()
         {}
 
         /// Ctor with initial capacity specified
         StripedSet(
             size_t nCapacity    ///< Initial size of bucket table and lock array. Must be power of two, the minimum is 16.
-        ) : base_class( nCapacity )
+        )
+
+            : base_class( nCapacity )
         {}
 
         /// Ctor with resizing policy (copy semantics)
@@ -508,7 +538,9 @@ namespace cds { namespace container {
         StripedSet(
             size_t nCapacity    ///< Initial size of bucket table and lock array. Must be power of two, the minimum is 16.
             ,resizing_policy const& resizingPolicy  ///< Resizing policy
-        ) : base_class( nCapacity, resizingPolicy )
+        )
+
+            : base_class( nCapacity, resizingPolicy )
         {}
 
         /// Ctor with resizing policy (move semantics)
@@ -519,7 +551,9 @@ namespace cds { namespace container {
         StripedSet(
             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
@@ -590,13 +624,14 @@ namespace cds { namespace container {
         {
             bool bOk;
             bool bResize;
-            size_t nHash = base_class::hashing( value_type( std::forward<Args>(args)...));
+            value_type val( std::forward<Args>( args )... );
+            size_t nHash = base_class::hashing( val );
             bucket_type * pBucket;
             {
                 scoped_cell_lock sl( base_class::m_MutexPolicy, nHash );
                 pBucket = base_class::bucket( nHash );
 
-                bOk = pBucket->emplace( std::forward<Args>(args)...);
+                bOk = pBucket->emplace( std::move( val ));
                 bResize = bOk && base_class::m_ResizingPolicy( ++base_class::m_ItemCounter, *this, *pBucket );
             }
 
@@ -605,53 +640,58 @@ namespace cds { namespace container {
             return bOk;
         }
 
-        /// Ensures that the \p val exists in the set
+        /// Updates the node
         /**
-            The operation performs inserting or changing data.
+            The operation performs inserting or changing data with lock-free manner.
 
-            If the \p val key not found in the set, then the new item created from \p val
-            is inserted into the set. Otherwise, the functor \p func is called with the item found.
-            The functor \p Func should be a function with signature:
-            \code
-                void func( bool bNew, value_type& item, const Q& val );
-            \endcode
-            or a functor:
+            If \p key is not found in the set, then \p key is inserted iff \p bAllowInsert is \p true.
+            Otherwise, the functor \p func is called with item found.
+
+            The functor signature is:
             \code
                 struct my_functor {
                     void operator()( bool bNew, value_type& item, const Q& val );
                 };
             \endcode
-
             with arguments:
             - \p bNew - \p true if the item has been inserted, \p false otherwise
-            - \p item - item of the list
-            - \p val - argument \p val passed into the \p ensure function
+            - \p item - item of the set
+            - \p val - argument \p val passed into the \p %update() function
 
-            The functor can change non-key fields of the \p item.
+            The functor may change non-key fields of the \p item.
 
-            Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
-            \p second is true if new item has been added or \p false if the item with \p val key
-            already exists.
+            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 map.
         */
         template <typename Q, typename Func>
-        std::pair<bool, bool> ensure( Q const& val, Func func )
+        std::pair<bool, bool> update( Q const& val, Func func, bool bAllowInsert = true )
         {
             std::pair<bool, bool> result;
-            bool bResize;
+            bool bResize = false;
             size_t nHash = base_class::hashing( val );
             bucket_type * pBucket;
             {
                 scoped_cell_lock sl( base_class::m_MutexPolicy, nHash );
                 pBucket = base_class::bucket( nHash );
 
-                result = pBucket->ensure( val, func );
-                bResize = result.first && result.second && base_class::m_ResizingPolicy( ++base_class::m_ItemCounter, *this, *pBucket );
+                result = pBucket->update( val, func, bAllowInsert );
+                if ( result.first && result.second )
+                    bResize = base_class::m_ResizingPolicy( ++base_class::m_ItemCounter, *this, *pBucket );
             }
 
             if ( bResize )
                 base_class::resize();
             return result;
         }
+        //@cond
+        template <typename Q, typename Func>
+        CDS_DEPRECATED("ensure() is deprecated, use update()")
+        std::pair<bool, bool> ensure( Q const& val, Func func )
+        {
+            return update( val, func, true );
+        }
+        //@endcond
 
         /// Delete \p key from the set
         /** \anchor cds_nonintrusive_StripedSet_erase
@@ -832,38 +872,50 @@ namespace cds { namespace container {
             return base_class::find_with( val, pred, f );
         }
 
-        /// Find the key \p val
-        /** \anchor cds_nonintrusive_StripedSet_find_val
-
-            The function searches the item with key equal to \p val
+        /// Checks whether the set 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.
 
             Note the hash functor specified for class \p Traits template parameter
             should accept a parameter of type \p Q that can be not the same as \p value_type.
+            Otherwise, you may use \p contains( Q const&, Less pred ) functions with explicit predicate for key comparing.
         */
         template <typename Q>
-        bool find( Q const& val )
+        bool contains( Q const& key )
+        {
+            return base_class::contains( key );
+        }
+        //@cond
+        template <typename Q>
+        CDS_DEPRECATED("use contains()")
+        bool find( Q const& key )
         {
-            return base_class::find( val );
+            return contains( key );
         }
+        //@endcond
 
-        /// Find the key \p val using \p pred predicate
+        /// Checks whether the map contains \p key using \p pred predicate for searching
         /**
-            The function is an analog of \ref cds_nonintrusive_StripedSet_find_val "find(Q 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.
-
-            @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.
+            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 map.
         */
         template <typename Q, typename Less
             , typename Bucket = bucket_type, typename = typename std::enable_if< Bucket::has_find_with >::type >
+        bool contains( Q const& key, Less pred )
+        {
+            return base_class::contains( key, pred );
+        }
+        //@cond
+        template <typename Q, typename Less
+            , typename Bucket = bucket_type, typename = typename std::enable_if< Bucket::has_find_with >::type >
+        CDS_DEPRECATED("use contains()")
         bool find_with( Q const& val, Less pred )
         {
-            return base_class::find_with( val, pred );
+            return contains( val, pred );
         }
+        //@endcond
 
         /// Clears the set
         /**