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 #ifdef CDS_MOVE_SEMANTICS_SUPPORT
49 // Move policy for map
50 template <typename Key, typename T, typename Hash, typename Pred, typename Alloc>
51 struct move_item_policy< std::unordered_map< Key, T, Hash, Pred, Alloc > >
53 typedef std::unordered_map< Key, T, Hash, Pred, Alloc > map_type;
54 typedef typename map_type::value_type pair_type;
55 typedef typename map_type::iterator iterator;
57 void operator()( map_type& map, iterator itWhat )
59 map.insert( std::move( *itWhat ) );
63 } // namespace striped_set
64 }} // namespace cds::container
66 namespace cds { namespace intrusive { namespace striped_set {
68 /// std::unordered_map adapter for hash map bucket
69 template <typename Key, typename T, class Hash, class Pred, class Alloc, CDS_SPEC_OPTIONS>
70 class adapt< std::unordered_map< Key, T, Hash, Pred, Alloc>, CDS_OPTIONS >
73 typedef std::unordered_map< Key, T, Hash, Pred, Alloc> container_type ; ///< underlying container type
76 /// Adapted container type
77 class adapted_container: public cds::container::striped_set::adapted_container
80 typedef typename container_type::value_type value_type ; ///< value type stored in the container
81 typedef typename container_type::key_type key_type;
82 typedef typename container_type::mapped_type mapped_type;
83 typedef typename container_type::iterator iterator ; ///< container iterator
84 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
86 static bool const has_find_with = false;
87 static bool const has_erase_with = false;
91 typedef typename cds::opt::select<
92 typename cds::opt::value<
93 typename cds::opt::find_option<
94 cds::opt::copy_policy< cds::container::striped_set::move_item >
98 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
99 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
100 #ifdef CDS_MOVE_SEMANTICS_SUPPORT
101 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
108 container_type m_Map;
112 template <typename Q, typename Func>
113 bool insert( const Q& key, Func f )
115 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ));
117 ::cds::unref(f)( const_cast<value_type&>(*res.first) );
121 # ifdef CDS_EMPLACE_SUPPORT
122 template <typename Q, typename... Args>
123 bool emplace( Q&& key, Args&&... args )
125 # if CDS_COMPILER == CDS_COMPILER_GCC && CDS_COMPILER_VERSION < 40800 || CDS_COMPILER == CDS_COMPILER_CLANG && !defined(__LIBCPP_VERSION)
126 // GCC < 4.8: std::map has no "emplace" member function. Emulate it
127 std::pair<iterator, bool> res = m_Map.insert( value_type( std::forward<Q>(key), mapped_type( std::forward<Args>(args)...)));
129 std::pair<iterator, bool> res = m_Map.emplace( std::forward<Q>(key), std::move( mapped_type(std::forward<Args>(args)...)) );
135 template <typename Q, typename Func>
136 std::pair<bool, bool> ensure( const Q& key, Func func )
138 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ) );
139 cds::unref(func)( res.second, const_cast<value_type&>(*res.first));
140 return std::make_pair( true, res.second );
143 template <typename Q, typename Func>
144 bool erase( const Q& key, Func f )
146 iterator it = m_Map.find( key_type(key) );
147 if ( it == m_Map.end() )
149 ::cds::unref(f)( const_cast<value_type&>(*it) );
154 template <typename Q, typename Func>
155 bool find( Q& val, Func f )
157 iterator it = m_Map.find( key_type(val) );
158 if ( it == m_Map.end() )
160 ::cds::unref(f)( const_cast<value_type&>(*it), val );
169 iterator begin() { return m_Map.begin(); }
170 const_iterator begin() const { return m_Map.begin(); }
171 iterator end() { return m_Map.end(); }
172 const_iterator end() const { return m_Map.end(); }
174 void move_item( adapted_container& /*from*/, iterator itWhat )
176 assert( m_Map.find( itWhat->first ) == m_Map.end() );
177 copy_item()( m_Map, itWhat );
187 typedef adapted_container type ; ///< Result of \p adapt metafunction
190 }}} // namespace cds::intrusive::striped_set
195 #endif // #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_STD_ADAPTER_H