3 #ifndef CDSLIB_INTRUSIVE_STRIPED_SET_BOOST_LIST_ADAPTER_H
4 #define CDSLIB_INTRUSIVE_STRIPED_SET_BOOST_LIST_ADAPTER_H
6 #include <boost/intrusive/list.hpp>
7 #include <cds/intrusive/striped_set/adapter.h>
10 namespace cds { namespace intrusive { namespace striped_set {
13 template <typename List, typename... Options >
14 class adapt_boost_list
17 typedef List container_type; ///< underlying intrusive container type
20 /// Adapted intrusive container
21 class adapted_container : public cds::intrusive::striped_set::adapted_sequential_container
24 typedef typename container_type::value_type value_type; ///< value type stored in the container
25 typedef typename container_type::iterator iterator; ///< container iterator
26 typedef typename container_type::const_iterator const_iterator; ///< container const iterator
27 typedef typename cds::opt::details::make_comparator_from_option_list< value_type, Options... >::type key_comparator;
32 bool operator()( value_type const& i1, value_type const& i2 ) const
34 return key_comparator()(i1, i2) < 0;
38 bool operator()( Q const& i1, value_type const& i2 ) const
40 return key_comparator()(i1, i2) < 0;
44 bool operator()( value_type const& i1, Q const& i2 ) const
46 return key_comparator()(i1, i2) < 0;
50 template <typename Q, typename Pred>
51 iterator find_key( Q const& key, Pred pred )
53 iterator itEnd = m_List.end();
55 for ( it = m_List.begin(); it != itEnd; ++it ) {
56 if ( !pred( *it, key ) )
63 container_type m_List;
69 container_type& base_container()
74 template <typename Func>
75 bool insert( value_type& val, Func f )
77 iterator it = find_key( val, find_predicate() );
78 if ( it == m_List.end() || key_comparator()(val, *it) != 0 ) {
79 m_List.insert( it, val );
89 template <typename Func>
90 std::pair<bool, bool> ensure( value_type& val, Func f )
92 iterator it = find_key( val, find_predicate() );
93 if ( it == m_List.end() || key_comparator()(val, *it) != 0 ) {
95 m_List.insert( it, val );
97 return std::make_pair( true, true );
101 f( false, *it, val );
102 return std::make_pair( true, false );
106 bool unlink( value_type& val )
108 iterator it = find_key( val, find_predicate() );
109 if ( it == m_List.end() || &(*it) != &val )
116 template <typename Q, typename Func>
117 value_type * erase( Q const& key, Func f )
119 iterator it = find_key( key, find_predicate() );
120 if ( it == m_List.end() || key_comparator()(key, *it) != 0 )
124 value_type& val = *it;
131 template <typename Q, typename Less, typename Func>
132 value_type * erase( Q const& key, Less pred, Func f )
134 iterator it = find_key( key, pred );
135 if ( it == m_List.end() || pred( key, *it ) || pred( *it, key ) )
139 value_type& val = *it;
146 template <typename Q, typename Func>
147 bool find( Q& key, Func f )
149 return find( key, find_predicate(), f );
152 template <typename Q, typename Less, typename Func>
153 bool find( Q& key, Less pred, Func f )
155 iterator it = find_key( key, pred );
156 if ( it == m_List.end() || pred( key, *it ) || pred( *it, key ) )
169 template <typename Disposer>
170 void clear( Disposer disposer )
172 m_List.clear_and_dispose( disposer );
175 iterator begin() { return m_List.begin(); }
176 const_iterator begin() const { return m_List.begin(); }
177 iterator end() { return m_List.end(); }
178 const_iterator end() const { return m_List.end(); }
182 return (size_t)m_List.size();
185 void move_item( adapted_container& from, iterator itWhat )
187 value_type& val = *itWhat;
188 from.base_container().erase( itWhat );
189 insert( val, []( value_type& ) {} );
194 typedef adapted_container type; ///< Result of the metafunction
196 } // namespace details
198 #if CDS_COMPILER == CDS_COMPILER_INTEL && CDS_COMPILER_VERSION <= 1500
199 template <typename T, typename P1, typename P2, typename P3, typename P4, typename... Options>
200 class adapt< boost::intrusive::list< T, P1, P2, P3, P4 >, Options... >
201 : public details::adapt_boost_list< boost::intrusive::list< T, P1, P2, P3, P4 >, Options... >
204 template <typename T, typename... BIOptions, typename... Options>
205 class adapt< boost::intrusive::list< T, BIOptions... >, Options... >
206 : public details::adapt_boost_list< boost::intrusive::list< T, BIOptions... >, Options... >
210 }}} // namespace cds::intrusive::striped_set
213 #endif // #ifndef CDSLIB_INTRUSIVE_STRIPED_SET_BOOST_LIST_ADAPTER_H