Make FDO more tolerant to source changes
[official-gcc.git] / libstdc++-v3 / include / bits / unordered_set.h
blob3cf248d7d1cd19313e8189fb31d54d1fead8a990
1 // unordered_set implementation -*- C++ -*-
3 // Copyright (C) 2010-2014 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/unordered_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.
38 template<bool _Cache>
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.
53 template<bool _Cache>
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,
62 __detail::_Identity,
63 _Pred, _Hash,
64 __detail::_Mod_range_hashing,
65 __detail::_Default_ranged_hash,
66 __detail::_Prime_rehash_policy, _Tr>;
68 /**
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
79 * equal_to<_Value>.
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> >
93 class unordered_set
95 typedef __uset_hashtable<_Value, _Hash, _Pred, _Alloc> _Hashtable;
96 _Hashtable _M_h;
98 public:
99 // typedefs:
100 //@{
101 /// Public typedefs.
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;
107 //@}
109 //@{
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;
121 //@}
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.
131 explicit
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,
154 size_type __n = 0,
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.
171 explicit
172 unordered_set(const allocator_type& __a)
173 : _M_h(__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,
208 size_type __n = 0,
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.
216 unordered_set&
217 operator=(const unordered_set&) = default;
219 /// Move assignment operator.
220 unordered_set&
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.
234 unordered_set&
235 operator=(initializer_list<value_type> __l)
237 _M_h = __l;
238 return *this;
241 /// Returns the allocator object with which the %unordered_set was
242 /// constructed.
243 allocator_type
244 get_allocator() const noexcept
245 { return _M_h.get_allocator(); }
247 // size and capacity:
249 /// Returns true if the %unordered_set is empty.
250 bool
251 empty() const noexcept
252 { return _M_h.empty(); }
254 /// Returns the size of the %unordered_set.
255 size_type
256 size() const noexcept
257 { return _M_h.size(); }
259 /// Returns the maximum size of the %unordered_set.
260 size_type
261 max_size() const noexcept
262 { return _M_h.max_size(); }
264 // iterators.
266 //@{
268 * Returns a read-only (constant) iterator that points to the first
269 * element in the %unordered_set.
271 iterator
272 begin() noexcept
273 { return _M_h.begin(); }
275 const_iterator
276 begin() const noexcept
277 { return _M_h.begin(); }
278 //@}
280 //@{
282 * Returns a read-only (constant) iterator that points one past the last
283 * element in the %unordered_set.
285 iterator
286 end() noexcept
287 { return _M_h.end(); }
289 const_iterator
290 end() const noexcept
291 { return _M_h.end(); }
292 //@}
295 * Returns a read-only (constant) iterator that points to the first
296 * element in the %unordered_set.
298 const_iterator
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.
306 const_iterator
307 cend() const noexcept
308 { return _M_h.end(); }
310 // modifiers.
313 * @brief Attempts to build and insert an element into the
314 * %unordered_set.
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
323 * %unordered_set.
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
337 * inserted.
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
340 * element itself).
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>
354 iterator
355 emplace_hint(const_iterator __pos, _Args&&... __args)
356 { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
358 //@{
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)); }
379 //@}
381 //@{
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.
401 iterator
402 insert(const_iterator __hint, const value_type& __x)
403 { return _M_h.insert(__hint, __x); }
405 iterator
406 insert(const_iterator __hint, value_type&& __x)
407 { return _M_h.insert(__hint, std::move(__x)); }
408 //@}
411 * @brief A template function that attempts to insert a range of
412 * elements.
413 * @param __first Iterator pointing to the start of the range to be
414 * inserted.
415 * @param __last Iterator pointing to the end of the range.
417 * Complexity similar to that of the range constructor.
419 template<typename _InputIterator>
420 void
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
427 * to be inserted.
429 * Complexity similar to that of the range constructor.
431 void
432 insert(initializer_list<value_type> __l)
433 { _M_h.insert(__l); }
435 //@{
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
447 * responsibility.
449 iterator
450 erase(const_iterator __position)
451 { return _M_h.erase(__position); }
453 // LWG 2059.
454 iterator
455 erase(iterator __position)
456 { return _M_h.erase(__position); }
457 //@}
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.
471 size_type
472 erase(const key_type& __x)
473 { return _M_h.erase(__x); }
476 * @brief Erases a [__first,__last) range of elements from an
477 * %unordered_set.
478 * @param __first Iterator pointing to the start of the range to be
479 * erased.
480 * @param __last Iterator pointing to the end of the range to
481 * be erased.
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.
489 iterator
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.
499 void
500 clear() noexcept
501 { _M_h.clear(); }
504 * @brief Swaps data with another %unordered_set.
505 * @param __x An %unordered_set of the same element and allocator
506 * types.
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.
512 void
513 swap(unordered_set& __x)
514 noexcept( noexcept(_M_h.swap(__x._M_h)) )
515 { _M_h.swap(__x._M_h); }
517 // observers.
519 /// Returns the hash functor object with which the %unordered_set was
520 /// constructed.
521 hasher
522 hash_function() const
523 { return _M_h.hash_function(); }
525 /// Returns the key comparison object with which the %unordered_set was
526 /// constructed.
527 key_equal
528 key_eq() const
529 { return _M_h.key_eq(); }
531 // lookup.
533 //@{
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
538 * found.
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.
545 iterator
546 find(const key_type& __x)
547 { return _M_h.find(__x); }
549 const_iterator
550 find(const key_type& __x) const
551 { return _M_h.find(__x); }
552 //@}
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
561 * (present).
563 size_type
564 count(const key_type& __x) const
565 { return _M_h.count(__x); }
567 //@{
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); }
583 //@}
585 // bucket interface.
587 /// Returns the number of buckets of the %unordered_set.
588 size_type
589 bucket_count() const noexcept
590 { return _M_h.bucket_count(); }
592 /// Returns the maximum number of buckets of the %unordered_set.
593 size_type
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.
602 size_type
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.
611 size_type
612 bucket(const key_type& __key) const
613 { return _M_h.bucket(__key); }
615 //@{
617 * @brief Returns a read-only (constant) iterator pointing to the first
618 * bucket element.
619 * @param __n The bucket index.
620 * @return A read-only local iterator.
622 local_iterator
623 begin(size_type __n)
624 { return _M_h.begin(__n); }
626 const_local_iterator
627 begin(size_type __n) const
628 { return _M_h.begin(__n); }
630 const_local_iterator
631 cbegin(size_type __n) const
632 { return _M_h.cbegin(__n); }
633 //@}
635 //@{
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.
642 local_iterator
643 end(size_type __n)
644 { return _M_h.end(__n); }
646 const_local_iterator
647 end(size_type __n) const
648 { return _M_h.end(__n); }
650 const_local_iterator
651 cend(size_type __n) const
652 { return _M_h.cend(__n); }
653 //@}
655 // hash policy.
657 /// Returns the average number of elements per bucket.
658 float
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.
664 float
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.
672 void
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.
683 void
684 rehash(size_type __n)
685 { _M_h.rehash(__n); }
688 * @brief Prepare the %unordered_set for a specified number of
689 * elements.
690 * @param __n Number of elements required.
692 * Same as rehash(ceil(n / max_load_factor())).
694 void
695 reserve(size_type __n)
696 { _M_h.reserve(__n); }
698 template<typename _Value1, typename _Hash1, typename _Pred1,
699 typename _Alloc1>
700 friend bool
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;
731 _Hashtable _M_h;
733 public:
734 // typedefs:
735 //@{
736 /// Public typedefs.
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;
742 //@}
744 //@{
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;
756 //@}
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.
766 explicit
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,
789 size_type __n = 0,
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,
814 size_type __n = 0,
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.
822 unordered_multiset&
823 operator=(const unordered_multiset&) = default;
825 /// Move assignment operator.
826 unordered_multiset&
827 operator=(unordered_multiset&&) = default;
830 * @brief Creates an %unordered_multiset with no elements.
831 * @param __a An allocator object.
833 explicit
834 unordered_multiset(const allocator_type& __a)
835 : _M_h(__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.
869 unordered_multiset&
870 operator=(initializer_list<value_type> __l)
872 _M_h = __l;
873 return *this;
876 /// Returns the allocator object with which the %unordered_multiset was
877 /// constructed.
878 allocator_type
879 get_allocator() const noexcept
880 { return _M_h.get_allocator(); }
882 // size and capacity:
884 /// Returns true if the %unordered_multiset is empty.
885 bool
886 empty() const noexcept
887 { return _M_h.empty(); }
889 /// Returns the size of the %unordered_multiset.
890 size_type
891 size() const noexcept
892 { return _M_h.size(); }
894 /// Returns the maximum size of the %unordered_multiset.
895 size_type
896 max_size() const noexcept
897 { return _M_h.max_size(); }
899 // iterators.
901 //@{
903 * Returns a read-only (constant) iterator that points to the first
904 * element in the %unordered_multiset.
906 iterator
907 begin() noexcept
908 { return _M_h.begin(); }
910 const_iterator
911 begin() const noexcept
912 { return _M_h.begin(); }
913 //@}
915 //@{
917 * Returns a read-only (constant) iterator that points one past the last
918 * element in the %unordered_multiset.
920 iterator
921 end() noexcept
922 { return _M_h.end(); }
924 const_iterator
925 end() const noexcept
926 { return _M_h.end(); }
927 //@}
930 * Returns a read-only (constant) iterator that points to the first
931 * element in the %unordered_multiset.
933 const_iterator
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.
941 const_iterator
942 cend() const noexcept
943 { return _M_h.end(); }
945 // modifiers.
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>
955 iterator
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
964 * inserted.
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>
977 iterator
978 emplace_hint(const_iterator __pos, _Args&&... __args)
979 { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
981 //@{
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.
989 iterator
990 insert(const value_type& __x)
991 { return _M_h.insert(__x); }
993 iterator
994 insert(value_type&& __x)
995 { return _M_h.insert(std::move(__x)); }
996 //@}
998 //@{
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.
1015 iterator
1016 insert(const_iterator __hint, const value_type& __x)
1017 { return _M_h.insert(__hint, __x); }
1019 iterator
1020 insert(const_iterator __hint, value_type&& __x)
1021 { return _M_h.insert(__hint, std::move(__x)); }
1022 //@}
1025 * @brief A template function that inserts a range of elements.
1026 * @param __first Iterator pointing to the start of the range to be
1027 * inserted.
1028 * @param __last Iterator pointing to the end of the range.
1030 * Complexity similar to that of the range constructor.
1032 template<typename _InputIterator>
1033 void
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
1040 * inserted.
1042 * Complexity similar to that of the range constructor.
1044 void
1045 insert(initializer_list<value_type> __l)
1046 { _M_h.insert(__l); }
1048 //@{
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.
1063 iterator
1064 erase(const_iterator __position)
1065 { return _M_h.erase(__position); }
1067 // LWG 2059.
1068 iterator
1069 erase(iterator __position)
1070 { return _M_h.erase(__position); }
1071 //@}
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.
1086 size_type
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
1094 * erased.
1095 * @param __last Iterator pointing to the end of the range to
1096 * be erased.
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.
1106 iterator
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.
1117 void
1118 clear() noexcept
1119 { _M_h.clear(); }
1122 * @brief Swaps data with another %unordered_multiset.
1123 * @param __x An %unordered_multiset of the same element and allocator
1124 * types.
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.
1130 void
1131 swap(unordered_multiset& __x)
1132 noexcept( noexcept(_M_h.swap(__x._M_h)) )
1133 { _M_h.swap(__x._M_h); }
1135 // observers.
1137 /// Returns the hash functor object with which the %unordered_multiset
1138 /// was constructed.
1139 hasher
1140 hash_function() const
1141 { return _M_h.hash_function(); }
1143 /// Returns the key comparison object with which the %unordered_multiset
1144 /// was constructed.
1145 key_equal
1146 key_eq() const
1147 { return _M_h.key_eq(); }
1149 // lookup.
1151 //@{
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
1156 * found.
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.
1163 iterator
1164 find(const key_type& __x)
1165 { return _M_h.find(__x); }
1167 const_iterator
1168 find(const key_type& __x) const
1169 { return _M_h.find(__x); }
1170 //@}
1173 * @brief Finds the number of elements.
1174 * @param __x Element to located.
1175 * @return Number of elements with specified key.
1177 size_type
1178 count(const key_type& __x) const
1179 { return _M_h.count(__x); }
1181 //@{
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); }
1195 //@}
1197 // bucket interface.
1199 /// Returns the number of buckets of the %unordered_multiset.
1200 size_type
1201 bucket_count() const noexcept
1202 { return _M_h.bucket_count(); }
1204 /// Returns the maximum number of buckets of the %unordered_multiset.
1205 size_type
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.
1214 size_type
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.
1223 size_type
1224 bucket(const key_type& __key) const
1225 { return _M_h.bucket(__key); }
1227 //@{
1229 * @brief Returns a read-only (constant) iterator pointing to the first
1230 * bucket element.
1231 * @param __n The bucket index.
1232 * @return A read-only local iterator.
1234 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); }
1245 //@}
1247 //@{
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.
1254 local_iterator
1255 end(size_type __n)
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); }
1265 //@}
1267 // hash policy.
1269 /// Returns the average number of elements per bucket.
1270 float
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.
1276 float
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.
1284 void
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.
1295 void
1296 rehash(size_type __n)
1297 { _M_h.rehash(__n); }
1300 * @brief Prepare the %unordered_multiset for a specified number of
1301 * elements.
1302 * @param __n Number of elements required.
1304 * Same as rehash(ceil(n / max_load_factor())).
1306 void
1307 reserve(size_type __n)
1308 { _M_h.reserve(__n); }
1310 template<typename _Value1, typename _Hash1, typename _Pred1,
1311 typename _Alloc1>
1312 friend bool
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>
1318 inline void
1319 swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
1320 unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
1321 { __x.swap(__y); }
1323 template<class _Value, class _Hash, class _Pred, class _Alloc>
1324 inline void
1325 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
1326 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
1327 { __x.swap(__y); }
1329 template<class _Value, class _Hash, class _Pred, class _Alloc>
1330 inline bool
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>
1336 inline bool
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>
1342 inline bool
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>
1348 inline bool
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
1354 } // namespace std
1356 #endif /* _UNORDERED_SET_H */