Merge branch 'dev' of github.com:khizmax/libcds into dev
[libcds.git] / cds / container / striped_map / std_hash_map.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_CONTAINER_STRIPED_MAP_STD_HASH_MAP_ADAPTER_H
4 #define CDSLIB_CONTAINER_STRIPED_MAP_STD_HASH_MAP_ADAPTER_H
5
6 #include <cds/container/striped_set/adapter.h>
7 #include <unordered_map>
8
9 //@cond
10 namespace cds { namespace container {
11     namespace striped_set {
12
13         // Copy policy for map
14         template <typename Key, typename T, typename Hash, typename Pred, typename Alloc>
15         struct copy_item_policy< std::unordered_map< Key, T, Hash, Pred, Alloc > >
16         {
17             typedef std::unordered_map< Key, T, Hash, Pred, Alloc > map_type;
18             typedef typename map_type::value_type   pair_type;
19             typedef typename map_type::iterator     iterator;
20
21             void operator()( map_type& map, iterator itWhat )
22             {
23                 map.insert( *itWhat );
24             }
25         };
26
27         // Swap policy for map
28         template <typename Key, typename T, typename Hash, typename Pred, typename Alloc>
29         struct swap_item_policy< std::unordered_map< Key, T, Hash, Pred, Alloc > >
30         {
31             typedef std::unordered_map< Key, T, Hash, Pred, Alloc > map_type;
32             typedef typename map_type::value_type   pair_type;
33             typedef typename map_type::iterator     iterator;
34
35             void operator()( map_type& map, iterator itWhat )
36             {
37                 pair_type pair( itWhat->first, typename pair_type::second_type() );
38                 std::pair<iterator, bool> res = map.insert( pair );
39                 assert( res.second );
40                 std::swap( res.first->second, itWhat->second );
41             }
42         };
43
44         // Move policy for map
45         template <typename Key, typename T, typename Hash, typename Pred, typename Alloc>
46         struct move_item_policy< std::unordered_map< Key, T, Hash, Pred, Alloc > >
47         {
48             typedef std::unordered_map< Key, T, Hash, Pred, Alloc > map_type;
49             typedef typename map_type::value_type pair_type;
50             typedef typename map_type::iterator     iterator;
51
52             void operator()( map_type& map, iterator itWhat )
53             {
54                 map.insert( std::move( *itWhat ) );
55             }
56         };
57     }   // namespace striped_set
58 }} // namespace cds::container
59
60 namespace cds { namespace intrusive { namespace striped_set {
61
62     /// std::unordered_map  adapter for hash map bucket
63     template <typename Key, typename T, class Hash, class Pred, class Alloc, typename... Options>
64     class adapt< std::unordered_map< Key, T, Hash, Pred, Alloc>, Options... >
65     {
66     public:
67         typedef std::unordered_map< Key, T, Hash, Pred, Alloc>  container_type  ;   ///< underlying container type
68
69     private:
70         /// Adapted container type
71         class adapted_container: public cds::container::striped_set::adapted_container
72         {
73         public:
74             typedef typename container_type::value_type     value_type  ;   ///< value type stored in the container
75             typedef typename container_type::key_type       key_type;
76             typedef typename container_type::mapped_type    mapped_type;
77             typedef typename container_type::iterator       iterator ;   ///< container iterator
78             typedef typename container_type::const_iterator const_iterator ;    ///< container const iterator
79
80             static bool const has_find_with = false;
81             static bool const has_erase_with = false;
82
83         private:
84             //@cond
85             typedef typename cds::opt::select<
86                 typename cds::opt::value<
87                     typename cds::opt::find_option<
88                         cds::opt::copy_policy< cds::container::striped_set::move_item >
89                         , Options...
90                     >::type
91                 >::copy_policy
92                 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
93                 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
94                 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
95             >::type copy_item;
96             //@endcond
97
98         private:
99             //@cond
100             container_type  m_Map;
101             //@endcond
102
103         public:
104             template <typename Q, typename Func>
105             bool insert( const Q& key, Func f )
106             {
107                 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ));
108                 if ( res.second )
109                     f( const_cast<value_type&>(*res.first) );
110                 return res.second;
111             }
112
113             template <typename Q, typename... Args>
114             bool emplace( Q&& key, Args&&... args )
115             {
116                 std::pair<iterator, bool> res = m_Map.emplace( std::forward<Q>(key), std::move( mapped_type(std::forward<Args>(args)...)) );
117                 return res.second;
118             }
119
120             template <typename Q, typename Func>
121             std::pair<bool, bool> update( const Q& key, Func func, bool bAllowInsert )
122             {
123                 if ( bAllowInsert ) {
124                     std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ) );
125                     func( res.second, const_cast<value_type&>(*res.first));
126                     return std::make_pair( true, res.second );
127                 }
128                 else {
129                     auto it = m_Map.find(key_type( key ));
130                     if ( it == end() )
131                         return std::make_pair( false, false );
132                     func( false, *it );
133                     return std::make_pair( true, false );
134                 }
135             }
136
137             template <typename Q, typename Func>
138             bool erase( const Q& key, Func f )
139             {
140                 iterator it = m_Map.find( key_type(key) );
141                 if ( it == m_Map.end() )
142                     return false;
143                 f( const_cast<value_type&>(*it) );
144                 m_Map.erase( it );
145                 return true;
146             }
147
148             template <typename Q, typename Func>
149             bool find( Q& val, Func f )
150             {
151                 iterator it = m_Map.find( key_type(val) );
152                 if ( it == m_Map.end() )
153                     return false;
154                 f( const_cast<value_type&>(*it), val );
155                 return true;
156             }
157
158             void clear()
159             {
160                 m_Map.clear();
161             }
162
163             iterator begin()                { return m_Map.begin(); }
164             const_iterator begin() const    { return m_Map.begin(); }
165             iterator end()                  { return m_Map.end(); }
166             const_iterator end() const      { return m_Map.end(); }
167
168             void move_item( adapted_container& /*from*/, iterator itWhat )
169             {
170                 assert( m_Map.find( itWhat->first ) == m_Map.end() );
171                 copy_item()( m_Map, itWhat );
172             }
173
174             size_t size() const
175             {
176                 return m_Map.size();
177             }
178         };
179
180     public:
181         typedef adapted_container type ; ///< Result of \p adapt metafunction
182
183     };
184 }}} // namespace cds::intrusive::striped_set
185
186
187 //@endcond
188
189 #endif  // #ifndef CDSLIB_CONTAINER_STRIPED_MAP_STD_HASH_MAP_ADAPTER_H