Fixed doc typo
[libcds.git] / cds / intrusive / striped_set / adapter.h
index 6c0436722a2333259462bcdb60332c2dc147a352..aa0bab6676bbcab5c01dbe21beaf10c768f1451d 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-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_INTRUSIVE_STRIPED_SET_ADAPTER_H
 #define CDSLIB_INTRUSIVE_STRIPED_SET_ADAPTER_H
@@ -12,7 +40,6 @@ namespace cds { namespace intrusive {
 
     /// StripedSet related definitions
     namespace striped_set {
-
         /// Default adapter for intrusive striped/refinable hash set
         /**
             By default, the metafunction does not make any transformation for container type \p Container.
@@ -49,11 +76,11 @@ namespace cds { namespace intrusive {
                 The user-defined functor \p f is called only if the inserting is success.
                 <hr>
 
-            <b>Ensures that the \p item exists in the container</b>
-            \code template <typename Func> std::pair<bool, bool> ensure( value_type& val, Func f ) \endcode
+            <b>Updates the item in the container</b>
+            \code template <typename Func> std::pair<bool, bool> update( value_type& val, Func f, bool bAllowInsert = true ) \endcode
                 The operation performs inserting or changing data.
 
-                If the \p val key not found in the container, then \p val is inserted.
+                If the \p val key not found in the container, then \p val is inserted iff \p bAllowInsert is \p true.
                 Otherwise, the functor \p f is called with the item found.
 
                 The \p Func functor has the following interface:
@@ -70,14 +97,14 @@ namespace cds { namespace intrusive {
                 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
 
                 If \p val has been inserted (i.e. <tt>bNew == true</tt>) then \p item and \p val
                 are the same element: <tt>&item == &val</tt>. Otherwise, they are different.
 
                 The functor can change non-key fields of the \p item.
 
-                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>
@@ -196,7 +223,7 @@ namespace cds { namespace intrusive {
                 typedef typename container_type::iterator       iterator        ;   ///< container iterator
                 typedef typename container_type::const_iterator const_iterator  ;   ///< container const iterator
 
-                typedef typename container_type::value_compare  key_comparator;
+                typedef typename container_type::key_compare  key_comparator;
 
             private:
                 container_type  m_Set;
@@ -220,16 +247,25 @@ namespace cds { namespace intrusive {
                 }
 
                 template <typename Func>
-                std::pair<bool, bool> ensure( value_type& val, Func f )
+                std::pair<bool, bool> update( value_type& val, Func f, bool bAllowInsert )
                 {
-                    std::pair<iterator, bool> res = m_Set.insert( val );
-                    f( res.second, *res.first, val );
-                    return std::make_pair( true, res.second );
+                    if ( bAllowInsert ) {
+                        std::pair<iterator, bool> res = m_Set.insert( val );
+                        f( res.second, *res.first, val );
+                        return std::make_pair( true, res.second );
+                    }
+                    else {
+                        auto it = m_Set.find( val, key_comparator() );
+                        if ( it == m_Set.end() )
+                            return std::make_pair( false, false );
+                        f( false, *it, val );
+                        return std::make_pair( true, false );
+                    }
                 }
 
                 bool unlink( value_type& val )
                 {
-                    iterator it = m_Set.find( value_type(val) );
+                    iterator it = m_Set.find( val, key_comparator() );
                     if ( it == m_Set.end() || &(*it) != &val )
                         return false;
                     m_Set.erase( it );
@@ -261,13 +297,13 @@ namespace cds { namespace intrusive {
                 }
 
                 template <typename Q, typename Func>
-                bool find( Q& key, Func f )
+                bool find( Q const& key, Func f )
                 {
                     return find( key, key_comparator(), f );
                 }
 
                 template <typename Q, typename Compare, typename Func>
-                bool find( Q& key, Compare cmp, Func f )
+                bool find( Q const& key, Compare cmp, Func f )
                 {
                     iterator it = m_Set.find( key, cmp );
                     if ( it == m_Set.end() )