Remove CDS_RVALUE_SUPPORT, CDS_MOVE_SEMANTICS_SUPPORT macros and emulating code
[libcds.git] / cds / container / striped_set / std_list.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_CONTAINER_STRIPED_SET_STD_LIST_ADAPTER_H
4 #define __CDS_CONTAINER_STRIPED_SET_STD_LIST_ADAPTER_H
5
6 #include <cds/container/striped_set/adapter.h>
7 #include <cds/ref.h>
8 #include <list>
9 #include <algorithm>    // std::lower_bound
10
11 //@cond
12 namespace cds { namespace container {
13     namespace striped_set {
14
15         // Copy policy for std::list
16         template <typename T, typename Alloc>
17         struct copy_item_policy< std::list< T, Alloc > >
18         {
19             typedef std::list< T, Alloc > list_type;
20             typedef typename list_type::iterator iterator;
21
22             void operator()( list_type& list, iterator itInsert, iterator itWhat )
23             {
24                 itInsert = list.insert( itInsert, *itWhat );
25             }
26         };
27
28         // Swap policy for std::list
29         template <typename T, typename Alloc>
30         struct swap_item_policy< std::list< T, Alloc > >
31         {
32             typedef std::list< T, Alloc > list_type;
33             typedef typename list_type::iterator iterator;
34
35             void operator()( list_type& list, iterator itInsert, iterator itWhat )
36             {
37                 typename list_type::value_type newVal;
38                 itInsert = list.insert( itInsert, newVal );
39                 std::swap( *itWhat, *itInsert );
40             }
41         };
42
43         // Move policy for std::list
44         template <typename T, typename Alloc>
45         struct move_item_policy< std::list< T, Alloc > >
46         {
47             typedef std::list< T, Alloc > list_type;
48             typedef typename list_type::iterator iterator;
49
50             void operator()( list_type& list, iterator itInsert, iterator itWhat )
51             {
52                 list.insert( itInsert, std::move( *itWhat ) );
53             }
54         };
55     }   // namespace striped_set
56 }} // namespace cds::container
57
58 namespace cds { namespace intrusive { namespace striped_set {
59
60     /// std::list adapter for hash set bucket
61     template <typename T, class Alloc, CDS_SPEC_OPTIONS>
62     class adapt< std::list<T, Alloc>, CDS_OPTIONS >
63     {
64     public:
65         typedef std::list<T, Alloc>     container_type          ;   ///< underlying container type
66
67     private:
68         /// Adapted container type
69         class adapted_container: public cds::container::striped_set::adapted_sequential_container
70         {
71         public:
72             typedef typename container_type::value_type value_type  ;   ///< value type stored in the container
73             typedef typename container_type::iterator      iterator ;   ///< container iterator
74             typedef typename container_type::const_iterator const_iterator ;    ///< container const iterator
75
76             static bool const has_find_with = true;
77             static bool const has_erase_with = true;
78
79         private:
80             //@cond
81             typedef typename cds::opt::details::make_comparator_from_option_list< value_type, CDS_OPTIONS >::type key_comparator;
82
83
84             typedef typename cds::opt::select<
85                 typename cds::opt::value<
86                     typename cds::opt::find_option<
87                         cds::opt::copy_policy< cds::container::striped_set::move_item >
88                         , CDS_OPTIONS
89                     >::type
90                 >::copy_policy
91                 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
92                 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
93                 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
94             >::type copy_item;
95
96             struct find_predicate
97             {
98                 bool operator()( value_type const& i1, value_type const& i2) const
99                 {
100                     return key_comparator()( i1, i2 ) < 0;
101                 }
102
103                 template <typename Q>
104                 bool operator()( Q const& i1, value_type const& i2) const
105                 {
106                     return key_comparator()( i1, i2 ) < 0;
107                 }
108
109                 template <typename Q>
110                 bool operator()( value_type const& i1, Q const& i2) const
111                 {
112                     return key_comparator()( i1, i2 ) < 0;
113                 }
114             };
115             //@endcond
116
117         private:
118             //@cond
119             container_type  m_List;
120 #       ifdef __GLIBCXX__
121             // GCC C++ lib bug:
122             // In GCC (at least up to 4.7.x), the complexity of std::list::size() is O(N)
123             // (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49561)
124             size_t          m_nSize ;   // list size
125 #       endif
126             //@endcond
127
128         public:
129             adapted_container()
130 #       ifdef __GLIBCXX__
131                 : m_nSize(0)
132 #       endif
133             {}
134
135             template <typename Q, typename Func>
136             bool insert( const Q& val, Func f )
137             {
138                 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, find_predicate() );
139                 if ( it == m_List.end() || key_comparator()( val, *it ) != 0 ) {
140                     value_type newItem( val );
141                     it = m_List.insert( it, newItem );
142                     cds::unref( f )( *it );
143
144 #           ifdef __GLIBCXX__
145                     ++m_nSize;
146 #           endif
147                     return true;
148                 }
149
150                 // key already exists
151                 return false;
152             }
153
154 #           ifdef CDS_EMPLACE_SUPPORT
155             template <typename... Args>
156             bool emplace( Args&&... args )
157             {
158 #if CDS_COMPILER == CDS_COMPILER_MSVC && CDS_COMPILER_VERSION == CDS_COMPILER_MSVC12
159                 // MS VC++ 2013: internal compiler error
160                 // Use assignment workaround, see http://connect.microsoft.com/VisualStudio/feedback/details/804941/visual-studio-2013-rc-c-internal-compiler-error-with-std-forward
161                 value_type val = value_type( std::forward<Args>(args)... );
162 #else
163                 value_type val(std::forward<Args>(args)...);
164 #endif
165                 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, find_predicate() );
166                 if ( it == m_List.end() || key_comparator()( val, *it ) != 0 ) {
167                     it = m_List.emplace( it, std::move( val ) );
168 #           ifdef __GLIBCXX__
169                     ++m_nSize;
170 #           endif
171                     return true;
172                 }
173                 return false;
174             }
175 #           endif
176
177             template <typename Q, typename Func>
178             std::pair<bool, bool> ensure( const Q& val, Func func )
179             {
180                 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, find_predicate() );
181                 if ( it == m_List.end() || key_comparator()( val, *it ) != 0 ) {
182                     // insert new
183                     value_type newItem( val );
184                     it = m_List.insert( it, newItem );
185                     cds::unref( func )( true, *it, val );
186 #           ifdef __GLIBCXX__
187                     ++m_nSize;
188 #           endif
189                     return std::make_pair( true, true );
190                 }
191                 else {
192                     // already exists
193                     cds::unref( func )( false, *it, val );
194                     return std::make_pair( true, false );
195                 }
196             }
197
198             template <typename Q, typename Func>
199             bool erase( const Q& key, Func f )
200             {
201                 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, find_predicate() );
202                 if ( it == m_List.end() || key_comparator()( key, *it ) != 0 )
203                     return false;
204
205                 // key exists
206                 cds::unref( f )( *it );
207                 m_List.erase( it );
208 #           ifdef __GLIBCXX__
209                 --m_nSize;
210 #           endif
211
212                 return true;
213             }
214
215             template <typename Q, typename Less, typename Func>
216             bool erase( Q const& key, Less pred, Func f )
217             {
218                 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, pred );
219                 if ( it == m_List.end() || pred( key, *it ) || pred( *it, key ) )
220                     return false;
221
222                 // key exists
223                 cds::unref( f )( *it );
224                 m_List.erase( it );
225 #           ifdef __GLIBCXX__
226                 --m_nSize;
227 #           endif
228
229                 return true;
230             }
231
232             template <typename Q, typename Func>
233             bool find( Q& val, Func f )
234             {
235                 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, find_predicate() );
236                 if ( it == m_List.end() || key_comparator()( val, *it ) != 0 )
237                     return false;
238
239                 // key exists
240                 cds::unref( f )( *it, val );
241                 return true;
242             }
243
244             template <typename Q, typename Less, typename Func>
245             bool find( Q& val, Less pred, Func f )
246             {
247                 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, pred );
248                 if ( it == m_List.end() || pred( val, *it ) || pred( *it, val ) )
249                     return false;
250
251                 // key exists
252                 cds::unref( f )( *it, val );
253                 return true;
254             }
255
256             /// Clears the container
257             void clear()
258             {
259                 m_List.clear();
260             }
261
262             iterator begin()                { return m_List.begin(); }
263             const_iterator begin() const    { return m_List.begin(); }
264             iterator end()                  { return m_List.end(); }
265             const_iterator end() const      { return m_List.end(); }
266
267             void move_item( adapted_container& /*from*/, iterator itWhat )
268             {
269                 iterator it = std::lower_bound( m_List.begin(), m_List.end(), *itWhat, find_predicate() );
270                 assert( it == m_List.end() || key_comparator()( *itWhat, *it ) != 0 );
271
272                 copy_item()( m_List, it, itWhat );
273 #           ifdef __GLIBCXX__
274                 ++m_nSize;
275 #           endif
276             }
277
278             size_t size() const
279             {
280 #           ifdef __GLIBCXX__
281                 return m_nSize;
282 #           else
283                 return m_List.size();
284 #           endif
285
286             }
287         };
288
289     public:
290         typedef adapted_container type ; ///< Result of \p adapt metafunction
291
292     };
293 }}}
294
295
296 //@endcond
297
298 #endif // #ifndef __CDS_CONTAINER_STRIPED_SET_STD_LIST_ADAPTER_H