3 #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_STD_ADAPTER_H
4 #define __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_STD_ADAPTER_H
6 #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_ADAPTER_H
7 # error <cds/container/striped_map/std_hash_map.h> must be included instead of <cds/container/striped_map/std_hash_map_std.h> header
10 #include <cds/container/striped_set/adapter.h>
11 #include <unordered_map>
14 namespace cds { namespace container {
15 namespace striped_set {
17 // Copy policy for map
18 template <typename Key, typename T, typename Hash, typename Pred, typename Alloc>
19 struct copy_item_policy< std::unordered_map< Key, T, Hash, Pred, Alloc > >
21 typedef std::unordered_map< Key, T, Hash, Pred, Alloc > map_type;
22 typedef typename map_type::value_type pair_type;
23 typedef typename map_type::iterator iterator;
25 void operator()( map_type& map, iterator itWhat )
27 map.insert( *itWhat );
31 // Swap policy for map
32 template <typename Key, typename T, typename Hash, typename Pred, typename Alloc>
33 struct swap_item_policy< std::unordered_map< Key, T, Hash, Pred, Alloc > >
35 typedef std::unordered_map< Key, T, Hash, Pred, Alloc > map_type;
36 typedef typename map_type::value_type pair_type;
37 typedef typename map_type::iterator iterator;
39 void operator()( map_type& map, iterator itWhat )
41 pair_type pair( itWhat->first, typename pair_type::second_type() );
42 std::pair<iterator, bool> res = map.insert( pair );
44 std::swap( res.first->second, itWhat->second );
48 // Move policy for map
49 template <typename Key, typename T, typename Hash, typename Pred, typename Alloc>
50 struct move_item_policy< std::unordered_map< Key, T, Hash, Pred, Alloc > >
52 typedef std::unordered_map< Key, T, Hash, Pred, Alloc > map_type;
53 typedef typename map_type::value_type pair_type;
54 typedef typename map_type::iterator iterator;
56 void operator()( map_type& map, iterator itWhat )
58 map.insert( std::move( *itWhat ) );
61 } // namespace striped_set
62 }} // namespace cds::container
64 namespace cds { namespace intrusive { namespace striped_set {
66 /// std::unordered_map adapter for hash map bucket
67 template <typename Key, typename T, class Hash, class Pred, class Alloc, CDS_SPEC_OPTIONS>
68 class adapt< std::unordered_map< Key, T, Hash, Pred, Alloc>, CDS_OPTIONS >
71 typedef std::unordered_map< Key, T, Hash, Pred, Alloc> container_type ; ///< underlying container type
74 /// Adapted container type
75 class adapted_container: public cds::container::striped_set::adapted_container
78 typedef typename container_type::value_type value_type ; ///< value type stored in the container
79 typedef typename container_type::key_type key_type;
80 typedef typename container_type::mapped_type mapped_type;
81 typedef typename container_type::iterator iterator ; ///< container iterator
82 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
84 static bool const has_find_with = false;
85 static bool const has_erase_with = false;
89 typedef typename cds::opt::select<
90 typename cds::opt::value<
91 typename cds::opt::find_option<
92 cds::opt::copy_policy< cds::container::striped_set::move_item >
96 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
97 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
98 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
104 container_type m_Map;
108 template <typename Q, typename Func>
109 bool insert( const Q& key, Func f )
111 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ));
113 ::cds::unref(f)( const_cast<value_type&>(*res.first) );
117 # ifdef CDS_EMPLACE_SUPPORT
118 template <typename Q, typename... Args>
119 bool emplace( Q&& key, Args&&... args )
121 # if CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION < 40800 || CDS_COMPILER == CDS_COMPILER_CLANG && !defined(__LIBCPP_VERSION)
122 // GCC < 4.8: std::map has no "emplace" member function. Emulate it
123 std::pair<iterator, bool> res = m_Map.insert( value_type( std::forward<Q>(key), mapped_type( std::forward<Args>(args)...)));
125 std::pair<iterator, bool> res = m_Map.emplace( std::forward<Q>(key), std::move( mapped_type(std::forward<Args>(args)...)) );
131 template <typename Q, typename Func>
132 std::pair<bool, bool> ensure( const Q& key, Func func )
134 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ) );
135 cds::unref(func)( res.second, const_cast<value_type&>(*res.first));
136 return std::make_pair( true, res.second );
139 template <typename Q, typename Func>
140 bool erase( const Q& key, Func f )
142 iterator it = m_Map.find( key_type(key) );
143 if ( it == m_Map.end() )
145 ::cds::unref(f)( const_cast<value_type&>(*it) );
150 template <typename Q, typename Func>
151 bool find( Q& val, Func f )
153 iterator it = m_Map.find( key_type(val) );
154 if ( it == m_Map.end() )
156 ::cds::unref(f)( const_cast<value_type&>(*it), val );
165 iterator begin() { return m_Map.begin(); }
166 const_iterator begin() const { return m_Map.begin(); }
167 iterator end() { return m_Map.end(); }
168 const_iterator end() const { return m_Map.end(); }
170 void move_item( adapted_container& /*from*/, iterator itWhat )
172 assert( m_Map.find( itWhat->first ) == m_Map.end() );
173 copy_item()( m_Map, itWhat );
183 typedef adapted_container type ; ///< Result of \p adapt metafunction
186 }}} // namespace cds::intrusive::striped_set
191 #endif // #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_STD_ADAPTER_H