SplitListMap, SplitListSet:
[libcds.git] / cds / container / split_list_map.h
index 86040560d274ba43771737e8148453534cdf5002..8576f036762d843a6d17e63cb5176f0fcf3035f1 100644 (file)
@@ -1,7 +1,7 @@
 //$$CDS-header$$
 
-#ifndef __CDS_CONTAINER_SPLIT_LIST_MAP_H
-#define __CDS_CONTAINER_SPLIT_LIST_MAP_H
+#ifndef CDSLIB_CONTAINER_SPLIT_LIST_MAP_H
+#define CDSLIB_CONTAINER_SPLIT_LIST_MAP_H
 
 #include <cds/container/split_list_set.h>
 #include <cds/details/binary_functor_wrapper.h>
@@ -19,7 +19,7 @@ namespace cds { namespace container {
         See intrusive::SplitListSet for a brief description of the split-list algorithm.
 
         Template parameters:
-        - \p GC - Garbage collector used
+        - \p GC - Garbage collector used like \p cds::gc::HP or \p cds::gc::DHP
         - \p Key - key type of an item stored in the map. It should be copy-constructible
         - \p Value - value type stored in the map
         - \p Traits - map traits, default is \p split_list::traits. Instead of declaring \p %split_list::traits -based
@@ -75,7 +75,7 @@ namespace cds { namespace container {
 
         You may use the modern option-based declaration instead of classic type-traits-based one:
         \code
-        typedef cc:SplitListMap<
+        typedef cc::SplitListMap<
             cs::gc::DHP             // GC used
             ,int                    // key type
             ,std::string            // value type
@@ -137,6 +137,10 @@ namespace cds { namespace container {
         typedef typename base_class::item_counter   item_counter; ///< Item counter type
         typedef typename base_class::stat           stat;         ///< Internal statistics
 
+        //@cond
+        typedef cds::container::split_list::implementation_tag implementation_tag;
+        //@endcond
+
     protected:
         //@cond
         typedef typename base_class::maker::traits::key_accessor key_accessor;
@@ -308,19 +312,14 @@ namespace cds { namespace container {
             return base_class::emplace( std::forward<K>(key), std::move(mapped_type(std::forward<Args>(args)...)));
         }
 
-        /// 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 \ref 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 );
@@ -329,25 +328,34 @@ namespace cds { namespace container {
 
             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
 
             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 key
-            already is in the list.
+            already is in the map.
 
             @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() ),
+            return base_class::update( std::make_pair( key, mapped_type() ),
                 [&func](bool bNew, value_type& item, value_type const& /*val*/) {
                     func( bNew, item );
-                } );
+                },
+                bAllowInsert );
+        }
+        //@cond
+        // Deprecated, use update()
+        template <typename K, typename Func>
+        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_erase_val
@@ -502,31 +510,49 @@ namespace cds { namespace container {
                 [&f](value_type& pair, K const&){ f( pair ); } );
         }
 
-        /// Finds the key \p key
-        /** \anchor cds_nonintrusive_SplitListMap_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.
+
+            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 K>
+        bool contains( K const& key )
+        {
+            return base_class::contains( key );
+        }
+        //@cond
+        // Deprecated, use contains()
+        template <typename K>
         bool find( K const& key )
         {
-            return base_class::find( key );
+            return contains( key );
         }
+        //@endcond
 
-        /// Finds the key \p val 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_find_val "find(K const&)"
-            but \p pred is used for key comparing.
+            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 K, typename Less>
-        bool find_with( K const& key, Less pred )
+        bool contains( K const& key, Less pred )
         {
             CDS_UNUSED( pred );
-            return base_class::find( 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
+        // Deprecated, use contains()
+        template <typename K, typename Less>
+        bool find_with( K const& key, Less pred )
+        {
+            return contains( key, pred );
+        }
+        //@endcond
 
         /// Finds \p key and return the item found
         /** \anchor cds_nonintrusive_SplitListMap_hp_get
@@ -612,4 +638,4 @@ namespace cds { namespace container {
 
 }} // namespace cds::container
 
-#endif // #ifndef __CDS_CONTAINER_SPLIT_LIST_MAP_H
+#endif // #ifndef CDSLIB_CONTAINER_SPLIT_LIST_MAP_H