3 #ifndef CDSLIB_CONTAINER_STRIPED_SET_BOOST_LIST_ADAPTER_H
4 #define CDSLIB_CONTAINER_STRIPED_SET_BOOST_LIST_ADAPTER_H
6 #include <boost/version.hpp>
7 #if BOOST_VERSION < 104800
8 # error "For boost::container::list you must use boost 1.48 or above"
11 #include <algorithm> // std::lower_bound
12 #include <functional> // ref
13 #include <cds/container/striped_set/adapter.h>
14 #include <boost/container/list.hpp>
17 namespace cds { namespace container {
18 namespace striped_set {
20 // Copy policy for boost::container::list
21 template <typename T, typename Alloc>
22 struct copy_item_policy< boost::container::list< T, Alloc > >
24 typedef boost::container::list< T, Alloc > list_type;
25 typedef typename list_type::iterator iterator;
27 void operator()( list_type& list, iterator itInsert, iterator itWhat )
29 itInsert = list.insert( itInsert, *itWhat );
33 // Swap policy for boost::container::list
34 template <typename T, typename Alloc>
35 struct swap_item_policy< boost::container::list< T, Alloc > >
37 typedef boost::container::list< T, Alloc > list_type;
38 typedef typename list_type::iterator iterator;
40 void operator()( list_type& list, iterator itInsert, iterator itWhat )
42 typename list_type::value_type newVal;
43 itInsert = list.insert( itInsert, newVal );
44 std::swap( *itWhat, *itInsert );
48 // Move policy for boost::container::list
49 template <typename T, typename Alloc>
50 struct move_item_policy< boost::container::list< T, Alloc > >
52 typedef boost::container::list< T, Alloc > list_type;
53 typedef typename list_type::iterator iterator;
55 void operator()( list_type& list, iterator itInsert, iterator itWhat )
57 list.insert( itInsert, std::move( *itWhat ) );
60 } // namespace striped_set
61 }} // namespace cds::container
63 namespace cds { namespace intrusive { namespace striped_set {
65 /// boost::container::list adapter for hash set bucket
66 template <typename T, class Alloc, typename... Options>
67 class adapt< boost::container::list<T, Alloc>, Options... >
70 typedef boost::container::list<T, Alloc> container_type ; ///< underlying container type
73 /// Adapted container type
74 class adapted_container: public cds::container::striped_set::adapted_sequential_container
77 typedef typename container_type::value_type value_type ; ///< value type stored in the container
78 typedef typename container_type::iterator iterator ; ///< container iterator
79 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
81 static bool const has_find_with = true;
82 static bool const has_erase_with = true;
86 typedef typename cds::opt::details::make_comparator_from_option_list< value_type, Options... >::type key_comparator;
88 typedef typename cds::opt::select<
89 typename cds::opt::value<
90 typename cds::opt::find_option<
91 cds::opt::copy_policy< cds::container::striped_set::move_item >
95 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
96 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
97 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
100 struct find_predicate
102 bool operator()( value_type const& i1, value_type const& i2) const
104 return key_comparator()( i1, i2 ) < 0;
107 template <typename Q>
108 bool operator()( Q const& i1, value_type const& i2) const
110 return key_comparator()( i1, i2 ) < 0;
113 template <typename Q>
114 bool operator()( value_type const& i1, Q const& i2) const
116 return key_comparator()( i1, i2 ) < 0;
123 container_type m_List;
130 template <typename Q, typename Func>
131 bool insert( Q const& val, Func f )
133 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, find_predicate() );
134 if ( it == m_List.end() || key_comparator()( val, *it ) != 0 ) {
135 value_type newItem( val );
136 it = m_List.insert( it, newItem );
142 // key already exists
146 template <typename... Args>
147 bool emplace( Args&&... args )
149 value_type val( std::forward<Args>(args)... );
150 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, find_predicate() );
151 if ( it == m_List.end() || key_comparator()( val, *it ) != 0 ) {
152 m_List.emplace( it, std::move( val ) );
158 template <typename Q, typename Func>
159 std::pair<bool, bool> ensure( Q const& val, Func func )
161 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, find_predicate() );
162 if ( it == m_List.end() || key_comparator()( val, *it ) != 0 ) {
164 value_type newItem( val );
165 it = m_List.insert( it, newItem );
166 func( true, *it, val );
167 return std::make_pair( true, true );
171 func( false, *it, val );
172 return std::make_pair( true, false );
176 template <typename Q, typename Func>
177 bool erase( Q const& key, Func f )
179 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, find_predicate() );
180 if ( it == m_List.end() || key_comparator()( key, *it ) != 0 )
190 template <typename Q, typename Less, typename Func>
191 bool erase( Q const& key, Less pred, Func f )
193 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, pred );
194 if ( it == m_List.end() || pred( key, *it ) || pred( *it, key ) )
204 template <typename Q, typename Func>
205 bool find( Q& val, Func f )
207 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, find_predicate() );
208 if ( it == m_List.end() || key_comparator()( val, *it ) != 0 )
216 template <typename Q, typename Less, typename Func>
217 bool find( Q& val, Less pred, Func f )
219 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, pred );
220 if ( it == m_List.end() || pred( val, *it ) || pred( *it, val ) )
228 /// Clears the container
234 iterator begin() { return m_List.begin(); }
235 const_iterator begin() const { return m_List.begin(); }
236 iterator end() { return m_List.end(); }
237 const_iterator end() const { return m_List.end(); }
239 void move_item( adapted_container& /*from*/, iterator itWhat )
241 iterator it = std::lower_bound( m_List.begin(), m_List.end(), *itWhat, find_predicate() );
242 assert( it == m_List.end() || key_comparator()( *itWhat, *it ) != 0 );
244 copy_item()( m_List, it, itWhat );
249 return m_List.size();
254 typedef adapted_container type ; ///< Result of \p adapt metafunction
257 }}} // namespace cds::intrsive::striped_set
260 #endif // #ifndef CDSLIB_CONTAINER_STRIPED_SET_BOOST_LIST_ADAPTER_H