Docfix, minor changes
[libcds.git] / cds / container / striped_map / std_list.h
index 8df61dcb18d2b67263ec59603ed1d764cad2c252..cc19f06d0d8ef0402e8be4ff33499935bddef39f 100644 (file)
@@ -1,13 +1,46 @@
-//$$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_CONTAINER_STRIPED_MAP_STD_LIST_ADAPTER_H
+#define CDSLIB_CONTAINER_STRIPED_MAP_STD_LIST_ADAPTER_H
 
-#ifndef __CDS_CONTAINER_STRIPED_MAP_STD_LIST_ADAPTER_H
-#define __CDS_CONTAINER_STRIPED_MAP_STD_LIST_ADAPTER_H
-
-#include <cds/container/striped_set/adapter.h>
-#include <cds/ref.h>
 #include <list>
+#include <functional>   // ref
 #include <algorithm>    // std::lower_bound
 #include <utility>      // std::pair
+#include <cds/container/striped_set/adapter.h>
+
+#undef CDS_STD_LIST_SIZE_CXX11_CONFORM
+#if !( defined(__GLIBCXX__ ) && (!defined(_GLIBCXX_USE_CXX11_ABI) || _GLIBCXX_USE_CXX11_ABI == 0 ))
+#   define CDS_STD_LIST_SIZE_CXX11_CONFORM
+#endif
 
 //@cond
 namespace cds { namespace container {
@@ -43,7 +76,6 @@ namespace cds { namespace container {
             }
         };
 
-#ifdef CDS_MOVE_SEMANTICS_SUPPORT
         // Move policy for map
         template <typename K, typename T, typename Alloc>
         struct move_item_policy< std::list< std::pair< K const, T >, Alloc > >
@@ -57,15 +89,14 @@ namespace cds { namespace container {
                 list.insert( itInsert, std::move( *itWhat ) );
             }
         };
-#endif
     } // namespace striped_set
 }} // namespace cds:container
 
 namespace cds { namespace intrusive { namespace striped_set {
 
     /// std::list adapter for hash map bucket
-    template <typename Key, typename T, class Alloc, CDS_SPEC_OPTIONS>
-    class adapt< std::list< std::pair<Key const, T>, Alloc>, CDS_OPTIONS >
+    template <typename Key, typename T, class Alloc, typename... Options>
+    class adapt< std::list< std::pair<Key const, T>, Alloc>, Options... >
     {
     public:
         typedef std::list< std::pair<Key const, T>, Alloc>     container_type          ;   ///< underlying container type
@@ -86,21 +117,19 @@ namespace cds { namespace intrusive { namespace striped_set {
 
         private:
             //@cond
-            typedef typename cds::opt::details::make_comparator_from_option_list< value_type, CDS_OPTIONS >::type key_comparator;
+            typedef typename cds::opt::details::make_comparator_from_option_list< value_type, Options... >::type key_comparator;
 
 
             typedef typename cds::opt::select<
                 typename cds::opt::value<
                     typename cds::opt::find_option<
                         cds::opt::copy_policy< cds::container::striped_set::move_item >
-                        , CDS_OPTIONS
+                        , Options...
                     >::type
                 >::copy_policy
                 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
                 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
-#ifdef CDS_MOVE_SEMANTICS_SUPPORT
                 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
-#endif
             >::type copy_item;
 
             struct find_predicate
@@ -127,17 +156,18 @@ namespace cds { namespace intrusive { namespace striped_set {
         private:
             //@cond
             container_type  m_List;
-#       ifdef __GLIBCXX__
+#       if !defined(CDS_STD_LIST_SIZE_CXX11_CONFORM)
             // GCC C++ lib bug:
             // In GCC (at least up to 4.7.x), the complexity of std::list::size() is O(N)
             // (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49561)
+            // Fixed in GCC 5
             size_t          m_nSize ;   // list size
 #       endif
             //@endcond
 
         public:
             adapted_container()
-#       ifdef __GLIBCXX__
+#       if !defined(CDS_STD_LIST_SIZE_CXX11_CONFORM)
                 : m_nSize(0)
 #       endif
             {}
@@ -147,11 +177,10 @@ namespace cds { namespace intrusive { namespace striped_set {
             {
                 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, find_predicate() );
                 if ( it == m_List.end() || key_comparator()( key, it->first ) != 0 ) {
-                    //value_type newItem( key );
-                    it = m_List.insert( it, value_type( key, mapped_type()) );
-                    cds::unref( f )( *it );
+                    it = m_List.insert( it, value_type( key_type( key ), mapped_type()) );
+                    f( *it );
 
-#           ifdef __GLIBCXX__
+#           if !defined(CDS_STD_LIST_SIZE_CXX11_CONFORM)
                     ++m_nSize;
 #           endif
                     return true;
@@ -161,41 +190,41 @@ namespace cds { namespace intrusive { namespace striped_set {
                 return false;
             }
 
-#           ifdef CDS_EMPLACE_SUPPORT
             template <typename K, typename... Args>
             bool emplace( K&& key, Args&&... args )
             {
-                iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, find_predicate() );
-                if ( it == m_List.end() || key_comparator()( key, it->first ) != 0 ) {
-                    //value_type newItem( key );
-                    it = m_List.emplace( it, value_type( std::forward<K>(key), std::move( mapped_type( std::forward<Args>(args)...) )) );
+                value_type val( key_type( std::forward<K>( key )), mapped_type( std::forward<Args>( args )... ));
+                iterator it = std::lower_bound( m_List.begin(), m_List.end(), val.first, find_predicate() );
+                if ( it == m_List.end() || key_comparator()( val.first, it->first ) != 0 ) {
+                    it = m_List.emplace( it, std::move( val ));
 
-#           ifdef __GLIBCXX__
+#           if !defined(CDS_STD_LIST_SIZE_CXX11_CONFORM)
                     ++m_nSize;
 #           endif
                     return true;
                 }
                 return false;
             }
-#           endif
 
             template <typename Q, typename Func>
-            std::pair<bool, bool> ensure( const Q& key, Func func )
+            std::pair<bool, bool> update( const Q& key, Func func, bool bAllowInsert )
             {
                 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, find_predicate() );
                 if ( it == m_List.end() || key_comparator()( key, it->first ) != 0 ) {
                     // insert new
-                    value_type newItem( key, mapped_type() );
-                    it = m_List.insert( it, newItem );
-                    cds::unref( func )( true, *it );
-#           ifdef __GLIBCXX__
+                    if ( !bAllowInsert )
+                        return std::make_pair( false, false );
+
+                    it = m_List.insert( it, value_type( key_type( key ), mapped_type() ));
+                    func( true, *it );
+#           if !defined(CDS_STD_LIST_SIZE_CXX11_CONFORM)
                     ++m_nSize;
 #           endif
                     return std::make_pair( true, true );
                 }
                 else {
                     // already exists
-                    cds::unref( func )( false, *it );
+                    func( false, *it );
                     return std::make_pair( true, false );
                 }
             }
@@ -208,9 +237,9 @@ namespace cds { namespace intrusive { namespace striped_set {
                     return false;
 
                 // key exists
-                cds::unref( f )( *it );
+                f( *it );
                 m_List.erase( it );
-#           ifdef __GLIBCXX__
+#           if !defined(CDS_STD_LIST_SIZE_CXX11_CONFORM)
                 --m_nSize;
 #           endif
 
@@ -225,9 +254,9 @@ namespace cds { namespace intrusive { namespace striped_set {
                     return false;
 
                 // key exists
-                cds::unref( f )( *it );
+                f( *it );
                 m_List.erase( it );
-#           ifdef __GLIBCXX__
+#           if !defined(CDS_STD_LIST_SIZE_CXX11_CONFORM)
                 --m_nSize;
 #           endif
 
@@ -242,7 +271,7 @@ namespace cds { namespace intrusive { namespace striped_set {
                     return false;
 
                 // key exists
-                cds::unref( f )( *it, val );
+                f( *it, val );
                 return true;
             }
 
@@ -254,7 +283,7 @@ namespace cds { namespace intrusive { namespace striped_set {
                     return false;
 
                 // key exists
-                cds::unref( f )( *it, val );
+                f( *it, val );
                 return true;
             }
 
@@ -274,14 +303,14 @@ namespace cds { namespace intrusive { namespace striped_set {
                 assert( it == m_List.end() || key_comparator()( itWhat->first, it->first ) != 0 );
 
                 copy_item()( m_List, it, itWhat );
-#           ifdef __GLIBCXX__
+#           if !defined(CDS_STD_LIST_SIZE_CXX11_CONFORM)
                 ++m_nSize;
 #           endif
             }
 
             size_t size() const
             {
-#           ifdef __GLIBCXX__
+#           if !defined(CDS_STD_LIST_SIZE_CXX11_CONFORM)
                 return m_nSize;
 #           else
                 return m_List.size();
@@ -298,4 +327,4 @@ namespace cds { namespace intrusive { namespace striped_set {
 
 //@endcond
 
-#endif // #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_LIST_ADAPTER_H
+#endif // #ifndef CDSLIB_CONTAINER_STRIPED_MAP_STD_LIST_ADAPTER_H