Fixed doc typo
[libcds.git] / cds / container / striped_set / adapter.h
index 6bd3927d297797980885faaabf6152132b59ecfb..3a6bc81d886e7a60e5000d738d66b725484e589e 100644 (file)
@@ -1,7 +1,35 @@
-//$$CDS-header$$
-
-#ifndef __CDS_CONTAINER_STRIPED_SET_ADAPTER_H
-#define __CDS_CONTAINER_STRIPED_SET_ADAPTER_H
+/*
+    This file is a part of libcds - Concurrent Data Structures library
+
+    (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
+
+    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_ADAPTER_H
+#define CDSLIB_CONTAINER_STRIPED_SET_ADAPTER_H
 
 #include <cds/intrusive/striped_set/adapter.h>
 #include <cds/intrusive/striped_set/striping_policy.h>
@@ -62,8 +90,7 @@ namespace cds { namespace container {
                 The type \p Q can differ from \ref value_type of items storing in the container.
                 Therefore, the \p value_type should be comparable with type \p Q and constructible from type \p Q,
 
-                The user-defined functor is called only if the inserting is success. It can be passed by reference
-                using <tt>boost::ref</tt>
+                The user-defined functor is called only if the inserting is success.
                 <hr>
 
             <b>Inserts data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt></b>
@@ -74,12 +101,12 @@ namespace cds { namespace container {
                 variadic template and move semantics
             <hr>
 
-            <b>Ensures that the \p item exists in the container</b>
-            \code template <typename Q, typename Func> std::pair<bool, bool> ensure( const Q& val, Func func ) \endcode
+            <b>Updates \p item</b>
+            \code template <typename Q, typename Func> std::pair<bool, bool> update( const Q& val, Func func, bool bAllowInsert ) \endcode
                 The operation performs inserting or changing data.
 
                 If the \p val key not found in the container, then the new item created from \p val
-                is inserted. Otherwise, the functor \p func is called with the item found.
+                is inserted iff \p bAllowInsert is \p true. Otherwise, the functor \p func is called with the item found.
                 The \p Func functor has interface:
                 \code
                     void func( bool bNew, value_type& item, const Q& val );
@@ -94,16 +121,14 @@ namespace cds { namespace container {
                 where arguments are:
                 - \p bNew - \p true if the item has been inserted, \p false otherwise
                 - \p item - container's item
-                - \p val - argument \p val passed into the \p ensure function
+                - \p val - argument \p val passed into the \p update() function
 
                 The functor can change non-key fields of the \p item.
 
                 The type \p Q can differ from \ref value_type of items storing in the container.
                 Therefore, the \p value_type should be comparable with type \p Q and constructible from type \p Q,
 
-                You can pass \p func argument by reference using <tt>boost::ref</tt>.
-
-                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 val key
                 already exists.
                 <hr>
@@ -120,7 +145,6 @@ namespace cds { namespace container {
                     void operator()(value_type const& val);
                 };
                 \endcode
-                The functor can be passed by reference using <tt>boost:ref</tt>
 
                 The type \p Q can differ from \ref value_type of items storing in the container.
                 Therefore, the \p value_type should be comparable with type \p Q.
@@ -140,8 +164,6 @@ namespace cds { namespace container {
                 \endcode
                 where \p item is the item found, \p val is the <tt>find</tt> function argument.
 
-                You can pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
-
                 The functor can change non-key fields of \p item.
                 The \p val argument is non-const since it can be used as \p f functor destination i.e., the functor
                 can modify both arguments.
@@ -169,7 +191,7 @@ namespace cds { namespace container {
             <hr>
 
         */
