Uses different pass count for different parallel queue test cases
[libcds.git] / cds / container / skip_list_map_nogc.h
index 2ab08886081fc46df169cebcac8b0602f6ec383d..d86ad94a0eb59fc0c6f55c19b50a20695fccfa71 100644 (file)
@@ -1,7 +1,35 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
 
-#ifndef __CDS_CONTAINER_SKIP_LIST_MAP_NOGC_H
-#define __CDS_CONTAINER_SKIP_LIST_MAP_NOGC_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_SKIP_LIST_MAP_NOGC_H
+#define CDSLIB_CONTAINER_SKIP_LIST_MAP_NOGC_H
 
 #include <cds/container/skip_list_set_nogc.h>
 
@@ -19,7 +47,6 @@ namespace cds { namespace container {
     }} // namespace skip_list::details
     //@endcond
 
-
     /// Lock-free skip-list map (template specialization for gc::nogc)
     /** @ingroup cds_nonintrusive_map
         \anchor cds_nonintrusive_SkipListMap_nogc
@@ -29,31 +56,17 @@ namespace cds { namespace container {
         See \ref cds_nonintrusive_SkipListMap_hp "SkipListMap" for detailed description.
 
         Template arguments:
-        - \p K - type of a key to be stored in the list.
-        - \p T - type of a value to be stored in the list.
-        - \p Traits - type traits. See skip_list::type_traits for explanation.
-
-        It is possible to declare option-based list with cds::container::skip_list::make_traits metafunction istead of \p Traits template
-        argument.
-        Template argument list \p Options of cds::container::skip_list::make_traits metafunction are:
-        - opt::compare - key compare functor. No default functor is provided.
-            If the option is not specified, the opt::less is used.
-        - opt::less - specifies binary predicate used for key comparison. Default is \p std::less<K>.
-        - opt::item_counter - the type of item counting feature. Default is \ref atomicity::empty_item_counter that is no item counting.
-        - opt::memory_model - C++ memory ordering model. Can be opt::v::relaxed_ordering (relaxed memory model, the default)
-            or opt::v::sequential_consistent (sequentially consisnent memory model).
-        - skip_list::random_level_generator - random level generator. Can be skip_list::xorshift, skip_list::turbo_pascal or
-            user-provided one. See skip_list::random_level_generator option description for explanation.
-            Default is \p %skip_list::turbo_pascal.
-        - opt::allocator - allocator for skip-list node. Default is \ref CDS_DEFAULT_ALLOCATOR.
-        - opt::back_off - back-off strategy used. If the option is not specified, the cds::backoff::Default is used.
-        - opt::stat - internal statistics. Available types: skip_list::stat, skip_list::empty_stat (the default)
+        - \p K - type of a key to be stored in the map.
+        - \p T - type of a value to be stored in the map.
+        - \p Traits - map traits, default is \p skip_list::traits
+            It is possible to declare option-based list with \p cds::container::skip_list::make_traits
+            metafunction istead of \p Traits template argument.
     */
     template <
         typename Key,
         typename T,
 #ifdef CDS_DOXYGEN_INVOKED
-        typename Traits = skip_list::type_traits
+        typename Traits = skip_list::traits
 #else
         typename Traits
 #endif
