3 #ifndef __CDS_CONTAINER_STRIPED_SET_STD_LIST_ADAPTER_H
4 #define __CDS_CONTAINER_STRIPED_SET_STD_LIST_ADAPTER_H
6 #include <cds/container/striped_set/adapter.h>
9 #include <algorithm> // std::lower_bound
12 namespace cds { namespace container {
13 namespace striped_set {
15 // Copy policy for std::list
16 template <typename T, typename Alloc>
17 struct copy_item_policy< std::list< T, Alloc > >
19 typedef std::list< T, Alloc > list_type;
20 typedef typename list_type::iterator iterator;
22 void operator()( list_type& list, iterator itInsert, iterator itWhat )
24 itInsert = list.insert( itInsert, *itWhat );
28 // Swap policy for std::list
29 template <typename T, typename Alloc>
30 struct swap_item_policy< std::list< T, Alloc > >
32 typedef std::list< T, Alloc > list_type;
33 typedef typename list_type::iterator iterator;
35 void operator()( list_type& list, iterator itInsert, iterator itWhat )
37 typename list_type::value_type newVal;
38 itInsert = list.insert( itInsert, newVal );
39 std::swap( *itWhat, *itInsert );
43 // Move policy for std::list
44 template <typename T, typename Alloc>
45 struct move_item_policy< std::list< T, Alloc > >
47 typedef std::list< T, Alloc > list_type;
48 typedef typename list_type::iterator iterator;
50 void operator()( list_type& list, iterator itInsert, iterator itWhat )
52 list.insert( itInsert, std::move( *itWhat ) );
55 } // namespace striped_set
56 }} // namespace cds::container
58 namespace cds { namespace intrusive { namespace striped_set {
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 >
65 typedef std::list<T, Alloc> container_type ; ///< underlying container type
68 /// Adapted container type
69 class adapted_container: public cds::container::striped_set::adapted_sequential_container
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
76 static bool const has_find_with = true;
77 static bool const has_erase_with = true;
81 typedef typename cds::opt::details::make_comparator_from_option_list< value_type, CDS_OPTIONS >::type key_comparator;
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 >
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>
98 bool operator()( value_type const& i1, value_type const& i2) const
100 return key_comparator()( i1, i2 ) < 0;
103 template <typename Q>
104 bool operator()( Q const& i1, value_type const& i2) const
106 return key_comparator()( i1, i2 ) < 0;
109 template <typename Q>
110 bool operator()( value_type const& i1, Q const& i2) const
112 return key_comparator()( i1, i2 ) < 0;
119 container_type m_List;
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
135 template <typename Q, typename Func>
136 bool insert( const Q& val, Func f )
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 );
150 // key already exists
154 # ifdef CDS_EMPLACE_SUPPORT
155 template <typename... Args>
156 bool emplace( Args&&... args )
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)... );
163 value_type val(std::forward<Args>(args)...);
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 ) );
177 template <typename Q, typename Func>
178 std::pair<bool, bool> ensure( const Q& val, Func func )
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 ) {
183 value_type newItem( val );
184 it = m_List.insert( it, newItem );
185 cds::unref( func )( true, *it, val );
189 return std::make_pair( true, true );
193 cds::unref( func )( false, *it, val );
194 return std::make_pair( true, false );
198 template <typename Q, typename Func>
199 bool erase( const Q& key, Func f )
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 )
206 cds::unref( f )( *it );
215 template <typename Q, typename Less, typename Func>
216 bool erase( Q const& key, Less pred, Func f )
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 ) )
223 cds::unref( f )( *it );
232 template <typename Q, typename Func>
233 bool find( Q& val, Func f )
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 )
240 cds::unref( f )( *it, val );
244 template <typename Q, typename Less, typename Func>
245 bool find( Q& val, Less pred, Func f )
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 ) )
252 cds::unref( f )( *it, val );
256 /// Clears the container
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(); }
267 void move_item( adapted_container& /*from*/, iterator itWhat )
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 );
272 copy_item()( m_List, it, itWhat );
283 return m_List.size();
290 typedef adapted_container type ; ///< Result of \p adapt metafunction
298 #endif // #ifndef __CDS_CONTAINER_STRIPED_SET_STD_LIST_ADAPTER_H