1 // unordered_map implementation -*- C++ -*-
3 // Copyright (C) 2010-2015 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/unordered_map.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{unordered_map}
30 #ifndef _UNORDERED_MAP_H
31 #define _UNORDERED_MAP_H
33 namespace std
_GLIBCXX_VISIBILITY(default)
35 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
37 /// Base types for unordered_map.
39 using __umap_traits
= __detail::_Hashtable_traits
<_Cache
, false, true>;
41 template<typename _Key
,
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
,
50 __detail::_Mod_range_hashing
,
51 __detail::_Default_ranged_hash
,
52 __detail::_Prime_rehash_policy
, _Tr
>;
54 /// Base types for unordered_multimap.
56 using __ummap_traits
= __detail::_Hashtable_traits
<_Cache
, false, false>;
58 template<typename _Key
,
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
,
67 __detail::_Mod_range_hashing
,
68 __detail::_Default_ranged_hash
,
69 __detail::_Prime_rehash_policy
, _Tr
>;
72 * @brief A standard container composed of unique keys (containing
73 * at most one of each key value) that associates values of another type
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
> > >
100 typedef __umap_hashtable
<_Key
, _Tp
, _Hash
, _Pred
, _Alloc
> _Hashtable
;
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
;
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
;
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.
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
)
154 unordered_map(size_type __n
,
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
,
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.
193 unordered_map(const allocator_type
& __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
,
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.
238 operator=(const unordered_map
&) = default;
240 /// Move assignment operator.
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.
256 operator=(initializer_list
<value_type
> __l
)
262 /// Returns the allocator object with which the %unordered_map was
265 get_allocator() const noexcept
266 { return _M_h
.get_allocator(); }
268 // size and capacity:
270 /// Returns true if the %unordered_map is empty.
272 empty() const noexcept
273 { return _M_h
.empty(); }
275 /// Returns the size of the %unordered_map.
277 size() const noexcept
278 { return _M_h
.size(); }
280 /// Returns the maximum size of the %unordered_map.
282 max_size() const noexcept
283 { return _M_h
.max_size(); }
288 * Returns a read/write iterator that points to the first element in the
293 { return _M_h
.begin(); }
297 * Returns a read-only (constant) iterator that points to the first
298 * element in the %unordered_map.
301 begin() const noexcept
302 { return _M_h
.begin(); }
305 cbegin() const noexcept
306 { return _M_h
.begin(); }
310 * Returns a read/write iterator that points one past the last element in
311 * the %unordered_map.
315 { return _M_h
.end(); }
319 * Returns a read-only (constant) iterator that points one past the last
320 * element in the %unordered_map.
324 { return _M_h
.end(); }
327 cend() const noexcept
328 { return _M_h
.end(); }
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
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
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()
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.
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
>
384 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
385 { return _M_h
.emplace_hint(__pos
, std::forward
<_Args
>(__args
)...); }
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>
414 { return _M_h
.insert(std::forward
<_Pair
>(__x
)); }
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
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.
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.
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
>
447 insert(const_iterator __hint
, _Pair
&& __x
)
448 { return _M_h
.insert(__hint
, std::forward
<_Pair
>(__x
)); }
452 * @brief A template function that attempts to insert a range of
454 * @param __first Iterator pointing to the start of the range to be
456 * @param __last Iterator pointing to the end of the range.
458 * Complexity similar to that of the range constructor.
460 template<typename _InputIterator
>
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
470 * Complexity similar to that of the range constructor.
473 insert(initializer_list
<value_type
> __l
)
474 { _M_h
.insert(__l
); }
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.
491 erase(const_iterator __position
)
492 { return _M_h
.erase(__position
); }
496 erase(iterator __position
)
497 { return _M_h
.erase(__position
); }
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.
513 erase(const key_type
& __x
)
514 { return _M_h
.erase(__x
); }
517 * @brief Erases a [__first,__last) range of elements from an
519 * @param __first Iterator pointing to the start of the range to be
521 * @param __last Iterator pointing to the end of the range to
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.
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.
545 * @brief Swaps data with another %unordered_map.
546 * @param __x An %unordered_map of the same element and allocator
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.
554 swap(unordered_map
& __x
)
555 noexcept( noexcept(_M_h
.swap(__x
._M_h
)) )
556 { _M_h
.swap(__x
._M_h
); }
560 /// Returns the hash functor object with which the %unordered_map was
563 hash_function() const
564 { return _M_h
.hash_function(); }
566 /// Returns the key comparison object with which the %unordered_map was
570 { return _M_h
.key_eq(); }
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
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.
587 find(const key_type
& __x
)
588 { return _M_h
.find(__x
); }
591 find(const key_type
& __x
) const
592 { return _M_h
.find(__x
); }
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
605 count(const key_type
& __x
) const
606 { return _M_h
.count(__x
); }
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
); }
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
637 * Lookup requires constant time.
640 operator[](const key_type
& __k
)
641 { return _M_h
[__k
]; }
644 operator[](key_type
&& __k
)
645 { return _M_h
[std::move(__k
)]; }
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.
657 at(const key_type
& __k
)
658 { return _M_h
.at(__k
); }
661 at(const key_type
& __k
) const
662 { return _M_h
.at(__k
); }
667 /// Returns the number of buckets of the %unordered_map.
669 bucket_count() const noexcept
670 { return _M_h
.bucket_count(); }
672 /// Returns the maximum number of buckets of the %unordered_map.
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.
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.
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
698 * @param __n The bucket index.
699 * @return A read/write local iterator.
703 { return _M_h
.begin(__n
); }
707 * @brief Returns a read-only (constant) iterator pointing to the first
709 * @param __n The bucket index.
710 * @return A read-only local iterator.
713 begin(size_type __n
) const
714 { return _M_h
.begin(__n
); }
717 cbegin(size_type __n
) const
718 { return _M_h
.cbegin(__n
); }
722 * @brief Returns a read/write iterator pointing to one past the last
724 * @param __n The bucket index.
725 * @return A read/write local iterator.
729 { return _M_h
.end(__n
); }
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.
739 end(size_type __n
) const
740 { return _M_h
.end(__n
); }
743 cend(size_type __n
) const
744 { return _M_h
.cend(__n
); }
749 /// Returns the average number of elements per bucket.
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.
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.
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.
776 rehash(size_type __n
)
777 { _M_h
.rehash(__n
); }
780 * @brief Prepare the %unordered_map for a specified number of
782 * @param __n Number of elements required.
784 * Same as rehash(ceil(n / max_load_factor())).
787 reserve(size_type __n
)
788 { _M_h
.reserve(__n
); }
790 template<typename _Key1
, typename _Tp1
, typename _Hash1
, typename _Pred1
,
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
;
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
;
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
;
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.
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
,
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
,
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.
918 unordered_multimap(const allocator_type
& __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
,
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.
963 operator=(const unordered_multimap
&) = default;
965 /// Move assignment operator.
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.
981 operator=(initializer_list
<value_type
> __l
)
987 /// Returns the allocator object with which the %unordered_multimap was
990 get_allocator() const noexcept
991 { return _M_h
.get_allocator(); }
993 // size and capacity:
995 /// Returns true if the %unordered_multimap is empty.
997 empty() const noexcept
998 { return _M_h
.empty(); }
1000 /// Returns the size of the %unordered_multimap.
1002 size() const noexcept
1003 { return _M_h
.size(); }
1005 /// Returns the maximum size of the %unordered_multimap.
1007 max_size() const noexcept
1008 { return _M_h
.max_size(); }
1013 * Returns a read/write iterator that points to the first element in the
1014 * %unordered_multimap.
1018 { return _M_h
.begin(); }
1022 * Returns a read-only (constant) iterator that points to the first
1023 * element in the %unordered_multimap.
1026 begin() const noexcept
1027 { return _M_h
.begin(); }
1030 cbegin() const noexcept
1031 { return _M_h
.begin(); }
1035 * Returns a read/write iterator that points one past the last element in
1036 * the %unordered_multimap.
1040 { return _M_h
.end(); }
1044 * Returns a read-only (constant) iterator that points one past the last
1045 * element in the %unordered_multimap.
1048 end() const noexcept
1049 { return _M_h
.end(); }
1052 cend() const noexcept
1053 { return _M_h
.end(); }
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
>
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.
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
>
1101 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
1102 { return _M_h
.emplace_hint(__pos
, std::forward
<_Args
>(__args
)...); }
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.
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
>
1123 { return _M_h
.insert(std::forward
<_Pair
>(__x
)); }
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
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.
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.
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
>
1154 insert(const_iterator __hint
, _Pair
&& __x
)
1155 { return _M_h
.insert(__hint
, std::forward
<_Pair
>(__x
)); }
1159 * @brief A template function that attempts to insert a range of
1161 * @param __first Iterator pointing to the start of the range to be
1163 * @param __last Iterator pointing to the end of the range.
1165 * Complexity similar to that of the range constructor.
1167 template<typename _InputIterator
>
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
1178 * Complexity similar to that of the range constructor.
1181 insert(initializer_list
<value_type
> __l
)
1182 { _M_h
.insert(__l
); }
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.
1199 erase(const_iterator __position
)
1200 { return _M_h
.erase(__position
); }
1204 erase(iterator __position
)
1205 { return _M_h
.erase(__position
); }
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.
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
1228 * @param __last Iterator pointing to the end of the range to
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.
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.
1253 * @brief Swaps data with another %unordered_multimap.
1254 * @param __x An %unordered_multimap of the same element and allocator
1257 * This exchanges the elements between two %unordered_multimap in
1259 * Note that the global std::swap() function is specialized such that
1260 * std::swap(m1,m2) will feed to this function.
1263 swap(unordered_multimap
& __x
)
1264 noexcept( noexcept(_M_h
.swap(__x
._M_h
)) )
1265 { _M_h
.swap(__x
._M_h
); }
1269 /// Returns the hash functor object with which the %unordered_multimap
1270 /// was constructed.
1272 hash_function() const
1273 { return _M_h
.hash_function(); }
1275 /// Returns the key comparison object with which the %unordered_multimap
1276 /// was constructed.
1279 { return _M_h
.key_eq(); }
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
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.
1296 find(const key_type
& __x
)
1297 { return _M_h
.find(__x
); }
1300 find(const key_type
& __x
) const
1301 { return _M_h
.find(__x
); }
1305 * @brief Finds the number of elements.
1306 * @param __x Key to count.
1307 * @return Number of elements with specified key.
1310 count(const key_type
& __x
) const
1311 { return _M_h
.count(__x
); }
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
); }
1329 // bucket interface.
1331 /// Returns the number of buckets of the %unordered_multimap.
1333 bucket_count() const noexcept
1334 { return _M_h
.bucket_count(); }
1336 /// Returns the maximum number of buckets of the %unordered_multimap.
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.
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.
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
1362 * @param __n The bucket index.
1363 * @return A read/write local iterator.
1366 begin(size_type __n
)
1367 { return _M_h
.begin(__n
); }
1371 * @brief Returns a read-only (constant) iterator pointing to the first
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
); }
1386 * @brief Returns a read/write iterator pointing to one past the last
1388 * @param __n The bucket index.
1389 * @return A read/write local iterator.
1393 { return _M_h
.end(__n
); }
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
); }
1413 /// Returns the average number of elements per bucket.
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.
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.
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.
1440 rehash(size_type __n
)
1441 { _M_h
.rehash(__n
); }
1444 * @brief Prepare the %unordered_multimap for a specified number of
1446 * @param __n Number of elements required.
1448 * Same as rehash(ceil(n / max_load_factor())).
1451 reserve(size_type __n
)
1452 { _M_h
.reserve(__n
); }
1454 template<typename _Key1
, typename _Tp1
, typename _Hash1
, typename _Pred1
,
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
>
1465 swap(unordered_map
<_Key
, _Tp
, _Hash
, _Pred
, _Alloc
>& __x
,
1466 unordered_map
<_Key
, _Tp
, _Hash
, _Pred
, _Alloc
>& __y
)
1469 template<class _Key
, class _Tp
, class _Hash
, class _Pred
, class _Alloc
>
1471 swap(unordered_multimap
<_Key
, _Tp
, _Hash
, _Pred
, _Alloc
>& __x
,
1472 unordered_multimap
<_Key
, _Tp
, _Hash
, _Pred
, _Alloc
>& __y
)
1475 template<class _Key
, class _Tp
, class _Hash
, class _Pred
, class _Alloc
>
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
>
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
>
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
>
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
1502 #endif /* _UNORDERED_MAP_H */