-        template < typename Container, CDS_DECL_OPTIONS >
+        template < typename Container, typename... Options>
         class adapt
         {
         public:
@@ -183,14 +205,34 @@ namespace cds { namespace container {
         //@cond
         using cds::intrusive::striped_set::adapted_sequential_container;
         using cds::intrusive::striped_set::adapted_container;
+        //@endcond
 
-        using cds::intrusive::striped_set::load_factor_resizing;
-        using cds::intrusive::striped_set::single_bucket_size_threshold;
-        using cds::intrusive::striped_set::no_resizing;
+        ///@copydoc cds::intrusive::striped_set::load_factor_resizing
+        template <size_t LoadFactor>
+        using load_factor_resizing = cds::intrusive::striped_set::load_factor_resizing<LoadFactor>;
 
-        using cds::intrusive::striped_set::striping;
-        using cds::intrusive::striped_set::refinable;
-        //@endcond
+        ///@copydoc cds::intrusive::striped_set::rational_load_factor_resizing
+        template <size_t Numerator, size_t Denominator = 1>
+        using rational_load_factor_resizing = cds::intrusive::striped_set::rational_load_factor_resizing<Numerator, Denominator>;
+
+        ///@copydoc cds::intrusive::striped_set::single_bucket_size_threshold
+        template <size_t Threshold>
+        using single_bucket_size_threshold = cds::intrusive::striped_set::single_bucket_size_threshold<Threshold>;
+
+        ///@copydoc cds::intrusive::striped_set::no_resizing
+        typedef cds::intrusive::striped_set::no_resizing no_resizing;
+
+        ///@copydoc cds::intrusive::striped_set::striping
+        template <class Lock = std::mutex, class Alloc = CDS_DEFAULT_ALLOCATOR >
+        using striping = cds::intrusive::striped_set::striping<Lock, Alloc>;
+
+        ///@copydoc cds::intrusive::striped_set::refinable
+        template <
+            class RecursiveLock = std::recursive_mutex,
+            typename BackOff = cds::backoff::yield,
+            class Alloc = CDS_DEFAULT_ALLOCATOR
+        >
+        using refinable = cds::intrusive::striped_set::refinable<RecursiveLock, BackOff, Alloc >;
 
         //@cond
         namespace details {
@@ -223,7 +265,7 @@ namespace cds { namespace container {
                 };
             };
 
-            template <class Set, CDS_SPEC_OPTIONS>
+            template <class Set, typename... Options>
             class boost_set_adapter: public striped_set::adapted_container
             {
             public:
@@ -241,7 +283,7 @@ namespace cds { namespace container {
                     typename cds::opt::value<
                         typename cds::opt::find_option<
                             cds::opt::copy_policy< cds::container::striped_set::move_item >
-                            , CDS_OPTIONS
+                            , Options...
                         >::type
                     >::copy_policy
                     , cds::container::striped_set::copy_item, copy_item_policy<container_type>
@@ -266,25 +308,32 @@ namespace cds { namespace container {
                 {
                     std::pair<iterator, bool> res = m_Set.insert( value_type(val) );
                     if ( res.second )
-                        cds::unref(f)( const_cast<value_type&>(*res.first) );
+                        f( const_cast<value_type&>(*res.first) );
                     return res.second;
                 }
 
-#           ifdef CDS_EMPLACE_SUPPORT
                 template <typename... Args>
                 bool emplace( Args&&... args )
                 {
                     std::pair<iterator, bool> res = m_Set.emplace( std::forward<Args>(args)... );
                     return res.second;
                 }
-#           endif
 
                 template <typename Q, typename Func>
-                std::pair<bool, bool> ensure( const Q& val, Func func )
+                std::pair<bool, bool> update( const Q& val, Func func, bool bAllowInsert )
                 {
-                    std::pair<iterator, bool> res = m_Set.insert( value_type(val) );
-                    cds::unref(func)( res.second, const_cast<value_type&>(*res.first), val );
-                    return std::make_pair( true, res.second );
+                    if ( bAllowInsert ) {
+                        std::pair<iterator, bool> res = m_Set.insert( value_type(val) );
+                        func( res.second, const_cast<value_type&>(*res.first), val );
+                        return std::make_pair( true, res.second );
+                    }
+                    else {
+                        auto it = m_Set.find( value_type( val ));
+                        if ( it == m_Set.end() )
+                            return std::make_pair( false, false );
+                        func( false, const_cast<value_type&>(*it), val );
+                        return std::make_pair( true, false );
+                    }
                 }
 
                 template <typename Q, typename Func>
@@ -293,7 +342,7 @@ namespace cds { namespace container {
                     const_iterator it = m_Set.find( value_type(key) );
                     if ( it == m_Set.end() )
                         return false;
-                    cds::unref(f)( const_cast<value_type&>(*it) );
+                    f( const_cast<value_type&>(*it) );
                     m_Set.erase( it );
                     return true;
                 }
@@ -304,7 +353,7 @@ namespace cds { namespace container {
                     iterator it = m_Set.find( value_type(val) );
                     if ( it == m_Set.end() )
                         return false;
-                    cds::unref(f)( const_cast<value_type&>(*it), val );
+                    f( const_cast<value_type&>(*it), val );
                     return true;
                 }
 
@@ -368,7 +417,7 @@ namespace cds { namespace container {
                 };
             };
 
-            template <class Map, CDS_SPEC_OPTIONS>
+            template <class Map, typename... Options>
             class boost_map_adapter: public striped_set::adapted_container
             {
             public:
@@ -388,7 +437,7 @@ namespace cds { namespace container {
                     typename cds::opt::value<
                     typename cds::opt::find_option<
                         cds::opt::copy_policy< cds::container::striped_set::move_item >
-                        , CDS_OPTIONS
+                        , Options...
                     >::type
                     >::copy_policy
                     , cds::container::striped_set::copy_item, copy_item_policy<container_type>
@@ -403,36 +452,43 @@ namespace cds { namespace container {
                 template <typename Q, typename Func>
                 bool insert( const Q& key, Func f )
                 {
-                    std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ) );
+                    std::pair<iterator, bool> res = m_Map.insert( value_type( key_type( key ), mapped_type() ) );
                     if ( res.second )
-                        cds::unref(f)( *res.first );
+                        f( *res.first );
                     return res.second;
                 }
 
-#           ifdef CDS_EMPLACE_SUPPORT
                 template <typename Q, typename... Args>
                 bool emplace( Q&& key, Args&&... args )
                 {
-                    std::pair<iterator, bool> res = m_Map.emplace( std::forward<Q>(key), std::move( mapped_type( std::forward<Args>(args)...)));
+                    std::pair<iterator, bool> res = m_Map.emplace( key_type( std::forward<Q>( key )), mapped_type( std::forward<Args>( args )...));
                     return res.second;
                 }
-#           endif
 
                 template <typename Q, typename Func>
-                std::pair<bool, bool> ensure( const Q& val, Func func )
+                std::pair<bool, bool> update( const Q& key, Func func, bool bAllowInsert )
                 {
-                    std::pair<iterator, bool> res = m_Map.insert( value_type( val, mapped_type() ));
-                    cds::unref(func)( res.second, *res.first );
-                    return std::make_pair( true, res.second );
+                    if ( bAllowInsert ) {
+                        std::pair<iterator, bool> res = m_Map.insert( value_type( key_type( key ), mapped_type() ));
+                        func( res.second, *res.first );
+                        return std::make_pair( true, res.second );
+                    }
+                    else {
+                        auto it = m_Map.find( key_type( key ));
+                        if ( it == end() )
+                            return std::make_pair( false, false );
+                        func( false, *it );
+                        return std::make_pair( true, false );
+                    }
                 }
 
                 template <typename Q, typename Func>
                 bool erase( const Q& key, Func f )
                 {
-                    iterator it = m_Map.find( key_type(key) );
+                    iterator it = m_Map.find( key_type( key ));
                     if ( it == m_Map.end() )
                         return false;
-                    cds::unref(f)( *it );
+                    f( *it );
                     m_Map.erase( it );
                     return true;
                 }
@@ -440,10 +496,10 @@ namespace cds { namespace container {
                 template <typename Q, typename Func>
                 bool find( Q& val, Func f )
                 {
-                    iterator it = m_Map.find( key_type(val) );
+                    iterator it = m_Map.find( key_type( val ));
                     if ( it == m_Map.end() )
                         return false;
-                    cds::unref(f)( *it, val );
+                    f( *it, val );
                     return true;
                 }
 
@@ -476,4 +532,4 @@ namespace cds { namespace container {
 }} // namespace cds::container
 
 
-#endif // #ifndef __CDS_CONTAINER_STRIPED_SET_ADAPTER_H
+#endif // #ifndef CDSLIB_CONTAINER_STRIPED_SET_ADAPTER_H