Replace cds::ref/boost::ref with std::ref, remove cds::unref and cds/ref.h header
[libcds.git] / cds / container / striped_map / std_hash_map_std.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_STD_ADAPTER_H
4 #define __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_STD_ADAPTER_H
5
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
8 #endif
9
10 #include <cds/container/striped_set/adapter.h>
11 #include <unordered_map>
12
13 //@cond
14 namespace cds { namespace container {
15     namespace striped_set {
16
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 > >
20         {
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;
24
25             void operator()( map_type& map, iterator itWhat )
26             {
27                 map.insert( *itWhat );
28             }
29         };
30
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 > >
34         {
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;
38
39             void operator()( map_type& map, iterator itWhat )
40             {
41                 pair_type pair( itWhat->first, typename pair_type::second_type() );
42                 std::pair<iterator, bool> res = map.insert( pair );
43                 assert( res.second );
44                 std::swap( res.first->second, itWhat->second );
45             }
46         };
47
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 > >
51         {
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;
55
56             void operator()( map_type& map, iterator itWhat )
57             {
58                 map.insert( std::move( *itWhat ) );
59             }
60         };
61     }   // namespace striped_set
62 }} // namespace cds::container
63
64 namespace cds { namespace intrusive { namespace striped_set {
65
66     /// std::unordered_map  adapter for hash map bucket
67     template <typename Key, typename T, class Hash, class Pred, class Alloc, typename... Options>
68     class adapt< std::unordered_map< Key, T, Hash, Pred, Alloc>, Options... >
69     {
70     public:
71         typedef std::unordered_map< Key, T, Hash, Pred, Alloc>  container_type  ;   ///< underlying container type
72
73     private:
74         /// Adapted container type
75         class adapted_container: public cds::container::striped_set::adapted_container
76         {
77         public:
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
83
84             static bool const has_find_with = false;
85             static bool const has_erase_with = false;
86
87         private:
88             //@cond
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 >
93                         , Options...
94                     >::type
95                 >::copy_policy
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>
99             >::type copy_item;
100             //@endcond
101
102         private:
103             //@cond
104             container_type  m_Map;
105             //@endcond
106
107         public:
108             template <typename Q, typename Func>
109             bool insert( const Q& key, Func f )
110             {
111                 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ));
112                 if ( res.second )
113                     ::f( const_cast<value_type&>(*res.first) );
114                 return res.second;
115             }
116
117             template <typename Q, typename... Args>
118             bool emplace( Q&& key, Args&&... args )
119             {
120                 std::pair<iterator, bool> res = m_Map.emplace( std::forward<Q>(key), std::move( mapped_type(std::forward<Args>(args)...)) );
121                 return res.second;
122             }
123
124             template <typename Q, typename Func>
125             std::pair<bool, bool> ensure( const Q& key, Func func )
126             {
127                 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ) );
128                 func( res.second, const_cast<value_type&>(*res.first));
129                 return std::make_pair( true, res.second );
130             }
131
132             template <typename Q, typename Func>
133             bool erase( const Q& key, Func f )
134             {
135                 iterator it = m_Map.find( key_type(key) );
136                 if ( it == m_Map.end() )
137                     return false;
138                 ::f( const_cast<value_type&>(*it) );
139                 m_Map.erase( it );
140                 return true;
141             }
142
143             template <typename Q, typename Func>
144             bool find( Q& val, Func f )
145             {
146                 iterator it = m_Map.find( key_type(val) );
147                 if ( it == m_Map.end() )
148                     return false;
149                 ::f( const_cast<value_type&>(*it), val );
150                 return true;
151             }
152
153             void clear()
154             {
155                 m_Map.clear();
156             }
157
158             iterator begin()                { return m_Map.begin(); }
159             const_iterator begin() const    { return m_Map.begin(); }
160             iterator end()                  { return m_Map.end(); }
161             const_iterator end() const      { return m_Map.end(); }
162
163             void move_item( adapted_container& /*from*/, iterator itWhat )
164             {
165                 assert( m_Map.find( itWhat->first ) == m_Map.end() );
166                 copy_item()( m_Map, itWhat );
167             }
168
169             size_t size() const
170             {
171                 return m_Map.size();
172             }
173         };
174
175     public:
176         typedef adapted_container type ; ///< Result of \p adapt metafunction
177
178     };
179 }}} // namespace cds::intrusive::striped_set
180
181
182 //@endcond
183
184 #endif  // #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_STD_ADAPTER_H