Fixed -Wshadow warnings
[libcds.git] / cds / intrusive / michael_set.h
index b264bb1f1aec7cf9cf5c5c13447612145eef0979..79b2b8cbf7264f41ea6be1c0df33698c5b95a39a 100644 (file)
@@ -1,11 +1,11 @@
 /*
     This file is a part of libcds - Concurrent Data Structures library
 
-    (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
+    (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:
 
@@ -50,7 +50,8 @@ namespace cds { namespace intrusive {
 
         Template parameters are:
         - \p GC - Garbage collector used. Note the \p GC must be the same as the GC used for \p OrderedList
-        - \p OrderedList - ordered list implementation used as bucket for hash set, for example, \p MichaelList, \p LazyList, \p IterableList.
+        - \p OrderedList - ordered list implementation used as bucket for hash set, possible implementations:
+            \p MichaelList, \p LazyList, \p IterableList.
             The intrusive ordered list implementation specifies the type \p T stored in the hash-set, the reclamation
             schema \p GC used by hash-set, the comparison functor for the type \p T and other features specific for
             the ordered list.
@@ -253,7 +254,9 @@ namespace cds { namespace intrusive {
         typedef typename ordered_list::value_type       value_type      ; ///< type of value to be stored in the set
         typedef typename ordered_list::key_comparator   key_comparator  ; ///< key comparing functor
         typedef typename ordered_list::disposer         disposer        ; ///< Node disposer functor
+#ifdef CDS_DOXYGEN_INVOKED
         typedef typename ordered_list::stat             stat            ; ///< Internal statistics
+#endif
 
         /// Hash functor for \p value_type and all its derivatives that you use
         typedef typename cds::opt::v::hash_selector< typename traits::hash >::type hash;
@@ -268,10 +271,6 @@ namespace cds { namespace intrusive {
         // GC and OrderedList::gc must be the same
         static_assert(std::is_same<gc, typename ordered_list::gc>::value, "GC and OrderedList::gc must be the same");
 
-        // atomicity::empty_item_counter is not allowed as a item counter
-        static_assert(!std::is_same<item_counter, atomicity::empty_item_counter>::value,
-            "cds::atomicity::empty_item_counter is not allowed as a item counter");
-
     protected:
         //@cond
         typedef typename ordered_list::template select_stat_wrapper< typename ordered_list::stat > bucket_stat;
@@ -282,12 +281,20 @@ namespace cds { namespace intrusive {
         >::type internal_bucket_type;
 
         typedef typename allocator::template rebind< internal_bucket_type >::other bucket_table_allocator;
+        //@endcond
+
+    public:
+        //@cond
+        typedef typename bucket_stat::stat stat;
+        //@endcond
 
-        hash                        m_HashFunctor;   ///< Hash functor
-        size_t const                m_nHashBitmask;
-        internal_bucket_type*       m_Buckets;       ///< bucket table
-        item_counter                m_ItemCounter;   ///< Item counter
-        typename bucket_stat::stat  m_Stat;          ///< Internal statistics
+    protected:
+        //@cond
+        hash                    m_HashFunctor;   ///< Hash functor
+        size_t const            m_nHashBitmask;
+        internal_bucket_type*   m_Buckets;       ///< bucket table
+        item_counter            m_ItemCounter;   ///< Item counter
+        stat                    m_Stat;          ///< Internal statistics
         //@endcond
 
     public:
@@ -307,7 +314,6 @@ namespace cds { namespace intrusive {
               Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread.
               Use this iterator on the concurrent container for debugging purpose only.
             - for \p IterableList: iterator is thread-safe. You may use it freely in concurrent environment.
-              
         */
         typedef michael_set::details::iterator< internal_bucket_type, false > iterator;
 
