3 #ifndef CDSLIB_CONTAINER_STRIPED_MAP_STD_MAP_ADAPTER_H
4 #define CDSLIB_CONTAINER_STRIPED_MAP_STD_MAP_ADAPTER_H
6 #include <cds/container/striped_set/adapter.h>
10 namespace cds { namespace container {
11 namespace striped_set {
13 // Copy policy for map
14 template <typename Key, typename T, typename Traits, typename Alloc>
15 struct copy_item_policy< std::map< Key, T, Traits, Alloc > >
17 typedef std::map< Key, T, Traits, Alloc > map_type;
18 typedef typename map_type::value_type pair_type;
19 typedef typename map_type::iterator iterator;
21 void operator()( map_type& map, iterator itWhat )
23 map.insert( *itWhat );
28 template <typename Key, typename T, typename Traits, typename Alloc>
29 struct swap_item_policy< std::map< Key, T, Traits, Alloc > >
31 typedef std::map< Key, T, Traits, Alloc > map_type;
32 typedef typename map_type::value_type pair_type;
33 typedef typename map_type::iterator iterator;
35 void operator()( map_type& map, iterator itWhat )
37 std::pair< typename map_type::iterator, bool > ret = map.insert( pair_type( itWhat->first, typename pair_type::second_type() ));
38 assert( ret.second ) ; // successful insertion
39 std::swap( ret.first->second, itWhat->second );
43 // Move policy for map
44 template <typename Key, typename T, typename Traits, typename Alloc>
45 struct move_item_policy< std::map< Key, T, Traits, Alloc > >
47 typedef std::map< Key, T, Traits, Alloc > map_type;
48 typedef typename map_type::value_type pair_type;
49 typedef typename map_type::iterator iterator;
51 void operator()( map_type& map, iterator itWhat )
53 map.insert( std::move( *itWhat ) );
56 } // namespace striped_set
57 }} // namespace cds::container
59 namespace cds { namespace intrusive { namespace striped_set {
61 /// std::set adapter for hash set bucket
62 template <typename Key, typename T, class Traits, class Alloc, typename... Options>
63 class adapt< std::map< Key, T, Traits, Alloc>, Options... >
66 typedef std::map< Key, T, Traits, Alloc> container_type ; ///< underlying container type
69 /// Adapted container type
70 class adapted_container: public cds::container::striped_set::adapted_container
73 typedef typename container_type::value_type value_type ; ///< value type stored in the container
74 typedef typename container_type::key_type key_type;
75 typedef typename container_type::mapped_type mapped_type;
76 typedef typename container_type::iterator iterator ; ///< container iterator
77 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
79 static bool const has_find_with = false;
80 static bool const has_erase_with = false;
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>
104 template <typename Q, typename Func>
105 bool insert( const Q& key, Func f )
107 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ) );
113 template <typename Q, typename... Args>
114 bool emplace( Q&& key, Args&&... args )
116 std::pair<iterator, bool> res = m_Map.emplace( std::forward<Q>(key), std::move(mapped_type( std::forward<Args>(args)...)));
120 template <typename Q, typename Func>
121 std::pair<bool, bool> ensure( const Q& key, Func func )
123 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ));
124 func( res.second, *res.first );
125 return std::make_pair( true, res.second );
128 template <typename Q, typename Func>
129 bool erase( const Q& key, Func f )
131 iterator it = m_Map.find( key_type(key) );
132 if ( it == m_Map.end() )
139 template <typename Q, typename Func>
140 bool find( Q& val, Func f )
142 iterator it = m_Map.find( key_type(val) );
143 if ( it == m_Map.end() )
149 /// Clears the container
155 iterator begin() { return m_Map.begin(); }
156 const_iterator begin() const { return m_Map.begin(); }
157 iterator end() { return m_Map.end(); }
158 const_iterator end() const { return m_Map.end(); }
160 void move_item( adapted_container& /*from*/, iterator itWhat )
162 assert( m_Map.find( itWhat->first ) == m_Map.end() );
163 copy_item()( m_Map, itWhat );
173 typedef adapted_container type ; ///< Result of \p adapt metafunction
175 }}} // namespace cds::intrusive::striped_set
179 #endif // #ifndef CDSLIB_CONTAINER_STRIPED_MAP_STD_MAP_ADAPTER_H