Extending intrusive MichaelSet<HP> to IterableList
[libcds.git] / cds / intrusive / details / michael_set_base.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef CDSLIB_INTRUSIVE_DETAILS_MICHAEL_SET_BASE_H
32 #define CDSLIB_INTRUSIVE_DETAILS_MICHAEL_SET_BASE_H
33
34 #include <cds/intrusive/details/base.h>
35 #include <cds/opt/compare.h>
36 #include <cds/opt/hash.h>
37 #include <cds/algo/bitop.h>
38 #include <cds/algo/atomic.h>
39
40 namespace cds { namespace intrusive {
41
42     /// MichaelHashSet related definitions
43     /** @ingroup cds_intrusive_helper
44     */
45     namespace michael_set {
46         /// MichaelHashSet traits
47         struct traits {
48             /// Hash function
49             /**
50                 Hash function converts the key fields of struct \p T stored in the hash-set
51                 into value of type \p size_t called hash value that is an index of hash table.
52
53                 This is mandatory type and has no predefined one.
54             */
55             typedef opt::none hash;
56
57             /// Item counter
58             /**
59                 The item counting is an important part of \p MichaelHashSet algorithm:
60                 the \p empty() member function depends on correct item counting.
61                 Therefore, \p atomicity::empty_item_counter is not allowed as a type of the option.
62
63                 Default is \p atomicity::item_counter.
64             */
65             typedef cds::atomicity::item_counter item_counter;
66
67             /// Bucket table allocator
68             /**
69                 Allocator for bucket table. Default is \ref CDS_DEFAULT_ALLOCATOR
70                 The allocator uses only in constructor for allocating bucket table
71                 and in destructor for destroying bucket table
72             */
73             typedef CDS_DEFAULT_ALLOCATOR   allocator;
74         };
75
76         /// Metafunction converting option list to traits struct
77         /**
78             Available \p Options:
79             - \p opt::hash - mandatory option, specifies hash functor.
80             - \p opt::item_counter - optional, specifies item counting policy. See \p traits::item_counter
81                 for default type.
82             - \p opt::allocator - optional, bucket table allocator. Default is \ref CDS_DEFAULT_ALLOCATOR.
83         */
84         template <typename... Options>
85         struct make_traits {
86             typedef typename cds::opt::make_options< traits, Options...>::type type;   ///< Metafunction result
87         };
88
89         //@cond
90         namespace details {
91             static inline size_t init_hash_bitmask( size_t nMaxItemCount, size_t nLoadFactor )
92             {
93                 if ( nLoadFactor == 0 )
94                     nLoadFactor = 1;
95                 if ( nMaxItemCount == 0 )
96                     nMaxItemCount = 4;
97                 const size_t nBucketCount = nMaxItemCount / nLoadFactor;
98                 const size_t exp2 = size_t( 1 ) << cds::bitop::MSB( nBucketCount );
99
100                 return ( exp2 < nBucketCount ? exp2 * 2 : exp2 ) - 1;
101             }
102
103             template <typename OrderedList, bool IsConst>
104             struct list_iterator_selector;
105
106             template <typename OrderedList>
107             struct list_iterator_selector< OrderedList, false>
108             {
109                 typedef OrderedList * bucket_ptr;
110                 typedef typename OrderedList::iterator  type;
111             };
112
113             template <typename OrderedList>
114             struct list_iterator_selector< OrderedList, true>
115             {
116                 typedef OrderedList const * bucket_ptr;
117                 typedef typename OrderedList::const_iterator  type;
118             };
119
120             template <typename OrderedList, bool IsConst>
121             class iterator
122             {
123                 friend class iterator< OrderedList, !IsConst >;
124
125             protected:
126                 typedef OrderedList bucket_type;
127                 typedef typename list_iterator_selector< bucket_type, IsConst>::bucket_ptr bucket_ptr;
128                 typedef typename list_iterator_selector< bucket_type, IsConst>::type list_iterator;
129
130                 bucket_ptr      m_pCurBucket;
131                 list_iterator   m_itList;
132                 bucket_ptr      m_pEndBucket;
133
134                 void next()
135                 {
136                     if ( m_pCurBucket < m_pEndBucket ) {
137                         if ( ++m_itList != m_pCurBucket->end() )
138                             return;
139                         while ( ++m_pCurBucket < m_pEndBucket ) {
140                             m_itList = m_pCurBucket->begin();
141                             if ( m_itList != m_pCurBucket->end() )
142                                 return;
143                         }
144                     }
145                     m_pCurBucket = m_pEndBucket - 1;
146                     m_itList = m_pCurBucket->end();
147                 }
148
149             public:
150                 typedef typename list_iterator::value_ptr   value_ptr;
151                 typedef typename list_iterator::value_ref   value_ref;
152
153             public:
154                 iterator()
155                     : m_pCurBucket( nullptr )
156                     , m_itList()
157                     , m_pEndBucket( nullptr )
158                 {}
159
160                 iterator( list_iterator const& it, bucket_ptr pFirst, bucket_ptr pLast )
161                     : m_pCurBucket( pFirst )
162                     , m_itList( it )
163                     , m_pEndBucket( pLast )
164                 {
165                     if ( it == pFirst->end() )
166                         next();
167                 }
168
169                 iterator( iterator const& src )
170                     : m_pCurBucket( src.m_pCurBucket )
171                     , m_itList( src.m_itList )
172                     , m_pEndBucket( src.m_pEndBucket )
173                 {}
174
175                 value_ptr operator ->() const
176                 {
177                     assert( m_pCurBucket != nullptr );
178                     return m_itList.operator ->();
179                 }
180
181                 value_ref operator *() const
182                 {
183                     assert( m_pCurBucket != nullptr );
184                     return m_itList.operator *();
185                 }
186
187                 /// Pre-increment
188                 iterator& operator ++()
189                 {
190                     next();
191                     return *this;
192                 }
193
194                 iterator& operator = (const iterator& src)
195                 {
196                     m_pCurBucket = src.m_pCurBucket;
197                     m_pEndBucket = src.m_pEndBucket;
198                     m_itList = src.m_itList;
199                     return *this;
200                 }
201
202                 bucket_ptr bucket() const
203                 {
204                     return m_pCurBucket != m_pEndBucket ? m_pCurBucket : nullptr;
205                 }
206
207                 template <bool C>
208                 bool operator ==(iterator<bucket_type, C> const& i) const
209                 {
210                     return m_pCurBucket == i.m_pCurBucket && m_itList == i.m_itList;
211                 }
212                 template <bool C>
213                 bool operator !=(iterator<OrderedList, C> const& i ) const
214                 {
215                     return !( *this == i );
216                 }
217
218             };
219         }
220         //@endcond
221     } // namespace michael_set
222
223     //@cond
224     // Forward declarations
225     template <class GC, class OrderedList, class Traits = michael_set::traits>
226     class MichaelHashSet;
227     //@endcond
228
229 }} // namespace cds::intrusive
230
231 #endif // #ifndef CDSLIB_INTRUSIVE_DETAILS_MICHAEL_SET_BASE_H