@@ -323,7 +329,7 @@ namespace cds { namespace intrusive {
         */
         iterator begin()
         {
-            return iterator( m_Buckets[0].begin(), bucket_begin(), bucket_end() );
+            return iterator( m_Buckets[0].begin(), bucket_begin(), bucket_end());
         }
 
         /// Returns an iterator that addresses the location succeeding the last element in a set
@@ -334,7 +340,7 @@ namespace cds { namespace intrusive {
         */
         iterator end()
         {
-            return iterator( bucket_end()[-1].end(), bucket_end() - 1, bucket_end() );
+            return iterator( bucket_end()[-1].end(), bucket_end() - 1, bucket_end());
         }
 
         /// Returns a forward const iterator addressing the first element in a set
@@ -364,7 +370,7 @@ namespace cds { namespace intrusive {
 
     public:
         /// Initializes hash set
-        /** @anchor cds_intrusive_MichaelHashSet_hp_ctor
+        /**
             The Michael's hash set is an unbounded container, but its hash table is non-expandable.
             At construction time you should pass estimated maximum item count and a load factor.
             The load factor is average size of one bucket - a small number between 1 and 10.
@@ -388,7 +394,7 @@ namespace cds { namespace intrusive {
 
             for ( auto it = m_Buckets, itEnd = m_Buckets + bucket_count(); it != itEnd; ++it )
                 it->~internal_bucket_type();
-            bucket_table_allocator().deallocate( m_Buckets, bucket_count() );
+            bucket_table_allocator().deallocate( m_Buckets, bucket_count());
         }
 
         /// Inserts new node
@@ -508,7 +514,7 @@ namespace cds { namespace intrusive {
         std::pair<bool, bool> upsert( value_type& val, bool bAllowInsert = true )
 #else
         template <typename Q>
-        typename std::enable_if< 
+        typename std::enable_if<
             std::is_same< Q, value_type>::value && is_iterable_list< ordered_list >::value,
             std::pair<bool, bool>
         >::type
@@ -616,6 +622,34 @@ namespace cds { namespace intrusive {
             return false;
         }
 
+        /// Deletes the item pointed by iterator \p iter (only for \p IterableList based set)
+        /**
+            Returns \p true if the operation is successful, \p false otherwise.
+            The function can return \p false if the node the iterator points to has already been deleted
+            by other thread.
+
+            The function does not invalidate the iterator, it remains valid and can be used for further traversing.
+
+            @note \p %erase_at() is supported only for \p %MichaelHashSet based on \p IterableList.
+        */
+#ifdef CDS_DOXYGEN_INVOKED
+        bool erase_at( iterator const& iter )
+#else
+        template <typename Iterator>
+        typename std::enable_if< std::is_same<Iterator, iterator>::value && is_iterable_list< ordered_list >::value, bool >::type
+        erase_at( Iterator const& iter )
+#endif
+        {
+            assert( iter != end());
+            assert( iter.bucket() != nullptr );
+
+            if ( iter.bucket()->erase_at( iter.underlying_iterator())) {
+                --m_ItemCounter;
+                return true;
+            }
+            return false;
+        }
+
         /// Extracts the item with specified \p key
         /** \anchor cds_intrusive_MichaelHashSet_hp_extract
             The function searches an item with key equal to \p key,
@@ -723,7 +757,7 @@ namespace cds { namespace intrusive {
         {
             internal_bucket_type& b = bucket( key );
             typename internal_bucket_type::iterator it = b.find( key );
-            if ( it == b.end() )
+            if ( it == b.end())
                 return end();
             return iterator( it, &b, bucket_end());
         }
@@ -734,9 +768,9 @@ namespace cds { namespace intrusive {
         {
             internal_bucket_type& b = bucket( key );
             typename internal_bucket_type::iterator it = b.find( key );
-            if ( it == b.end() )
+            if ( it == b.end())
                 return end();
-            return iterator( it, &b, bucket_end() );
+            return iterator( it, &b, bucket_end());
         }
         //@endcond
 
@@ -781,9 +815,9 @@ namespace cds { namespace intrusive {
         {
             internal_bucket_type& b = bucket( key );
             typename internal_bucket_type::iterator it = b.find_with( key, pred );
-            if ( it == b.end() )
+            if ( it == b.end())
                 return end();
-            return iterator( it, &b, bucket_end() );
+            return iterator( it, &b, bucket_end());
         }
         //@cond
         template <typename Q, typename Less>
@@ -792,9 +826,9 @@ namespace cds { namespace intrusive {
         {
             internal_bucket_type& b = bucket( key );
             typename internal_bucket_type::iterator it = b.find_with( key, pred );
-            if ( it == b.end() )
+            if ( it == b.end())
                 return end();
-            return iterator( it, &b, bucket_end() );
+            return iterator( it, &b, bucket_end());
         }
         //@endcond
 
@@ -891,8 +925,8 @@ namespace cds { namespace intrusive {
 
         /// Checks if the set is empty
         /**
-            Emptiness is checked by item counting: if item count is zero then the set is empty.
-            Thus, the correct item counting feature is an important part of Michael's set implementation.
+            @warning If you use \p atomicity::empty_item_counter in \p traits::item_counter,
+            the function always returns \p true.
         */
         bool empty() const
         {
@@ -900,6 +934,10 @@ namespace cds { namespace intrusive {
         }
 
         /// Returns item count in the set
+        /**
+            If you use \p atomicity::empty_item_counter in \p traits::item_counter,
+            the function always returns 0.
+        */
         size_t size() const
         {
             return m_ItemCounter;
@@ -936,23 +974,23 @@ namespace cds { namespace intrusive {
 
         const_iterator get_const_begin() const
         {
-            return const_iterator( m_Buckets[0].cbegin(), bucket_begin(), bucket_end() );
+            return const_iterator( m_Buckets[0].cbegin(), bucket_begin(), bucket_end());
         }
         const_iterator get_const_end() const
         {
-            return const_iterator( bucket_end()[-1].cend(), bucket_end() - 1, bucket_end() );
+            return const_iterator( bucket_end()[-1].cend(), bucket_end() - 1, bucket_end());
         }
 
         template <typename Stat>
-        typename std::enable_if< Stat::empty >::type construct_bucket( internal_bucket_type * bucket )
+        typename std::enable_if< Stat::empty >::type construct_bucket( internal_bucket_type * b )
         {
-            new (bucket) internal_bucket_type;
+            new (b) internal_bucket_type;
         }
 
         template <typename Stat>
-        typename std::enable_if< !Stat::empty >::type construct_bucket( internal_bucket_type * bucket )
+        typename std::enable_if< !Stat::empty >::type construct_bucket( internal_bucket_type * b )
         {
-            new (bucket) internal_bucket_type( m_Stat );
+            new (b) internal_bucket_type( m_Stat );
         }
 
         /// Calculates hash value of \p key