1 // unordered_map implementation -*- C++ -*-
3 // Copyright (C) 2010, 2011 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/unordered_map.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{unordered_map}
30 #ifndef _UNORDERED_MAP_H
31 #define _UNORDERED_MAP_H
33 namespace std _GLIBCXX_VISIBILITY(default)
35 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
37 // NB: When we get typedef templates these class definitions
38 // will be unnecessary.
39 template<class _Key, class _Tp,
40 class _Hash = hash<_Key>,
41 class _Pred = std::equal_to<_Key>,
42 class _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
43 bool __cache_hash_code =
44 __not_<__and_<is_integral<_Key>,
45 __detail::__is_noexcept_hash<_Key, _Hash>>>::value>
47 : public _Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc,
48 std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
49 _Hash, __detail::_Mod_range_hashing,
50 __detail::_Default_ranged_hash,
51 __detail::_Prime_rehash_policy,
52 __cache_hash_code, false, true>
54 typedef _Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc,
55 std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
56 _Hash, __detail::_Mod_range_hashing,
57 __detail::_Default_ranged_hash,
58 __detail::_Prime_rehash_policy,
59 __cache_hash_code, false, true>
63 typedef typename _Base::value_type value_type;
64 typedef typename _Base::size_type size_type;
65 typedef typename _Base::hasher hasher;
66 typedef typename _Base::key_equal key_equal;
67 typedef typename _Base::allocator_type allocator_type;
70 __unordered_map(size_type __n = 10,
71 const hasher& __hf = hasher(),
72 const key_equal& __eql = key_equal(),
73 const allocator_type& __a = allocator_type())
74 : _Base(__n, __hf, __detail::_Mod_range_hashing(),
75 __detail::_Default_ranged_hash(),
76 __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
79 template<typename _InputIterator>
80 __unordered_map(_InputIterator __f, _InputIterator __l,
82 const hasher& __hf = hasher(),
83 const key_equal& __eql = key_equal(),
84 const allocator_type& __a = allocator_type())
85 : _Base(__f, __l, __n, __hf, __detail::_Mod_range_hashing(),
86 __detail::_Default_ranged_hash(),
87 __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
90 __unordered_map(initializer_list<value_type> __l,
92 const hasher& __hf = hasher(),
93 const key_equal& __eql = key_equal(),
94 const allocator_type& __a = allocator_type())
95 : _Base(__l.begin(), __l.end(), __n, __hf,
96 __detail::_Mod_range_hashing(),
97 __detail::_Default_ranged_hash(),
98 __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
102 operator=(initializer_list<value_type> __l)
105 this->insert(__l.begin(), __l.end());
110 template<class _Key, class _Tp,
111 class _Hash = hash<_Key>,
112 class _Pred = std::equal_to<_Key>,
113 class _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
114 bool __cache_hash_code =
115 __not_<__and_<is_integral<_Key>,
116 __detail::__is_noexcept_hash<_Key, _Hash>>>::value>
117 class __unordered_multimap
118 : public _Hashtable<_Key, std::pair<const _Key, _Tp>,
120 std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
121 _Hash, __detail::_Mod_range_hashing,
122 __detail::_Default_ranged_hash,
123 __detail::_Prime_rehash_policy,
124 __cache_hash_code, false, false>
126 typedef _Hashtable<_Key, std::pair<const _Key, _Tp>,
128 std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
129 _Hash, __detail::_Mod_range_hashing,
130 __detail::_Default_ranged_hash,
131 __detail::_Prime_rehash_policy,
132 __cache_hash_code, false, false>
136 typedef typename _Base::value_type value_type;
137 typedef typename _Base::size_type size_type;
138 typedef typename _Base::hasher hasher;
139 typedef typename _Base::key_equal key_equal;
140 typedef typename _Base::allocator_type allocator_type;
143 __unordered_multimap(size_type __n = 10,
144 const hasher& __hf = hasher(),
145 const key_equal& __eql = key_equal(),
146 const allocator_type& __a = allocator_type())
147 : _Base(__n, __hf, __detail::_Mod_range_hashing(),
148 __detail::_Default_ranged_hash(),
149 __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
153 template<typename _InputIterator>
154 __unordered_multimap(_InputIterator __f, _InputIterator __l,
156 const hasher& __hf = hasher(),
157 const key_equal& __eql = key_equal(),
158 const allocator_type& __a = allocator_type())
159 : _Base(__f, __l, __n, __hf, __detail::_Mod_range_hashing(),
160 __detail::_Default_ranged_hash(),
161 __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
164 __unordered_multimap(initializer_list<value_type> __l,
166 const hasher& __hf = hasher(),
167 const key_equal& __eql = key_equal(),
168 const allocator_type& __a = allocator_type())
169 : _Base(__l.begin(), __l.end(), __n, __hf,
170 __detail::_Mod_range_hashing(),
171 __detail::_Default_ranged_hash(),
172 __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
175 __unordered_multimap&
176 operator=(initializer_list<value_type> __l)
179 this->insert(__l.begin(), __l.end());
184 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
185 bool __cache_hash_code>
187 swap(__unordered_map<_Key, _Tp, _Hash, _Pred,
188 _Alloc, __cache_hash_code>& __x,
189 __unordered_map<_Key, _Tp, _Hash, _Pred,
190 _Alloc, __cache_hash_code>& __y)
193 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
194 bool __cache_hash_code>
196 swap(__unordered_multimap<_Key, _Tp, _Hash, _Pred,
197 _Alloc, __cache_hash_code>& __x,
198 __unordered_multimap<_Key, _Tp, _Hash, _Pred,
199 _Alloc, __cache_hash_code>& __y)
202 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
203 bool __cache_hash_code>
205 operator==(const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
206 __cache_hash_code>& __x,
207 const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
208 __cache_hash_code>& __y)
209 { return __x._M_equal(__y); }
211 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
212 bool __cache_hash_code>
214 operator!=(const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
215 __cache_hash_code>& __x,
216 const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
217 __cache_hash_code>& __y)
218 { return !(__x == __y); }
220 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
221 bool __cache_hash_code>
223 operator==(const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
224 __cache_hash_code>& __x,
225 const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
226 __cache_hash_code>& __y)
227 { return __x._M_equal(__y); }
229 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
230 bool __cache_hash_code>
232 operator!=(const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
233 __cache_hash_code>& __x,
234 const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
235 __cache_hash_code>& __y)
236 { return !(__x == __y); }
239 * @brief A standard container composed of unique keys (containing
240 * at most one of each key value) that associates values of another type
243 * @ingroup unordered_associative_containers
245 * Meets the requirements of a <a href="tables.html#65">container</a>, and
246 * <a href="tables.html#xx">unordered associative container</a>
248 * @param Key Type of key objects.
249 * @param Tp Type of mapped objects.
250 * @param Hash Hashing function object type, defaults to hash<Value>.
251 * @param Pred Predicate function object type, defaults to equal_to<Value>.
252 * @param Alloc Allocator type, defaults to allocator<Key>.
254 * The resulting value type of the container is std::pair<const Key, Tp>.
256 template<class _Key, class _Tp,
257 class _Hash = hash<_Key>,
258 class _Pred = std::equal_to<_Key>,
259 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
261 : public __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>
263 typedef __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc> _Base;
266 typedef typename _Base::value_type value_type;
267 typedef typename _Base::size_type size_type;
268 typedef typename _Base::hasher hasher;
269 typedef typename _Base::key_equal key_equal;
270 typedef typename _Base::allocator_type allocator_type;
273 unordered_map(size_type __n = 10,
274 const hasher& __hf = hasher(),
275 const key_equal& __eql = key_equal(),
276 const allocator_type& __a = allocator_type())
277 : _Base(__n, __hf, __eql, __a)
280 template<typename _InputIterator>
281 unordered_map(_InputIterator __f, _InputIterator __l,
283 const hasher& __hf = hasher(),
284 const key_equal& __eql = key_equal(),
285 const allocator_type& __a = allocator_type())
286 : _Base(__f, __l, __n, __hf, __eql, __a)
289 unordered_map(initializer_list<value_type> __l,
291 const hasher& __hf = hasher(),
292 const key_equal& __eql = key_equal(),
293 const allocator_type& __a = allocator_type())
294 : _Base(__l.begin(), __l.end(), __n, __hf, __eql, __a)
298 operator=(initializer_list<value_type> __l)
301 this->insert(__l.begin(), __l.end());
307 * @brief A standard container composed of equivalent keys
308 * (possibly containing multiple of each key value) that associates
309 * values of another type with the keys.
311 * @ingroup unordered_associative_containers
313 * Meets the requirements of a <a href="tables.html#65">container</a>, and
314 * <a href="tables.html#xx">unordered associative container</a>
316 * @param Key Type of key objects.
317 * @param Tp Type of mapped objects.
318 * @param Hash Hashing function object type, defaults to hash<Value>.
319 * @param Pred Predicate function object type, defaults to equal_to<Value>.
320 * @param Alloc Allocator type, defaults to allocator<Key>.
322 * The resulting value type of the container is std::pair<const Key, Tp>.
324 template<class _Key, class _Tp,
325 class _Hash = hash<_Key>,
326 class _Pred = std::equal_to<_Key>,
327 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
328 class unordered_multimap
329 : public __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>
331 typedef __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc> _Base;
334 typedef typename _Base::value_type value_type;
335 typedef typename _Base::size_type size_type;
336 typedef typename _Base::hasher hasher;
337 typedef typename _Base::key_equal key_equal;
338 typedef typename _Base::allocator_type allocator_type;
341 unordered_multimap(size_type __n = 10,
342 const hasher& __hf = hasher(),
343 const key_equal& __eql = key_equal(),
344 const allocator_type& __a = allocator_type())
345 : _Base(__n, __hf, __eql, __a)
348 template<typename _InputIterator>
349 unordered_multimap(_InputIterator __f, _InputIterator __l,
351 const hasher& __hf = hasher(),
352 const key_equal& __eql = key_equal(),
353 const allocator_type& __a = allocator_type())
354 : _Base(__f, __l, __n, __hf, __eql, __a)
357 unordered_multimap(initializer_list<value_type> __l,
359 const hasher& __hf = hasher(),
360 const key_equal& __eql = key_equal(),
361 const allocator_type& __a = allocator_type())
362 : _Base(__l.begin(), __l.end(), __n, __hf, __eql, __a)
366 operator=(initializer_list<value_type> __l)
369 this->insert(__l.begin(), __l.end());
374 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
376 swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
377 unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
380 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
382 swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
383 unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
386 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
388 operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
389 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
390 { return __x._M_equal(__y); }
392 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
394 operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
395 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
396 { return !(__x == __y); }
398 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
400 operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
401 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
402 { return __x._M_equal(__y); }
404 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
406 operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
407 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
408 { return !(__x == __y); }
410 _GLIBCXX_END_NAMESPACE_CONTAINER
413 #endif /* _UNORDERED_MAP_H */