MichaelList, LazyList, MichaelMap:
[libcds.git] / cds / intrusive / impl / lazy_list.h
index da8a5f6a0749de8be13a9cd771cd94d1b3ebb602..d8372e84f49d945602867dc29baaab9c94c508bb 100644 (file)
@@ -526,36 +526,49 @@ namespace cds { namespace intrusive {
             return insert_at( &m_Head, val, f );
         }
 
-        /// Ensures that the \p item exists in the list
+        /// Updates the item
         /**
             The operation performs inserting or changing data with lock-free manner.
 
-            If the item \p val not found in the list, then \p val is inserted into the list.
+            If the item \p val not found in the list, then \p val is inserted into the list
+            iff \p bAllowInsert is \p true.
             Otherwise, the functor \p func is called with item found.
             The functor signature is:
             \code
-                void func( bool bNew, value_type& item, value_type& val );
+            struct functor {
+                void operator()( bool bNew, value_type& item, value_type& 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 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
             refer to the same thing.
 
             The functor may change non-key fields of the \p item.
             While the functor \p f is working the item \p item is locked,
-            so \p f has exclusive access to the item.
+            so \p func has exclusive access to the item.
 
-            Returns std::pair<bool, bool> where \p first is \p true if operation is successfull,
+            Returns <tt> std::pair<bool, bool>  </tt> where \p first is \p true if operation is successfull,
             \p second is \p true if new item has been added or \p false if the item with \p key
             already is in the list.
+
+            The function makes RCU lock internally.
         */
         template <typename Func>
+        std::pair<bool, bool> update( value_type& val, Func func, bool bAllowInsert = true )
+        {
+            return update_at( &m_Head, val, func, bAllowInsert );
+        }
+        //@cond
+        // Deprecated, use update()
+        template <typename Func>
         std::pair<bool, bool> ensure( value_type& val, Func func )
         {
-            return ensure_at( &m_Head, val, func );
+            return update( val, func, true );
         }
+        //@endcond
 
         /// Unlinks the item \p val from the list
         /**
@@ -737,30 +750,45 @@ namespace cds { namespace intrusive {
         }
         //@endcond
 
-        /// Finds the key \p key
-        /** \anchor cds_intrusive_LazyList_hp_find_val
+        /// Checks whether the list 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
+            and returns \p true if it is found, and \p false otherwise.
         */
         template <typename Q>
-        bool find( Q const& key )
+        bool contains( Q const& key )
         {
             return find_at( &m_Head, key, key_comparator() );
         }
+        //@cond
+        // Deprecated, use contains()
+        template <typename Q>
+        bool find( Q const& key )
+        {
+            return contains( key );
+        }
+        //@cond
 
-        /// Finds \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_intrusive_LazyList_hp_find_val "find(Q 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 pred must imply the same element order as the comparator used for building the list.
+            \p Less must imply the same element order as the comparator used for building the list.
         */
         template <typename Q, typename Less>
-        bool find_with( Q const& key, Less pred )
+        bool contains( Q const& key, Less pred )
         {
             CDS_UNUSED( pred );
             return find_at( &m_Head, key, cds::opt::details::make_comparator_from_less<Less>() );
         }
+        //@cond
+        // Deprecated, use contains()
+        template <typename Q, typename Less>
+        bool find_with( Q const& key, Less pred )
+        {
+            return contains( key, pred );
+        }
+        //@endcond
 
         /// Finds \p key and return the item found
         /** \anchor cds_intrusive_LazyList_hp_get
@@ -930,7 +958,7 @@ namespace cds { namespace intrusive {
         }
 
         template <typename Func>
-        std::pair<bool, bool> ensure_at( node_type * pHead, value_type& val, Func func )
+        std::pair<bool, bool> update_at( node_type * pHead, value_type& val, Func func, bool bAllowInsert )
         {
             position pos;
             key_comparator  cmp;
@@ -948,6 +976,9 @@ namespace cds { namespace intrusive {
                         }
                         else {
                             // new key
+                            if ( !bAllowInsert )
+                                return std::make_pair( false, false );
+
                             link_checker::is_empty( node_traits::to_node_ptr( val ) );
 
                             link_node( node_traits::to_node_ptr( val ), pos.pPred, pos.pCur );