1 // unordered_map implementation -*- C++ -*-
3 // Copyright (C) 2010 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 * You should not attempt to use it directly.
30 #ifndef _UNORDERED_MAP_H
31 #define _UNORDERED_MAP_H
33 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
35 // XXX When we get typedef templates these class definitions
36 // will be unnecessary.
37 template<class _Key, class _Tp,
38 class _Hash = hash<_Key>,
39 class _Pred = std::equal_to<_Key>,
40 class _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
41 bool __cache_hash_code = false>
43 : public _Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc,
44 std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
45 _Hash, __detail::_Mod_range_hashing,
46 __detail::_Default_ranged_hash,
47 __detail::_Prime_rehash_policy,
48 __cache_hash_code, false, true>
50 typedef _Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc,
51 std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
52 _Hash, __detail::_Mod_range_hashing,
53 __detail::_Default_ranged_hash,
54 __detail::_Prime_rehash_policy,
55 __cache_hash_code, false, true>
59 typedef typename _Base::size_type size_type;
60 typedef typename _Base::hasher hasher;
61 typedef typename _Base::key_equal key_equal;
62 typedef typename _Base::allocator_type allocator_type;
65 __unordered_map(size_type __n = 10,
66 const hasher& __hf = hasher(),
67 const key_equal& __eql = key_equal(),
68 const allocator_type& __a = allocator_type())
69 : _Base(__n, __hf, __detail::_Mod_range_hashing(),
70 __detail::_Default_ranged_hash(),
71 __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
74 template<typename _InputIterator>
75 __unordered_map(_InputIterator __f, _InputIterator __l,
77 const hasher& __hf = hasher(),
78 const key_equal& __eql = key_equal(),
79 const allocator_type& __a = allocator_type())
80 : _Base(__f, __l, __n, __hf, __detail::_Mod_range_hashing(),
81 __detail::_Default_ranged_hash(),
82 __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
85 __unordered_map(const __unordered_map& __x) = default;
87 __unordered_map(__unordered_map&& __x)
88 : _Base(std::move(__x)) { }
91 template<class _Key, class _Tp,
92 class _Hash = hash<_Key>,
93 class _Pred = std::equal_to<_Key>,
94 class _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
95 bool __cache_hash_code = false>
96 class __unordered_multimap
97 : public _Hashtable<_Key, std::pair<const _Key, _Tp>,
99 std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
100 _Hash, __detail::_Mod_range_hashing,
101 __detail::_Default_ranged_hash,
102 __detail::_Prime_rehash_policy,
103 __cache_hash_code, false, false>
105 typedef _Hashtable<_Key, std::pair<const _Key, _Tp>,
107 std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
108 _Hash, __detail::_Mod_range_hashing,
109 __detail::_Default_ranged_hash,
110 __detail::_Prime_rehash_policy,
111 __cache_hash_code, false, false>
115 typedef typename _Base::size_type size_type;
116 typedef typename _Base::hasher hasher;
117 typedef typename _Base::key_equal key_equal;
118 typedef typename _Base::allocator_type allocator_type;
121 __unordered_multimap(size_type __n = 10,
122 const hasher& __hf = hasher(),
123 const key_equal& __eql = key_equal(),
124 const allocator_type& __a = allocator_type())
125 : _Base(__n, __hf, __detail::_Mod_range_hashing(),
126 __detail::_Default_ranged_hash(),
127 __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
131 template<typename _InputIterator>
132 __unordered_multimap(_InputIterator __f, _InputIterator __l,
133 typename _Base::size_type __n = 0,
134 const hasher& __hf = hasher(),
135 const key_equal& __eql = key_equal(),
136 const allocator_type& __a = allocator_type())
137 : _Base(__f, __l, __n, __hf, __detail::_Mod_range_hashing(),
138 __detail::_Default_ranged_hash(),
139 __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
142 __unordered_multimap(const __unordered_multimap& __x) = default;
144 __unordered_multimap(__unordered_multimap&& __x)
145 : _Base(std::move(__x)) { }
148 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
149 bool __cache_hash_code>
151 swap(__unordered_map<_Key, _Tp, _Hash, _Pred,
152 _Alloc, __cache_hash_code>& __x,
153 __unordered_map<_Key, _Tp, _Hash, _Pred,
154 _Alloc, __cache_hash_code>& __y)
157 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
158 bool __cache_hash_code>
160 swap(__unordered_multimap<_Key, _Tp, _Hash, _Pred,
161 _Alloc, __cache_hash_code>& __x,
162 __unordered_multimap<_Key, _Tp, _Hash, _Pred,
163 _Alloc, __cache_hash_code>& __y)
166 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
167 bool __cache_hash_code>
169 operator==(const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
170 __cache_hash_code>& __x,
171 const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
172 __cache_hash_code>& __y)
173 { return __x._M_equal(__y); }
175 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
176 bool __cache_hash_code>
178 operator!=(const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
179 __cache_hash_code>& __x,
180 const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
181 __cache_hash_code>& __y)
182 { return !(__x == __y); }
184 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
185 bool __cache_hash_code>
187 operator==(const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
188 __cache_hash_code>& __x,
189 const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
190 __cache_hash_code>& __y)
191 { return __x._M_equal(__y); }
193 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
194 bool __cache_hash_code>
196 operator!=(const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
197 __cache_hash_code>& __x,
198 const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
199 __cache_hash_code>& __y)
200 { return !(__x == __y); }
203 * @brief A standard container composed of unique keys (containing
204 * at most one of each key value) that associates values of another type
207 * @ingroup unordered_associative_containers
209 * Meets the requirements of a <a href="tables.html#65">container</a>, and
210 * <a href="tables.html#xx">unordered associative container</a>
212 * @param Key Type of key objects.
213 * @param Tp Type of mapped objects.
214 * @param Hash Hashing function object type, defaults to hash<Value>.
215 * @param Pred Predicate function object type, defaults to equal_to<Value>.
216 * @param Alloc Allocator type, defaults to allocator<Key>.
218 * The resulting value type of the container is std::pair<const Key, Tp>.
220 template<class _Key, class _Tp,
221 class _Hash = hash<_Key>,
222 class _Pred = std::equal_to<_Key>,
223 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
225 : public __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>
227 typedef __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc> _Base;
230 typedef typename _Base::value_type value_type;
231 typedef typename _Base::size_type size_type;
232 typedef typename _Base::hasher hasher;
233 typedef typename _Base::key_equal key_equal;
234 typedef typename _Base::allocator_type allocator_type;
237 unordered_map(size_type __n = 10,
238 const hasher& __hf = hasher(),
239 const key_equal& __eql = key_equal(),
240 const allocator_type& __a = allocator_type())
241 : _Base(__n, __hf, __eql, __a)
244 template<typename _InputIterator>
245 unordered_map(_InputIterator __f, _InputIterator __l,
247 const hasher& __hf = hasher(),
248 const key_equal& __eql = key_equal(),
249 const allocator_type& __a = allocator_type())
250 : _Base(__f, __l, __n, __hf, __eql, __a)
253 unordered_map(const unordered_map& __x) = default;
255 unordered_map(unordered_map&& __x)
256 : _Base(std::move(__x)) { }
258 unordered_map(initializer_list<value_type> __l,
260 const hasher& __hf = hasher(),
261 const key_equal& __eql = key_equal(),
262 const allocator_type& __a = allocator_type())
263 : _Base(__l.begin(), __l.end(), __n, __hf, __eql, __a)
267 operator=(const unordered_map& __x) = default;
270 operator=(unordered_map&& __x)
280 operator=(initializer_list<value_type> __l)
283 this->insert(__l.begin(), __l.end());
289 * @brief A standard container composed of equivalent keys
290 * (possibly containing multiple of each key value) that associates
291 * values of another type with the keys.
293 * @ingroup unordered_associative_containers
295 * Meets the requirements of a <a href="tables.html#65">container</a>, and
296 * <a href="tables.html#xx">unordered associative container</a>
298 * @param Key Type of key objects.
299 * @param Tp Type of mapped objects.
300 * @param Hash Hashing function object type, defaults to hash<Value>.
301 * @param Pred Predicate function object type, defaults to equal_to<Value>.
302 * @param Alloc Allocator type, defaults to allocator<Key>.
304 * The resulting value type of the container is std::pair<const Key, Tp>.
306 template<class _Key, class _Tp,
307 class _Hash = hash<_Key>,
308 class _Pred = std::equal_to<_Key>,
309 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
310 class unordered_multimap
311 : public __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>
313 typedef __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc> _Base;
316 typedef typename _Base::value_type value_type;
317 typedef typename _Base::size_type size_type;
318 typedef typename _Base::hasher hasher;
319 typedef typename _Base::key_equal key_equal;
320 typedef typename _Base::allocator_type allocator_type;
323 unordered_multimap(size_type __n = 10,
324 const hasher& __hf = hasher(),
325 const key_equal& __eql = key_equal(),
326 const allocator_type& __a = allocator_type())
327 : _Base(__n, __hf, __eql, __a)
331 template<typename _InputIterator>
332 unordered_multimap(_InputIterator __f, _InputIterator __l,
333 typename _Base::size_type __n = 0,
334 const hasher& __hf = hasher(),
335 const key_equal& __eql = key_equal(),
336 const allocator_type& __a = allocator_type())
337 : _Base(__f, __l, __n, __hf, __eql, __a)
340 unordered_multimap(const unordered_multimap& __x) = default;
342 unordered_multimap(unordered_multimap&& __x)
343 : _Base(std::move(__x)) { }
345 unordered_multimap(initializer_list<value_type> __l,
347 const hasher& __hf = hasher(),
348 const key_equal& __eql = key_equal(),
349 const allocator_type& __a = allocator_type())
350 : _Base(__l.begin(), __l.end(), __n, __hf, __eql, __a)
354 operator=(const unordered_multimap& __x) = default;
357 operator=(unordered_multimap&& __x)
367 operator=(initializer_list<value_type> __l)
370 this->insert(__l.begin(), __l.end());
375 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
377 swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
378 unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
381 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
383 swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
384 unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
387 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
389 operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
390 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
391 { return __x._M_equal(__y); }
393 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
395 operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
396 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
397 { return !(__x == __y); }
399 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
401 operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
402 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
403 { return __x._M_equal(__y); }
405 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
407 operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
408 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
409 { return !(__x == __y); }
411 _GLIBCXX_END_NESTED_NAMESPACE
413 #endif /* _UNORDERED_MAP_H */