1 // unordered_set 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_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 /// Default constructor.
126 unordered_set() = default;
129 * @brief Default constructor creates no elements.
130 * @param __n Minimal initial number of buckets.
131 * @param __hf A hash functor.
132 * @param __eql A key equality functor.
133 * @param __a An allocator object.
136 unordered_set(size_type __n
,
137 const hasher
& __hf
= hasher(),
138 const key_equal
& __eql
= key_equal(),
139 const allocator_type
& __a
= allocator_type())
140 : _M_h(__n
, __hf
, __eql
, __a
)
143 unordered_set(size_type __n
, const allocator_type
& __a
)
144 : _M_h(__n
, hasher(), key_equal(), __a
)
147 unordered_set(size_type __n
,
149 const allocator_type
& __a
)
150 : unordered_set(__n
, __hf
, key_equal(), __a
)
154 * @brief Builds an %unordered_set from a range.
155 * @param __first An input iterator.
156 * @param __last An input iterator.
157 * @param __n Minimal initial number of buckets.
158 * @param __hf A hash functor.
159 * @param __eql A key equality functor.
160 * @param __a An allocator object.
162 * Create an %unordered_set consisting of copies of the elements from
163 * [__first,__last). This is linear in N (where N is
164 * distance(__first,__last)).
166 template<typename _InputIterator
>
167 unordered_set(_InputIterator __first
, _InputIterator __last
,
169 const hasher
& __hf
= hasher(),
170 const key_equal
& __eql
= key_equal(),
171 const allocator_type
& __a
= allocator_type())
172 : _M_h(__first
, __last
, __n
, __hf
, __eql
, __a
)
175 /// Copy constructor.
176 unordered_set(const unordered_set
&) = default;
178 /// Move constructor.
179 unordered_set(unordered_set
&&) = default;
182 * @brief Creates an %unordered_set with no elements.
183 * @param __a An allocator object.
186 unordered_set(const allocator_type
& __a
)
191 * @brief Copy constructor with allocator argument.
192 * @param __uset Input %unordered_set to copy.
193 * @param __a An allocator object.
195 unordered_set(const unordered_set
& __uset
,
196 const allocator_type
& __a
)
197 : _M_h(__uset
._M_h
, __a
)
201 * @brief Move constructor with allocator argument.
202 * @param __uset Input %unordered_set to move.
203 * @param __a An allocator object.
205 unordered_set(unordered_set
&& __uset
,
206 const allocator_type
& __a
)
207 : _M_h(std::move(__uset
._M_h
), __a
)
211 * @brief Builds an %unordered_set from an initializer_list.
212 * @param __l An initializer_list.
213 * @param __n Minimal initial number of buckets.
214 * @param __hf A hash functor.
215 * @param __eql A key equality functor.
216 * @param __a An allocator object.
218 * Create an %unordered_set consisting of copies of the elements in the
219 * list. This is linear in N (where N is @a __l.size()).
221 unordered_set(initializer_list
<value_type
> __l
,
223 const hasher
& __hf
= hasher(),
224 const key_equal
& __eql
= key_equal(),
225 const allocator_type
& __a
= allocator_type())
226 : _M_h(__l
, __n
, __hf
, __eql
, __a
)
229 /// Copy assignment operator.
231 operator=(const unordered_set
&) = default;
233 /// Move assignment operator.
235 operator=(unordered_set
&&) = default;
238 * @brief %Unordered_set list assignment operator.
239 * @param __l An initializer_list.
241 * This function fills an %unordered_set with copies of the elements in
242 * the initializer list @a __l.
244 * Note that the assignment completely changes the %unordered_set and
245 * that the resulting %unordered_set's size is the same as the number
246 * of elements assigned. Old data may be lost.
249 operator=(initializer_list
<value_type
> __l
)
255 /// Returns the allocator object with which the %unordered_set was
258 get_allocator() const noexcept
259 { return _M_h
.get_allocator(); }
261 // size and capacity:
263 /// Returns true if the %unordered_set is empty.
265 empty() const noexcept
266 { return _M_h
.empty(); }
268 /// Returns the size of the %unordered_set.
270 size() const noexcept
271 { return _M_h
.size(); }
273 /// Returns the maximum size of the %unordered_set.
275 max_size() const noexcept
276 { return _M_h
.max_size(); }
282 * Returns a read-only (constant) iterator that points to the first
283 * element in the %unordered_set.
287 { return _M_h
.begin(); }
290 begin() const noexcept
291 { return _M_h
.begin(); }
296 * Returns a read-only (constant) iterator that points one past the last
297 * element in the %unordered_set.
301 { return _M_h
.end(); }
305 { return _M_h
.end(); }
309 * Returns a read-only (constant) iterator that points to the first
310 * element in the %unordered_set.
313 cbegin() const noexcept
314 { return _M_h
.begin(); }
317 * Returns a read-only (constant) iterator that points one past the last
318 * element in the %unordered_set.
321 cend() const noexcept
322 { return _M_h
.end(); }
327 * @brief Attempts to build and insert an element into the
329 * @param __args Arguments used to generate an element.
330 * @return A pair, of which the first element is an iterator that points
331 * to the possibly inserted element, and the second is a bool
332 * that is true if the element was actually inserted.
334 * This function attempts to build and insert an element into the
335 * %unordered_set. An %unordered_set relies on unique keys and thus an
336 * element is only inserted if it is not already present in the
339 * Insertion requires amortized constant time.
341 template<typename
... _Args
>
342 std::pair
<iterator
, bool>
343 emplace(_Args
&&... __args
)
344 { return _M_h
.emplace(std::forward
<_Args
>(__args
)...); }
347 * @brief Attempts to insert an element into the %unordered_set.
348 * @param __pos An iterator that serves as a hint as to where the
349 * element should be inserted.
350 * @param __args Arguments used to generate the element to be
352 * @return An iterator that points to the element with key equivalent to
353 * the one generated from @a __args (may or may not be the
356 * This function is not concerned about whether the insertion took place,
357 * and thus does not return a boolean like the single-argument emplace()
358 * does. Note that the first parameter is only a hint and can
359 * potentially improve the performance of the insertion process. A bad
360 * hint would cause no gains in efficiency.
362 * For more on @a hinting, see:
363 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
365 * Insertion requires amortized constant time.
367 template<typename
... _Args
>
369 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
370 { return _M_h
.emplace_hint(__pos
, std::forward
<_Args
>(__args
)...); }
374 * @brief Attempts to insert an element into the %unordered_set.
375 * @param __x Element to be inserted.
376 * @return A pair, of which the first element is an iterator that points
377 * to the possibly inserted element, and the second is a bool
378 * that is true if the element was actually inserted.
380 * This function attempts to insert an element into the %unordered_set.
381 * An %unordered_set relies on unique keys and thus an element is only
382 * inserted if it is not already present in the %unordered_set.
384 * Insertion requires amortized constant time.
386 std::pair
<iterator
, bool>
387 insert(const value_type
& __x
)
388 { return _M_h
.insert(__x
); }
390 std::pair
<iterator
, bool>
391 insert(value_type
&& __x
)
392 { return _M_h
.insert(std::move(__x
)); }
397 * @brief Attempts to insert an element into the %unordered_set.
398 * @param __hint An iterator that serves as a hint as to where the
399 * element should be inserted.
400 * @param __x Element to be inserted.
401 * @return An iterator that points to the element with key of
402 * @a __x (may or may not be the element passed in).
404 * This function is not concerned about whether the insertion took place,
405 * and thus does not return a boolean like the single-argument insert()
406 * does. Note that the first parameter is only a hint and can
407 * potentially improve the performance of the insertion process. A bad
408 * hint would cause no gains in efficiency.
410 * For more on @a hinting, see:
411 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
413 * Insertion requires amortized constant.
416 insert(const_iterator __hint
, const value_type
& __x
)
417 { return _M_h
.insert(__hint
, __x
); }
420 insert(const_iterator __hint
, value_type
&& __x
)
421 { return _M_h
.insert(__hint
, std::move(__x
)); }
425 * @brief A template function that attempts to insert a range of
427 * @param __first Iterator pointing to the start of the range to be
429 * @param __last Iterator pointing to the end of the range.
431 * Complexity similar to that of the range constructor.
433 template<typename _InputIterator
>
435 insert(_InputIterator __first
, _InputIterator __last
)
436 { _M_h
.insert(__first
, __last
); }
439 * @brief Attempts to insert a list of elements into the %unordered_set.
440 * @param __l A std::initializer_list<value_type> of elements
443 * Complexity similar to that of the range constructor.
446 insert(initializer_list
<value_type
> __l
)
447 { _M_h
.insert(__l
); }
451 * @brief Erases an element from an %unordered_set.
452 * @param __position An iterator pointing to the element to be erased.
453 * @return An iterator pointing to the element immediately following
454 * @a __position prior to the element being erased. If no such
455 * element exists, end() is returned.
457 * This function erases an element, pointed to by the given iterator,
458 * from an %unordered_set. Note that this function only erases the
459 * element, and that if the element is itself a pointer, the pointed-to
460 * memory is not touched in any way. Managing the pointer is the user's
464 erase(const_iterator __position
)
465 { return _M_h
.erase(__position
); }
469 erase(iterator __position
)
470 { return _M_h
.erase(__position
); }
474 * @brief Erases elements according to the provided key.
475 * @param __x Key of element to be erased.
476 * @return The number of elements erased.
478 * This function erases all the elements located by the given key from
479 * an %unordered_set. For an %unordered_set the result of this function
480 * can only be 0 (not present) or 1 (present).
481 * Note that this function only erases the element, and that if
482 * the element is itself a pointer, the pointed-to memory is not touched
483 * in any way. Managing the pointer is the user's responsibility.
486 erase(const key_type
& __x
)
487 { return _M_h
.erase(__x
); }
490 * @brief Erases a [__first,__last) range of elements from an
492 * @param __first Iterator pointing to the start of the range to be
494 * @param __last Iterator pointing to the end of the range to
496 * @return The iterator @a __last.
498 * This function erases a sequence of elements from an %unordered_set.
499 * Note that this function only erases the element, and that if
500 * the element is itself a pointer, the pointed-to memory is not touched
501 * in any way. Managing the pointer is the user's responsibility.
504 erase(const_iterator __first
, const_iterator __last
)
505 { return _M_h
.erase(__first
, __last
); }
508 * Erases all elements in an %unordered_set. Note that this function only
509 * erases the elements, and that if the elements themselves are pointers,
510 * the pointed-to memory is not touched in any way. Managing the pointer
511 * is the user's responsibility.
518 * @brief Swaps data with another %unordered_set.
519 * @param __x An %unordered_set of the same element and allocator
522 * This exchanges the elements between two sets in constant time.
523 * Note that the global std::swap() function is specialized such that
524 * std::swap(s1,s2) will feed to this function.
527 swap(unordered_set
& __x
)
528 noexcept( noexcept(_M_h
.swap(__x
._M_h
)) )
529 { _M_h
.swap(__x
._M_h
); }
533 /// Returns the hash functor object with which the %unordered_set was
536 hash_function() const
537 { return _M_h
.hash_function(); }
539 /// Returns the key comparison object with which the %unordered_set was
543 { return _M_h
.key_eq(); }
549 * @brief Tries to locate an element in an %unordered_set.
550 * @param __x Element to be located.
551 * @return Iterator pointing to sought-after element, or end() if not
554 * This function takes a key and tries to locate the element with which
555 * the key matches. If successful the function returns an iterator
556 * pointing to the sought after element. If unsuccessful it returns the
557 * past-the-end ( @c end() ) iterator.
560 find(const key_type
& __x
)
561 { return _M_h
.find(__x
); }
564 find(const key_type
& __x
) const
565 { return _M_h
.find(__x
); }
569 * @brief Finds the number of elements.
570 * @param __x Element to located.
571 * @return Number of elements with specified key.
573 * This function only makes sense for unordered_multisets; for
574 * unordered_set the result will either be 0 (not present) or 1
578 count(const key_type
& __x
) const
579 { return _M_h
.count(__x
); }
583 * @brief Finds a subsequence matching given key.
584 * @param __x Key to be located.
585 * @return Pair of iterators that possibly points to the subsequence
586 * matching given key.
588 * This function probably only makes sense for multisets.
590 std::pair
<iterator
, iterator
>
591 equal_range(const key_type
& __x
)
592 { return _M_h
.equal_range(__x
); }
594 std::pair
<const_iterator
, const_iterator
>
595 equal_range(const key_type
& __x
) const
596 { return _M_h
.equal_range(__x
); }
601 /// Returns the number of buckets of the %unordered_set.
603 bucket_count() const noexcept
604 { return _M_h
.bucket_count(); }
606 /// Returns the maximum number of buckets of the %unordered_set.
608 max_bucket_count() const noexcept
609 { return _M_h
.max_bucket_count(); }
612 * @brief Returns the number of elements in a given bucket.
613 * @param __n A bucket index.
614 * @return The number of elements in the bucket.
617 bucket_size(size_type __n
) const
618 { return _M_h
.bucket_size(__n
); }
621 * @brief Returns the bucket index of a given element.
622 * @param __key A key instance.
623 * @return The key bucket index.
626 bucket(const key_type
& __key
) const
627 { return _M_h
.bucket(__key
); }
631 * @brief Returns a read-only (constant) iterator pointing to the first
633 * @param __n The bucket index.
634 * @return A read-only local iterator.
638 { return _M_h
.begin(__n
); }
641 begin(size_type __n
) const
642 { return _M_h
.begin(__n
); }
645 cbegin(size_type __n
) const
646 { return _M_h
.cbegin(__n
); }
651 * @brief Returns a read-only (constant) iterator pointing to one past
652 * the last bucket elements.
653 * @param __n The bucket index.
654 * @return A read-only local iterator.
658 { return _M_h
.end(__n
); }
661 end(size_type __n
) const
662 { return _M_h
.end(__n
); }
665 cend(size_type __n
) const
666 { return _M_h
.cend(__n
); }
671 /// Returns the average number of elements per bucket.
673 load_factor() const noexcept
674 { return _M_h
.load_factor(); }
676 /// Returns a positive number that the %unordered_set tries to keep the
677 /// load factor less than or equal to.
679 max_load_factor() const noexcept
680 { return _M_h
.max_load_factor(); }
683 * @brief Change the %unordered_set maximum load factor.
684 * @param __z The new maximum load factor.
687 max_load_factor(float __z
)
688 { _M_h
.max_load_factor(__z
); }
691 * @brief May rehash the %unordered_set.
692 * @param __n The new number of buckets.
694 * Rehash will occur only if the new number of buckets respect the
695 * %unordered_set maximum load factor.
698 rehash(size_type __n
)
699 { _M_h
.rehash(__n
); }
702 * @brief Prepare the %unordered_set for a specified number of
704 * @param __n Number of elements required.
706 * Same as rehash(ceil(n / max_load_factor())).
709 reserve(size_type __n
)
710 { _M_h
.reserve(__n
); }
712 template<typename _Value1
, typename _Hash1
, typename _Pred1
,
715 operator==(const unordered_set
<_Value1
, _Hash1
, _Pred1
, _Alloc1
>&,
716 const unordered_set
<_Value1
, _Hash1
, _Pred1
, _Alloc1
>&);
720 * @brief A standard container composed of equivalent keys
721 * (possibly containing multiple of each key value) in which the
722 * elements' keys are the elements themselves.
724 * @ingroup unordered_associative_containers
726 * @tparam _Value Type of key objects.
727 * @tparam _Hash Hashing function object type, defaults to hash<_Value>.
728 * @tparam _Pred Predicate function object type, defaults
729 * to equal_to<_Value>.
730 * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
732 * Meets the requirements of a <a href="tables.html#65">container</a>, and
733 * <a href="tables.html#xx">unordered associative container</a>
735 * Base is _Hashtable, dispatched at compile time via template
736 * alias __umset_hashtable.
738 template<class _Value
,
739 class _Hash
= hash
<_Value
>,
740 class _Pred
= std::equal_to
<_Value
>,
741 class _Alloc
= std::allocator
<_Value
> >
742 class unordered_multiset
744 typedef __umset_hashtable
<_Value
, _Hash
, _Pred
, _Alloc
> _Hashtable
;
751 typedef typename
_Hashtable::key_type key_type
;
752 typedef typename
_Hashtable::value_type value_type
;
753 typedef typename
_Hashtable::hasher hasher
;
754 typedef typename
_Hashtable::key_equal key_equal
;
755 typedef typename
_Hashtable::allocator_type allocator_type
;
759 /// Iterator-related typedefs.
760 typedef typename
_Hashtable::pointer pointer
;
761 typedef typename
_Hashtable::const_pointer const_pointer
;
762 typedef typename
_Hashtable::reference reference
;
763 typedef typename
_Hashtable::const_reference const_reference
;
764 typedef typename
_Hashtable::iterator iterator
;
765 typedef typename
_Hashtable::const_iterator const_iterator
;
766 typedef typename
_Hashtable::local_iterator local_iterator
;
767 typedef typename
_Hashtable::const_local_iterator const_local_iterator
;
768 typedef typename
_Hashtable::size_type size_type
;
769 typedef typename
_Hashtable::difference_type difference_type
;
772 // construct/destroy/copy
774 /// Default constructor.
775 unordered_multiset() = default;
778 * @brief Default constructor creates no elements.
779 * @param __n Minimal initial number of buckets.
780 * @param __hf A hash functor.
781 * @param __eql A key equality functor.
782 * @param __a An allocator object.
785 unordered_multiset(size_type __n
,
786 const hasher
& __hf
= hasher(),
787 const key_equal
& __eql
= key_equal(),
788 const allocator_type
& __a
= allocator_type())
789 : _M_h(__n
, __hf
, __eql
, __a
)
792 unordered_multiset(size_type __n
, const allocator_type
& __a
)
793 : _M_h(__n
, hasher(), key_equal(), __a
)
796 unordered_multiset(size_type __n
,
798 const allocator_type
& __a
)
799 : _M_h(__n
, __hf
, key_equal(), __a
)
803 * @brief Builds an %unordered_multiset from a range.
804 * @param __first An input iterator.
805 * @param __last An input iterator.
806 * @param __n Minimal initial number of buckets.
807 * @param __hf A hash functor.
808 * @param __eql A key equality functor.
809 * @param __a An allocator object.
811 * Create an %unordered_multiset consisting of copies of the elements
812 * from [__first,__last). This is linear in N (where N is
813 * distance(__first,__last)).
815 template<typename _InputIterator
>
816 unordered_multiset(_InputIterator __first
, _InputIterator __last
,
818 const hasher
& __hf
= hasher(),
819 const key_equal
& __eql
= key_equal(),
820 const allocator_type
& __a
= allocator_type())
821 : _M_h(__first
, __last
, __n
, __hf
, __eql
, __a
)
824 /// Copy constructor.
825 unordered_multiset(const unordered_multiset
&) = default;
827 /// Move constructor.
828 unordered_multiset(unordered_multiset
&&) = default;
831 * @brief Builds an %unordered_multiset from an initializer_list.
832 * @param __l An initializer_list.
833 * @param __n Minimal initial number of buckets.
834 * @param __hf A hash functor.
835 * @param __eql A key equality functor.
836 * @param __a An allocator object.
838 * Create an %unordered_multiset consisting of copies of the elements in
839 * the list. This is linear in N (where N is @a __l.size()).
841 unordered_multiset(initializer_list
<value_type
> __l
,
843 const hasher
& __hf
= hasher(),
844 const key_equal
& __eql
= key_equal(),
845 const allocator_type
& __a
= allocator_type())
846 : _M_h(__l
, __n
, __hf
, __eql
, __a
)
849 /// Copy assignment operator.
851 operator=(const unordered_multiset
&) = default;
853 /// Move assignment operator.
855 operator=(unordered_multiset
&&) = default;
858 * @brief Creates an %unordered_multiset with no elements.
859 * @param __a An allocator object.
862 unordered_multiset(const allocator_type
& __a
)
867 * @brief Copy constructor with allocator argument.
868 * @param __uset Input %unordered_multiset to copy.
869 * @param __a An allocator object.
871 unordered_multiset(const unordered_multiset
& __umset
,
872 const allocator_type
& __a
)
873 : _M_h(__umset
._M_h
, __a
)
877 * @brief Move constructor with allocator argument.
878 * @param __umset Input %unordered_multiset to move.
879 * @param __a An allocator object.
881 unordered_multiset(unordered_multiset
&& __umset
,
882 const allocator_type
& __a
)
883 : _M_h(std::move(__umset
._M_h
), __a
)
887 * @brief %Unordered_multiset list assignment operator.
888 * @param __l An initializer_list.
890 * This function fills an %unordered_multiset with copies of the elements
891 * in the initializer list @a __l.
893 * Note that the assignment completely changes the %unordered_multiset
894 * and that the resulting %unordered_set's size is the same as the number
895 * of elements assigned. Old data may be lost.
898 operator=(initializer_list
<value_type
> __l
)
904 /// Returns the allocator object with which the %unordered_multiset was
907 get_allocator() const noexcept
908 { return _M_h
.get_allocator(); }
910 // size and capacity:
912 /// Returns true if the %unordered_multiset is empty.
914 empty() const noexcept
915 { return _M_h
.empty(); }
917 /// Returns the size of the %unordered_multiset.
919 size() const noexcept
920 { return _M_h
.size(); }
922 /// Returns the maximum size of the %unordered_multiset.
924 max_size() const noexcept
925 { return _M_h
.max_size(); }
931 * Returns a read-only (constant) iterator that points to the first
932 * element in the %unordered_multiset.
936 { return _M_h
.begin(); }
939 begin() const noexcept
940 { return _M_h
.begin(); }
945 * Returns a read-only (constant) iterator that points one past the last
946 * element in the %unordered_multiset.
950 { return _M_h
.end(); }
954 { return _M_h
.end(); }
958 * Returns a read-only (constant) iterator that points to the first
959 * element in the %unordered_multiset.
962 cbegin() const noexcept
963 { return _M_h
.begin(); }
966 * Returns a read-only (constant) iterator that points one past the last
967 * element in the %unordered_multiset.
970 cend() const noexcept
971 { return _M_h
.end(); }
976 * @brief Builds and insert an element into the %unordered_multiset.
977 * @param __args Arguments used to generate an element.
978 * @return An iterator that points to the inserted element.
980 * Insertion requires amortized constant time.
982 template<typename
... _Args
>
984 emplace(_Args
&&... __args
)
985 { return _M_h
.emplace(std::forward
<_Args
>(__args
)...); }
988 * @brief Inserts an element into the %unordered_multiset.
989 * @param __pos An iterator that serves as a hint as to where the
990 * element should be inserted.
991 * @param __args Arguments used to generate the element to be
993 * @return An iterator that points to the inserted element.
995 * Note that the first parameter is only a hint and can potentially
996 * improve the performance of the insertion process. A bad hint would
997 * cause no gains in efficiency.
999 * For more on @a hinting, see:
1000 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
1002 * Insertion requires amortized constant time.
1004 template<typename
... _Args
>
1006 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
1007 { return _M_h
.emplace_hint(__pos
, std::forward
<_Args
>(__args
)...); }
1011 * @brief Inserts an element into the %unordered_multiset.
1012 * @param __x Element to be inserted.
1013 * @return An iterator that points to the inserted element.
1015 * Insertion requires amortized constant time.
1018 insert(const value_type
& __x
)
1019 { return _M_h
.insert(__x
); }
1022 insert(value_type
&& __x
)
1023 { return _M_h
.insert(std::move(__x
)); }
1028 * @brief Inserts an element into the %unordered_multiset.
1029 * @param __hint An iterator that serves as a hint as to where the
1030 * element should be inserted.
1031 * @param __x Element to be inserted.
1032 * @return An iterator that points to the inserted element.
1034 * Note that the first parameter is only a hint and can potentially
1035 * improve the performance of the insertion process. A bad hint would
1036 * cause no gains in efficiency.
1038 * For more on @a hinting, see:
1039 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
1041 * Insertion requires amortized constant.
1044 insert(const_iterator __hint
, const value_type
& __x
)
1045 { return _M_h
.insert(__hint
, __x
); }
1048 insert(const_iterator __hint
, value_type
&& __x
)
1049 { return _M_h
.insert(__hint
, std::move(__x
)); }
1053 * @brief A template function that inserts a range of elements.
1054 * @param __first Iterator pointing to the start of the range to be
1056 * @param __last Iterator pointing to the end of the range.
1058 * Complexity similar to that of the range constructor.
1060 template<typename _InputIterator
>
1062 insert(_InputIterator __first
, _InputIterator __last
)
1063 { _M_h
.insert(__first
, __last
); }
1066 * @brief Inserts a list of elements into the %unordered_multiset.
1067 * @param __l A std::initializer_list<value_type> of elements to be
1070 * Complexity similar to that of the range constructor.
1073 insert(initializer_list
<value_type
> __l
)
1074 { _M_h
.insert(__l
); }
1078 * @brief Erases an element from an %unordered_multiset.
1079 * @param __position An iterator pointing to the element to be erased.
1080 * @return An iterator pointing to the element immediately following
1081 * @a __position prior to the element being erased. If no such
1082 * element exists, end() is returned.
1084 * This function erases an element, pointed to by the given iterator,
1085 * from an %unordered_multiset.
1087 * Note that this function only erases the element, and that if the
1088 * element is itself a pointer, the pointed-to memory is not touched in
1089 * any way. Managing the pointer is the user's responsibility.
1092 erase(const_iterator __position
)
1093 { return _M_h
.erase(__position
); }
1097 erase(iterator __position
)
1098 { return _M_h
.erase(__position
); }
1103 * @brief Erases elements according to the provided key.
1104 * @param __x Key of element to be erased.
1105 * @return The number of elements erased.
1107 * This function erases all the elements located by the given key from
1108 * an %unordered_multiset.
1110 * Note that this function only erases the element, and that if the
1111 * element is itself a pointer, the pointed-to memory is not touched in
1112 * any way. Managing the pointer is the user's responsibility.
1115 erase(const key_type
& __x
)
1116 { return _M_h
.erase(__x
); }
1119 * @brief Erases a [__first,__last) range of elements from an
1120 * %unordered_multiset.
1121 * @param __first Iterator pointing to the start of the range to be
1123 * @param __last Iterator pointing to the end of the range to
1125 * @return The iterator @a __last.
1127 * This function erases a sequence of elements from an
1128 * %unordered_multiset.
1130 * Note that this function only erases the element, and that if
1131 * the element is itself a pointer, the pointed-to memory is not touched
1132 * in any way. Managing the pointer is the user's responsibility.
1135 erase(const_iterator __first
, const_iterator __last
)
1136 { return _M_h
.erase(__first
, __last
); }
1139 * Erases all elements in an %unordered_multiset.
1141 * Note that this function only erases the elements, and that if the
1142 * elements themselves are pointers, the pointed-to memory is not touched
1143 * in any way. Managing the pointer is the user's responsibility.
1150 * @brief Swaps data with another %unordered_multiset.
1151 * @param __x An %unordered_multiset of the same element and allocator
1154 * This exchanges the elements between two sets in constant time.
1155 * Note that the global std::swap() function is specialized such that
1156 * std::swap(s1,s2) will feed to this function.
1159 swap(unordered_multiset
& __x
)
1160 noexcept( noexcept(_M_h
.swap(__x
._M_h
)) )
1161 { _M_h
.swap(__x
._M_h
); }
1165 /// Returns the hash functor object with which the %unordered_multiset
1166 /// was constructed.
1168 hash_function() const
1169 { return _M_h
.hash_function(); }
1171 /// Returns the key comparison object with which the %unordered_multiset
1172 /// was constructed.
1175 { return _M_h
.key_eq(); }
1181 * @brief Tries to locate an element in an %unordered_multiset.
1182 * @param __x Element to be located.
1183 * @return Iterator pointing to sought-after element, or end() if not
1186 * This function takes a key and tries to locate the element with which
1187 * the key matches. If successful the function returns an iterator
1188 * pointing to the sought after element. If unsuccessful it returns the
1189 * past-the-end ( @c end() ) iterator.
1192 find(const key_type
& __x
)
1193 { return _M_h
.find(__x
); }
1196 find(const key_type
& __x
) const
1197 { return _M_h
.find(__x
); }
1201 * @brief Finds the number of elements.
1202 * @param __x Element to located.
1203 * @return Number of elements with specified key.
1206 count(const key_type
& __x
) const
1207 { return _M_h
.count(__x
); }
1211 * @brief Finds a subsequence matching given key.
1212 * @param __x Key to be located.
1213 * @return Pair of iterators that possibly points to the subsequence
1214 * matching given key.
1216 std::pair
<iterator
, iterator
>
1217 equal_range(const key_type
& __x
)
1218 { return _M_h
.equal_range(__x
); }
1220 std::pair
<const_iterator
, const_iterator
>
1221 equal_range(const key_type
& __x
) const
1222 { return _M_h
.equal_range(__x
); }
1225 // bucket interface.
1227 /// Returns the number of buckets of the %unordered_multiset.
1229 bucket_count() const noexcept
1230 { return _M_h
.bucket_count(); }
1232 /// Returns the maximum number of buckets of the %unordered_multiset.
1234 max_bucket_count() const noexcept
1235 { return _M_h
.max_bucket_count(); }
1238 * @brief Returns the number of elements in a given bucket.
1239 * @param __n A bucket index.
1240 * @return The number of elements in the bucket.
1243 bucket_size(size_type __n
) const
1244 { return _M_h
.bucket_size(__n
); }
1247 * @brief Returns the bucket index of a given element.
1248 * @param __key A key instance.
1249 * @return The key bucket index.
1252 bucket(const key_type
& __key
) const
1253 { return _M_h
.bucket(__key
); }
1257 * @brief Returns a read-only (constant) iterator pointing to the first
1259 * @param __n The bucket index.
1260 * @return A read-only local iterator.
1263 begin(size_type __n
)
1264 { return _M_h
.begin(__n
); }
1266 const_local_iterator
1267 begin(size_type __n
) const
1268 { return _M_h
.begin(__n
); }
1270 const_local_iterator
1271 cbegin(size_type __n
) const
1272 { return _M_h
.cbegin(__n
); }
1277 * @brief Returns a read-only (constant) iterator pointing to one past
1278 * the last bucket elements.
1279 * @param __n The bucket index.
1280 * @return A read-only local iterator.
1284 { return _M_h
.end(__n
); }
1286 const_local_iterator
1287 end(size_type __n
) const
1288 { return _M_h
.end(__n
); }
1290 const_local_iterator
1291 cend(size_type __n
) const
1292 { return _M_h
.cend(__n
); }
1297 /// Returns the average number of elements per bucket.
1299 load_factor() const noexcept
1300 { return _M_h
.load_factor(); }
1302 /// Returns a positive number that the %unordered_multiset tries to keep the
1303 /// load factor less than or equal to.
1305 max_load_factor() const noexcept
1306 { return _M_h
.max_load_factor(); }
1309 * @brief Change the %unordered_multiset maximum load factor.
1310 * @param __z The new maximum load factor.
1313 max_load_factor(float __z
)
1314 { _M_h
.max_load_factor(__z
); }
1317 * @brief May rehash the %unordered_multiset.
1318 * @param __n The new number of buckets.
1320 * Rehash will occur only if the new number of buckets respect the
1321 * %unordered_multiset maximum load factor.
1324 rehash(size_type __n
)
1325 { _M_h
.rehash(__n
); }
1328 * @brief Prepare the %unordered_multiset for a specified number of
1330 * @param __n Number of elements required.
1332 * Same as rehash(ceil(n / max_load_factor())).
1335 reserve(size_type __n
)
1336 { _M_h
.reserve(__n
); }
1338 template<typename _Value1
, typename _Hash1
, typename _Pred1
,
1341 operator==(const unordered_multiset
<_Value1
, _Hash1
, _Pred1
, _Alloc1
>&,
1342 const unordered_multiset
<_Value1
, _Hash1
, _Pred1
, _Alloc1
>&);
1345 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1347 swap(unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1348 unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1351 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1353 swap(unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1354 unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1357 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1359 operator==(const unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1360 const unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1361 { return __x
._M_h
._M_equal(__y
._M_h
); }
1363 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1365 operator!=(const unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1366 const unordered_set
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1367 { return !(__x
== __y
); }
1369 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1371 operator==(const unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1372 const unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1373 { return __x
._M_h
._M_equal(__y
._M_h
); }
1375 template<class _Value
, class _Hash
, class _Pred
, class _Alloc
>
1377 operator!=(const unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __x
,
1378 const unordered_multiset
<_Value
, _Hash
, _Pred
, _Alloc
>& __y
)
1379 { return !(__x
== __y
); }
1381 _GLIBCXX_END_NAMESPACE_CONTAINER
1384 #endif /* _UNORDERED_SET_H */