2017-09-26 Thomas Koenig <tkoenig@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / bits / unordered_map.h
blobdf1302c80c0ae68086e67e09f42ad71ad0508fbb
1 // unordered_map implementation -*- C++ -*-
3 // Copyright (C) 2010-2017 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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_VERSION
36 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
38 /// Base types for unordered_map.
39 template<bool _Cache>
40 using __umap_traits = __detail::_Hashtable_traits<_Cache, false, true>;
42 template<typename _Key,
43 typename _Tp,
44 typename _Hash = hash<_Key>,
45 typename _Pred = std::equal_to<_Key>,
46 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
47 typename _Tr = __umap_traits<__cache_default<_Key, _Hash>::value>>
48 using __umap_hashtable = _Hashtable<_Key, std::pair<const _Key, _Tp>,
49 _Alloc, __detail::_Select1st,
50 _Pred, _Hash,
51 __detail::_Mod_range_hashing,
52 __detail::_Default_ranged_hash,
53 __detail::_Prime_rehash_policy, _Tr>;
55 /// Base types for unordered_multimap.
56 template<bool _Cache>
57 using __ummap_traits = __detail::_Hashtable_traits<_Cache, false, false>;
59 template<typename _Key,
60 typename _Tp,
61 typename _Hash = hash<_Key>,
62 typename _Pred = std::equal_to<_Key>,
63 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
64 typename _Tr = __ummap_traits<__cache_default<_Key, _Hash>::value>>
65 using __ummap_hashtable = _Hashtable<_Key, std::pair<const _Key, _Tp>,
66 _Alloc, __detail::_Select1st,
67 _Pred, _Hash,
68 __detail::_Mod_range_hashing,
69 __detail::_Default_ranged_hash,
70 __detail::_Prime_rehash_policy, _Tr>;
72 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
73 class unordered_multimap;
75 /**
76 * @brief A standard container composed of unique keys (containing
77 * at most one of each key value) that associates values of another type
78 * with the keys.
80 * @ingroup unordered_associative_containers
82 * @tparam _Key Type of key objects.
83 * @tparam _Tp Type of mapped objects.
84 * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
85 * @tparam _Pred Predicate function object type, defaults
86 * to equal_to<_Value>.
87 * @tparam _Alloc Allocator type, defaults to
88 * std::allocator<std::pair<const _Key, _Tp>>.
90 * Meets the requirements of a <a href="tables.html#65">container</a>, and
91 * <a href="tables.html#xx">unordered associative container</a>
93 * The resulting value type of the container is std::pair<const _Key, _Tp>.
95 * Base is _Hashtable, dispatched at compile time via template
96 * alias __umap_hashtable.
98 template<class _Key, class _Tp,
99 class _Hash = hash<_Key>,
100 class _Pred = std::equal_to<_Key>,
101 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
102 class unordered_map
104 typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
105 _Hashtable _M_h;
107 public:
108 // typedefs:
109 //@{
110 /// Public typedefs.
111 typedef typename _Hashtable::key_type key_type;
112 typedef typename _Hashtable::value_type value_type;
113 typedef typename _Hashtable::mapped_type mapped_type;
114 typedef typename _Hashtable::hasher hasher;
115 typedef typename _Hashtable::key_equal key_equal;
116 typedef typename _Hashtable::allocator_type allocator_type;
117 //@}
119 //@{
120 /// Iterator-related typedefs.
121 typedef typename _Hashtable::pointer pointer;
122 typedef typename _Hashtable::const_pointer const_pointer;
123 typedef typename _Hashtable::reference reference;
124 typedef typename _Hashtable::const_reference const_reference;
125 typedef typename _Hashtable::iterator iterator;
126 typedef typename _Hashtable::const_iterator const_iterator;
127 typedef typename _Hashtable::local_iterator local_iterator;
128 typedef typename _Hashtable::const_local_iterator const_local_iterator;
129 typedef typename _Hashtable::size_type size_type;
130 typedef typename _Hashtable::difference_type difference_type;
131 //@}
133 #if __cplusplus > 201402L
134 using node_type = typename _Hashtable::node_type;
135 using insert_return_type = typename _Hashtable::insert_return_type;
136 #endif
138 //construct/destroy/copy
140 /// Default constructor.
141 unordered_map() = default;
144 * @brief Default constructor creates no elements.
145 * @param __n Minimal initial number of buckets.
146 * @param __hf A hash functor.
147 * @param __eql A key equality functor.
148 * @param __a An allocator object.
150 explicit
151 unordered_map(size_type __n,
152 const hasher& __hf = hasher(),
153 const key_equal& __eql = key_equal(),
154 const allocator_type& __a = allocator_type())
155 : _M_h(__n, __hf, __eql, __a)
159 * @brief Builds an %unordered_map from a range.
160 * @param __first An input iterator.
161 * @param __last An input iterator.
162 * @param __n Minimal initial number of buckets.
163 * @param __hf A hash functor.
164 * @param __eql A key equality functor.
165 * @param __a An allocator object.
167 * Create an %unordered_map consisting of copies of the elements from
168 * [__first,__last). This is linear in N (where N is
169 * distance(__first,__last)).
171 template<typename _InputIterator>
172 unordered_map(_InputIterator __first, _InputIterator __last,
173 size_type __n = 0,
174 const hasher& __hf = hasher(),
175 const key_equal& __eql = key_equal(),
176 const allocator_type& __a = allocator_type())
177 : _M_h(__first, __last, __n, __hf, __eql, __a)
180 /// Copy constructor.
181 unordered_map(const unordered_map&) = default;
183 /// Move constructor.
184 unordered_map(unordered_map&&) = default;
187 * @brief Creates an %unordered_map with no elements.
188 * @param __a An allocator object.
190 explicit
191 unordered_map(const allocator_type& __a)
192 : _M_h(__a)
196 * @brief Copy constructor with allocator argument.
197 * @param __uset Input %unordered_map to copy.
198 * @param __a An allocator object.
200 unordered_map(const unordered_map& __umap,
201 const allocator_type& __a)
202 : _M_h(__umap._M_h, __a)
206 * @brief Move constructor with allocator argument.
207 * @param __uset Input %unordered_map to move.
208 * @param __a An allocator object.
210 unordered_map(unordered_map&& __umap,
211 const allocator_type& __a)
212 : _M_h(std::move(__umap._M_h), __a)
216 * @brief Builds an %unordered_map from an initializer_list.
217 * @param __l An initializer_list.
218 * @param __n Minimal initial number of buckets.
219 * @param __hf A hash functor.
220 * @param __eql A key equality functor.
221 * @param __a An allocator object.
223 * Create an %unordered_map consisting of copies of the elements in the
224 * list. This is linear in N (where N is @a __l.size()).
226 unordered_map(initializer_list<value_type> __l,
227 size_type __n = 0,
228 const hasher& __hf = hasher(),
229 const key_equal& __eql = key_equal(),
230 const allocator_type& __a = allocator_type())
231 : _M_h(__l, __n, __hf, __eql, __a)
234 unordered_map(size_type __n, const allocator_type& __a)
235 : unordered_map(__n, hasher(), key_equal(), __a)
238 unordered_map(size_type __n, const hasher& __hf,
239 const allocator_type& __a)
240 : unordered_map(__n, __hf, key_equal(), __a)
243 template<typename _InputIterator>
244 unordered_map(_InputIterator __first, _InputIterator __last,
245 size_type __n,
246 const allocator_type& __a)
247 : unordered_map(__first, __last, __n, hasher(), key_equal(), __a)
250 template<typename _InputIterator>
251 unordered_map(_InputIterator __first, _InputIterator __last,
252 size_type __n, const hasher& __hf,
253 const allocator_type& __a)
254 : unordered_map(__first, __last, __n, __hf, key_equal(), __a)
257 unordered_map(initializer_list<value_type> __l,
258 size_type __n,
259 const allocator_type& __a)
260 : unordered_map(__l, __n, hasher(), key_equal(), __a)
263 unordered_map(initializer_list<value_type> __l,
264 size_type __n, const hasher& __hf,
265 const allocator_type& __a)
266 : unordered_map(__l, __n, __hf, key_equal(), __a)
269 /// Copy assignment operator.
270 unordered_map&
271 operator=(const unordered_map&) = default;
273 /// Move assignment operator.
274 unordered_map&
275 operator=(unordered_map&&) = default;
278 * @brief %Unordered_map list assignment operator.
279 * @param __l An initializer_list.
281 * This function fills an %unordered_map with copies of the elements in
282 * the initializer list @a __l.
284 * Note that the assignment completely changes the %unordered_map and
285 * that the resulting %unordered_map's size is the same as the number
286 * of elements assigned.
288 unordered_map&
289 operator=(initializer_list<value_type> __l)
291 _M_h = __l;
292 return *this;
295 /// Returns the allocator object used by the %unordered_map.
296 allocator_type
297 get_allocator() const noexcept
298 { return _M_h.get_allocator(); }
300 // size and capacity:
302 /// Returns true if the %unordered_map is empty.
303 bool
304 empty() const noexcept
305 { return _M_h.empty(); }
307 /// Returns the size of the %unordered_map.
308 size_type
309 size() const noexcept
310 { return _M_h.size(); }
312 /// Returns the maximum size of the %unordered_map.
313 size_type
314 max_size() const noexcept
315 { return _M_h.max_size(); }
317 // iterators.
320 * Returns a read/write iterator that points to the first element in the
321 * %unordered_map.
323 iterator
324 begin() noexcept
325 { return _M_h.begin(); }
327 //@{
329 * Returns a read-only (constant) iterator that points to the first
330 * element in the %unordered_map.
332 const_iterator
333 begin() const noexcept
334 { return _M_h.begin(); }
336 const_iterator
337 cbegin() const noexcept
338 { return _M_h.begin(); }
339 //@}
342 * Returns a read/write iterator that points one past the last element in
343 * the %unordered_map.
345 iterator
346 end() noexcept
347 { return _M_h.end(); }
349 //@{
351 * Returns a read-only (constant) iterator that points one past the last
352 * element in the %unordered_map.
354 const_iterator
355 end() const noexcept
356 { return _M_h.end(); }
358 const_iterator
359 cend() const noexcept
360 { return _M_h.end(); }
361 //@}
363 // modifiers.
366 * @brief Attempts to build and insert a std::pair into the
367 * %unordered_map.
369 * @param __args Arguments used to generate a new pair instance (see
370 * std::piecewise_contruct for passing arguments to each
371 * part of the pair constructor).
373 * @return A pair, of which the first element is an iterator that points
374 * to the possibly inserted pair, and the second is a bool that
375 * is true if the pair was actually inserted.
377 * This function attempts to build and insert a (key, value) %pair into
378 * the %unordered_map.
379 * An %unordered_map relies on unique keys and thus a %pair is only
380 * inserted if its first element (the key) is not already present in the
381 * %unordered_map.
383 * Insertion requires amortized constant time.
385 template<typename... _Args>
386 std::pair<iterator, bool>
387 emplace(_Args&&... __args)
388 { return _M_h.emplace(std::forward<_Args>(__args)...); }
391 * @brief Attempts to build and insert a std::pair into the
392 * %unordered_map.
394 * @param __pos An iterator that serves as a hint as to where the pair
395 * should be inserted.
396 * @param __args Arguments used to generate a new pair instance (see
397 * std::piecewise_contruct for passing arguments to each
398 * part of the pair constructor).
399 * @return An iterator that points to the element with key of the
400 * std::pair built from @a __args (may or may not be that
401 * std::pair).
403 * This function is not concerned about whether the insertion took place,
404 * and thus does not return a boolean like the single-argument emplace()
405 * does.
406 * Note that the first parameter is only a hint and can potentially
407 * improve the performance of the insertion process. A bad hint would
408 * cause no gains in efficiency.
410 * See
411 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
412 * for more on @a hinting.
414 * Insertion requires amortized constant time.
416 template<typename... _Args>
417 iterator
418 emplace_hint(const_iterator __pos, _Args&&... __args)
419 { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
421 #if __cplusplus > 201402L
422 /// Extract a node.
423 node_type
424 extract(const_iterator __pos)
426 __glibcxx_assert(__pos != end());
427 return _M_h.extract(__pos);
430 /// Extract a node.
431 node_type
432 extract(const key_type& __key)
433 { return _M_h.extract(__key); }
435 /// Re-insert an extracted node.
436 insert_return_type
437 insert(node_type&& __nh)
438 { return _M_h._M_reinsert_node(std::move(__nh)); }
440 /// Re-insert an extracted node.
441 iterator
442 insert(const_iterator, node_type&& __nh)
443 { return _M_h._M_reinsert_node(std::move(__nh)).position; }
445 #define __cpp_lib_unordered_map_try_emplace 201411
447 * @brief Attempts to build and insert a std::pair into the
448 * %unordered_map.
450 * @param __k Key to use for finding a possibly existing pair in
451 * the unordered_map.
452 * @param __args Arguments used to generate the .second for a
453 * new pair instance.
455 * @return A pair, of which the first element is an iterator that points
456 * to the possibly inserted pair, and the second is a bool that
457 * is true if the pair was actually inserted.
459 * This function attempts to build and insert a (key, value) %pair into
460 * the %unordered_map.
461 * An %unordered_map relies on unique keys and thus a %pair is only
462 * inserted if its first element (the key) is not already present in the
463 * %unordered_map.
464 * If a %pair is not inserted, this function has no effect.
466 * Insertion requires amortized constant time.
468 template <typename... _Args>
469 pair<iterator, bool>
470 try_emplace(const key_type& __k, _Args&&... __args)
472 iterator __i = find(__k);
473 if (__i == end())
475 __i = emplace(std::piecewise_construct,
476 std::forward_as_tuple(__k),
477 std::forward_as_tuple(
478 std::forward<_Args>(__args)...))
479 .first;
480 return {__i, true};
482 return {__i, false};
485 // move-capable overload
486 template <typename... _Args>
487 pair<iterator, bool>
488 try_emplace(key_type&& __k, _Args&&... __args)
490 iterator __i = find(__k);
491 if (__i == end())
493 __i = emplace(std::piecewise_construct,
494 std::forward_as_tuple(std::move(__k)),
495 std::forward_as_tuple(
496 std::forward<_Args>(__args)...))
497 .first;
498 return {__i, true};
500 return {__i, false};
504 * @brief Attempts to build and insert a std::pair into the
505 * %unordered_map.
507 * @param __hint An iterator that serves as a hint as to where the pair
508 * should be inserted.
509 * @param __k Key to use for finding a possibly existing pair in
510 * the unordered_map.
511 * @param __args Arguments used to generate the .second for a
512 * new pair instance.
513 * @return An iterator that points to the element with key of the
514 * std::pair built from @a __args (may or may not be that
515 * std::pair).
517 * This function is not concerned about whether the insertion took place,
518 * and thus does not return a boolean like the single-argument emplace()
519 * does. However, if insertion did not take place,
520 * this function has no effect.
521 * Note that the first parameter is only a hint and can potentially
522 * improve the performance of the insertion process. A bad hint would
523 * cause no gains in efficiency.
525 * See
526 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
527 * for more on @a hinting.
529 * Insertion requires amortized constant time.
531 template <typename... _Args>
532 iterator
533 try_emplace(const_iterator __hint, const key_type& __k,
534 _Args&&... __args)
536 iterator __i = find(__k);
537 if (__i == end())
538 __i = emplace_hint(__hint, std::piecewise_construct,
539 std::forward_as_tuple(__k),
540 std::forward_as_tuple(
541 std::forward<_Args>(__args)...));
542 return __i;
545 // move-capable overload
546 template <typename... _Args>
547 iterator
548 try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
550 iterator __i = find(__k);
551 if (__i == end())
552 __i = emplace_hint(__hint, std::piecewise_construct,
553 std::forward_as_tuple(std::move(__k)),
554 std::forward_as_tuple(
555 std::forward<_Args>(__args)...));
556 return __i;
558 #endif // C++17
560 //@{
562 * @brief Attempts to insert a std::pair into the %unordered_map.
564 * @param __x Pair to be inserted (see std::make_pair for easy
565 * creation of pairs).
567 * @return A pair, of which the first element is an iterator that
568 * points to the possibly inserted pair, and the second is
569 * a bool that is true if the pair was actually inserted.
571 * This function attempts to insert a (key, value) %pair into the
572 * %unordered_map. An %unordered_map relies on unique keys and thus a
573 * %pair is only inserted if its first element (the key) is not already
574 * present in the %unordered_map.
576 * Insertion requires amortized constant time.
578 std::pair<iterator, bool>
579 insert(const value_type& __x)
580 { return _M_h.insert(__x); }
582 template<typename _Pair, typename = typename
583 std::enable_if<std::is_constructible<value_type,
584 _Pair&&>::value>::type>
585 std::pair<iterator, bool>
586 insert(_Pair&& __x)
587 { return _M_h.insert(std::forward<_Pair>(__x)); }
588 //@}
590 //@{
592 * @brief Attempts to insert a std::pair into the %unordered_map.
593 * @param __hint An iterator that serves as a hint as to where the
594 * pair should be inserted.
595 * @param __x Pair to be inserted (see std::make_pair for easy creation
596 * of pairs).
597 * @return An iterator that points to the element with key of
598 * @a __x (may or may not be the %pair passed in).
600 * This function is not concerned about whether the insertion took place,
601 * and thus does not return a boolean like the single-argument insert()
602 * does. Note that the first parameter is only a hint and can
603 * potentially improve the performance of the insertion process. A bad
604 * hint would cause no gains in efficiency.
606 * See
607 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
608 * for more on @a hinting.
610 * Insertion requires amortized constant time.
612 iterator
613 insert(const_iterator __hint, const value_type& __x)
614 { return _M_h.insert(__hint, __x); }
616 template<typename _Pair, typename = typename
617 std::enable_if<std::is_constructible<value_type,
618 _Pair&&>::value>::type>
619 iterator
620 insert(const_iterator __hint, _Pair&& __x)
621 { return _M_h.insert(__hint, std::forward<_Pair>(__x)); }
622 //@}
625 * @brief A template function that attempts to insert a range of
626 * elements.
627 * @param __first Iterator pointing to the start of the range to be
628 * inserted.
629 * @param __last Iterator pointing to the end of the range.
631 * Complexity similar to that of the range constructor.
633 template<typename _InputIterator>
634 void
635 insert(_InputIterator __first, _InputIterator __last)
636 { _M_h.insert(__first, __last); }
639 * @brief Attempts to insert a list of elements into the %unordered_map.
640 * @param __l A std::initializer_list<value_type> of elements
641 * to be inserted.
643 * Complexity similar to that of the range constructor.
645 void
646 insert(initializer_list<value_type> __l)
647 { _M_h.insert(__l); }
650 #if __cplusplus > 201402L
651 #define __cpp_lib_unordered_map_insertion 201411
653 * @brief Attempts to insert a std::pair into the %unordered_map.
654 * @param __k Key to use for finding a possibly existing pair in
655 * the map.
656 * @param __obj Argument used to generate the .second for a pair
657 * instance.
659 * @return A pair, of which the first element is an iterator that
660 * points to the possibly inserted pair, and the second is
661 * a bool that is true if the pair was actually inserted.
663 * This function attempts to insert a (key, value) %pair into the
664 * %unordered_map. An %unordered_map relies on unique keys and thus a
665 * %pair is only inserted if its first element (the key) is not already
666 * present in the %unordered_map.
667 * If the %pair was already in the %unordered_map, the .second of
668 * the %pair is assigned from __obj.
670 * Insertion requires amortized constant time.
672 template <typename _Obj>
673 pair<iterator, bool>
674 insert_or_assign(const key_type& __k, _Obj&& __obj)
676 iterator __i = find(__k);
677 if (__i == end())
679 __i = emplace(std::piecewise_construct,
680 std::forward_as_tuple(__k),
681 std::forward_as_tuple(std::forward<_Obj>(__obj)))
682 .first;
683 return {__i, true};
685 (*__i).second = std::forward<_Obj>(__obj);
686 return {__i, false};
689 // move-capable overload
690 template <typename _Obj>
691 pair<iterator, bool>
692 insert_or_assign(key_type&& __k, _Obj&& __obj)
694 iterator __i = find(__k);
695 if (__i == end())
697 __i = emplace(std::piecewise_construct,
698 std::forward_as_tuple(std::move(__k)),
699 std::forward_as_tuple(std::forward<_Obj>(__obj)))
700 .first;
701 return {__i, true};
703 (*__i).second = std::forward<_Obj>(__obj);
704 return {__i, false};
708 * @brief Attempts to insert a std::pair into the %unordered_map.
709 * @param __hint An iterator that serves as a hint as to where the
710 * pair should be inserted.
711 * @param __k Key to use for finding a possibly existing pair in
712 * the unordered_map.
713 * @param __obj Argument used to generate the .second for a pair
714 * instance.
715 * @return An iterator that points to the element with key of
716 * @a __x (may or may not be the %pair passed in).
718 * This function is not concerned about whether the insertion took place,
719 * and thus does not return a boolean like the single-argument insert()
720 * does.
721 * If the %pair was already in the %unordered map, the .second of
722 * the %pair is assigned from __obj.
723 * Note that the first parameter is only a hint and can
724 * potentially improve the performance of the insertion process. A bad
725 * hint would cause no gains in efficiency.
727 * See
728 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
729 * for more on @a hinting.
731 * Insertion requires amortized constant time.
733 template <typename _Obj>
734 iterator
735 insert_or_assign(const_iterator __hint, const key_type& __k,
736 _Obj&& __obj)
738 iterator __i = find(__k);
739 if (__i == end())
741 return emplace_hint(__hint, std::piecewise_construct,
742 std::forward_as_tuple(__k),
743 std::forward_as_tuple(
744 std::forward<_Obj>(__obj)));
746 (*__i).second = std::forward<_Obj>(__obj);
747 return __i;
750 // move-capable overload
751 template <typename _Obj>
752 iterator
753 insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
755 iterator __i = find(__k);
756 if (__i == end())
758 return emplace_hint(__hint, std::piecewise_construct,
759 std::forward_as_tuple(std::move(__k)),
760 std::forward_as_tuple(
761 std::forward<_Obj>(__obj)));
763 (*__i).second = std::forward<_Obj>(__obj);
764 return __i;
766 #endif
768 //@{
770 * @brief Erases an element from an %unordered_map.
771 * @param __position An iterator pointing to the element to be erased.
772 * @return An iterator pointing to the element immediately following
773 * @a __position prior to the element being erased. If no such
774 * element exists, end() is returned.
776 * This function erases an element, pointed to by the given iterator,
777 * from an %unordered_map.
778 * Note that this function only erases the element, and that if the
779 * element is itself a pointer, the pointed-to memory is not touched in
780 * any way. Managing the pointer is the user's responsibility.
782 iterator
783 erase(const_iterator __position)
784 { return _M_h.erase(__position); }
786 // LWG 2059.
787 iterator
788 erase(iterator __position)
789 { return _M_h.erase(__position); }
790 //@}
793 * @brief Erases elements according to the provided key.
794 * @param __x Key of element to be erased.
795 * @return The number of elements erased.
797 * This function erases all the elements located by the given key from
798 * an %unordered_map. For an %unordered_map the result of this function
799 * can only be 0 (not present) or 1 (present).
800 * Note that this function only erases the element, and that if the
801 * element is itself a pointer, the pointed-to memory is not touched in
802 * any way. Managing the pointer is the user's responsibility.
804 size_type
805 erase(const key_type& __x)
806 { return _M_h.erase(__x); }
809 * @brief Erases a [__first,__last) range of elements from an
810 * %unordered_map.
811 * @param __first Iterator pointing to the start of the range to be
812 * erased.
813 * @param __last Iterator pointing to the end of the range to
814 * be erased.
815 * @return The iterator @a __last.
817 * This function erases a sequence of elements from an %unordered_map.
818 * Note that this function only erases the elements, and that if
819 * the element is itself a pointer, the pointed-to memory is not touched
820 * in any way. Managing the pointer is the user's responsibility.
822 iterator
823 erase(const_iterator __first, const_iterator __last)
824 { return _M_h.erase(__first, __last); }
827 * Erases all elements in an %unordered_map.
828 * Note that this function only erases the elements, and that if the
829 * elements themselves are pointers, the pointed-to memory is not touched
830 * in any way. Managing the pointer is the user's responsibility.
832 void
833 clear() noexcept
834 { _M_h.clear(); }
837 * @brief Swaps data with another %unordered_map.
838 * @param __x An %unordered_map of the same element and allocator
839 * types.
841 * This exchanges the elements between two %unordered_map in constant
842 * time.
843 * Note that the global std::swap() function is specialized such that
844 * std::swap(m1,m2) will feed to this function.
846 void
847 swap(unordered_map& __x)
848 noexcept( noexcept(_M_h.swap(__x._M_h)) )
849 { _M_h.swap(__x._M_h); }
851 #if __cplusplus > 201402L
852 template<typename, typename, typename>
853 friend class _Hash_merge_helper;
855 template<typename _H2, typename _P2>
856 void
857 merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>& __source)
859 using _Merge_helper = _Hash_merge_helper<unordered_map, _H2, _P2>;
860 _M_h._M_merge_unique(_Merge_helper::_S_get_table(__source));
863 template<typename _H2, typename _P2>
864 void
865 merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
866 { merge(__source); }
868 template<typename _H2, typename _P2>
869 void
870 merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>& __source)
872 using _Merge_helper = _Hash_merge_helper<unordered_map, _H2, _P2>;
873 _M_h._M_merge_unique(_Merge_helper::_S_get_table(__source));
876 template<typename _H2, typename _P2>
877 void
878 merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
879 { merge(__source); }
880 #endif // C++17
882 // observers.
884 /// Returns the hash functor object with which the %unordered_map was
885 /// constructed.
886 hasher
887 hash_function() const
888 { return _M_h.hash_function(); }
890 /// Returns the key comparison object with which the %unordered_map was
891 /// constructed.
892 key_equal
893 key_eq() const
894 { return _M_h.key_eq(); }
896 // lookup.
898 //@{
900 * @brief Tries to locate an element in an %unordered_map.
901 * @param __x Key to be located.
902 * @return Iterator pointing to sought-after element, or end() if not
903 * found.
905 * This function takes a key and tries to locate the element with which
906 * the key matches. If successful the function returns an iterator
907 * pointing to the sought after element. If unsuccessful it returns the
908 * past-the-end ( @c end() ) iterator.
910 iterator
911 find(const key_type& __x)
912 { return _M_h.find(__x); }
914 const_iterator
915 find(const key_type& __x) const
916 { return _M_h.find(__x); }
917 //@}
920 * @brief Finds the number of elements.
921 * @param __x Key to count.
922 * @return Number of elements with specified key.
924 * This function only makes sense for %unordered_multimap; for
925 * %unordered_map the result will either be 0 (not present) or 1
926 * (present).
928 size_type
929 count(const key_type& __x) const
930 { return _M_h.count(__x); }
932 //@{
934 * @brief Finds a subsequence matching given key.
935 * @param __x Key to be located.
936 * @return Pair of iterators that possibly points to the subsequence
937 * matching given key.
939 * This function probably only makes sense for %unordered_multimap.
941 std::pair<iterator, iterator>
942 equal_range(const key_type& __x)
943 { return _M_h.equal_range(__x); }
945 std::pair<const_iterator, const_iterator>
946 equal_range(const key_type& __x) const
947 { return _M_h.equal_range(__x); }
948 //@}
950 //@{
952 * @brief Subscript ( @c [] ) access to %unordered_map data.
953 * @param __k The key for which data should be retrieved.
954 * @return A reference to the data of the (key,data) %pair.
956 * Allows for easy lookup with the subscript ( @c [] )operator. Returns
957 * data associated with the key specified in subscript. If the key does
958 * not exist, a pair with that key is created using default values, which
959 * is then returned.
961 * Lookup requires constant time.
963 mapped_type&
964 operator[](const key_type& __k)
965 { return _M_h[__k]; }
967 mapped_type&
968 operator[](key_type&& __k)
969 { return _M_h[std::move(__k)]; }
970 //@}
972 //@{
974 * @brief Access to %unordered_map data.
975 * @param __k The key for which data should be retrieved.
976 * @return A reference to the data whose key is equal to @a __k, if
977 * such a data is present in the %unordered_map.
978 * @throw std::out_of_range If no such data is present.
980 mapped_type&
981 at(const key_type& __k)
982 { return _M_h.at(__k); }
984 const mapped_type&
985 at(const key_type& __k) const
986 { return _M_h.at(__k); }
987 //@}
989 // bucket interface.
991 /// Returns the number of buckets of the %unordered_map.
992 size_type
993 bucket_count() const noexcept
994 { return _M_h.bucket_count(); }
996 /// Returns the maximum number of buckets of the %unordered_map.
997 size_type
998 max_bucket_count() const noexcept
999 { return _M_h.max_bucket_count(); }
1002 * @brief Returns the number of elements in a given bucket.
1003 * @param __n A bucket index.
1004 * @return The number of elements in the bucket.
1006 size_type
1007 bucket_size(size_type __n) const
1008 { return _M_h.bucket_size(__n); }
1011 * @brief Returns the bucket index of a given element.
1012 * @param __key A key instance.
1013 * @return The key bucket index.
1015 size_type
1016 bucket(const key_type& __key) const
1017 { return _M_h.bucket(__key); }
1020 * @brief Returns a read/write iterator pointing to the first bucket
1021 * element.
1022 * @param __n The bucket index.
1023 * @return A read/write local iterator.
1025 local_iterator
1026 begin(size_type __n)
1027 { return _M_h.begin(__n); }
1029 //@{
1031 * @brief Returns a read-only (constant) iterator pointing to the first
1032 * bucket element.
1033 * @param __n The bucket index.
1034 * @return A read-only local iterator.
1036 const_local_iterator
1037 begin(size_type __n) const
1038 { return _M_h.begin(__n); }
1040 const_local_iterator
1041 cbegin(size_type __n) const
1042 { return _M_h.cbegin(__n); }
1043 //@}
1046 * @brief Returns a read/write iterator pointing to one past the last
1047 * bucket elements.
1048 * @param __n The bucket index.
1049 * @return A read/write local iterator.
1051 local_iterator
1052 end(size_type __n)
1053 { return _M_h.end(__n); }
1055 //@{
1057 * @brief Returns a read-only (constant) iterator pointing to one past
1058 * the last bucket elements.
1059 * @param __n The bucket index.
1060 * @return A read-only local iterator.
1062 const_local_iterator
1063 end(size_type __n) const
1064 { return _M_h.end(__n); }
1066 const_local_iterator
1067 cend(size_type __n) const
1068 { return _M_h.cend(__n); }
1069 //@}
1071 // hash policy.
1073 /// Returns the average number of elements per bucket.
1074 float
1075 load_factor() const noexcept
1076 { return _M_h.load_factor(); }
1078 /// Returns a positive number that the %unordered_map tries to keep the
1079 /// load factor less than or equal to.
1080 float
1081 max_load_factor() const noexcept
1082 { return _M_h.max_load_factor(); }
1085 * @brief Change the %unordered_map maximum load factor.
1086 * @param __z The new maximum load factor.
1088 void
1089 max_load_factor(float __z)
1090 { _M_h.max_load_factor(__z); }
1093 * @brief May rehash the %unordered_map.
1094 * @param __n The new number of buckets.
1096 * Rehash will occur only if the new number of buckets respect the
1097 * %unordered_map maximum load factor.
1099 void
1100 rehash(size_type __n)
1101 { _M_h.rehash(__n); }
1104 * @brief Prepare the %unordered_map for a specified number of
1105 * elements.
1106 * @param __n Number of elements required.
1108 * Same as rehash(ceil(n / max_load_factor())).
1110 void
1111 reserve(size_type __n)
1112 { _M_h.reserve(__n); }
1114 template<typename _Key1, typename _Tp1, typename _Hash1, typename _Pred1,
1115 typename _Alloc1>
1116 friend bool
1117 operator==(const unordered_map<_Key1, _Tp1, _Hash1, _Pred1, _Alloc1>&,
1118 const unordered_map<_Key1, _Tp1, _Hash1, _Pred1, _Alloc1>&);
1122 * @brief A standard container composed of equivalent keys
1123 * (possibly containing multiple of each key value) that associates
1124 * values of another type with the keys.
1126 * @ingroup unordered_associative_containers
1128 * @tparam _Key Type of key objects.
1129 * @tparam _Tp Type of mapped objects.
1130 * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
1131 * @tparam _Pred Predicate function object type, defaults
1132 * to equal_to<_Value>.
1133 * @tparam _Alloc Allocator type, defaults to
1134 * std::allocator<std::pair<const _Key, _Tp>>.
1136 * Meets the requirements of a <a href="tables.html#65">container</a>, and
1137 * <a href="tables.html#xx">unordered associative container</a>
1139 * The resulting value type of the container is std::pair<const _Key, _Tp>.
1141 * Base is _Hashtable, dispatched at compile time via template
1142 * alias __ummap_hashtable.
1144 template<class _Key, class _Tp,
1145 class _Hash = hash<_Key>,
1146 class _Pred = std::equal_to<_Key>,
1147 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
1148 class unordered_multimap
1150 typedef __ummap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
1151 _Hashtable _M_h;
1153 public:
1154 // typedefs:
1155 //@{
1156 /// Public typedefs.
1157 typedef typename _Hashtable::key_type key_type;
1158 typedef typename _Hashtable::value_type value_type;
1159 typedef typename _Hashtable::mapped_type mapped_type;
1160 typedef typename _Hashtable::hasher hasher;
1161 typedef typename _Hashtable::key_equal key_equal;
1162 typedef typename _Hashtable::allocator_type allocator_type;
1163 //@}
1165 //@{
1166 /// Iterator-related typedefs.
1167 typedef typename _Hashtable::pointer pointer;
1168 typedef typename _Hashtable::const_pointer const_pointer;
1169 typedef typename _Hashtable::reference reference;
1170 typedef typename _Hashtable::const_reference const_reference;
1171 typedef typename _Hashtable::iterator iterator;
1172 typedef typename _Hashtable::const_iterator const_iterator;
1173 typedef typename _Hashtable::local_iterator local_iterator;
1174 typedef typename _Hashtable::const_local_iterator const_local_iterator;
1175 typedef typename _Hashtable::size_type size_type;
1176 typedef typename _Hashtable::difference_type difference_type;
1177 //@}
1179 #if __cplusplus > 201402L
1180 using node_type = typename _Hashtable::node_type;
1181 #endif
1183 //construct/destroy/copy
1185 /// Default constructor.
1186 unordered_multimap() = default;
1189 * @brief Default constructor creates no elements.
1190 * @param __n Mnimal initial number of buckets.
1191 * @param __hf A hash functor.
1192 * @param __eql A key equality functor.
1193 * @param __a An allocator object.
1195 explicit
1196 unordered_multimap(size_type __n,
1197 const hasher& __hf = hasher(),
1198 const key_equal& __eql = key_equal(),
1199 const allocator_type& __a = allocator_type())
1200 : _M_h(__n, __hf, __eql, __a)
1204 * @brief Builds an %unordered_multimap from a range.
1205 * @param __first An input iterator.
1206 * @param __last An input iterator.
1207 * @param __n Minimal initial number of buckets.
1208 * @param __hf A hash functor.
1209 * @param __eql A key equality functor.
1210 * @param __a An allocator object.
1212 * Create an %unordered_multimap consisting of copies of the elements
1213 * from [__first,__last). This is linear in N (where N is
1214 * distance(__first,__last)).
1216 template<typename _InputIterator>
1217 unordered_multimap(_InputIterator __first, _InputIterator __last,
1218 size_type __n = 0,
1219 const hasher& __hf = hasher(),
1220 const key_equal& __eql = key_equal(),
1221 const allocator_type& __a = allocator_type())
1222 : _M_h(__first, __last, __n, __hf, __eql, __a)
1225 /// Copy constructor.
1226 unordered_multimap(const unordered_multimap&) = default;
1228 /// Move constructor.
1229 unordered_multimap(unordered_multimap&&) = default;
1232 * @brief Creates an %unordered_multimap with no elements.
1233 * @param __a An allocator object.
1235 explicit
1236 unordered_multimap(const allocator_type& __a)
1237 : _M_h(__a)
1241 * @brief Copy constructor with allocator argument.
1242 * @param __uset Input %unordered_multimap to copy.
1243 * @param __a An allocator object.
1245 unordered_multimap(const unordered_multimap& __ummap,
1246 const allocator_type& __a)
1247 : _M_h(__ummap._M_h, __a)
1251 * @brief Move constructor with allocator argument.
1252 * @param __uset Input %unordered_multimap to move.
1253 * @param __a An allocator object.
1255 unordered_multimap(unordered_multimap&& __ummap,
1256 const allocator_type& __a)
1257 : _M_h(std::move(__ummap._M_h), __a)
1261 * @brief Builds an %unordered_multimap from an initializer_list.
1262 * @param __l An initializer_list.
1263 * @param __n Minimal initial number of buckets.
1264 * @param __hf A hash functor.
1265 * @param __eql A key equality functor.
1266 * @param __a An allocator object.
1268 * Create an %unordered_multimap consisting of copies of the elements in
1269 * the list. This is linear in N (where N is @a __l.size()).
1271 unordered_multimap(initializer_list<value_type> __l,
1272 size_type __n = 0,
1273 const hasher& __hf = hasher(),
1274 const key_equal& __eql = key_equal(),
1275 const allocator_type& __a = allocator_type())
1276 : _M_h(__l, __n, __hf, __eql, __a)
1279 unordered_multimap(size_type __n, const allocator_type& __a)
1280 : unordered_multimap(__n, hasher(), key_equal(), __a)
1283 unordered_multimap(size_type __n, const hasher& __hf,
1284 const allocator_type& __a)
1285 : unordered_multimap(__n, __hf, key_equal(), __a)
1288 template<typename _InputIterator>
1289 unordered_multimap(_InputIterator __first, _InputIterator __last,
1290 size_type __n,
1291 const allocator_type& __a)
1292 : unordered_multimap(__first, __last, __n, hasher(), key_equal(), __a)
1295 template<typename _InputIterator>
1296 unordered_multimap(_InputIterator __first, _InputIterator __last,
1297 size_type __n, const hasher& __hf,
1298 const allocator_type& __a)
1299 : unordered_multimap(__first, __last, __n, __hf, key_equal(), __a)
1302 unordered_multimap(initializer_list<value_type> __l,
1303 size_type __n,
1304 const allocator_type& __a)
1305 : unordered_multimap(__l, __n, hasher(), key_equal(), __a)
1308 unordered_multimap(initializer_list<value_type> __l,
1309 size_type __n, const hasher& __hf,
1310 const allocator_type& __a)
1311 : unordered_multimap(__l, __n, __hf, key_equal(), __a)
1314 /// Copy assignment operator.
1315 unordered_multimap&
1316 operator=(const unordered_multimap&) = default;
1318 /// Move assignment operator.
1319 unordered_multimap&
1320 operator=(unordered_multimap&&) = default;
1323 * @brief %Unordered_multimap list assignment operator.
1324 * @param __l An initializer_list.
1326 * This function fills an %unordered_multimap with copies of the
1327 * elements in the initializer list @a __l.
1329 * Note that the assignment completely changes the %unordered_multimap
1330 * and that the resulting %unordered_multimap's size is the same as the
1331 * number of elements assigned.
1333 unordered_multimap&
1334 operator=(initializer_list<value_type> __l)
1336 _M_h = __l;
1337 return *this;
1340 /// Returns the allocator object used by the %unordered_multimap.
1341 allocator_type
1342 get_allocator() const noexcept
1343 { return _M_h.get_allocator(); }
1345 // size and capacity:
1347 /// Returns true if the %unordered_multimap is empty.
1348 bool
1349 empty() const noexcept
1350 { return _M_h.empty(); }
1352 /// Returns the size of the %unordered_multimap.
1353 size_type
1354 size() const noexcept
1355 { return _M_h.size(); }
1357 /// Returns the maximum size of the %unordered_multimap.
1358 size_type
1359 max_size() const noexcept
1360 { return _M_h.max_size(); }
1362 // iterators.
1365 * Returns a read/write iterator that points to the first element in the
1366 * %unordered_multimap.
1368 iterator
1369 begin() noexcept
1370 { return _M_h.begin(); }
1372 //@{
1374 * Returns a read-only (constant) iterator that points to the first
1375 * element in the %unordered_multimap.
1377 const_iterator
1378 begin() const noexcept
1379 { return _M_h.begin(); }
1381 const_iterator
1382 cbegin() const noexcept
1383 { return _M_h.begin(); }
1384 //@}
1387 * Returns a read/write iterator that points one past the last element in
1388 * the %unordered_multimap.
1390 iterator
1391 end() noexcept
1392 { return _M_h.end(); }
1394 //@{
1396 * Returns a read-only (constant) iterator that points one past the last
1397 * element in the %unordered_multimap.
1399 const_iterator
1400 end() const noexcept
1401 { return _M_h.end(); }
1403 const_iterator
1404 cend() const noexcept
1405 { return _M_h.end(); }
1406 //@}
1408 // modifiers.
1411 * @brief Attempts to build and insert a std::pair into the
1412 * %unordered_multimap.
1414 * @param __args Arguments used to generate a new pair instance (see
1415 * std::piecewise_contruct for passing arguments to each
1416 * part of the pair constructor).
1418 * @return An iterator that points to the inserted pair.
1420 * This function attempts to build and insert a (key, value) %pair into
1421 * the %unordered_multimap.
1423 * Insertion requires amortized constant time.
1425 template<typename... _Args>
1426 iterator
1427 emplace(_Args&&... __args)
1428 { return _M_h.emplace(std::forward<_Args>(__args)...); }
1431 * @brief Attempts to build and insert a std::pair into the
1432 * %unordered_multimap.
1434 * @param __pos An iterator that serves as a hint as to where the pair
1435 * should be inserted.
1436 * @param __args Arguments used to generate a new pair instance (see
1437 * std::piecewise_contruct for passing arguments to each
1438 * part of the pair constructor).
1439 * @return An iterator that points to the element with key of the
1440 * std::pair built from @a __args.
1442 * Note that the first parameter is only a hint and can potentially
1443 * improve the performance of the insertion process. A bad hint would
1444 * cause no gains in efficiency.
1446 * See
1447 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
1448 * for more on @a hinting.
1450 * Insertion requires amortized constant time.
1452 template<typename... _Args>
1453 iterator
1454 emplace_hint(const_iterator __pos, _Args&&... __args)
1455 { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
1457 //@{
1459 * @brief Inserts a std::pair into the %unordered_multimap.
1460 * @param __x Pair to be inserted (see std::make_pair for easy
1461 * creation of pairs).
1463 * @return An iterator that points to the inserted pair.
1465 * Insertion requires amortized constant time.
1467 iterator
1468 insert(const value_type& __x)
1469 { return _M_h.insert(__x); }
1471 template<typename _Pair, typename = typename
1472 std::enable_if<std::is_constructible<value_type,
1473 _Pair&&>::value>::type>
1474 iterator
1475 insert(_Pair&& __x)
1476 { return _M_h.insert(std::forward<_Pair>(__x)); }
1477 //@}
1479 //@{
1481 * @brief Inserts a std::pair into the %unordered_multimap.
1482 * @param __hint An iterator that serves as a hint as to where the
1483 * pair should be inserted.
1484 * @param __x Pair to be inserted (see std::make_pair for easy creation
1485 * of pairs).
1486 * @return An iterator that points to the element with key of
1487 * @a __x (may or may not be the %pair passed in).
1489 * Note that the first parameter is only a hint and can potentially
1490 * improve the performance of the insertion process. A bad hint would
1491 * cause no gains in efficiency.
1493 * See
1494 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
1495 * for more on @a hinting.
1497 * Insertion requires amortized constant time.
1499 iterator
1500 insert(const_iterator __hint, const value_type& __x)
1501 { return _M_h.insert(__hint, __x); }
1503 template<typename _Pair, typename = typename
1504 std::enable_if<std::is_constructible<value_type,
1505 _Pair&&>::value>::type>
1506 iterator
1507 insert(const_iterator __hint, _Pair&& __x)
1508 { return _M_h.insert(__hint, std::forward<_Pair>(__x)); }
1509 //@}
1512 * @brief A template function that attempts to insert a range of
1513 * elements.
1514 * @param __first Iterator pointing to the start of the range to be
1515 * inserted.
1516 * @param __last Iterator pointing to the end of the range.
1518 * Complexity similar to that of the range constructor.
1520 template<typename _InputIterator>
1521 void
1522 insert(_InputIterator __first, _InputIterator __last)
1523 { _M_h.insert(__first, __last); }
1526 * @brief Attempts to insert a list of elements into the
1527 * %unordered_multimap.
1528 * @param __l A std::initializer_list<value_type> of elements
1529 * to be inserted.
1531 * Complexity similar to that of the range constructor.
1533 void
1534 insert(initializer_list<value_type> __l)
1535 { _M_h.insert(__l); }
1537 #if __cplusplus > 201402L
1538 /// Extract a node.
1539 node_type
1540 extract(const_iterator __pos)
1542 __glibcxx_assert(__pos != end());
1543 return _M_h.extract(__pos);
1546 /// Extract a node.
1547 node_type
1548 extract(const key_type& __key)
1549 { return _M_h.extract(__key); }
1551 /// Re-insert an extracted node.
1552 iterator
1553 insert(node_type&& __nh)
1554 { return _M_h._M_reinsert_node_multi(cend(), std::move(__nh)); }
1556 /// Re-insert an extracted node.
1557 iterator
1558 insert(const_iterator __hint, node_type&& __nh)
1559 { return _M_h._M_reinsert_node_multi(__hint, std::move(__nh)); }
1560 #endif // C++17
1562 //@{
1564 * @brief Erases an element from an %unordered_multimap.
1565 * @param __position An iterator pointing to the element to be erased.
1566 * @return An iterator pointing to the element immediately following
1567 * @a __position prior to the element being erased. If no such
1568 * element exists, end() is returned.
1570 * This function erases an element, pointed to by the given iterator,
1571 * from an %unordered_multimap.
1572 * Note that this function only erases the element, and that if the
1573 * element is itself a pointer, the pointed-to memory is not touched in
1574 * any way. Managing the pointer is the user's responsibility.
1576 iterator
1577 erase(const_iterator __position)
1578 { return _M_h.erase(__position); }
1580 // LWG 2059.
1581 iterator
1582 erase(iterator __position)
1583 { return _M_h.erase(__position); }
1584 //@}
1587 * @brief Erases elements according to the provided key.
1588 * @param __x Key of elements to be erased.
1589 * @return The number of elements erased.
1591 * This function erases all the elements located by the given key from
1592 * an %unordered_multimap.
1593 * Note that this function only erases the element, and that if the
1594 * element is itself a pointer, the pointed-to memory is not touched in
1595 * any way. Managing the pointer is the user's responsibility.
1597 size_type
1598 erase(const key_type& __x)
1599 { return _M_h.erase(__x); }
1602 * @brief Erases a [__first,__last) range of elements from an
1603 * %unordered_multimap.
1604 * @param __first Iterator pointing to the start of the range to be
1605 * erased.
1606 * @param __last Iterator pointing to the end of the range to
1607 * be erased.
1608 * @return The iterator @a __last.
1610 * This function erases a sequence of elements from an
1611 * %unordered_multimap.
1612 * Note that this function only erases the elements, and that if
1613 * the element is itself a pointer, the pointed-to memory is not touched
1614 * in any way. Managing the pointer is the user's responsibility.
1616 iterator
1617 erase(const_iterator __first, const_iterator __last)
1618 { return _M_h.erase(__first, __last); }
1621 * Erases all elements in an %unordered_multimap.
1622 * Note that this function only erases the elements, and that if the
1623 * elements themselves are pointers, the pointed-to memory is not touched
1624 * in any way. Managing the pointer is the user's responsibility.
1626 void
1627 clear() noexcept
1628 { _M_h.clear(); }
1631 * @brief Swaps data with another %unordered_multimap.
1632 * @param __x An %unordered_multimap of the same element and allocator
1633 * types.
1635 * This exchanges the elements between two %unordered_multimap in
1636 * constant time.
1637 * Note that the global std::swap() function is specialized such that
1638 * std::swap(m1,m2) will feed to this function.
1640 void
1641 swap(unordered_multimap& __x)
1642 noexcept( noexcept(_M_h.swap(__x._M_h)) )
1643 { _M_h.swap(__x._M_h); }
1645 #if __cplusplus > 201402L
1646 template<typename, typename, typename>
1647 friend class _Hash_merge_helper;
1649 template<typename _H2, typename _P2>
1650 void
1651 merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>& __source)
1653 using _Merge_helper
1654 = _Hash_merge_helper<unordered_multimap, _H2, _P2>;
1655 _M_h._M_merge_multi(_Merge_helper::_S_get_table(__source));
1658 template<typename _H2, typename _P2>
1659 void
1660 merge(unordered_multimap<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
1661 { merge(__source); }
1663 template<typename _H2, typename _P2>
1664 void
1665 merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>& __source)
1667 using _Merge_helper
1668 = _Hash_merge_helper<unordered_multimap, _H2, _P2>;
1669 _M_h._M_merge_multi(_Merge_helper::_S_get_table(__source));
1672 template<typename _H2, typename _P2>
1673 void
1674 merge(unordered_map<_Key, _Tp, _H2, _P2, _Alloc>&& __source)
1675 { merge(__source); }
1676 #endif // C++17
1678 // observers.
1680 /// Returns the hash functor object with which the %unordered_multimap
1681 /// was constructed.
1682 hasher
1683 hash_function() const
1684 { return _M_h.hash_function(); }
1686 /// Returns the key comparison object with which the %unordered_multimap
1687 /// was constructed.
1688 key_equal
1689 key_eq() const
1690 { return _M_h.key_eq(); }
1692 // lookup.
1694 //@{
1696 * @brief Tries to locate an element in an %unordered_multimap.
1697 * @param __x Key to be located.
1698 * @return Iterator pointing to sought-after element, or end() if not
1699 * found.
1701 * This function takes a key and tries to locate the element with which
1702 * the key matches. If successful the function returns an iterator
1703 * pointing to the sought after element. If unsuccessful it returns the
1704 * past-the-end ( @c end() ) iterator.
1706 iterator
1707 find(const key_type& __x)
1708 { return _M_h.find(__x); }
1710 const_iterator
1711 find(const key_type& __x) const
1712 { return _M_h.find(__x); }
1713 //@}
1716 * @brief Finds the number of elements.
1717 * @param __x Key to count.
1718 * @return Number of elements with specified key.
1720 size_type
1721 count(const key_type& __x) const
1722 { return _M_h.count(__x); }
1724 //@{
1726 * @brief Finds a subsequence matching given key.
1727 * @param __x Key to be located.
1728 * @return Pair of iterators that possibly points to the subsequence
1729 * matching given key.
1731 std::pair<iterator, iterator>
1732 equal_range(const key_type& __x)
1733 { return _M_h.equal_range(__x); }
1735 std::pair<const_iterator, const_iterator>
1736 equal_range(const key_type& __x) const
1737 { return _M_h.equal_range(__x); }
1738 //@}
1740 // bucket interface.
1742 /// Returns the number of buckets of the %unordered_multimap.
1743 size_type
1744 bucket_count() const noexcept
1745 { return _M_h.bucket_count(); }
1747 /// Returns the maximum number of buckets of the %unordered_multimap.
1748 size_type
1749 max_bucket_count() const noexcept
1750 { return _M_h.max_bucket_count(); }
1753 * @brief Returns the number of elements in a given bucket.
1754 * @param __n A bucket index.
1755 * @return The number of elements in the bucket.
1757 size_type
1758 bucket_size(size_type __n) const
1759 { return _M_h.bucket_size(__n); }
1762 * @brief Returns the bucket index of a given element.
1763 * @param __key A key instance.
1764 * @return The key bucket index.
1766 size_type
1767 bucket(const key_type& __key) const
1768 { return _M_h.bucket(__key); }
1771 * @brief Returns a read/write iterator pointing to the first bucket
1772 * element.
1773 * @param __n The bucket index.
1774 * @return A read/write local iterator.
1776 local_iterator
1777 begin(size_type __n)
1778 { return _M_h.begin(__n); }
1780 //@{
1782 * @brief Returns a read-only (constant) iterator pointing to the first
1783 * bucket element.
1784 * @param __n The bucket index.
1785 * @return A read-only local iterator.
1787 const_local_iterator
1788 begin(size_type __n) const
1789 { return _M_h.begin(__n); }
1791 const_local_iterator
1792 cbegin(size_type __n) const
1793 { return _M_h.cbegin(__n); }
1794 //@}
1797 * @brief Returns a read/write iterator pointing to one past the last
1798 * bucket elements.
1799 * @param __n The bucket index.
1800 * @return A read/write local iterator.
1802 local_iterator
1803 end(size_type __n)
1804 { return _M_h.end(__n); }
1806 //@{
1808 * @brief Returns a read-only (constant) iterator pointing to one past
1809 * the last bucket elements.
1810 * @param __n The bucket index.
1811 * @return A read-only local iterator.
1813 const_local_iterator
1814 end(size_type __n) const
1815 { return _M_h.end(__n); }
1817 const_local_iterator
1818 cend(size_type __n) const
1819 { return _M_h.cend(__n); }
1820 //@}
1822 // hash policy.
1824 /// Returns the average number of elements per bucket.
1825 float
1826 load_factor() const noexcept
1827 { return _M_h.load_factor(); }
1829 /// Returns a positive number that the %unordered_multimap tries to keep
1830 /// the load factor less than or equal to.
1831 float
1832 max_load_factor() const noexcept
1833 { return _M_h.max_load_factor(); }
1836 * @brief Change the %unordered_multimap maximum load factor.
1837 * @param __z The new maximum load factor.
1839 void
1840 max_load_factor(float __z)
1841 { _M_h.max_load_factor(__z); }
1844 * @brief May rehash the %unordered_multimap.
1845 * @param __n The new number of buckets.
1847 * Rehash will occur only if the new number of buckets respect the
1848 * %unordered_multimap maximum load factor.
1850 void
1851 rehash(size_type __n)
1852 { _M_h.rehash(__n); }
1855 * @brief Prepare the %unordered_multimap for a specified number of
1856 * elements.
1857 * @param __n Number of elements required.
1859 * Same as rehash(ceil(n / max_load_factor())).
1861 void
1862 reserve(size_type __n)
1863 { _M_h.reserve(__n); }
1865 template<typename _Key1, typename _Tp1, typename _Hash1, typename _Pred1,
1866 typename _Alloc1>
1867 friend bool
1868 operator==(const unordered_multimap<_Key1, _Tp1,
1869 _Hash1, _Pred1, _Alloc1>&,
1870 const unordered_multimap<_Key1, _Tp1,
1871 _Hash1, _Pred1, _Alloc1>&);
1874 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1875 inline void
1876 swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1877 unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1878 noexcept(noexcept(__x.swap(__y)))
1879 { __x.swap(__y); }
1881 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1882 inline void
1883 swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1884 unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1885 noexcept(noexcept(__x.swap(__y)))
1886 { __x.swap(__y); }
1888 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1889 inline bool
1890 operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1891 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1892 { return __x._M_h._M_equal(__y._M_h); }
1894 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1895 inline bool
1896 operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1897 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1898 { return !(__x == __y); }
1900 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1901 inline bool
1902 operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1903 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1904 { return __x._M_h._M_equal(__y._M_h); }
1906 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1907 inline bool
1908 operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1909 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1910 { return !(__x == __y); }
1912 _GLIBCXX_END_NAMESPACE_CONTAINER
1914 #if __cplusplus > 201402L
1915 // Allow std::unordered_map access to internals of compatible maps.
1916 template<typename _Key, typename _Val, typename _Hash1, typename _Eq1,
1917 typename _Alloc, typename _Hash2, typename _Eq2>
1918 struct _Hash_merge_helper<
1919 _GLIBCXX_STD_C::unordered_map<_Key, _Val, _Hash1, _Eq1, _Alloc>,
1920 _Hash2, _Eq2>
1922 private:
1923 template<typename... _Tp>
1924 using unordered_map = _GLIBCXX_STD_C::unordered_map<_Tp...>;
1925 template<typename... _Tp>
1926 using unordered_multimap = _GLIBCXX_STD_C::unordered_multimap<_Tp...>;
1928 friend unordered_map<_Key, _Val, _Hash1, _Eq1, _Alloc>;
1930 static auto&
1931 _S_get_table(unordered_map<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
1932 { return __map._M_h; }
1934 static auto&
1935 _S_get_table(unordered_multimap<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
1936 { return __map._M_h; }
1939 // Allow std::unordered_multimap access to internals of compatible maps.
1940 template<typename _Key, typename _Val, typename _Hash1, typename _Eq1,
1941 typename _Alloc, typename _Hash2, typename _Eq2>
1942 struct _Hash_merge_helper<
1943 _GLIBCXX_STD_C::unordered_multimap<_Key, _Val, _Hash1, _Eq1, _Alloc>,
1944 _Hash2, _Eq2>
1946 private:
1947 template<typename... _Tp>
1948 using unordered_map = _GLIBCXX_STD_C::unordered_map<_Tp...>;
1949 template<typename... _Tp>
1950 using unordered_multimap = _GLIBCXX_STD_C::unordered_multimap<_Tp...>;
1952 friend unordered_multimap<_Key, _Val, _Hash1, _Eq1, _Alloc>;
1954 static auto&
1955 _S_get_table(unordered_map<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
1956 { return __map._M_h; }
1958 static auto&
1959 _S_get_table(unordered_multimap<_Key, _Val, _Hash2, _Eq2, _Alloc>& __map)
1960 { return __map._M_h; }
1962 #endif // C++17
1964 _GLIBCXX_END_NAMESPACE_VERSION
1965 } // namespace std
1967 #endif /* _UNORDERED_MAP_H */