2015-05-14 Nathan Myers <ncm@cantrip.org>
[official-gcc.git] / libstdc++-v3 / include / bits / unordered_map.h
blob069b85922ead045bf18bb18a9bff605e6b4eea6e
1 // unordered_map implementation -*- C++ -*-
3 // Copyright (C) 2010-2015 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_CONTAINER
37 /// Base types for unordered_map.
38 template<bool _Cache>
39 using __umap_traits = __detail::_Hashtable_traits<_Cache, false, true>;
41 template<typename _Key,
42 typename _Tp,
43 typename _Hash = hash<_Key>,
44 typename _Pred = std::equal_to<_Key>,
45 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
46 typename _Tr = __umap_traits<__cache_default<_Key, _Hash>::value>>
47 using __umap_hashtable = _Hashtable<_Key, std::pair<const _Key, _Tp>,
48 _Alloc, __detail::_Select1st,
49 _Pred, _Hash,
50 __detail::_Mod_range_hashing,
51 __detail::_Default_ranged_hash,
52 __detail::_Prime_rehash_policy, _Tr>;
54 /// Base types for unordered_multimap.
55 template<bool _Cache>
56 using __ummap_traits = __detail::_Hashtable_traits<_Cache, false, false>;
58 template<typename _Key,
59 typename _Tp,
60 typename _Hash = hash<_Key>,
61 typename _Pred = std::equal_to<_Key>,
62 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
63 typename _Tr = __ummap_traits<__cache_default<_Key, _Hash>::value>>
64 using __ummap_hashtable = _Hashtable<_Key, std::pair<const _Key, _Tp>,
65 _Alloc, __detail::_Select1st,
66 _Pred, _Hash,
67 __detail::_Mod_range_hashing,
68 __detail::_Default_ranged_hash,
69 __detail::_Prime_rehash_policy, _Tr>;
71 /**
72 * @brief A standard container composed of unique keys (containing
73 * at most one of each key value) that associates values of another type
74 * with the keys.
76 * @ingroup unordered_associative_containers
78 * @tparam _Key Type of key objects.
79 * @tparam _Tp Type of mapped objects.
80 * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
81 * @tparam _Pred Predicate function object type, defaults
82 * to equal_to<_Value>.
83 * @tparam _Alloc Allocator type, defaults to
84 * std::allocator<std::pair<const _Key, _Tp>>.
86 * Meets the requirements of a <a href="tables.html#65">container</a>, and
87 * <a href="tables.html#xx">unordered associative container</a>
89 * The resulting value type of the container is std::pair<const _Key, _Tp>.
91 * Base is _Hashtable, dispatched at compile time via template
92 * alias __umap_hashtable.
94 template<class _Key, class _Tp,
95 class _Hash = hash<_Key>,
96 class _Pred = std::equal_to<_Key>,
97 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
98 class unordered_map
100 typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
101 _Hashtable _M_h;
103 public:
104 // typedefs:
105 //@{
106 /// Public typedefs.
107 typedef typename _Hashtable::key_type key_type;
108 typedef typename _Hashtable::value_type value_type;
109 typedef typename _Hashtable::mapped_type mapped_type;
110 typedef typename _Hashtable::hasher hasher;
111 typedef typename _Hashtable::key_equal key_equal;
112 typedef typename _Hashtable::allocator_type allocator_type;
113 //@}
115 //@{
116 /// Iterator-related typedefs.
117 typedef typename _Hashtable::pointer pointer;
118 typedef typename _Hashtable::const_pointer const_pointer;
119 typedef typename _Hashtable::reference reference;
120 typedef typename _Hashtable::const_reference const_reference;
121 typedef typename _Hashtable::iterator iterator;
122 typedef typename _Hashtable::const_iterator const_iterator;
123 typedef typename _Hashtable::local_iterator local_iterator;
124 typedef typename _Hashtable::const_local_iterator const_local_iterator;
125 typedef typename _Hashtable::size_type size_type;
126 typedef typename _Hashtable::difference_type difference_type;
127 //@}
129 //construct/destroy/copy
131 /// Default constructor.
132 unordered_map() = default;
135 * @brief Default constructor creates no elements.
136 * @param __n Minimal initial number of buckets.
137 * @param __hf A hash functor.
138 * @param __eql A key equality functor.
139 * @param __a An allocator object.
141 explicit
142 unordered_map(size_type __n,
143 const hasher& __hf = hasher(),
144 const key_equal& __eql = key_equal(),
145 const allocator_type& __a = allocator_type())
146 : _M_h(__n, __hf, __eql, __a)
149 unordered_map(size_type __n, const allocator_type& __a)
150 : _M_h(__n, hasher(), key_equal(), __a)
153 explicit
154 unordered_map(size_type __n,
155 const hasher& __hf,
156 const allocator_type& __a)
157 : _M_h(__n, __hf, key_equal(), __a)
161 * @brief Builds an %unordered_map from a range.
162 * @param __first An input iterator.
163 * @param __last An input iterator.
164 * @param __n Minimal initial number of buckets.
165 * @param __hf A hash functor.
166 * @param __eql A key equality functor.
167 * @param __a An allocator object.
169 * Create an %unordered_map consisting of copies of the elements from
170 * [__first,__last). This is linear in N (where N is
171 * distance(__first,__last)).
173 template<typename _InputIterator>
174 unordered_map(_InputIterator __first, _InputIterator __last,
175 size_type __n = 0,
176 const hasher& __hf = hasher(),
177 const key_equal& __eql = key_equal(),
178 const allocator_type& __a = allocator_type())
179 : _M_h(__first, __last, __n, __hf, __eql, __a)
182 /// Copy constructor.
183 unordered_map(const unordered_map&) = default;
185 /// Move constructor.
186 unordered_map(unordered_map&&) = default;
189 * @brief Creates an %unordered_map with no elements.
190 * @param __a An allocator object.
192 explicit
193 unordered_map(const allocator_type& __a)
194 : _M_h(__a)
198 * @brief Copy constructor with allocator argument.
199 * @param __uset Input %unordered_map to copy.
200 * @param __a An allocator object.
202 unordered_map(const unordered_map& __umap,
203 const allocator_type& __a)
204 : _M_h(__umap._M_h, __a)
208 * @brief Move constructor with allocator argument.
209 * @param __uset Input %unordered_map to move.
210 * @param __a An allocator object.
212 unordered_map(unordered_map&& __umap,
213 const allocator_type& __a)
214 : _M_h(std::move(__umap._M_h), __a)
218 * @brief Builds an %unordered_map from an initializer_list.
219 * @param __l An initializer_list.
220 * @param __n Minimal initial number of buckets.
221 * @param __hf A hash functor.
222 * @param __eql A key equality functor.
223 * @param __a An allocator object.
225 * Create an %unordered_map consisting of copies of the elements in the
226 * list. This is linear in N (where N is @a __l.size()).
228 unordered_map(initializer_list<value_type> __l,
229 size_type __n = 0,
230 const hasher& __hf = hasher(),
231 const key_equal& __eql = key_equal(),
232 const allocator_type& __a = allocator_type())
233 : _M_h(__l, __n, __hf, __eql, __a)
236 /// Copy assignment operator.
237 unordered_map&
238 operator=(const unordered_map&) = default;
240 /// Move assignment operator.
241 unordered_map&
242 operator=(unordered_map&&) = default;
245 * @brief %Unordered_map list assignment operator.
246 * @param __l An initializer_list.
248 * This function fills an %unordered_map with copies of the elements in
249 * the initializer list @a __l.
251 * Note that the assignment completely changes the %unordered_map and
252 * that the resulting %unordered_map's size is the same as the number
253 * of elements assigned. Old data may be lost.
255 unordered_map&
256 operator=(initializer_list<value_type> __l)
258 _M_h = __l;
259 return *this;
262 /// Returns the allocator object with which the %unordered_map was
263 /// constructed.
264 allocator_type
265 get_allocator() const noexcept
266 { return _M_h.get_allocator(); }
268 // size and capacity:
270 /// Returns true if the %unordered_map is empty.
271 bool
272 empty() const noexcept
273 { return _M_h.empty(); }
275 /// Returns the size of the %unordered_map.
276 size_type
277 size() const noexcept
278 { return _M_h.size(); }
280 /// Returns the maximum size of the %unordered_map.
281 size_type
282 max_size() const noexcept
283 { return _M_h.max_size(); }
285 // iterators.
288 * Returns a read/write iterator that points to the first element in the
289 * %unordered_map.
291 iterator
292 begin() noexcept
293 { return _M_h.begin(); }
295 //@{
297 * Returns a read-only (constant) iterator that points to the first
298 * element in the %unordered_map.
300 const_iterator
301 begin() const noexcept
302 { return _M_h.begin(); }
304 const_iterator
305 cbegin() const noexcept
306 { return _M_h.begin(); }
307 //@}
310 * Returns a read/write iterator that points one past the last element in
311 * the %unordered_map.
313 iterator
314 end() noexcept
315 { return _M_h.end(); }
317 //@{
319 * Returns a read-only (constant) iterator that points one past the last
320 * element in the %unordered_map.
322 const_iterator
323 end() const noexcept
324 { return _M_h.end(); }
326 const_iterator
327 cend() const noexcept
328 { return _M_h.end(); }
329 //@}
331 // modifiers.
334 * @brief Attempts to build and insert a std::pair into the %unordered_map.
336 * @param __args Arguments used to generate a new pair instance (see
337 * std::piecewise_contruct for passing arguments to each
338 * part of the pair constructor).
340 * @return A pair, of which the first element is an iterator that points
341 * to the possibly inserted pair, and the second is a bool that
342 * is true if the pair was actually inserted.
344 * This function attempts to build and insert a (key, value) %pair into
345 * the %unordered_map.
346 * An %unordered_map relies on unique keys and thus a %pair is only
347 * inserted if its first element (the key) is not already present in the
348 * %unordered_map.
350 * Insertion requires amortized constant time.
352 template<typename... _Args>
353 std::pair<iterator, bool>
354 emplace(_Args&&... __args)
355 { return _M_h.emplace(std::forward<_Args>(__args)...); }
358 * @brief Attempts to build and insert a std::pair into the %unordered_map.
360 * @param __pos An iterator that serves as a hint as to where the pair
361 * should be inserted.
362 * @param __args Arguments used to generate a new pair instance (see
363 * std::piecewise_contruct for passing arguments to each
364 * part of the pair constructor).
365 * @return An iterator that points to the element with key of the
366 * std::pair built from @a __args (may or may not be that
367 * std::pair).
369 * This function is not concerned about whether the insertion took place,
370 * and thus does not return a boolean like the single-argument emplace()
371 * does.
372 * Note that the first parameter is only a hint and can potentially
373 * improve the performance of the insertion process. A bad hint would
374 * cause no gains in efficiency.
376 * See
377 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
378 * for more on @a hinting.
380 * Insertion requires amortized constant time.
382 template<typename... _Args>
383 iterator
384 emplace_hint(const_iterator __pos, _Args&&... __args)
385 { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
387 //@{
389 * @brief Attempts to insert a std::pair into the %unordered_map.
391 * @param __x Pair to be inserted (see std::make_pair for easy
392 * creation of pairs).
394 * @return A pair, of which the first element is an iterator that
395 * points to the possibly inserted pair, and the second is
396 * a bool that is true if the pair was actually inserted.
398 * This function attempts to insert a (key, value) %pair into the
399 * %unordered_map. An %unordered_map relies on unique keys and thus a
400 * %pair is only inserted if its first element (the key) is not already
401 * present in the %unordered_map.
403 * Insertion requires amortized constant time.
405 std::pair<iterator, bool>
406 insert(const value_type& __x)
407 { return _M_h.insert(__x); }
409 template<typename _Pair, typename = typename
410 std::enable_if<std::is_constructible<value_type,
411 _Pair&&>::value>::type>
412 std::pair<iterator, bool>
413 insert(_Pair&& __x)
414 { return _M_h.insert(std::forward<_Pair>(__x)); }
415 //@}
417 //@{
419 * @brief Attempts to insert a std::pair into the %unordered_map.
420 * @param __hint An iterator that serves as a hint as to where the
421 * pair should be inserted.
422 * @param __x Pair to be inserted (see std::make_pair for easy creation
423 * of pairs).
424 * @return An iterator that points to the element with key of
425 * @a __x (may or may not be the %pair passed in).
427 * This function is not concerned about whether the insertion took place,
428 * and thus does not return a boolean like the single-argument insert()
429 * does. Note that the first parameter is only a hint and can
430 * potentially improve the performance of the insertion process. A bad
431 * hint would cause no gains in efficiency.
433 * See
434 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
435 * for more on @a hinting.
437 * Insertion requires amortized constant time.
439 iterator
440 insert(const_iterator __hint, const value_type& __x)
441 { return _M_h.insert(__hint, __x); }
443 template<typename _Pair, typename = typename
444 std::enable_if<std::is_constructible<value_type,
445 _Pair&&>::value>::type>
446 iterator
447 insert(const_iterator __hint, _Pair&& __x)
448 { return _M_h.insert(__hint, std::forward<_Pair>(__x)); }
449 //@}
452 * @brief A template function that attempts to insert a range of
453 * elements.
454 * @param __first Iterator pointing to the start of the range to be
455 * inserted.
456 * @param __last Iterator pointing to the end of the range.
458 * Complexity similar to that of the range constructor.
460 template<typename _InputIterator>
461 void
462 insert(_InputIterator __first, _InputIterator __last)
463 { _M_h.insert(__first, __last); }
466 * @brief Attempts to insert a list of elements into the %unordered_map.
467 * @param __l A std::initializer_list<value_type> of elements
468 * to be inserted.
470 * Complexity similar to that of the range constructor.
472 void
473 insert(initializer_list<value_type> __l)
474 { _M_h.insert(__l); }
476 //@{
478 * @brief Erases an element from an %unordered_map.
479 * @param __position An iterator pointing to the element to be erased.
480 * @return An iterator pointing to the element immediately following
481 * @a __position prior to the element being erased. If no such
482 * element exists, end() is returned.
484 * This function erases an element, pointed to by the given iterator,
485 * from an %unordered_map.
486 * Note that this function only erases the element, and that if the
487 * element is itself a pointer, the pointed-to memory is not touched in
488 * any way. Managing the pointer is the user's responsibility.
490 iterator
491 erase(const_iterator __position)
492 { return _M_h.erase(__position); }
494 // LWG 2059.
495 iterator
496 erase(iterator __position)
497 { return _M_h.erase(__position); }
498 //@}
501 * @brief Erases elements according to the provided key.
502 * @param __x Key of element to be erased.
503 * @return The number of elements erased.
505 * This function erases all the elements located by the given key from
506 * an %unordered_map. For an %unordered_map the result of this function
507 * can only be 0 (not present) or 1 (present).
508 * Note that this function only erases the element, and that if the
509 * element is itself a pointer, the pointed-to memory is not touched in
510 * any way. Managing the pointer is the user's responsibility.
512 size_type
513 erase(const key_type& __x)
514 { return _M_h.erase(__x); }
517 * @brief Erases a [__first,__last) range of elements from an
518 * %unordered_map.
519 * @param __first Iterator pointing to the start of the range to be
520 * erased.
521 * @param __last Iterator pointing to the end of the range to
522 * be erased.
523 * @return The iterator @a __last.
525 * This function erases a sequence of elements from an %unordered_map.
526 * Note that this function only erases the elements, and that if
527 * the element is itself a pointer, the pointed-to memory is not touched
528 * in any way. Managing the pointer is the user's responsibility.
530 iterator
531 erase(const_iterator __first, const_iterator __last)
532 { return _M_h.erase(__first, __last); }
535 * Erases all elements in an %unordered_map.
536 * Note that this function only erases the elements, and that if the
537 * elements themselves are pointers, the pointed-to memory is not touched
538 * in any way. Managing the pointer is the user's responsibility.
540 void
541 clear() noexcept
542 { _M_h.clear(); }
545 * @brief Swaps data with another %unordered_map.
546 * @param __x An %unordered_map of the same element and allocator
547 * types.
549 * This exchanges the elements between two %unordered_map in constant time.
550 * Note that the global std::swap() function is specialized such that
551 * std::swap(m1,m2) will feed to this function.
553 void
554 swap(unordered_map& __x)
555 noexcept( noexcept(_M_h.swap(__x._M_h)) )
556 { _M_h.swap(__x._M_h); }
558 // observers.
560 /// Returns the hash functor object with which the %unordered_map was
561 /// constructed.
562 hasher
563 hash_function() const
564 { return _M_h.hash_function(); }
566 /// Returns the key comparison object with which the %unordered_map was
567 /// constructed.
568 key_equal
569 key_eq() const
570 { return _M_h.key_eq(); }
572 // lookup.
574 //@{
576 * @brief Tries to locate an element in an %unordered_map.
577 * @param __x Key to be located.
578 * @return Iterator pointing to sought-after element, or end() if not
579 * found.
581 * This function takes a key and tries to locate the element with which
582 * the key matches. If successful the function returns an iterator
583 * pointing to the sought after element. If unsuccessful it returns the
584 * past-the-end ( @c end() ) iterator.
586 iterator
587 find(const key_type& __x)
588 { return _M_h.find(__x); }
590 const_iterator
591 find(const key_type& __x) const
592 { return _M_h.find(__x); }
593 //@}
596 * @brief Finds the number of elements.
597 * @param __x Key to count.
598 * @return Number of elements with specified key.
600 * This function only makes sense for %unordered_multimap; for
601 * %unordered_map the result will either be 0 (not present) or 1
602 * (present).
604 size_type
605 count(const key_type& __x) const
606 { return _M_h.count(__x); }
608 //@{
610 * @brief Finds a subsequence matching given key.
611 * @param __x Key to be located.
612 * @return Pair of iterators that possibly points to the subsequence
613 * matching given key.
615 * This function probably only makes sense for %unordered_multimap.
617 std::pair<iterator, iterator>
618 equal_range(const key_type& __x)
619 { return _M_h.equal_range(__x); }
621 std::pair<const_iterator, const_iterator>
622 equal_range(const key_type& __x) const
623 { return _M_h.equal_range(__x); }
624 //@}
626 //@{
628 * @brief Subscript ( @c [] ) access to %unordered_map data.
629 * @param __k The key for which data should be retrieved.
630 * @return A reference to the data of the (key,data) %pair.
632 * Allows for easy lookup with the subscript ( @c [] )operator. Returns
633 * data associated with the key specified in subscript. If the key does
634 * not exist, a pair with that key is created using default values, which
635 * is then returned.
637 * Lookup requires constant time.
639 mapped_type&
640 operator[](const key_type& __k)
641 { return _M_h[__k]; }
643 mapped_type&
644 operator[](key_type&& __k)
645 { return _M_h[std::move(__k)]; }
646 //@}
648 //@{
650 * @brief Access to %unordered_map data.
651 * @param __k The key for which data should be retrieved.
652 * @return A reference to the data whose key is equal to @a __k, if
653 * such a data is present in the %unordered_map.
654 * @throw std::out_of_range If no such data is present.
656 mapped_type&
657 at(const key_type& __k)
658 { return _M_h.at(__k); }
660 const mapped_type&
661 at(const key_type& __k) const
662 { return _M_h.at(__k); }
663 //@}
665 // bucket interface.
667 /// Returns the number of buckets of the %unordered_map.
668 size_type
669 bucket_count() const noexcept
670 { return _M_h.bucket_count(); }
672 /// Returns the maximum number of buckets of the %unordered_map.
673 size_type
674 max_bucket_count() const noexcept
675 { return _M_h.max_bucket_count(); }
678 * @brief Returns the number of elements in a given bucket.
679 * @param __n A bucket index.
680 * @return The number of elements in the bucket.
682 size_type
683 bucket_size(size_type __n) const
684 { return _M_h.bucket_size(__n); }
687 * @brief Returns the bucket index of a given element.
688 * @param __key A key instance.
689 * @return The key bucket index.
691 size_type
692 bucket(const key_type& __key) const
693 { return _M_h.bucket(__key); }
696 * @brief Returns a read/write iterator pointing to the first bucket
697 * element.
698 * @param __n The bucket index.
699 * @return A read/write local iterator.
701 local_iterator
702 begin(size_type __n)
703 { return _M_h.begin(__n); }
705 //@{
707 * @brief Returns a read-only (constant) iterator pointing to the first
708 * bucket element.
709 * @param __n The bucket index.
710 * @return A read-only local iterator.
712 const_local_iterator
713 begin(size_type __n) const
714 { return _M_h.begin(__n); }
716 const_local_iterator
717 cbegin(size_type __n) const
718 { return _M_h.cbegin(__n); }
719 //@}
722 * @brief Returns a read/write iterator pointing to one past the last
723 * bucket elements.
724 * @param __n The bucket index.
725 * @return A read/write local iterator.
727 local_iterator
728 end(size_type __n)
729 { return _M_h.end(__n); }
731 //@{
733 * @brief Returns a read-only (constant) iterator pointing to one past
734 * the last bucket elements.
735 * @param __n The bucket index.
736 * @return A read-only local iterator.
738 const_local_iterator
739 end(size_type __n) const
740 { return _M_h.end(__n); }
742 const_local_iterator
743 cend(size_type __n) const
744 { return _M_h.cend(__n); }
745 //@}
747 // hash policy.
749 /// Returns the average number of elements per bucket.
750 float
751 load_factor() const noexcept
752 { return _M_h.load_factor(); }
754 /// Returns a positive number that the %unordered_map tries to keep the
755 /// load factor less than or equal to.
756 float
757 max_load_factor() const noexcept
758 { return _M_h.max_load_factor(); }
761 * @brief Change the %unordered_map maximum load factor.
762 * @param __z The new maximum load factor.
764 void
765 max_load_factor(float __z)
766 { _M_h.max_load_factor(__z); }
769 * @brief May rehash the %unordered_map.
770 * @param __n The new number of buckets.
772 * Rehash will occur only if the new number of buckets respect the
773 * %unordered_map maximum load factor.
775 void
776 rehash(size_type __n)
777 { _M_h.rehash(__n); }
780 * @brief Prepare the %unordered_map for a specified number of
781 * elements.
782 * @param __n Number of elements required.
784 * Same as rehash(ceil(n / max_load_factor())).
786 void
787 reserve(size_type __n)
788 { _M_h.reserve(__n); }
790 template<typename _Key1, typename _Tp1, typename _Hash1, typename _Pred1,
791 typename _Alloc1>
792 friend bool
793 operator==(const unordered_map<_Key1, _Tp1, _Hash1, _Pred1, _Alloc1>&,
794 const unordered_map<_Key1, _Tp1, _Hash1, _Pred1, _Alloc1>&);
798 * @brief A standard container composed of equivalent keys
799 * (possibly containing multiple of each key value) that associates
800 * values of another type with the keys.
802 * @ingroup unordered_associative_containers
804 * @tparam _Key Type of key objects.
805 * @tparam _Tp Type of mapped objects.
806 * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
807 * @tparam _Pred Predicate function object type, defaults
808 * to equal_to<_Value>.
809 * @tparam _Alloc Allocator type, defaults to
810 * std::allocator<std::pair<const _Key, _Tp>>.
812 * Meets the requirements of a <a href="tables.html#65">container</a>, and
813 * <a href="tables.html#xx">unordered associative container</a>
815 * The resulting value type of the container is std::pair<const _Key, _Tp>.
817 * Base is _Hashtable, dispatched at compile time via template
818 * alias __ummap_hashtable.
820 template<class _Key, class _Tp,
821 class _Hash = hash<_Key>,
822 class _Pred = std::equal_to<_Key>,
823 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
824 class unordered_multimap
826 typedef __ummap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
827 _Hashtable _M_h;
829 public:
830 // typedefs:
831 //@{
832 /// Public typedefs.
833 typedef typename _Hashtable::key_type key_type;
834 typedef typename _Hashtable::value_type value_type;
835 typedef typename _Hashtable::mapped_type mapped_type;
836 typedef typename _Hashtable::hasher hasher;
837 typedef typename _Hashtable::key_equal key_equal;
838 typedef typename _Hashtable::allocator_type allocator_type;
839 //@}
841 //@{
842 /// Iterator-related typedefs.
843 typedef typename _Hashtable::pointer pointer;
844 typedef typename _Hashtable::const_pointer const_pointer;
845 typedef typename _Hashtable::reference reference;
846 typedef typename _Hashtable::const_reference const_reference;
847 typedef typename _Hashtable::iterator iterator;
848 typedef typename _Hashtable::const_iterator const_iterator;
849 typedef typename _Hashtable::local_iterator local_iterator;
850 typedef typename _Hashtable::const_local_iterator const_local_iterator;
851 typedef typename _Hashtable::size_type size_type;
852 typedef typename _Hashtable::difference_type difference_type;
853 //@}
855 //construct/destroy/copy
857 /// Default constructor.
858 unordered_multimap() = default;
861 * @brief Default constructor creates no elements.
862 * @param __n Mnimal initial number of buckets.
863 * @param __hf A hash functor.
864 * @param __eql A key equality functor.
865 * @param __a An allocator object.
867 explicit
868 unordered_multimap(size_type __n,
869 const hasher& __hf = hasher(),
870 const key_equal& __eql = key_equal(),
871 const allocator_type& __a = allocator_type())
872 : _M_h(__n, __hf, __eql, __a)
875 unordered_multimap(size_type __n, const allocator_type& __a)
876 : _M_h(__n, hasher(), key_equal(), __a)
879 unordered_multimap(size_type __n,
880 const hasher& __hf,
881 const allocator_type& __a)
882 : _M_h(__n, __hf, key_equal(), __a)
886 * @brief Builds an %unordered_multimap from a range.
887 * @param __first An input iterator.
888 * @param __last An input iterator.
889 * @param __n Minimal initial number of buckets.
890 * @param __hf A hash functor.
891 * @param __eql A key equality functor.
892 * @param __a An allocator object.
894 * Create an %unordered_multimap consisting of copies of the elements
895 * from [__first,__last). This is linear in N (where N is
896 * distance(__first,__last)).
898 template<typename _InputIterator>
899 unordered_multimap(_InputIterator __first, _InputIterator __last,
900 size_type __n = 0,
901 const hasher& __hf = hasher(),
902 const key_equal& __eql = key_equal(),
903 const allocator_type& __a = allocator_type())
904 : _M_h(__first, __last, __n, __hf, __eql, __a)
907 /// Copy constructor.
908 unordered_multimap(const unordered_multimap&) = default;
910 /// Move constructor.
911 unordered_multimap(unordered_multimap&&) = default;
914 * @brief Creates an %unordered_multimap with no elements.
915 * @param __a An allocator object.
917 explicit
918 unordered_multimap(const allocator_type& __a)
919 : _M_h(__a)
923 * @brief Copy constructor with allocator argument.
924 * @param __uset Input %unordered_multimap to copy.
925 * @param __a An allocator object.
927 unordered_multimap(const unordered_multimap& __ummap,
928 const allocator_type& __a)
929 : _M_h(__ummap._M_h, __a)
933 * @brief Move constructor with allocator argument.
934 * @param __uset Input %unordered_multimap to move.
935 * @param __a An allocator object.
937 unordered_multimap(unordered_multimap&& __ummap,
938 const allocator_type& __a)
939 : _M_h(std::move(__ummap._M_h), __a)
943 * @brief Builds an %unordered_multimap from an initializer_list.
944 * @param __l An initializer_list.
945 * @param __n Minimal initial number of buckets.
946 * @param __hf A hash functor.
947 * @param __eql A key equality functor.
948 * @param __a An allocator object.
950 * Create an %unordered_multimap consisting of copies of the elements in
951 * the list. This is linear in N (where N is @a __l.size()).
953 unordered_multimap(initializer_list<value_type> __l,
954 size_type __n = 0,
955 const hasher& __hf = hasher(),
956 const key_equal& __eql = key_equal(),
957 const allocator_type& __a = allocator_type())
958 : _M_h(__l, __n, __hf, __eql, __a)
961 /// Copy assignment operator.
962 unordered_multimap&
963 operator=(const unordered_multimap&) = default;
965 /// Move assignment operator.
966 unordered_multimap&
967 operator=(unordered_multimap&&) = default;
970 * @brief %Unordered_multimap list assignment operator.
971 * @param __l An initializer_list.
973 * This function fills an %unordered_multimap with copies of the elements
974 * in the initializer list @a __l.
976 * Note that the assignment completely changes the %unordered_multimap
977 * and that the resulting %unordered_multimap's size is the same as the
978 * number of elements assigned. Old data may be lost.
980 unordered_multimap&
981 operator=(initializer_list<value_type> __l)
983 _M_h = __l;
984 return *this;
987 /// Returns the allocator object with which the %unordered_multimap was
988 /// constructed.
989 allocator_type
990 get_allocator() const noexcept
991 { return _M_h.get_allocator(); }
993 // size and capacity:
995 /// Returns true if the %unordered_multimap is empty.
996 bool
997 empty() const noexcept
998 { return _M_h.empty(); }
1000 /// Returns the size of the %unordered_multimap.
1001 size_type
1002 size() const noexcept
1003 { return _M_h.size(); }
1005 /// Returns the maximum size of the %unordered_multimap.
1006 size_type
1007 max_size() const noexcept
1008 { return _M_h.max_size(); }
1010 // iterators.
1013 * Returns a read/write iterator that points to the first element in the
1014 * %unordered_multimap.
1016 iterator
1017 begin() noexcept
1018 { return _M_h.begin(); }
1020 //@{
1022 * Returns a read-only (constant) iterator that points to the first
1023 * element in the %unordered_multimap.
1025 const_iterator
1026 begin() const noexcept
1027 { return _M_h.begin(); }
1029 const_iterator
1030 cbegin() const noexcept
1031 { return _M_h.begin(); }
1032 //@}
1035 * Returns a read/write iterator that points one past the last element in
1036 * the %unordered_multimap.
1038 iterator
1039 end() noexcept
1040 { return _M_h.end(); }
1042 //@{
1044 * Returns a read-only (constant) iterator that points one past the last
1045 * element in the %unordered_multimap.
1047 const_iterator
1048 end() const noexcept
1049 { return _M_h.end(); }
1051 const_iterator
1052 cend() const noexcept
1053 { return _M_h.end(); }
1054 //@}
1056 // modifiers.
1059 * @brief Attempts to build and insert a std::pair into the
1060 * %unordered_multimap.
1062 * @param __args Arguments used to generate a new pair instance (see
1063 * std::piecewise_contruct for passing arguments to each
1064 * part of the pair constructor).
1066 * @return An iterator that points to the inserted pair.
1068 * This function attempts to build and insert a (key, value) %pair into
1069 * the %unordered_multimap.
1071 * Insertion requires amortized constant time.
1073 template<typename... _Args>
1074 iterator
1075 emplace(_Args&&... __args)
1076 { return _M_h.emplace(std::forward<_Args>(__args)...); }
1079 * @brief Attempts to build and insert a std::pair into the %unordered_multimap.
1081 * @param __pos An iterator that serves as a hint as to where the pair
1082 * should be inserted.
1083 * @param __args Arguments used to generate a new pair instance (see
1084 * std::piecewise_contruct for passing arguments to each
1085 * part of the pair constructor).
1086 * @return An iterator that points to the element with key of the
1087 * std::pair built from @a __args.
1089 * Note that the first parameter is only a hint and can potentially
1090 * improve the performance of the insertion process. A bad hint would
1091 * cause no gains in efficiency.
1093 * See
1094 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
1095 * for more on @a hinting.
1097 * Insertion requires amortized constant time.
1099 template<typename... _Args>
1100 iterator
1101 emplace_hint(const_iterator __pos, _Args&&... __args)
1102 { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
1104 //@{
1106 * @brief Inserts a std::pair into the %unordered_multimap.
1107 * @param __x Pair to be inserted (see std::make_pair for easy
1108 * creation of pairs).
1110 * @return An iterator that points to the inserted pair.
1112 * Insertion requires amortized constant time.
1114 iterator
1115 insert(const value_type& __x)
1116 { return _M_h.insert(__x); }
1118 template<typename _Pair, typename = typename
1119 std::enable_if<std::is_constructible<value_type,
1120 _Pair&&>::value>::type>
1121 iterator
1122 insert(_Pair&& __x)
1123 { return _M_h.insert(std::forward<_Pair>(__x)); }
1124 //@}
1126 //@{
1128 * @brief Inserts a std::pair into the %unordered_multimap.
1129 * @param __hint An iterator that serves as a hint as to where the
1130 * pair should be inserted.
1131 * @param __x Pair to be inserted (see std::make_pair for easy creation
1132 * of pairs).
1133 * @return An iterator that points to the element with key of
1134 * @a __x (may or may not be the %pair passed in).
1136 * Note that the first parameter is only a hint and can potentially
1137 * improve the performance of the insertion process. A bad hint would
1138 * cause no gains in efficiency.
1140 * See
1141 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
1142 * for more on @a hinting.
1144 * Insertion requires amortized constant time.
1146 iterator
1147 insert(const_iterator __hint, const value_type& __x)
1148 { return _M_h.insert(__hint, __x); }
1150 template<typename _Pair, typename = typename
1151 std::enable_if<std::is_constructible<value_type,
1152 _Pair&&>::value>::type>
1153 iterator
1154 insert(const_iterator __hint, _Pair&& __x)
1155 { return _M_h.insert(__hint, std::forward<_Pair>(__x)); }
1156 //@}
1159 * @brief A template function that attempts to insert a range of
1160 * elements.
1161 * @param __first Iterator pointing to the start of the range to be
1162 * inserted.
1163 * @param __last Iterator pointing to the end of the range.
1165 * Complexity similar to that of the range constructor.
1167 template<typename _InputIterator>
1168 void
1169 insert(_InputIterator __first, _InputIterator __last)
1170 { _M_h.insert(__first, __last); }
1173 * @brief Attempts to insert a list of elements into the
1174 * %unordered_multimap.
1175 * @param __l A std::initializer_list<value_type> of elements
1176 * to be inserted.
1178 * Complexity similar to that of the range constructor.
1180 void
1181 insert(initializer_list<value_type> __l)
1182 { _M_h.insert(__l); }
1184 //@{
1186 * @brief Erases an element from an %unordered_multimap.
1187 * @param __position An iterator pointing to the element to be erased.
1188 * @return An iterator pointing to the element immediately following
1189 * @a __position prior to the element being erased. If no such
1190 * element exists, end() is returned.
1192 * This function erases an element, pointed to by the given iterator,
1193 * from an %unordered_multimap.
1194 * Note that this function only erases the element, and that if the
1195 * element is itself a pointer, the pointed-to memory is not touched in
1196 * any way. Managing the pointer is the user's responsibility.
1198 iterator
1199 erase(const_iterator __position)
1200 { return _M_h.erase(__position); }
1202 // LWG 2059.
1203 iterator
1204 erase(iterator __position)
1205 { return _M_h.erase(__position); }
1206 //@}
1209 * @brief Erases elements according to the provided key.
1210 * @param __x Key of elements to be erased.
1211 * @return The number of elements erased.
1213 * This function erases all the elements located by the given key from
1214 * an %unordered_multimap.
1215 * Note that this function only erases the element, and that if the
1216 * element is itself a pointer, the pointed-to memory is not touched in
1217 * any way. Managing the pointer is the user's responsibility.
1219 size_type
1220 erase(const key_type& __x)
1221 { return _M_h.erase(__x); }
1224 * @brief Erases a [__first,__last) range of elements from an
1225 * %unordered_multimap.
1226 * @param __first Iterator pointing to the start of the range to be
1227 * erased.
1228 * @param __last Iterator pointing to the end of the range to
1229 * be erased.
1230 * @return The iterator @a __last.
1232 * This function erases a sequence of elements from an
1233 * %unordered_multimap.
1234 * Note that this function only erases the elements, and that if
1235 * the element is itself a pointer, the pointed-to memory is not touched
1236 * in any way. Managing the pointer is the user's responsibility.
1238 iterator
1239 erase(const_iterator __first, const_iterator __last)
1240 { return _M_h.erase(__first, __last); }
1243 * Erases all elements in an %unordered_multimap.
1244 * Note that this function only erases the elements, and that if the
1245 * elements themselves are pointers, the pointed-to memory is not touched
1246 * in any way. Managing the pointer is the user's responsibility.
1248 void
1249 clear() noexcept
1250 { _M_h.clear(); }
1253 * @brief Swaps data with another %unordered_multimap.
1254 * @param __x An %unordered_multimap of the same element and allocator
1255 * types.
1257 * This exchanges the elements between two %unordered_multimap in
1258 * constant time.
1259 * Note that the global std::swap() function is specialized such that
1260 * std::swap(m1,m2) will feed to this function.
1262 void
1263 swap(unordered_multimap& __x)
1264 noexcept( noexcept(_M_h.swap(__x._M_h)) )
1265 { _M_h.swap(__x._M_h); }
1267 // observers.
1269 /// Returns the hash functor object with which the %unordered_multimap
1270 /// was constructed.
1271 hasher
1272 hash_function() const
1273 { return _M_h.hash_function(); }
1275 /// Returns the key comparison object with which the %unordered_multimap
1276 /// was constructed.
1277 key_equal
1278 key_eq() const
1279 { return _M_h.key_eq(); }
1281 // lookup.
1283 //@{
1285 * @brief Tries to locate an element in an %unordered_multimap.
1286 * @param __x Key to be located.
1287 * @return Iterator pointing to sought-after element, or end() if not
1288 * found.
1290 * This function takes a key and tries to locate the element with which
1291 * the key matches. If successful the function returns an iterator
1292 * pointing to the sought after element. If unsuccessful it returns the
1293 * past-the-end ( @c end() ) iterator.
1295 iterator
1296 find(const key_type& __x)
1297 { return _M_h.find(__x); }
1299 const_iterator
1300 find(const key_type& __x) const
1301 { return _M_h.find(__x); }
1302 //@}
1305 * @brief Finds the number of elements.
1306 * @param __x Key to count.
1307 * @return Number of elements with specified key.
1309 size_type
1310 count(const key_type& __x) const
1311 { return _M_h.count(__x); }
1313 //@{
1315 * @brief Finds a subsequence matching given key.
1316 * @param __x Key to be located.
1317 * @return Pair of iterators that possibly points to the subsequence
1318 * matching given key.
1320 std::pair<iterator, iterator>
1321 equal_range(const key_type& __x)
1322 { return _M_h.equal_range(__x); }
1324 std::pair<const_iterator, const_iterator>
1325 equal_range(const key_type& __x) const
1326 { return _M_h.equal_range(__x); }
1327 //@}
1329 // bucket interface.
1331 /// Returns the number of buckets of the %unordered_multimap.
1332 size_type
1333 bucket_count() const noexcept
1334 { return _M_h.bucket_count(); }
1336 /// Returns the maximum number of buckets of the %unordered_multimap.
1337 size_type
1338 max_bucket_count() const noexcept
1339 { return _M_h.max_bucket_count(); }
1342 * @brief Returns the number of elements in a given bucket.
1343 * @param __n A bucket index.
1344 * @return The number of elements in the bucket.
1346 size_type
1347 bucket_size(size_type __n) const
1348 { return _M_h.bucket_size(__n); }
1351 * @brief Returns the bucket index of a given element.
1352 * @param __key A key instance.
1353 * @return The key bucket index.
1355 size_type
1356 bucket(const key_type& __key) const
1357 { return _M_h.bucket(__key); }
1360 * @brief Returns a read/write iterator pointing to the first bucket
1361 * element.
1362 * @param __n The bucket index.
1363 * @return A read/write local iterator.
1365 local_iterator
1366 begin(size_type __n)
1367 { return _M_h.begin(__n); }
1369 //@{
1371 * @brief Returns a read-only (constant) iterator pointing to the first
1372 * bucket element.
1373 * @param __n The bucket index.
1374 * @return A read-only local iterator.
1376 const_local_iterator
1377 begin(size_type __n) const
1378 { return _M_h.begin(__n); }
1380 const_local_iterator
1381 cbegin(size_type __n) const
1382 { return _M_h.cbegin(__n); }
1383 //@}
1386 * @brief Returns a read/write iterator pointing to one past the last
1387 * bucket elements.
1388 * @param __n The bucket index.
1389 * @return A read/write local iterator.
1391 local_iterator
1392 end(size_type __n)
1393 { return _M_h.end(__n); }
1395 //@{
1397 * @brief Returns a read-only (constant) iterator pointing to one past
1398 * the last bucket elements.
1399 * @param __n The bucket index.
1400 * @return A read-only local iterator.
1402 const_local_iterator
1403 end(size_type __n) const
1404 { return _M_h.end(__n); }
1406 const_local_iterator
1407 cend(size_type __n) const
1408 { return _M_h.cend(__n); }
1409 //@}
1411 // hash policy.
1413 /// Returns the average number of elements per bucket.
1414 float
1415 load_factor() const noexcept
1416 { return _M_h.load_factor(); }
1418 /// Returns a positive number that the %unordered_multimap tries to keep
1419 /// the load factor less than or equal to.
1420 float
1421 max_load_factor() const noexcept
1422 { return _M_h.max_load_factor(); }
1425 * @brief Change the %unordered_multimap maximum load factor.
1426 * @param __z The new maximum load factor.
1428 void
1429 max_load_factor(float __z)
1430 { _M_h.max_load_factor(__z); }
1433 * @brief May rehash the %unordered_multimap.
1434 * @param __n The new number of buckets.
1436 * Rehash will occur only if the new number of buckets respect the
1437 * %unordered_multimap maximum load factor.
1439 void
1440 rehash(size_type __n)
1441 { _M_h.rehash(__n); }
1444 * @brief Prepare the %unordered_multimap for a specified number of
1445 * elements.
1446 * @param __n Number of elements required.
1448 * Same as rehash(ceil(n / max_load_factor())).
1450 void
1451 reserve(size_type __n)
1452 { _M_h.reserve(__n); }
1454 template<typename _Key1, typename _Tp1, typename _Hash1, typename _Pred1,
1455 typename _Alloc1>
1456 friend bool
1457 operator==(const unordered_multimap<_Key1, _Tp1,
1458 _Hash1, _Pred1, _Alloc1>&,
1459 const unordered_multimap<_Key1, _Tp1,
1460 _Hash1, _Pred1, _Alloc1>&);
1463 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1464 inline void
1465 swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1466 unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1467 { __x.swap(__y); }
1469 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1470 inline void
1471 swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1472 unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1473 { __x.swap(__y); }
1475 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1476 inline bool
1477 operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1478 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1479 { return __x._M_h._M_equal(__y._M_h); }
1481 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1482 inline bool
1483 operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1484 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1485 { return !(__x == __y); }
1487 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1488 inline bool
1489 operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1490 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1491 { return __x._M_h._M_equal(__y._M_h); }
1493 template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
1494 inline bool
1495 operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
1496 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
1497 { return !(__x == __y); }
1499 _GLIBCXX_END_NAMESPACE_CONTAINER
1500 } // namespace std
1502 #endif /* _UNORDERED_MAP_H */