add support for GCC 5: std::list::size() complexity is O(1)
[libcds.git] / cds / container / striped_map / std_list.h
index 683c2d6f63415a087172143b8663d11cbfe18a6c..4297e5e928d0c460c8d3ac0c27db0571729fe2d2 100644 (file)
@@ -3,11 +3,11 @@
 #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>
 
 //@cond
 namespace cds { namespace container {
@@ -62,8 +62,8 @@ namespace cds { namespace 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
@@ -84,14 +84,14 @@ 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>
@@ -123,17 +123,18 @@ namespace cds { namespace intrusive { namespace striped_set {
         private:
             //@cond
             container_type  m_List;
-#       ifdef __GLIBCXX__
+#       if defined(__GLIBCXX__ ) && !( CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 50000 )
             // 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(__GLIBCXX__ ) && !( CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 50000 )
                 : m_nSize(0)
 #       endif
             {}
@@ -145,9 +146,9 @@ namespace cds { namespace intrusive { namespace striped_set {
                 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 );
+                    f( *it );
 
-#           ifdef __GLIBCXX__
+#           if defined(__GLIBCXX__ ) && !( CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 50000 )
                     ++m_nSize;
 #           endif
                     return true;
@@ -157,7 +158,6 @@ namespace cds { namespace intrusive { namespace striped_set {
                 return false;
             }
 
-#           ifdef CDS_EMPLACE_SUPPORT
             template <typename K, typename... Args>
             bool emplace( K&& key, Args&&... args )
             {
@@ -166,14 +166,13 @@ namespace cds { namespace intrusive { namespace striped_set {
                     //value_type newItem( key );
                     it = m_List.emplace( it, value_type( std::forward<K>(key), std::move( mapped_type( std::forward<Args>(args)...) )) );
 
-#           ifdef __GLIBCXX__
+#           if defined(__GLIBCXX__ ) && !( CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 50000 )
                     ++m_nSize;
 #           endif
                     return true;
                 }
                 return false;
             }
-#           endif
 
             template <typename Q, typename Func>
             std::pair<bool, bool> ensure( const Q& key, Func func )
@@ -183,15 +182,15 @@ namespace cds { namespace intrusive { namespace striped_set {
                     // insert new
                     value_type newItem( key, mapped_type() );
                     it = m_List.insert( it, newItem );
-                    cds::unref( func )( true, *it );
-#           ifdef __GLIBCXX__
+                    func( true, *it );
+#           if defined(__GLIBCXX__ ) && !( CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 50000 )
                     ++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 );
                 }
             }
@@ -204,9 +203,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(__GLIBCXX__ ) && !( CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 50000 )
                 --m_nSize;
 #           endif
 
@@ -221,9 +220,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(__GLIBCXX__ ) && !( CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 50000 )
                 --m_nSize;
 #           endif
 
@@ -238,7 +237,7 @@ namespace cds { namespace intrusive { namespace striped_set {
                     return false;
 
                 // key exists
-                cds::unref( f )( *it, val );
+                f( *it, val );
                 return true;
             }
 
@@ -250,7 +249,7 @@ namespace cds { namespace intrusive { namespace striped_set {
                     return false;
 
                 // key exists
-                cds::unref( f )( *it, val );
+                f( *it, val );
                 return true;
             }
 
@@ -270,14 +269,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(__GLIBCXX__ ) && !( CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 50000 )
                 ++m_nSize;
 #           endif
             }
 
             size_t size() const
             {
-#           ifdef __GLIBCXX__
+#           if defined(__GLIBCXX__ ) && !( CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION >= 50000 )
                 return m_nSize;
 #           else
                 return m_List.size();