Move libcds 1.6.0 from SVN
[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 #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 > >
52         {
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;
56
57             void operator()( map_type& map, iterator itWhat )
58             {
59                 map.insert( std::move( *itWhat ) );
60             }
61         };
62 #endif
63     }   // namespace striped_set
64 }} // namespace cds::container
65
66 namespace cds { namespace intrusive { namespace striped_set {
67
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 >
71     {
72     public:
73         typedef std::unordered_map< Key, T, Hash, Pred, Alloc>  container_type  ;   ///< underlying container type
74
75     private:
76         /// Adapted container type
77         class adapted_container: public cds::container::striped_set::adapted_container
78         {
79         public:
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
85
86             static bool const has_find_with = false;
87             static bool const has_erase_with = false;
88
89         private:
90             //@cond
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 >
95                         , CDS_OPTIONS
96                     >::type
97                 >::copy_policy
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>
102 #endif
103             >::type copy_item;
104             //@endcond
105
106         private:
107             //@cond
108             container_type  m_Map;
109             //@endcond
110
111         public:
112             template <typename Q, typename Func>
113             bool insert( const Q& key, Func f )
114             {
115                 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ));
116                 if ( res.second )
117                     ::cds::unref(f)( const_cast<value_type&>(*res.first) );
118                 return res.second;
119             }
120
121 #       ifdef CDS_EMPLACE_SUPPORT
122             template <typename Q, typename... Args>
123             bool emplace( Q&& key, Args&&... args )
124             {
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)...)));
128 #           else
129                 std::pair<iterator, bool> res = m_Map.emplace( std::forward<Q>(key), std::move( mapped_type(std::forward<Args>(args)...)) );
130 #           endif
131                 return res.second;
132             }
133 #       endif
134
135             template <typename Q, typename Func>
136             std::pair<bool, bool> ensure( const Q& key, Func func )
137             {
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 );
141             }
142
143             template <typename Q, typename Func>
144             bool erase( const Q& key, Func f )
145             {
146                 iterator it = m_Map.find( key_type(key) );
147                 if ( it == m_Map.end() )
148                     return false;
149                 ::cds::unref(f)( const_cast<value_type&>(*it) );
150                 m_Map.erase( it );
151                 return true;
152             }
153
154             template <typename Q, typename Func>
155             bool find( Q& val, Func f )
156             {
157                 iterator it = m_Map.find( key_type(val) );
158                 if ( it == m_Map.end() )
159                     return false;
160                 ::cds::unref(f)( const_cast<value_type&>(*it), val );
161                 return true;
162             }
163
164             void clear()
165             {
166                 m_Map.clear();
167             }
168
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(); }
173
174             void move_item( adapted_container& /*from*/, iterator itWhat )
175             {
176                 assert( m_Map.find( itWhat->first ) == m_Map.end() );
177                 copy_item()( m_Map, itWhat );
178             }
179
180             size_t size() const
181             {
182                 return m_Map.size();
183             }
184         };
185
186     public:
187         typedef adapted_container type ; ///< Result of \p adapt metafunction
188
189     };
190 }}} // namespace cds::intrusive::striped_set
191
192
193 //@endcond
194
195 #endif  // #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_HASH_MAP_STD_ADAPTER_H