Updated copyright
[libcds.git] / cds / container / split_list_map_rcu.h
index c18aa9dc12130c4066abd403f2696b5167546a3b..99bf6c958060485718a77f22e8c82fcc54e46281 100644 (file)
@@ -1,7 +1,35 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
 
-#ifndef __CDS_CONTAINER_SPLIT_LIST_MAP_RCU_H
-#define __CDS_CONTAINER_SPLIT_LIST_MAP_RCU_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_SPLIT_LIST_MAP_RCU_H
+#define CDSLIB_CONTAINER_SPLIT_LIST_MAP_RCU_H
 
 #include <cds/container/split_list_set_rcu.h>
 #include <cds/details/binary_functor_wrapper.h>
@@ -112,7 +140,7 @@ namespace cds { namespace container {
 
         You may use the modern option-based declaration instead of classic traits-based one:
         \code
-        typedef cc:SplitListMap<
+        typedef cc::SplitListMap<
             cds::urcu::gc<cds::urcu::general_buffered<> >  // RCU type
             ,int                    // key type
             ,std::string            // value type
@@ -178,6 +206,7 @@ namespace cds { namespace container {
         typedef typename base_class::exempt_ptr     exempt_ptr; ///< pointer to extracted node
         /// Group of \p extract_xxx functions require external locking if underlying ordered list requires that
         static CDS_CONSTEXPR const bool c_bExtractLockExternal = base_class::c_bExtractLockExternal;
+        typedef typename base_class::raw_ptr        raw_ptr;    ///< type of \p get() return value
 
     protected:
         //@cond
@@ -217,7 +246,7 @@ namespace cds { namespace container {
         {
             return base_class::begin();
         }
-        const_iterator cbegin()
+        const_iterator cbegin() const
         {
             return base_class::cbegin();
         }
@@ -229,7 +258,7 @@ namespace cds { namespace container {
         {
             return base_class::end();
         }
-        const_iterator cend()
+        const_iterator cend() const
         {
             return base_class::cend();
         }
@@ -270,8 +299,7 @@ namespace cds { namespace container {
         template <typename K>
         bool insert( K const& key )
         {
-            //TODO: pass arguments by reference (make_pair makes copy)
-            return base_class::insert( std::make_pair( key, mapped_type() ) );
+            return base_class::emplace( key_type( key ), mapped_type());
         }
 
         /// Inserts new node
@@ -291,7 +319,7 @@ namespace cds { namespace container {
         bool insert( K const& key, V const& val )
         {
             //TODO: pass arguments by reference (make_pair makes copy)
-            return base_class::insert( std::make_pair(key, val) );
+            return base_class::emplace( key_type( key ), mapped_type( val ));
         }
 
         /// Inserts new node and initialize it by a functor
@@ -326,10 +354,10 @@ namespace cds { namespace container {
             The function applies RCU lock internally.
         */
         template <typename K, typename Func>
-        bool insert_key( K const& key, Func func )
+        bool insert_with( K const& key, Func func )
         {
             //TODO: pass arguments by reference (make_pair makes copy)
-            return base_class::insert( std::make_pair( key, mapped_type() ), func );
+            return base_class::insert( std::make_pair( key_type( key ), mapped_type()), func );
         }
 
         /// For key \p key inserts data of type \p mapped_type created in-place from \p args
@@ -343,17 +371,18 @@ namespace cds { namespace container {
         template <typename K, typename... Args>
         bool emplace( K&& key, Args&&... args )
         {
-            return base_class::emplace( std::forward<K>(key), std::move(mapped_type(std::forward<Args>(args)...)));
+            return base_class::emplace( key_type( std::forward<K>( key )), mapped_type( std::forward<Args>(args)... ));
         }
 
-        /// Ensures that the \p key exists in the map
+        /// Updates data by \p key
         /**
-            The operation performs inserting or changing data with lock-free manner.
+            The operation performs inserting or replacing the element 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; in this case the \p key_type should be
-            constructible from type \p K.
-            Otherwise, the functor \p func is called with item found.
+            will be inserted into the map iff \p bAllowInsert is \p true.
+            (note that in this case the \ref key_type should be constructible from type \p K).
+            Otherwise, if \p key is found, the functor \p func is called with item found.
+
             The functor \p Func signature is:
             \code
                 struct my_functor {
@@ -362,31 +391,40 @@ namespace cds { namespace container {
             \endcode
             with arguments:
             - \p bNew - \p true if the item has been inserted, \p false otherwise
-            - \p item - item of the list
+            - \p item - the item found or inserted
 
-            The functor may change any fields of the \p item.second that is \p mapped_type;
-            however, \p func must guarantee that during changing no any other modifications
-            could be made on this item by concurrent threads.
+            The functor may change any fields of the \p item.second that is \p mapped_type.
 
             The function applies RCU lock internally.
 
-            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 exists.
 
-            @warning For \ref cds_intrusive_MichaelKVList_hp "MichaelKVList" as the ordered list see \ref cds_intrusive_item_creating "insert item troubleshooting".
-            \ref cds_intrusive_LazyKVList_hp "LazyKVList" provides exclusive access to inserted item and does not require any node-level
+            @warning For \ref cds_nonintrusive_MichaelKVList_gc "MichaelKVList" as the ordered list see \ref cds_intrusive_item_creating "insert item troubleshooting".
+            \ref cds_nonintrusive_LazyKVList_gc "LazyKVList" provides exclusive access to inserted item and does not require any node-level
             synchronization.
         */
         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 )
         {
             //TODO: pass arguments by reference (make_pair makes copy)
-            return base_class::ensure( std::make_pair( key, mapped_type() ),
-                [&func](bool bNew, value_type& item, value_type const& /*val*/) {
+            typedef decltype( std::make_pair( key_type( key ), mapped_type())) arg_pair_type;
+
+            return base_class::update( std::make_pair( key_type( key ), mapped_type()),
+                [&func]( bool bNew, value_type& item, arg_pair_type const& /*val*/ ) {
                     func( bNew, item );
-                } );
+                },
+                bAllowInsert );
         }
+        //@cond
+        template <typename K, typename Func>
+        CDS_DEPRECATED("ensure() is deprecated, use update()")
+        std::pair<bool, bool> ensure( K const& key, Func func )
+        {
+            return update( key, func, true );
+        }
+        //@endcond
 
         /// Deletes \p key from the map
         /** \anchor cds_nonintrusive_SplitListMap_rcu_erase_val
@@ -411,7 +449,8 @@ namespace cds { namespace container {
         template <typename K, typename Less>
         bool erase_with( K const& key, Less pred )
         {
-            return base_class::erase_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>() );
+            CDS_UNUSED( pred );
+            return base_class::erase_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
         }
 
         /// Deletes \p key from the map
@@ -426,7 +465,6 @@ namespace cds { namespace container {
                 void operator()(value_type& item) { ... }
             };
             \endcode
-            The functor may be passed by reference using <tt>boost:ref</tt>
 
             RCU \p synchronize method can be called. RCU should not be locked.
 
@@ -448,39 +486,39 @@ namespace cds { namespace container {
         template <typename K, typename Less, typename Func>
         bool erase_with( K const& key, Less pred, Func f )
         {
+            CDS_UNUSED( pred );
             return base_class::erase_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>(), f );
         }
 
         /// Extracts an item from the map
         /** \anchor cds_nonintrusive_SplitListMap_rcu_extract
             The function searches an item with key equal to \p key in the map,
-            unlinks it from the map, places item pointer into \p dest argument, and returns \p true.
-            If the item with the key equal to \p key is not found the function return \p false.
+            unlinks it from the map, and returns \ref cds::urcu::exempt_ptr "exempt_ptr" pointer to the item found.
+            If the item with the key equal to \p key is not found the function returns an empty \p exempt_ptr.
 
-            @note The function does NOT call RCU read-side lock or synchronization,
-            and does NOT dispose the item found. It just excludes the item from the map
-            and returns a pointer to item found.
-            You should lock RCU before calling of the function, and you should synchronize RCU
-            outside the RCU lock to free extracted item
+            Depends on ordered list you should or should not lock RCU before calling of this function:
+            - for the set based on \ref cds_intrusive_MichaelList_rcu "MichaelList" RCU should not be locked
+            - for the set based on \ref cds_intrusive_LazyList_rcu "LazyList" RCU should be locked
+            See ordered list implementation for details.
 
             \code
             typedef cds::urcu::gc< general_buffered<> > rcu;
+
+            // Split-list set based on MichaelList by default
             typedef cds::container::SplitListMap< rcu, int, Foo > splitlist_map;
 
             splitlist_map theMap;
             // ...
 
             typename splitlist_map::exempt_ptr p;
-            {
-                // first, we should lock RCU
-                typename splitlist_map::rcu_lock lock;
 
-                // Now, you can apply extract function
-                // Note that you must not delete the item found inside the RCU lock
-                if ( theMap.extract( p, 10 )) {
-                    // do something with p
-                    ...
-                }
+            // For MichaelList we should not lock RCU
+
+            // Now, you can apply extract function
+            p = theMap.extract( 10 )
+            if ( p ) {
+                // do something with p
+                ...
             }
 
             // We may safely release p here
@@ -489,22 +527,22 @@ namespace cds { namespace container {
             \endcode
         */
         template <typename K>
-        bool extract( exempt_ptr& dest, K const& key )
+        exempt_ptr extract( K const& key )
         {
-            return base_class::extract( dest, key );
+            return base_class::extract( key );
         }
 
         /// Extracts an item from the map using \p pred predicate for searching
         /**
-            The function is an analog of \ref cds_nonintrusive_SplitListMap_rcu_extract "extract(exempt_ptr&, K const&)"
-            but \p pred is used for key comparing.
+            The function is an analog of \p extract(K const&) but \p pred is used for key comparing.
             \p Less functor has the interface like \p std::less.
             \p pred must imply the same element order as the comparator used for building the map.
         */
         template <typename K, typename Less>
-        bool extract_with( exempt_ptr& dest, K const& key, Less pred )
+        exempt_ptr extract_with( K const& key, Less pred )
         {
-            return base_class::extract_with( dest, key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
+            CDS_UNUSED( pred );
+            return base_class::extract_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
         }
 
         /// Finds the key \p key
@@ -519,8 +557,6 @@ namespace cds { namespace container {
             \endcode
             where \p item is the item found.
 
-            You may pass \p f argument by reference using \p std::ref.
-
             The functor may change \p item.second. Note that the functor is only guarantee
             that \p item cannot be disposed during functor is executing.
             The functor does not serialize simultaneous access to the map's \p item. If such access is
@@ -546,42 +582,58 @@ namespace cds { namespace container {
         template <typename K, typename Less, typename Func>
         bool find_with( K const& key, Less pred, Func f )
         {
+            CDS_UNUSED( pred );
             return base_class::find_with( key,
                 cds::details::predicate_wrapper<value_type, Less, key_accessor>(),
                 [&f](value_type& pair, K const&){ f( pair ); } );
         }
 
-        /// Finds the key \p key
-        /** \anchor cds_nonintrusive_SplitListMap_rcu_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.
 
             The function applies RCU lock internally.
         */
         template <typename K>
+        bool contains( K const& key )
+        {
+            return base_class::contains( key );
+        }
+        //@cond
+        template <typename K>
+        CDS_DEPRECATED("deprecated, use contains()")
         bool find( K const& key )
         {
             return base_class::find( key );
         }
+        //@endcond
 
-        /// Finds the key \p key using \p pred predicate for searching
+        /// Checks whether the map contains \p key using \p pred predicate for searching
         /**
-            The function is an analog of \ref cds_nonintrusive_SplitListMap_rcu_find_val "find(K const&)"
-            but \p pred is used for key comparing.
+            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 map.
         */
         template <typename K, typename Less>
+        bool contains( K const& key, Less pred )
+        {
+            CDS_UNUSED( pred );
+            return base_class::contains( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
+        }
+        //@cond
+        template <typename K, typename Less>
+        CDS_DEPRECATED("deprecated, use contains()")
         bool find_with( K const& key, Less pred )
         {
-            return base_class::find_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>() );
+            return contains( key, pred );
         }
+        //@endcond
 
         /// Finds \p key and return the item found
         /** \anchor cds_intrusive_SplitListMap_rcu_get
             The function searches the item with key equal to \p key and returns the pointer to item found.
-            If \p key is not found it returns \p nullptr.
+            If \p key is not found it returns empty \p raw_ptr.
 
             Note the compare functor should accept a parameter of type \p K that can be not the same as \p value_type.
 
@@ -596,7 +648,7 @@ namespace cds { namespace container {
                 // Lock RCU
                 typename splitlist_map::rcu_lock lock;
 
-                typename splitlist_map::value_type * pVal = theMap.get( 5 );
+                typename splitlist_map::raw_ptr pVal = theMap.get( 5 );
                 if ( pVal ) {
                     // Deal with pVal
                     //...
@@ -607,7 +659,7 @@ namespace cds { namespace container {
             \endcode
         */
         template <typename K>
-        value_type * get( K const& key )
+        raw_ptr get( K const& key )
         {
             return base_class::get( key );
         }
@@ -622,8 +674,9 @@ namespace cds { namespace container {
             \p pred must imply the same element order as the comparator used for building the map.
         */
         template <typename K, typename Less>
-        value_type * get_with( K const& key, Less pred )
+        raw_ptr get_with( K const& key, Less pred )
         {
+            CDS_UNUSED( pred );
             return base_class::get_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>());
         }
 
@@ -654,8 +707,14 @@ namespace cds { namespace container {
         {
             return base_class::statistics();
         }
+
+        /// Returns internal statistics for \p ordered_list
+        typename ordered_list::stat const& list_statistics() const
+        {
+            return base_class::list_statistics();
+        }
     };
 
 }} // namespace cds::container
 
-#endif // #ifndef __CDS_CONTAINER_SPLIT_LIST_MAP_RCU_H
+#endif // #ifndef CDSLIB_CONTAINER_SPLIT_LIST_MAP_RCU_H