1 // unordered_set implementation -*- C++ -*-
3 // Copyright (C) 2010-2014 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_set.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{unordered_set}
30 #ifndef _UNORDERED_SET_H
31 #define _UNORDERED_SET_H
33 namespace std
_GLIBCXX_VISIBILITY(default)
35 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
37 /// Base types for unordered_set.
39 using __uset_traits
= __detail::_Hashtable_traits
<_Cache
, true, true>;
41 template<typename _Value
,
42 typename _Hash
= hash
<_Value
>,
43 typename _Pred
= std::equal_to
<_Value
>,
44 typename _Alloc
= std::allocator
<_Value
>,
45 typename _Tr
= __uset_traits
<__cache_default
<_Value
, _Hash
>::value
>>
46 using __uset_hashtable
= _Hashtable
<_Value
, _Value
, _Alloc
,
47 __detail::_Identity
, _Pred
, _Hash
,
48 __detail::_Mod_range_hashing
,
49 __detail::_Default_ranged_hash
,
50 __detail::_Prime_rehash_policy
, _Tr
>;
52 /// Base types for unordered_multiset.
54 using __umset_traits
= __detail::_Hashtable_traits
<_Cache
, true, false>;
56 template<typename _Value
,
57 typename _Hash
= hash
<_Value
>,
58 typename _Pred
= std::equal_to
<_Value
>,
59 typename _Alloc
= std::allocator
<_Value
>,
60 typename _Tr
= __umset_traits
<__cache_default
<_Value
, _Hash
>::value
>>
61 using __umset_hashtable
= _Hashtable
<_Value
, _Value
, _Alloc
,
64 __detail::_Mod_range_hashing
,
65 __detail::_Default_ranged_hash
,
66 __detail::_Prime_rehash_policy
, _Tr
>;
69 * @brief A standard container composed of unique keys (containing
70 * at most one of each key value) in which the elements' keys are
71 * the elements themselves.
73 * @ingroup unordered_associative_containers
75 * @tparam _Value Type of key objects.
76 * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
78 * @tparam _Pred Predicate function object type, defaults to
81 * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
83 * Meets the requirements of a <a href="tables.html#65">container</a>, and
84 * <a href="tables.html#xx">unordered associative container</a>
86 * Base is _Hashtable, dispatched at compile time via template
87 * alias __uset_hashtable.
89 template<class _Value
,
90 class _Hash
= hash
<_Value
>,
91 class _Pred
= std::equal_to
<_Value
>,
92 class _Alloc
= std::allocator
<_Value
> >
95 typedef __uset_hashtable
<_Value
, _Hash
, _Pred
, _Alloc
> _Hashtable
;
102 typedef typename
_Hashtable::key_type key_type
;
103 typedef typename
_Hashtable::value_type value_type
;
104 typedef typename
_Hashtable::hasher hasher
;
105 typedef typename
_Hashtable::key_equal key_equal
;
106 typedef typename
_Hashtable::allocator_type allocator_type
;
110 /// Iterator-related typedefs.
111 typedef typename
_Hashtable::pointer pointer
;
112 typedef typename
_Hashtable::const_pointer const_pointer
;
113 typedef typename
_Hashtable::reference reference
;
114 typedef typename
_Hashtable::const_reference const_reference
;
115 typedef typename
_Hashtable::iterator iterator
;
116 typedef typename
_Hashtable::const_iterator const_iterator
;
117 typedef typename
_Hashtable::local_iterator local_iterator
;
118 typedef typename
_Hashtable::const_local_iterator const_local_iterator
;
119 typedef typename
_Hashtable::size_type size_type
;
120 typedef typename
_Hashtable::difference_type difference_type
;
123 // construct/destroy/copy
125 * @brief Default constructor creates no elements.
126 * @param __n Initial number of buckets.
127 * @param __hf A hash functor.
128 * @param __eql A key equality functor.
129 * @param __a An allocator object.
132 unordered_set(size_type __n
= 10,
133 const hasher
& __hf
= hasher(),
134 const key_equal
& __eql
= key_equal(),
135 const allocator_type
& __a
= allocator_type())
136 : _M_h(__n
, __hf
, __eql
, __a
)
140 * @brief Builds an %unordered_set from a range.
141 * @param __first An input iterator.
142 * @param __last An input iterator.
143 * @param __n Minimal initial number of buckets.
144 * @param __hf A hash functor.
145 * @param __eql A key equality functor.
146 * @param __a An allocator object.
148 * Create an %unordered_set consisting of copies of the elements from
149 * [__first,__last). This is linear in N (where N is
150 * distance(__first,__last)).
152 template<typename _InputIterator
>
153 unordered_set(_InputIterator __first
, _InputIterator __last
,
155 const hasher
& __hf
= hasher(),
156 const key_equal
& __eql
= key_equal(),
157 const allocator_type
& __a
= allocator_type())
158 : _M_h(__first
, __last
, __n
, __hf
, __eql
, __a
)
161 /// Copy constructor.
162 unordered_set(const unordered_set
&) = default;
164 /// Move constructor.
165 unordered_set(unordered_set
&&) = default;
168 * @brief Creates an %unordered_set with no elements.
169 * @param __a An allocator object.
172 unordered_set(const allocator_type
& __a
)
177 * @brief Copy constructor with allocator argument.
178 * @param __uset Input %unordered_set to copy.
179 * @param __a An allocator object.
181 unordered_set(const unordered_set
& __uset
,
182 const allocator_type
& __a
)
183 : _M_h(__uset
._M_h
, __a
)
187 * @brief Move constructor with allocator argument.
188 * @param __uset Input %unordered_set to move.
189 * @param __a An allocator object.
191 unordered_set(unordered_set
&& __uset
,
192 const allocator_type
& __a
)
193 : _M_h(std::move(__uset
._M_h
), __a
)
197 * @brief Builds an %unordered_set from an initializer_list.
198 * @param __l An initializer_list.
199 * @param __n Minimal initial number of buckets.
200 * @param __hf A hash functor.
201 * @param __eql A key equality functor.
202 * @param __a An allocator object.
204 * Create an %unordered_set consisting of copies of the elements in the
205 * list. This is linear in N (where N is @a __l.size()).
207 unordered_set(initializer_list
<value_type
> __l
,
209 const hasher
& __hf
= hasher(),
210 const key_equal
& __eql
= key_equal(),
211 const allocator_type
& __a
= allocator_type())
212 : _M_h(__l
, __n
, __hf
, __eql
, __a
)
215 /// Copy assignment operator.
217 operator=(const unordered_set
&) = default;
219 /// Move assignment operator.
221 operator=(unordered_set
&&) = default;
224 * @brief %Unordered_set list assignment operator.
225 * @param __l An initializer_list.
227 * This function fills an %unordered_set with copies of the elements in
228 * the initializer list @a __l.
230 * Note that the assignment completely changes the %unordered_set and
231 * that the resulting %unordered_set's size is the same as the number
232 * of elements assigned. Old data may be lost.
235 operator=(initializer_list
<value_type
> __l
)
241 /// Returns the allocator object with which the %unordered_set was
244 get_allocator() const noexcept
245 { return _M_h
.get_allocator(); }
247 // size and capacity:
249 /// Returns true if the %unordered_set is empty.
251 empty() const noexcept
252 { return _M_h
.empty(); }
254 /// Returns the size of the %unordered_set.
256 size() const noexcept
257 { return _M_h
.size(); }
259 /// Returns the maximum size of the %unordered_set.
261 max_size() const noexcept
262 { return _M_h
.max_size(); }
268 * Returns a read-only (constant) iterator that points to the first
269 * element in the %unordered_set.
273 { return _M_h
.begin(); }
276 begin() const noexcept
277 { return _M_h
.begin(); }
282 * Returns a read-only (constant) iterator that points one past the last
283 * element in the %unordered_set.
287 { return _M_h
.end(); }
291 { return _M_h
.end(); }
295 * Returns a read-only (constant) iterator that points to the first
296 * element in the %unordered_set.
299 cbegin() const noexcept
300 { return _M_h
.begin(); }
303 * Returns a read-only (constant) iterator that points one past the last
304 * element in the %unordered_set.
307 cend() const noexcept
308 { return _M_h
.end(); }
313 * @brief Attempts to build and insert an element into the
315 * @param __args Arguments used to generate an element.
316 * @return A pair, of which the first element is an iterator that points
317 * to the possibly inserted element, and the second is a bool
318 * that is true if the element was actually inserted.
320 * This function attempts to build and insert an element into the
321 * %unordered_set. An %unordered_set relies on unique keys and thus an
322 * element is only inserted if it is not already present in the
325 * Insertion requires amortized constant time.
327 template<typename
... _Args
>
328 std::pair
<iterator
, bool>
329 emplace(_Args
&&... __args
)
330 { return _M_h
.emplace(std::forward
<_Args
>(__args
)...); }
333 * @brief Attempts to insert an element into the %unordered_set.
334 * @param __pos An iterator that serves as a hint as to where the
335 * element should be inserted.
336 * @param __args Arguments used to generate the element to be
338 * @return An iterator that points to the element with key equivalent to
339 * the one generated from @a __args (may or may not be the
342 * This function is not concerned about whether the insertion took place,
343 * and thus does not return a boolean like the single-argument emplace()
344 * does. Note that the first parameter is only a hint and can
345 * potentially improve the performance of the insertion process. A bad
346 * hint would cause no gains in efficiency.
348 * For more on @a hinting, see:
349 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
351 * Insertion requires amortized constant time.
353 template<typename
... _Args
>
355 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
356 { return _M_h
.emplace_hint(__pos
, std::forward
<_Args
>(__args
)...); }
360 * @brief Attempts to insert an element into the %unordered_set.
361 * @param __x Element to be inserted.
362 * @return A pair, of which the first element is an iterator that points
363 * to the possibly inserted element, and the second is a bool
364 * that is true if the element was actually inserted.
366 * This function attempts to insert an element into the %unordered_set.
367 * An %unordered_set relies on unique keys and thus an element is only
368 * inserted if it is not already present in the %unordered_set.
370 * Insertion requires amortized constant time.
372 std::pair
<iterator
, bool>
373 insert(const value_type
& __x
)
374 { return _M_h
.insert(__x
); }
376 std::pair
<iterator
, bool>
377 insert(value_type
&& __x
)
378 { return _M_h
.insert(std::move(__x
)); }
383 * @brief Attempts to insert an element into the %unordered_set.
384 * @param __hint An iterator that serves as a hint as to where the
385 * element should be inserted.
386 * @param __x Element to be inserted.
387 * @return An iterator that points to the element with key of
388 * @a __x (may or may not be the element passed in).
390 * This function is not concerned about whether the insertion took place,
391 * and thus does not return a boolean like the single-argument insert()
392 * does. Note that the first parameter is only a hint and can
393 * potentially improve the performance of the insertion process. A bad
394 * hint would cause no gains in efficiency.
396 * For more on @a hinting, see:
397 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
399 * Insertion requires amortized constant.
402 insert(const_iterator __hint
, const value_type
& __x
)
403 { return _M_h
.insert(__hint
, __x
); }
406 insert(const_iterator __hint
, value_type
&& __x
)
407 { return _M_h
.insert(__hint
, std::move(__x
)); }
411 * @brief A template function that attempts to insert a range of
413 * @param __first Iterator pointing to the start of the range to be
415 * @param __last Iterator pointing to the end of the range.
417 * Complexity similar to that of the range constructor.
419 template<typename _InputIterator
>
421 insert(_InputIterator __first
, _InputIterator __last
)
422 { _M_h
.insert(__first
, __last
); }
425 * @brief Attempts to insert a list of elements into the %unordered_set.
426 * @param __l A std::initializer_list<value_type> of elements
429 * Complexity similar to that of the range constructor.
432 insert(initializer_list
<value_type
> __l
)
433 { _M_h
.insert(__l
); }
437 * @brief Erases an element from an %unordered_set.
438 * @param __position An iterator pointing to the element to be erased.
439 * @return An iterator pointing to the element immediately following
440 * @a __position prior to the element being erased. If no such
441 * element exists, end() is returned.
443 * This function erases an element, pointed to by the given iterator,
444 * from an %unordered_set. Note that this function only erases the
445 * element, and that if the element is itself a pointer, the pointed-to
446 * memory is not touched in any way. Managing the pointer is the user's
450 erase(const_iterator __position
)
451 { return _M_h
.erase(__position
); }
455 erase(iterator __position
)
456 { return _M_h
.erase(__position
); }
460 * @brief Erases elements according to the provided key.
461 * @param __x Key of element to be erased.
462 * @return The number of elements erased.
464 * This function erases all the elements located by the given key from
465 * an %unordered_set. For an %unordered_set the result of this function
466 * can only be 0 (not present) or 1 (present).
467 * Note that this function only erases the element, and that if
468 * the element is itself a pointer, the pointed-to memory is not touched
469 * in any way. Managing the pointer is the user's responsibility.
472 erase(const key_type
& __x
)
473 { return _M_h
.erase(__x
); }
476 * @brief Erases a [__first,__last) range of elements from an
478 * @param __first Iterator pointing to the start of the range to be
480 * @param __last Iterator pointing to the end of the range to
482 * @return The iterator @a __last.
484 * This function erases a sequence of elements from an %unordered_set.
485 * Note that this function only erases the element, and that if
486 * the element is itself a pointer, the pointed-to memory is not touched
487 * in any way. Managing the pointer is the user's responsibility.
490 erase(const_iterator __first
, const_iterator __last
)
491 { return _M_h
.erase(__first
, __last
); }
494 * Erases all elements in an %unordered_set. Note that this function only
495 * erases the elements, and that if the elements themselves are pointers,
496 * the pointed-to memory is not touched in any way. Managing the pointer
497 * is the user's responsibility.
504 * @brief Swaps data with another %unordered_set.
505 * @param __x An %unordered_set of the same element and allocator
508 * This exchanges the elements between two sets in constant time.
509 * Note that the global std::swap() function is specialized such that
510 * std::swap(s1,s2) will feed to this function.
513 swap(unordered_set
& __x
)
514 noexcept( noexcept(_M_h
.swap(__x
._M_h
)) )
515 { _M_h
.swap(__x
._M_h
); }
519 /// Returns the hash functor object with which the %unordered_set was
522 hash_function() const
523 { return _M_h
.hash_function(); }
525 /// Returns the key comparison object with which the %unordered_set was
529 { return _M_h
.key_eq(); }
535 * @brief Tries to locate an element in an %unordered_set.
536 * @param __x Element to be located.
537 * @return Iterator pointing to sought-after element, or end() if not
540 * This function takes a key and tries to locate the element with which
541 * the key matches. If successful the function returns an iterator
542 * pointing to the sought after element. If unsuccessful it returns the
543 * past-the-end ( @c end() ) iterator.
546 find(const key_type
& __x
)
547 { return _M_h
.find(__x
); }
550 find(const key_type
& __x
) const
551 { return _M_h
.find(__x
); }
555 * @brief Finds the number of elements.
556 * @param __x Element to located.
557 * @return Number of elements with specified key.
559 * This function only makes sense for unordered_multisets; for
560 * unordered_set the result will either be 0 (not present) or 1
564 count(const key_type
& __x
) const
565 { return _M_h
.count(__x
); }
569 * @brief Finds a subsequence matching given key.
570 * @param __x Key to be located.
571 * @return Pair of iterators that possibly points to the subsequence
572 * matching given key.
574 * This function probably only makes sense for multisets.
576 std::pair
<iterator
, iterator
>
577 equal_range(const key_type
& __x
)
578 { return _M_h
.equal_range(__x
); }
580 std::pair
<const_iterator
, const_iterator
>
581 equal_range(const key_type
& __x
) const
582 { return _M_h
.equal_range(__x
); }
587 /// Returns the number of buckets of the %unordered_set.
589 bucket_count() const noexcept
590 { return _M_h
.bucket_count(); }
592 /// Returns the maximum number of buckets of the %unordered_set.
594 max_bucket_count() const noexcept
595 { return _M_h
.max_bucket_count(); }
598 * @brief Returns the number of elements in a given bucket.
599 * @param __n A bucket index.
600 * @return The number of elements in the bucket.
603 bucket_size(size_type __n
) const
604 { return _M_h
.bucket_size(__n
); }
607 * @brief Returns the bucket index of a given element.
608 * @param __key A key instance.
609 * @return The key bucket index.
612 bucket(const key_type
& __key
) const
613 { return _M_h
.bucket(__key
); }
617 * @brief Returns a read-only (constant) iterator pointing to the first
619 * @param __n The bucket index.
620 * @return A read-only local iterator.
624 { return _M_h
.begin(__n
); }
627 begin(size_type __n
) const
628 { return _M_h
.begin(__n
); }
631 cbegin(size_type __n
) const
632 { return _M_h
.cbegin(__n
); }
637 * @brief Returns a read-only (constant) iterator pointing to one past
638 * the last bucket elements.
639 * @param __n The bucket index.
640 * @return A read-only local iterator.
644 { return _M_h
.end(__n
); }
647 end(size_type __n
) const
648 { return _M_h
.end(__n
); }
651 cend(size_type __n
) const
652 { return _M_h
.cend(__n
); }
657 /// Returns the average number of elements per bucket.
659 load_factor() const noexcept
660 { return _M_h
.load_factor(); }
662 /// Returns a positive number that the %unordered_set tries to keep the
663 /// load factor less than or equal to.
665 max_load_factor() const noexcept
666 { return _M_h
.max_load_factor(); }
669 * @brief Change the %unordered_set maximum load factor.
670 * @param __z The new maximum load factor.
673 max_load_factor(float __z
)
674 { _M_h
.max_load_factor(__z
); }
677 * @brief May rehash the %unordered_set.
678 * @param __n The new number of buckets.
680 * Rehash will occur only if the new number of buckets respect the
681 * %unordered_set maximum load factor.
684 rehash(size_type __n
)
685 { _M_h
.rehash(__n
); }
688 * @brief Prepare the %unordered_set for a specified number of
690 * @param __n Number of elements required.
692 * Same as rehash(ceil(n / max_load_factor())).
695 reserve(size_type __n
)
696 { _M_h
.reserve(__n
); }
698 template<typename _Value1
, typename _Hash1
, typename _Pred1
,
701 operator==(const unordered_set
<_Value1
, _Hash1
, _Pred1
, _Alloc1
>&,
702 const unordered_set
<_Value1
, _Hash1
, _Pred1
, _Alloc1
>&);
706 * @brief A standard container composed of equivalent keys
707 * (possibly containing multiple of each key value) in which the
708 * elements' keys are the elements themselves.
710 * @ingroup unordered_associative_containers
712 * @tparam _Value Type of key objects.
713 * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
714 * @tparam _Pred Predicate function object type, defaults
715 * to equal_to<_Value>.
716 * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
718 * Meets the requirements of a <a href="tables.html#65">container</a>, and
719 * <a href="tables.html#xx">unordered associative container</a>
721 * Base is _Hashtable, dispatched at compile time via template
722 * alias __umset_hashtable.
724 template<class _Value
,
725 class _Hash
= hash
<_Value
>,
726 class _Pred
= std::equal_to
<_Value
>,
727 class _Alloc
= std::allocator
<_Value
> >
728 class unordered_multiset
730 typedef __umset_hashtable
<_Value
, _Hash
, _Pred
, _Alloc
> _Hashtable
;
737 typedef typename
_Hashtable::key_type key_type
;
738 typedef typename
_Hashtable::value_type value_type
;
739 typedef typename
_Hashtable::hasher hasher
;
740 typedef typename
_Hashtable::key_equal key_equal
;
741 typedef typename
_Hashtable::allocator_type allocator_type
;
745 /// Iterator-related typedefs.
746 typedef typename
_Hashtable::pointer pointer
;
747 typedef typename
_Hashtable::const_pointer const_pointer
;
748 typedef typename
_Hashtable::reference reference
;
749 typedef typename
_Hashtable::const_reference const_reference
;
750 typedef typename
_Hashtable::iterator iterator
;
751 typedef typename
_Hashtable::const_iterator const_iterator
;
752 typedef typename
_Hashtable::local_iterator local_iterator
;
753 typedef typename
_Hashtable::const_local_iterator const_local_iterator
;
754 typedef typename
_Hashtable::size_type size_type
;
755 typedef typename
_Hashtable::difference_type difference_type
;
758 // construct/destroy/copy
760 * @brief Default constructor creates no elements.
761 * @param __n Initial number of buckets.
762 * @param __hf A hash functor.
763 * @param __eql A key equality functor.
764 * @param __a An allocator object.
767 unordered_multiset(size_type __n
= 10,
768 const hasher
& __hf
= hasher(),
769 const key_equal
& __eql
= key_equal(),
770 const allocator_type
& __a
= allocator_type())
771 : _M_h(__n
, __hf
, __eql
, __a
)
775 * @brief Builds an %unordered_multiset from a range.
776 * @param __first An input iterator.
777 * @param __last An input iterator.
778 * @param __n Minimal initial number of buckets.
779 * @param __hf A hash functor.
780 * @param __eql A key equality functor.
781 * @param __a An allocator object.
783 * Create an %unordered_multiset consisting of copies of the elements
784 * from [__first,__last). This is linear in N (where N is
785 * distance(__first,__last)).
787 template<typename _InputIterator
>
788 unordered_multiset(_InputIterator __first
, _InputIterator __last
,
790 const hasher
& __hf
= hasher(),
791 const key_equal
& __eql
= key_equal(),
792 const allocator_type
& __a
= allocator_type())
793 : _M_h(__first
, __last
, __n
, __hf
, __eql
, __a
)
796 /// Copy constructor.
797 unordered_multiset(const unordered_multiset
&) = default;
799 /// Move constructor.
800 unordered_multiset(unordered_multiset
&&) = default;
803 * @brief Builds an %unordered_multiset from an initializer_list.
804 * @param __l An initializer_list.
805 * @param __n Minimal initial number of buckets.
806 * @param __hf A hash functor.
807 * @param __eql A key equality functor.
808 * @param __a An allocator object.
810 * Create an %unordered_multiset consisting of copies of the elements in
811 * the list. This is linear in N (where N is @a __l.size()).
813 unordered_multiset(initializer_list
<value_type
> __l
,
815 const hasher
& __hf
= hasher(),
816 const key_equal
& __eql
= key_equal(),
817 const allocator_type
& __a
= allocator_type())
818 : _M_h(__l
, __n
, __hf
, __eql
, __a
)
821 /// Copy assignment operator.
823 operator=(const unordered_multiset
&) = default;
825 /// Move assignment operator.
827 operator=(unordered_multiset
&&) = default;
830 * @brief Creates an %unordered_multiset with no elements.
831 * @param __a An allocator object.
834 unordered_multiset(const allocator_type
& __a
)
839 * @brief Copy constructor with allocator argument.
840 * @param __uset Input %unordered_multiset to copy.
841 * @param __a An allocator object.
843 unordered_multiset(const unordered_multiset
& __umset
,
844 const allocator_type
& __a
)
845 : _M_h(__umset
._M_h
, __a
)
849 * @brief Move constructor with allocator argument.
850 * @param __umset Input %unordered_multiset to move.
851 * @param __a An allocator object.
853 unordered_multiset(unordered_multiset
&& __umset
,
854 const allocator_type
& __a
)
855 : _M_h(std::move(__umset
._M_h
), __a
)
859 * @brief %Unordered_multiset list assignment operator.
860 * @param __l An initializer_list.
862 * This function fills an %unordered_multiset with copies of the elements
863 * in the initializer list @a __l.
865 * Note that the assignment completely changes the %unordered_multiset
866 * and that the resulting %unordered_set's size is the same as the number
867 * of elements assigned. Old data may be lost.
870 operator=(initializer_list
<value_type
> __l
)
876 /// Returns the allocator object with which the %unordered_multiset was
879 get_allocator() const noexcept
880 { return _M_h
.get_allocator(); }
882 // size and capacity:
884 /// Returns true if the %unordered_multiset is empty.
886 empty() const noexcept
887 { return _M_h
.empty(); }
889 /// Returns the size of the %unordered_multiset.
891 size() const noexcept
892 { return _M_h
.size(); }
894 /// Returns the maximum size of the %unordered_multiset.
896 max_size() const noexcept
897 { return _M_h
.max_size(); }
903 * Returns a read-only (constant) iterator that points to the first
904 * element in the %unordered_multiset.
908 { return _M_h
.begin(); }
911 begin() const noexcept
912 { return _M_h
.begin(); }
917 * Returns a read-only (constant) iterator that points one past the last
918 * element in the %unordered_multiset.
922 { return _M_h
.end(); }
926 { return _M_h
.end(); }
930 * Returns a read-only (constant) iterator that points to the first
931 * element in the %unordered_multiset.
934 cbegin() const noexcept
935 { return _M_h
.begin(); }
938 * Returns a read-only (constant) iterator that points one past the last
939 * element in the %unordered_multiset.
942 cend() const noexcept
943 { return _M_h
.end(); }
948 * @brief Builds and insert an element into the %unordered_multiset.
949 * @param __args Arguments used to generate an element.
950 * @return An iterator that points to the inserted element.
952 * Insertion requires amortized constant time.
954 template<typename
... _Args
>
956 emplace(_Args
&&... __args
)
957 { return _M_h
.emplace(std::forward
<_Args
>(__args
)...); }
960 * @brief Inserts an element into the %unordered_multiset.
961 * @param __pos An iterator that serves as a hint as to where the
962 * element should be inserted.
963 * @param __args Arguments used to generate the element to be
965 * @return An iterator that points to the inserted element.
967 * Note that the first parameter is only a hint and can potentially
968 * improve the performance of the insertion process. A bad hint would
969 * cause no gains in efficiency.
971 * For more on @a hinting, see:
972 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
974 * Insertion requires amortized constant time.
976 template<typename
... _Args
>
978 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
979 { return _M_h
.emplace_hint(__pos
, std::forward
<_Args
>(__args
)...); }
983 * @brief Inserts an element into the %unordered_multiset.
984 * @param __x Element to be inserted.
985 * @return An iterator that points to the inserted element.
987 * Insertion requires amortized constant time.
990 insert(const value_type
& __x
)
991 { return _M_h
.insert(__x
); }
994 insert(value_type
&& __x
)
995 { return _M_h
.insert(std::move(__x
)); }
1000 * @brief Inserts an element into the %unordered_multiset.
1001 * @param __hint An iterator that serves as a hint as to where the
1002 * element should be inserted.
1003 * @param __x Element to be inserted.
1004 * @return An iterator that points to the inserted element.
1006 * Note that the first parameter is only a hint and can potentially
1007 * improve the performance of the insertion process. A bad hint would
1008 * cause no gains in efficiency.
1010 * For more on @a hinting, see:
1011 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
1013 * Insertion requires amortized constant.
1016 insert(const_iterator __hint
, const value_type
& __x
)
1017 { return _M_h
.insert(__hint
, __x
); }
1020 insert(const_iterator __hint
, value_type
&& __x
)
1021 { return _M_h
.insert(__hint
, std::move(__x
)); }
1025 * @brief A template function that inserts a range of elements.
1026 * @param __first Iterator pointing to the start of the range to be
1028 * @param __last Iterator pointing to the end of the range.
1030 * Complexity similar to that of the range constructor.
1032 template<typename _InputIterator
>
1034 insert(_InputIterator __first
, _InputIterator __last
)
1035 { _M_h
.insert(__first
, __last
); }
1038 * @brief Inserts a list of elements into the %unordered_multiset.
1039 * @param __l A std::initializer_list<value_type> of elements to be
1042 * Complexity similar to that of the range constructor.
1045 insert(initializer_list
<value_type
> __l
)
1046 { _M_h
.insert(__l
); }
1050 * @brief Erases an element from an %unordered_multiset.
1051 * @param __position An iterator pointing to the element to be erased.
1052 * @return An iterator pointing to the element immediately following
1053 * @a __position prior to the element being erased. If no such
1054 * element exists, end() is returned.
1056 * This function erases an element, pointed to by the given iterator,
1057 * from an %unordered_multiset.
1059 * Note that this function only erases the element, and that if the
1060 * element is itself a pointer, the pointed-to memory is not touched in
1061 * any way. Managing the pointer is the user's responsibility.
1064 erase(const_iterator __position
)
1065 { return _M_h
.erase(__position
); }
1069 erase(iterator __position
)
1070 { return _M_h
.erase(__position
); }
1075 * @brief Erases elements according to the provided key.
1076 * @param __x Key of element to be erased.
1077 * @return The number of elements erased.
1079 * This function erases all the elements located by the given key from
1080 * an %unordered_multiset.
1082 * Note that this function only erases the element, and that if the
1083 * element is itself a pointer, the pointed-to memory is not touched in
1084 * any way. Managing the pointer is the user's responsibility.
1087 erase(const key_type
& __x
)
1088 { return _M_h
.erase(__x
); }
1091 * @brief Erases a [__first,__last) range of elements from an
1092 * %unordered_multiset.
1093 * @param __first Iterator pointing to the start of the range to be
1095 * @param __last Iterator pointing to the end of the range to
1097 * @return The iterator @a __last.
1099 * This function erases a sequence of elements from an
1100 * %unordered_multiset.
1102 * Note that this function only erases the element, and that if
1103 * the element is itself a pointer, the pointed-to memory is not touched
1104 * in any way. Managing the pointer is the user's responsibility.
1107 erase(const_iterator __first
, const_iterator __last
)
1108 { return _M_h
.erase(__first
, __last
); }
1111 * Erases all elements in an %unordered_multiset.
1113 * Note that this function only erases the elements, and that if the
1114 * elements themselves are pointers, the pointed-to memory is not touched
1115 * in any way. Managing the pointer is the user's responsibility.
1122 * @brief Swaps data with another %unordered_multiset.
1123 * @param __x An %unordered_multiset of the same element and allocator
1126 * This exchanges the elements between two sets in constant time.
1127 * Note that the global std::swap() function is specialized such that
1128 * std::swap(s1,s2) will feed to this function.
1131 swap(unordered_multiset
& __x
)
1132 noexcept( noexcept(_M_h
.swap(__x
._M_h
)) )
1133 { _M_h
.swap(__x
._M_h
); }
1137 /// Returns the hash functor object with which the %unordered_multiset
1138 /// was constructed.
1140 hash_function() const
1141 { return _M_h
.hash_function(); }
1143 /// Returns the key comparison object with which the %unordered_multiset
1144 /// was constructed.
1147 { return _M_h
.key_eq(); }
1153 * @brief Tries to locate an element in an %unordered_multiset.
1154 * @param __x Element to be located.
1155 * @return Iterator pointing to sought-after element, or end() if not
1158 * This function takes a key and tries to locate the element with which
1159 * the key matches. If successful the function returns an iterator
1160 * pointing to the sought after element. If unsuccessful it returns the
1161 * past-the-end ( @c end() ) iterator.
1164 find(const key_type
& __x
)
1165 { return _M_h
.find(__x
); }
1168 find(const key_type
& __x
) const
1169 { return _M_h
.find(__x
); }
1173 * @brief Finds the number of elements.
1174 * @param __x Element to located.
1175 * @return Number of elements with specified key.
1178 count(const key_type
& __x
) const
1179 { return _M_h
.count(__x
); }
1183 * @brief Finds a subsequence matching given key.
1184 * @param __x Key to be located.
1185 * @return Pair of iterators that possibly points to the subsequence
1186 * matching given key.
1188 std::pair
<iterator
, iterator
>
1189 equal_range(const key_type
& __x
)
1190 { return _M_h
.equal_range(__x
); }
1192 std::pair
<const_iterator
, const_iterator
>
1193 equal_range(const key_type
& __x
) const
1194 { return _M_h
.equal_range(__x
); }
1197 // bucket interface.
1199 /// Returns the number of buckets of the %unordered_multiset.
1201 bucket_count() const noexcept
1202 { return _M_h
.bucket_count(); }
1204 /// Returns the maximum number of buckets of the %unordered_multiset.
1206 max_bucket_count() const noexcept
1207 { return _M_h
.max_bucket_count(); }
1210 * @brief Returns the number of elements in a given bucket.
1211 * @param __n A bucket index.
1212 * @return The number of elements in the bucket.
1215 bucket_size(size_type __n
) const
1216 { return _M_h
.bucket_size(__n
); }
1219 * @brief Returns the bucket index of a given element.
1220 * @param __key A key instance.
1221 * @return The key bucket index.
1224 bucket(const key_type
& __key
) const
1225 { return _M_h
.bucket(__key
); }
1229 * @brief Returns a read-only (constant) iterator pointing to the first
1231 * @param __n The bucket index.
1232 * @return A read-only local iterator.
1235 begin(size_type __n
)
1236 { return _M_h
.begin(__n
); }
1238 const_local_iterator
1239 begin(size_type __n
) const
1240 { return _M_h
.begin(__n
); }
1242 const_local_iterator
1243 cbegin(size_type __n
) const
1244 { return _M_h
.cbegin(__n
); }
1249 * @brief Returns a read-only (constant) iterator pointing to one past
1250 * the last bucket elements.
1251 * @param __n The bucket index.
1252 * @return A read-only local iterator.
1256 { return _M_h
.end(__n
); }
1258 const_local_iterator
1259 end(size_type __n
) const
1260 { return _M_h
.end(__n
); }
1262 const_local_iterator
1263 cend(size_type __n
) const
1264 { return _M_h
.cend(__n
); }
1269 /// Returns the average number of elements per bucket.
1271 load_factor() const noexcept
1272 { return _M_h
.load_factor(); }
1274 /// Returns a positive number that the %unordered_multiset tries to keep the
1275 /// load factor less than or equal to.
1277 max_load_factor() const noexcept
1278 { return _M_h
.max_load_factor(); }
1281 * @brief Change the %unordered_multiset maximum load factor.
1282 * @param __z The new maximum load factor.
1285 max_load_factor(float __z
)
1286 { _M_h
.max_load_factor(__z
); }
1289 * @brief May rehash the %unordered_multiset.
1290 * @param __n The new number of buckets.
1292 * Rehash will occur only if the new number of buckets respect the
1293 * %unordered_multiset maximum load factor.
1296 rehash(size_type __n
)
1297 { _M_h
.rehash(__n
); }
1300 * @brief Prepare the %unordered_multiset for a specified number of
1302 * @param __n Number of elements required.
1304 * Same as rehash(ceil(n / max_load_factor())).
1307 reserve(size_type __n
)
1308 { _M_h
.reserve(__n
); }
1310 template<typename _Value1
, typename _Hash1
, typename _Pred1
,
1313 operator==(const unordered_multiset
<_Value1
, _Hash1
, _Pred1
, _Alloc1
>&,
1314 const unordered_multiset
<_Value1
, _Hash1
, _Pred1
, _Alloc1
>&);
1317 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1319 swap(unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1320 unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1323 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1325 swap(unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1326 unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1329 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1331 operator==(const unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1332 const unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1333 { return __x
._M_h
._M_equal(__y
._M_h
); }
1335 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1337 operator!=(const unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1338 const unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1339 { return !(__x
== __y
); }
1341 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1343 operator==(const unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1344 const unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1345 { return __x
._M_h
._M_equal(__y
._M_h
); }
1347 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1349 operator!=(const unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1350 const unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1351 { return !(__x
== __y
); }
1353 _GLIBCXX_END_NAMESPACE_CONTAINER
1356 #endif /* _UNORDERED_SET_H */