3 #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_LIST_ADAPTER_H
4 #define __CDS_CONTAINER_STRIPED_MAP_STD_LIST_ADAPTER_H
6 #include <cds/container/striped_set/adapter.h>
9 #include <algorithm> // std::lower_bound
10 #include <utility> // std::pair
13 namespace cds { namespace container {
14 namespace striped_set {
16 // Copy policy for map
17 template <typename K, typename T, typename Alloc>
18 struct copy_item_policy< std::list< std::pair< K const, T >, Alloc > >
20 typedef std::pair< K const, T> pair_type;
21 typedef std::list< pair_type, Alloc > list_type;
22 typedef typename list_type::iterator iterator;
24 void operator()( list_type& list, iterator itInsert, iterator itWhat )
26 list.insert( itInsert, *itWhat );
30 // Swap policy for map
31 template <typename K, typename T, typename Alloc>
32 struct swap_item_policy< std::list< std::pair< K const, T >, Alloc > >
34 typedef std::pair< K const, T> pair_type;
35 typedef std::list< pair_type, Alloc > list_type;
36 typedef typename list_type::iterator iterator;
38 void operator()( list_type& list, iterator itInsert, iterator itWhat )
40 pair_type newVal( itWhat->first, typename pair_type::second_type() );
41 itInsert = list.insert( itInsert, newVal );
42 std::swap( itInsert->second, itWhat->second );
46 // Move policy for map
47 template <typename K, typename T, typename Alloc>
48 struct move_item_policy< std::list< std::pair< K const, T >, Alloc > >
50 typedef std::pair< K const, T> pair_type;
51 typedef std::list< pair_type, Alloc > list_type;
52 typedef typename list_type::iterator iterator;
54 void operator()( list_type& list, iterator itInsert, iterator itWhat )
56 list.insert( itInsert, std::move( *itWhat ) );
59 } // namespace striped_set
60 }} // namespace cds:container
62 namespace cds { namespace intrusive { namespace striped_set {
64 /// std::list adapter for hash map bucket
65 template <typename Key, typename T, class Alloc, CDS_SPEC_OPTIONS>
66 class adapt< std::list< std::pair<Key const, T>, Alloc>, CDS_OPTIONS >
69 typedef std::list< std::pair<Key const, T>, Alloc> container_type ; ///< underlying container type
72 /// Adapted container type
73 class adapted_container: public cds::container::striped_set::adapted_sequential_container
76 typedef typename container_type::value_type value_type ; ///< value type stored in the container
77 typedef typename value_type::first_type key_type;
78 typedef typename value_type::second_type mapped_type;
79 typedef typename container_type::iterator iterator ; ///< container iterator
80 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
82 static bool const has_find_with = true;
83 static bool const has_erase_with = true;
87 typedef typename cds::opt::details::make_comparator_from_option_list< value_type, CDS_OPTIONS >::type key_comparator;
90 typedef typename cds::opt::select<
91 typename cds::opt::value<
92 typename cds::opt::find_option<
93 cds::opt::copy_policy< cds::container::striped_set::move_item >
97 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
98 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
99 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
102 struct find_predicate
104 bool operator()( value_type const& i1, value_type const& i2) const
106 return key_comparator()( i1.first, i2.first ) < 0;
109 template <typename Q>
110 bool operator()( Q const& i1, value_type const& i2) const
112 return key_comparator()( i1, i2.first ) < 0;
115 template <typename Q>
116 bool operator()( value_type const& i1, Q const& i2) const
118 return key_comparator()( i1.first, i2 ) < 0;
125 container_type m_List;
128 // In GCC (at least up to 4.7.x), the complexity of std::list::size() is O(N)
129 // (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49561)
130 size_t m_nSize ; // list size
141 template <typename Q, typename Func>
142 bool insert( const Q& key, Func f )
144 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, find_predicate() );
145 if ( it == m_List.end() || key_comparator()( key, it->first ) != 0 ) {
146 //value_type newItem( key );
147 it = m_List.insert( it, value_type( key, mapped_type()) );
148 cds::unref( f )( *it );
156 // key already exists
160 # ifdef CDS_EMPLACE_SUPPORT
161 template <typename K, typename... Args>
162 bool emplace( K&& key, Args&&... args )
164 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, find_predicate() );
165 if ( it == m_List.end() || key_comparator()( key, it->first ) != 0 ) {
166 //value_type newItem( key );
167 it = m_List.emplace( it, value_type( std::forward<K>(key), std::move( mapped_type( std::forward<Args>(args)...) )) );
178 template <typename Q, typename Func>
179 std::pair<bool, bool> ensure( const Q& key, Func func )
181 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, find_predicate() );
182 if ( it == m_List.end() || key_comparator()( key, it->first ) != 0 ) {
184 value_type newItem( key, mapped_type() );
185 it = m_List.insert( it, newItem );
186 cds::unref( func )( true, *it );
190 return std::make_pair( true, true );
194 cds::unref( func )( false, *it );
195 return std::make_pair( true, false );
199 template <typename Q, typename Func>
200 bool erase( Q const& key, Func f )
202 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, find_predicate() );
203 if ( it == m_List.end() || key_comparator()( key, it->first ) != 0 )
207 cds::unref( f )( *it );
216 template <typename Q, typename Less, typename Func>
217 bool erase( Q const& key, Less pred, Func f )
219 iterator it = std::lower_bound( m_List.begin(), m_List.end(), key, pred );
220 if ( it == m_List.end() || pred( key, it->first ) || pred( it->first, key ) )
224 cds::unref( f )( *it );
233 template <typename Q, typename Func>
234 bool find( Q& val, Func f )
236 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, find_predicate() );
237 if ( it == m_List.end() || key_comparator()( val, it->first ) != 0 )
241 cds::unref( f )( *it, val );
245 template <typename Q, typename Less, typename Func>
246 bool find( Q& val, Less pred, Func f )
248 iterator it = std::lower_bound( m_List.begin(), m_List.end(), val, pred );
249 if ( it == m_List.end() || pred( val, it->first ) || pred( it->first, val ) )
253 cds::unref( f )( *it, val );
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->first, it->first ) != 0 );
272 copy_item()( m_List, it, itWhat );
283 return m_List.size();
290 typedef adapted_container type ; ///< Result of \p adapt metafunction
293 }}} // namespace cds::intrusive::striped_set
297 #endif // #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_LIST_ADAPTER_H