3 #ifndef __CDS_INTRUSIVE_STRIPED_SET_BOOST_LIST_ADAPTER_H
4 #define __CDS_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 {
12 template <typename T, CDS_BOOST_INTRUSIVE_DECL_OPTIONS3, typename... Options>
13 class adapt< boost::intrusive::list< T, CDS_BOOST_INTRUSIVE_OPTIONS3 >, Options... >
16 typedef boost::intrusive::list< T, CDS_BOOST_INTRUSIVE_OPTIONS3 > container_type ; ///< underlying intrusive container type
19 /// Adapted intrusive container
20 class adapted_container: public cds::intrusive::striped_set::adapted_sequential_container
23 typedef typename container_type::value_type value_type ; ///< value type stored in the container
24 typedef typename container_type::iterator iterator ; ///< container iterator
25 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
26 typedef typename cds::opt::details::make_comparator_from_option_list< value_type, Options... >::type key_comparator;
31 bool operator()( value_type const& i1, value_type const& i2) const
33 return key_comparator()( i1, i2 ) < 0;
37 bool operator()( Q const& i1, value_type const& i2) const
39 return key_comparator()( i1, i2 ) < 0;
43 bool operator()( value_type const& i1, Q const& i2) const
45 return key_comparator()( i1, i2 ) < 0;
49 # ifndef CDS_CXX11_LAMBDA_SUPPORT
50 struct empty_insert_functor {
51 void operator()( value_type& )
56 template <typename Q, typename Pred>
57 iterator find_key( Q const& key, Pred pred)
59 iterator itEnd = m_List.end();
61 for ( it = m_List.begin(); it != itEnd; ++it ) {
62 if ( !pred( *it, key ) )
69 container_type m_List;
75 container_type& base_container()
80 template <typename Func>
81 bool insert( value_type& val, Func f )
83 iterator it = find_key( val, find_predicate() );
84 if ( it == m_List.end() || key_comparator()( val, *it ) != 0 ) {
85 m_List.insert( it, val );
86 cds::unref( f )( val );
95 template <typename Func>
96 std::pair<bool, bool> ensure( value_type& val, Func f )
98 iterator it = find_key( val, find_predicate() );
99 if ( it == m_List.end() || key_comparator()( val, *it ) != 0 ) {
101 m_List.insert( it, val );
102 cds::unref( f )( true, val, val );
103 return std::make_pair( true, true );
107 cds::unref( f )( false, *it, val );
108 return std::make_pair( true, false );
112 bool unlink( value_type& val )
114 iterator it = find_key( val, find_predicate() );
115 if ( it == m_List.end() || &(*it) != &val )
122 template <typename Q, typename Func>
123 value_type * erase( Q const& key, Func f )
125 iterator it = find_key( key, find_predicate() );
126 if ( it == m_List.end() || key_comparator()( key, *it ) != 0 )
130 value_type& val = *it;
131 cds::unref( f )( val );
137 template <typename Q, typename Less, typename Func>
138 value_type * erase( Q const& key, Less pred, Func f )
140 iterator it = find_key( key, pred );
141 if ( it == m_List.end() || pred( key, *it ) || pred( *it, key ) )
145 value_type& val = *it;
146 cds::unref( f )( val );
152 template <typename Q, typename Func>
153 bool find( Q& key, Func f )
155 return find( key, find_predicate(), f );
158 template <typename Q, typename Less, typename Func>
159 bool find( Q& key, Less pred, Func f )
161 iterator it = find_key( key, pred );
162 if ( it == m_List.end() || pred( key, *it ) || pred( *it, key ))
166 cds::unref( f )( *it, key );
175 template <typename Disposer>
176 void clear( Disposer disposer )
178 m_List.clear_and_dispose( disposer );
181 iterator begin() { return m_List.begin(); }
182 const_iterator begin() const { return m_List.begin(); }
183 iterator end() { return m_List.end(); }
184 const_iterator end() const { return m_List.end(); }
188 return (size_t) m_List.size();
191 void move_item( adapted_container& from, iterator itWhat )
193 value_type& val = *itWhat;
194 from.base_container().erase( itWhat );
195 # ifdef CDS_CXX11_LAMBDA_SUPPORT
196 insert( val, []( value_type& ) {} );
198 insert( val, empty_insert_functor() );
204 typedef adapted_container type ; ///< Result of the metafunction
206 }}} // namespace cds::intrusive::striped_set
209 #endif // #ifndef __CDS_INTRUSIVE_STRIPED_SET_BOOST_LIST_ADAPTER_H