@@ -78,48 +91,24 @@ namespace cds { namespace container {
         //@endcond
 
     public:
-        typedef typename base_class::gc gc  ; ///< Garbage collector used
-        typedef Key key_type      ;   ///< Key type
-        typedef T   mapped_type   ;   ///< Mapped type
-        typedef std::pair< key_type const, mapped_type> value_type  ;   ///< Key-value pair stored in the map
-        typedef Traits  options     ;   ///< Options specified
-
-        typedef typename base_class::back_off       back_off        ;   ///< Back-off strategy used
-        typedef typename base_class::allocator_type allocator_type  ;   ///< Allocator type used for allocate/deallocate the skip-list nodes
-        typedef typename base_class::item_counter   item_counter    ;   ///< Item counting policy used
-        typedef typename base_class::key_comparator key_comparator  ;   ///< key compare functor
-        typedef typename base_class::memory_model   memory_model    ;   ///< Memory ordering. See cds::opt::memory_model option
-        typedef typename base_class::stat           stat            ;   ///< internal statistics type
-        typedef typename base_class::random_level_generator random_level_generator  ;   ///< random level generator
+        typedef cds::gc::nogc gc;   ///< Garbage collector
+        typedef Key key_type;       ///< Key type
+        typedef T   mapped_type   ///< Mapped type
+        typedef std::pair< key_type const, mapped_type> value_type; ///< Key-value pair stored in the map
+        typedef Traits  traits;     ///< Options specified
+
+        typedef typename base_class::back_off       back_off;       ///< Back-off strategy
+        typedef typename base_class::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the skip-list nodes
+        typedef typename base_class::item_counter   item_counter;   ///< Item counting policy
+        typedef typename base_class::key_comparator key_comparator; ///< key compare functor
+        typedef typename base_class::memory_model   memory_model;   ///< Memory ordering, see \p cds::opt::memory_model option
+        typedef typename base_class::stat           stat;           ///< internal statistics type
+        typedef typename base_class::random_level_generator random_level_generator; ///< random level generator
 
     protected:
         //@cond
         typedef typename base_class::node_type      node_type;
         typedef typename base_class::node_allocator node_allocator;
-
-        /*
-        template <class Less>
-        struct less_wrapper {
-            typedef Less    less_op;
-
-            bool operator()( value_type const& v1, value_type const& v2 ) const
-            {
-                return less_op()( v1.first, v2.first);
-            }
-
-            template <typename Q>
-            bool operator()( value_type const& v1, Q const& v2 ) const
-            {
-                return less_op()( v1.first, v2 );
-            }
-
-            template <typename Q>
-            bool operator()( Q const& v1, value_type const& v2 ) const
-            {
-                return less_op()( v1, v2.first );
-            }
-        };
-        */
         //@endcond
 
     public:
@@ -133,10 +122,13 @@ namespace cds { namespace container {
         {}
 
     public:
+    ///@name Forward ordered iterators
+    //@{
         /// Forward iterator
         /**
-            Remember, the iterator <tt>operator -> </tt> and <tt>operator *</tt> returns \ref value_type pointer and reference.
-            To access item key and value use <tt>it->first</tt> and <tt>it->second</tt> respectively.
+            The forward iterator for a split-list has some features:
+            - it has no post-increment operator
+            - it depends on iterator of underlying \p OrderedList
         */
         typedef typename base_class::iterator iterator;
 
@@ -164,28 +156,29 @@ namespace cds { namespace container {
         }
 
         /// Returns a forward const iterator addressing the first element in a map
-        //@{
         const_iterator begin() const
         {
             return base_class::begin();
         }
-        const_iterator cbegin()
+
+        /// Returns a forward const iterator addressing the first element in a map
+        const_iterator cbegin() const
         {
             return base_class::cbegin();
         }
-        //@}
 
         /// Returns an const iterator that addresses the location succeeding the last element in a map
-        //@{
         const_iterator end() const
         {
             return base_class::end();
         }
-        const_iterator cend()
+
+        /// Returns an const iterator that addresses the location succeeding the last element in a map
+        const_iterator cend() const
         {
             return base_class::cend();
         }
-        //@}
+    //@}
 
     public:
         /// Inserts new node with key and default value
@@ -203,7 +196,7 @@ namespace cds { namespace container {
         iterator 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::insert( std::make_pair( key_type( key ), mapped_type()));
         }
 
         /// Inserts new node
@@ -221,7 +214,7 @@ namespace cds { namespace container {
         iterator 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::insert( std::make_pair( key_type( key ), mapped_type( val )));
         }
 
         /// Inserts new node and initialize it by a functor
@@ -238,12 +231,10 @@ namespace cds { namespace container {
             to the map's item inserted. <tt>item.second</tt> is a reference to item's value that may be changed.
             User-defined functor \p func should guarantee that during changing item's value no any other changes
             could be made on this map's item by concurrent threads.
-            The user-defined functor can be passed by reference using <tt>boost::ref</tt>
-            and it is called only if the inserting is successful.
 
             The key_type should be constructible from value of type \p K.
 
-            The function allows to split creating of new item into two part:
+            The function allows to split creating of new item into three part:
             - create item from \p key;
             - insert new item into the map;
             - if inserting is successful, initialize the value of item by calling \p f functor
@@ -254,72 +245,90 @@ namespace cds { namespace container {
             Returns an iterator pointed to inserted value, or \p end() if inserting is failed
         */
         template <typename K, typename Func>
-        iterator insert_key( K const& key, Func func )
+        iterator insert_with( K const& key, Func func )
         {
             iterator it = insert( key );
-            if ( it != end() )
-                cds::unref( func )( (*it) );
+            if ( it != end())
+                func( (*it));
             return it;
         }
 
-#   ifdef CDS_EMPLACE_SUPPORT
-        /// For key \p key inserts data of type \ref mapped_type constructed with <tt>std::forward<Args>(args)...</tt>
+        /// For key \p key inserts data of type \p mapped_type created in-place from \p args
         /**
             \p key_type should be constructible from type \p K
 
             Returns \p true if inserting successful, \p false otherwise.
-
-            This function is available only for compiler that supports
-            variadic template and move semantics
         */
         template <typename K, typename... Args>
         iterator 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)... ));
         }
-#   endif
 
-        /// Ensures that the key \p key exists in the map
+        /// UPdates data by \p key
         /**
-            The operation inserts new item if the key \p key is not found in the map.
-            Otherwise, the function returns an iterator that points to item found.
+            The operation inserts new item if \p key is not found in the map and \p bInsert is \p true.
+            Otherwise, if \p key is found, the function returns an iterator that points to item found.
 
             Returns <tt> std::pair<iterator, bool>  </tt> where \p first is an iterator pointing to
-            item found or inserted, \p second is true if new item has been added or \p false if the item
-            already is in the list.
+            item found or inserted or \p end() if \p key is not found and insertion is not allowed (\p bInsert is \p false),
+            \p second is \p true if new item has been added or \p false if the item already exists.
         */
         template <typename K>
-        std::pair<iterator, bool> ensure( K const& key )
+        std::pair<iterator, bool> update( K const& key, bool bInsert = 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_type( key ), mapped_type()), bInsert );
         }
+        //@cond
+        template <typename K>
+        CDS_DEPRECATED("ensure() is deprecated, use update()")
+        std::pair<iterator, bool> ensure( K const& key )
+        {
+            return update( key, true );
+        }
+        //@endcond
 
-        /// Finds the key \p key
-        /** \anchor cds_nonintrusive_SkipListMap_nogc_find_val
-
+        /// Checks whether the map contains \p key
+        /**
             The function searches the item with key equal to \p key
             and returns an iterator pointed to item found if the key is found,
             and \ref end() otherwise
         */
         template <typename K>
+        iterator contains( K const& key )
+        {
+            return base_class::contains( key );
+        }
+        //@cond
+
+        template <typename K>
+        CDS_DEPRECATED("deprecated, use contains()")
         iterator find( K const& key )
         {
-            return base_class::find( key );
+            return contains( key );
         }
+        //@endcond
 
-        /// Finds the key \p key with comparing functor \p cmp
+        /// Checks whether the map contains \p key using \p pred predicate for searching
         /**
-            The function is an analog of \ref cds_nonintrusive_SkipListMap_nogc_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 set.
+            \p Less must imply the same element order as the comparator used for building the map.
         */
         template <typename K, typename Less>
+        iterator contains( K const& key, Less pred ) const
+        {
+            return base_class::contains( key, pred );
+        }
+        //@cond
+        template <typename K, typename Less>
+        CDS_DEPRECATED("deprecated, use contains()")
         iterator find_with( K const& key, Less pred ) const
         {
-            return base_class::find_with( key, pred );
+            return contains( key, pred );
         }
+        //@endcond
 
         /// Gets minimum key from the map
         /**
@@ -339,9 +348,8 @@ namespace cds { namespace container {
             return base_class::get_max();
         }
 
-        /// Clears the map (non-atomic)
+        /// Clears the map (noatomic)
         /**
-            The function is not atomic.
             Finding and/or inserting is prohibited while clearing.
             Otherwise an unpredictable result may be encountered.
             Thus, \p clear() may be used only for debugging purposes.
@@ -383,4 +391,4 @@ namespace cds { namespace container {
 }} // namespace cds::container
 
 
-#endif // #ifndef __CDS_CONTAINER_SKIP_LIST_MAP_NOGC_H
+#endif // #ifndef CDSLIB_CONTAINER_SKIP_LIST_MAP_NOGC_H