Move libcds 1.6.0 from SVN
[libcds.git] / cds / container / striped_map / std_map.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_MAP_ADAPTER_H
4 #define __CDS_CONTAINER_STRIPED_MAP_STD_MAP_ADAPTER_H
5
6 #include <cds/container/striped_set/adapter.h>
7 #include <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 Traits, typename Alloc>
15         struct copy_item_policy< std::map< Key, T, Traits, Alloc > >
16         {
17             typedef std::map< Key, T, Traits, 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 item policy
28         template <typename Key, typename T, typename Traits, typename Alloc>
29         struct swap_item_policy< std::map< Key, T, Traits, Alloc > >
30         {
31             typedef std::map< Key, T, Traits, 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                 std::pair< typename map_type::iterator, bool > ret = map.insert( pair_type( itWhat->first, typename pair_type::second_type() ));
38                 assert( ret.second )    ;   // successful insertion
39                 std::swap( ret.first->second, itWhat->second );
40             }
41         };
42
43 #ifdef CDS_MOVE_SEMANTICS_SUPPORT
44         // Move policy for map
45         template <typename Key, typename T, typename Traits, typename Alloc>
46         struct move_item_policy< std::map< Key, T, Traits, Alloc > >
47         {
48             typedef std::map< Key, T, Traits, 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 #endif
58     }   // namespace striped_set
59 }} // namespace cds::container
60
61 namespace cds { namespace intrusive { namespace striped_set {
62
63     /// std::set adapter for hash set bucket
64     template <typename Key, typename T, class Traits, class Alloc, CDS_SPEC_OPTIONS>
65     class adapt< std::map< Key, T, Traits, Alloc>, CDS_OPTIONS >
66     {
67     public:
68         typedef std::map< Key, T, Traits, Alloc>     container_type          ;   ///< underlying container type
69
70     private:
71         /// Adapted container type
72         class adapted_container: public cds::container::striped_set::adapted_container
73         {
74         public:
75             typedef typename container_type::value_type     value_type  ;   ///< value type stored in the container
76             typedef typename container_type::key_type       key_type;
77             typedef typename container_type::mapped_type    mapped_type;
78             typedef typename container_type::iterator       iterator ;   ///< container iterator
79             typedef typename container_type::const_iterator const_iterator ;    ///< container const iterator
80
81             static bool const has_find_with = false;
82             static bool const has_erase_with = false;
83
84         private:
85             //@cond
86             typedef typename cds::opt::select<
87                 typename cds::opt::value<
88                     typename cds::opt::find_option<
89                         cds::opt::copy_policy< cds::container::striped_set::move_item >
90                         , CDS_OPTIONS
91                     >::type
92                 >::copy_policy
93                 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
94                 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
95 #ifdef CDS_MOVE_SEMANTICS_SUPPORT
96                 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
97 #endif
98             >::type copy_item;
99             //@endcond
100
101         private:
102             //@cond
103             container_type  m_Map;
104             //@endcond
105
106         public:
107
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                     ::cds::unref(f)( *res.first );
114                 return res.second;
115             }
116
117 #       ifdef CDS_EMPLACE_SUPPORT
118             template <typename Q, typename... Args>
119             bool emplace( Q&& key, Args&&... args )
120             {
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)...)));
124 #           else
125                 std::pair<iterator, bool> res = m_Map.emplace( std::forward<Q>(key), std::move(mapped_type( std::forward<Args>(args)...)));
126 #           endif
127                 return res.second;
128             }
129 #       endif
130
131             template <typename Q, typename Func>
132             std::pair<bool, bool> ensure( const Q& key, Func func )
133             {
134                 std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ));
135                 cds::unref(func)( res.second, *res.first );
136                 return std::make_pair( true, res.second );
137             }
138
139             template <typename Q, typename Func>
140             bool erase( const Q& key, Func f )
141             {
142                 iterator it = m_Map.find( key_type(key) );
143                 if ( it == m_Map.end() )
144                     return false;
145                 cds::unref(f)( *it );
146                 m_Map.erase( it );
147                 return true;
148             }
149
150             template <typename Q, typename Func>
151             bool find( Q& val, Func f )
152             {
153                 iterator it = m_Map.find( key_type(val) );
154                 if ( it == m_Map.end() )
155                     return false;
156                 cds::unref(f)( *it, val );
157                 return true;
158             }
159
160             /// Clears the container
161             void clear()
162             {
163                 m_Map.clear();
164             }
165
166             iterator begin()                { return m_Map.begin(); }
167             const_iterator begin() const    { return m_Map.begin(); }
168             iterator end()                  { return m_Map.end(); }
169             const_iterator end() const      { return m_Map.end(); }
170
171             void move_item( adapted_container& /*from*/, iterator itWhat )
172             {
173                 assert( m_Map.find( itWhat->first ) == m_Map.end() );
174                 copy_item()( m_Map, itWhat );
175             }
176
177             size_t size() const
178             {
179                 return m_Map.size();
180             }
181         };
182
183     public:
184         typedef adapted_container type ; ///< Result of \p adapt metafunction
185     };
186 }}} // namespace cds::intrusive::striped_set
187
188 //@endcond
189
190 #endif // #ifndef __CDS_CONTAINER_STRIPED_MAP_STD_MAP_ADAPTER_H