[UBsan] Added proper alignment for EllenBinTree node
[libcds.git] / cds / intrusive / striped_set.h
index 62c0799dee92fb17554d017359d35adb995015ce..923fdfb358dd1fd35836368920f65fca2c9c19c5 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_INTRUSIVE_STRIPED_SET_H
 #define CDSLIB_INTRUSIVE_STRIPED_SET_H
@@ -64,8 +92,8 @@ namespace cds { namespace intrusive {
             Note that the choose of resizing policy depends of \p Container type:
             for sequential containers like \p boost::intrusive::list the right policy can significantly improve performance.
             For other, non-sequential types of \p Container (like a \p boost::intrusive::set) the resizing policy is not so important.
-        - \p cds::opt::buffer - a buffer type used only for \p boost::intrusive::unordered_set.
-            Default is <tt>cds::opt::v::static_buffer< cds::any_type, 256 > </tt>.
+        - \p cds::opt::buffer - an initialized buffer type used only for \p boost::intrusive::unordered_set.
+            Default is <tt>cds::opt::v::initialized_static_buffer< cds::any_type, 256 > </tt>.
 
             \p opt::compare or \p opt::less options are used in some \p Container class for ordering.
             \p %opt::compare option has the highest priority: if \p %opt::compare is specified, \p %opt::less is not used.
@@ -167,7 +195,8 @@ namespace cds { namespace intrusive {
                         You should provide two different hash function \p h1 and \p h2 - one for \p boost::intrusive::unordered_set
                         and other for \p %StripedSet. For the best result, \p h1 and \p h2 must be orthogonal i.e. <tt>h1(X) != h2(X)</tt> for any value \p X
 
-                        The option \p opt::buffer is used for \p boost::intrusive::bucket_traits. Default is <tt> cds::opt::v::static_buffer< cds::any_type, 256 > </tt>.
+                        The option \p opt::buffer is used for \p boost::intrusive::bucket_traits.
+                        Default is <tt> cds::opt::v::initialized_static_buffer< cds::any_type, 256 > </tt>.
                         The resizing policy should correlate with the buffer capacity.
                         The default resizing policy is <tt>cds::container::striped_set::load_factor_resizing<256> </tt> what gives load factor 1 for
                         default bucket buffer that is the best for \p boost::intrusive::unordered_set.
@@ -296,10 +325,6 @@ namespace cds { namespace intrusive {
 
         typedef cds::details::Allocator< bucket_type, allocator_type > bucket_allocator;  ///< bucket allocator type based on allocator_type
 
-        //@cond
-        typedef cds::intrusive::striped_set::implementation_tag implementation_tag;
-        //@endcond
-
     protected:
         bucket_type *   m_Buckets       ;   ///< Bucket table
         size_t          m_nBucketMask   ;   ///< Bucket table size - 1. m_nBucketMask + 1 should be power of two.
@@ -384,7 +409,7 @@ namespace cds { namespace intrusive {
                 for ( bucket_iterator it = pCur->begin(); it != itEnd; it = itNext ) {
                     itNext = it;
                     ++itNext;
-                    bucket( m_Hash( *it ) )->move_item( *pCur, it );
+                    bucket( m_Hash( *it ))->move_item( *pCur, it );
                 }
                 pCur->clear();
             }
@@ -400,7 +425,7 @@ namespace cds { namespace intrusive {
             size_t volatile& refBucketMask = m_nBucketMask;
 
             scoped_resize_lock al( m_MutexPolicy );
-            if ( al.success() ) {
+            if ( al.success()) {
                 if ( nOldCapacity != refBucketMask + 1 ) {
                     // someone resized already
                     return;
@@ -461,7 +486,7 @@ namespace cds { namespace intrusive {
         : m_Buckets( nullptr )
         , m_nBucketMask( ( nCapacity ? calc_init_capacity(nCapacity) : c_nMinimalCapacity ) - 1 )
         , m_MutexPolicy( m_nBucketMask + 1 )
-        , m_ResizingPolicy( std::forward<resizing_policy>( resizingPolicy ) )
+        , m_ResizingPolicy( std::forward<resizing_policy>( resizingPolicy ))
         {
             alloc_bucket_table( m_nBucketMask + 1 );
         }
@@ -517,11 +542,12 @@ namespace cds { namespace intrusive {
             return bOk;
         }
 
-        /// Ensures that the \p val exists in the set
+        /// Updates the node
         /**
             The operation performs inserting or changing data with lock-free manner.
 
-            If the item \p val not found in the set, then \p val is inserted into the set.
+            If the item \p val is not found in the set, then \p val is inserted
+            iff \p bAllowInsert is \p true.
             Otherwise, the functor \p func is called with item found.
             The functor signature is:
             \code
@@ -530,18 +556,18 @@ namespace cds { namespace intrusive {
             with arguments:
             - \p bNew - \p true if the item has been inserted, \p false otherwise
             - \p item - item of the set
-            - \p val - argument \p val passed into the \p ensure function
+            - \p val - argument \p val passed into the \p update() function
             If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
             refers to the same thing.
 
             The functor may change non-key fields of the \p item.
 
-            Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successful,
-            \p second is \p true if new item has been added or \p false if the item with \p key
+            Returns std::pair<bool, bool> where \p first is \p true if operation is successful,
+            \p second is \p true if new item has been added or \p false if the item with \p val
             already is in the set.
         */
         template <typename Func>
-        std::pair<bool, bool> ensure( value_type& val, Func func )
+        std::pair<bool, bool> update( value_type& val, Func func, bool bAllowInsert = true )
         {
             std::pair<bool, bool> result;
             bool bResize;
@@ -551,7 +577,7 @@ namespace cds { namespace intrusive {
                 scoped_cell_lock sl( m_MutexPolicy, nHash );
                 pBucket = bucket( nHash );
 
-                result = pBucket->ensure( val, func );
+                result = pBucket->update( val, func, bAllowInsert );
                 bResize = result.first && result.second && m_ResizingPolicy( ++m_ItemCounter, *this, *pBucket );
             }
 
@@ -559,6 +585,13 @@ namespace cds { namespace intrusive {
                 resize();
             return result;
         }
+        //@cond
+        template <typename Func>
+        std::pair<bool, bool> ensure( value_type& val, Func func )
+        {
+            return update( val, func, true );
+        }
+        //@endcond
 
         /// Unlink the item \p val from the set
         /**
@@ -742,32 +775,48 @@ namespace cds { namespace intrusive {
             return find_with_( val, pred, f );
         }
 
-        /// Find the key \p val
-        /** \anchor cds_intrusive_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 contains( Q const& key )
+        {
+            return find( key, [](value_type&, Q const& ) {} );
+        }
+        //@cond
+        template <typename Q>
+        CDS_DEPRECATED("use contains()")
         bool find( Q const& val )
         {
-            return find( val, [](value_type&, Q const& ) {} );
+            return contains( val );
         }
+        //@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_intrusive_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.
+            The function is an analog of <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.
         */
         template <typename Q, typename Less>
+        bool contains( Q const& key, Less pred )
+        {
+            return find_with( key, pred, [](value_type& , Q const& ) {} );
+        }
+        //@cond
+        template <typename Q, typename Less>
+        CDS_DEPRECATED("use contains()")
         bool find_with( Q const& val, Less pred )
         {
-            return find_with( val, pred, [](value_type& , Q const& ) {} );
+            return contains( val, pred );
         }
+        //@endcond
 
         /// Clears the set
         /**