re PR libstdc++/66055 (hash containers missing required reserving constructors)
[official-gcc.git] / libstdc++-v3 / include / bits / unordered_set.h
blobc91eab8e113b14ccee07ccf7c8cb7f398468efd2
1 // unordered_set implementation -*- C++ -*-
3 // Copyright (C) 2010-2015 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/unordered_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 /// 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.
135 explicit
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,
148 const hasher& __hf,
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,
168 size_type __n = 0,
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.
185 explicit
186 unordered_set(const allocator_type& __a)
187 : _M_h(__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,
222 size_type __n = 0,
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.
230 unordered_set&
231 operator=(const unordered_set&) = default;
233 /// Move assignment operator.
234 unordered_set&
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.
248 unordered_set&
249 operator=(initializer_list<value_type> __l)
251 _M_h = __l;
252 return *this;
255 /// Returns the allocator object with which the %unordered_set was
256 /// constructed.
257 allocator_type
258 get_allocator() const noexcept
259 { return _M_h.get_allocator(); }
261 // size and capacity:
263 /// Returns true if the %unordered_set is empty.
264 bool
265 empty() const noexcept
266 { return _M_h.empty(); }
268 /// Returns the size of the %unordered_set.
269 size_type
270 size() const noexcept
271 { return _M_h.size(); }
273 /// Returns the maximum size of the %unordered_set.
274 size_type
275 max_size() const noexcept
276 { return _M_h.max_size(); }
278 // iterators.
280 //@{
282 * Returns a read-only (constant) iterator that points to the first
283 * element in the %unordered_set.
285 iterator
286 begin() noexcept
287 { return _M_h.begin(); }
289 const_iterator
290 begin() const noexcept
291 { return _M_h.begin(); }
292 //@}
294 //@{
296 * Returns a read-only (constant) iterator that points one past the last
297 * element in the %unordered_set.
299 iterator
300 end() noexcept
301 { return _M_h.end(); }
303 const_iterator
304 end() const noexcept
305 { return _M_h.end(); }
306 //@}
309 * Returns a read-only (constant) iterator that points to the first
310 * element in the %unordered_set.
312 const_iterator
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.
320 const_iterator
321 cend() const noexcept
322 { return _M_h.end(); }
324 // modifiers.
327 * @brief Attempts to build and insert an element into the
328 * %unordered_set.
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
337 * %unordered_set.
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
351 * inserted.
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
354 * element itself).
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>
368 iterator
369 emplace_hint(const_iterator __pos, _Args&&... __args)
370 { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
372 //@{
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)); }
393 //@}
395 //@{
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.
415 iterator
416 insert(const_iterator __hint, const value_type& __x)
417 { return _M_h.insert(__hint, __x); }
419 iterator
420 insert(const_iterator __hint, value_type&& __x)
421 { return _M_h.insert(__hint, std::move(__x)); }
422 //@}
425 * @brief A template function that attempts to insert a range of
426 * elements.
427 * @param __first Iterator pointing to the start of the range to be
428 * inserted.
429 * @param __last Iterator pointing to the end of the range.
431 * Complexity similar to that of the range constructor.
433 template<typename _InputIterator>
434 void
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
441 * to be inserted.
443 * Complexity similar to that of the range constructor.
445 void
446 insert(initializer_list<value_type> __l)
447 { _M_h.insert(__l); }
449 //@{
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
461 * responsibility.
463 iterator
464 erase(const_iterator __position)
465 { return _M_h.erase(__position); }
467 // LWG 2059.
468 iterator
469 erase(iterator __position)
470 { return _M_h.erase(__position); }
471 //@}
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.
485 size_type
486 erase(const key_type& __x)
487 { return _M_h.erase(__x); }
490 * @brief Erases a [__first,__last) range of elements from an
491 * %unordered_set.
492 * @param __first Iterator pointing to the start of the range to be
493 * erased.
494 * @param __last Iterator pointing to the end of the range to
495 * be erased.
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.
503 iterator
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.
513 void
514 clear() noexcept
515 { _M_h.clear(); }
518 * @brief Swaps data with another %unordered_set.
519 * @param __x An %unordered_set of the same element and allocator
520 * types.
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.
526 void
527 swap(unordered_set& __x)
528 noexcept( noexcept(_M_h.swap(__x._M_h)) )
529 { _M_h.swap(__x._M_h); }
531 // observers.
533 /// Returns the hash functor object with which the %unordered_set was
534 /// constructed.
535 hasher
536 hash_function() const
537 { return _M_h.hash_function(); }
539 /// Returns the key comparison object with which the %unordered_set was
540 /// constructed.
541 key_equal
542 key_eq() const
543 { return _M_h.key_eq(); }
545 // lookup.
547 //@{
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
552 * found.
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.
559 iterator
560 find(const key_type& __x)
561 { return _M_h.find(__x); }
563 const_iterator
564 find(const key_type& __x) const
565 { return _M_h.find(__x); }
566 //@}
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
575 * (present).
577 size_type
578 count(const key_type& __x) const
579 { return _M_h.count(__x); }
581 //@{
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); }
597 //@}
599 // bucket interface.
601 /// Returns the number of buckets of the %unordered_set.
602 size_type
603 bucket_count() const noexcept
604 { return _M_h.bucket_count(); }
606 /// Returns the maximum number of buckets of the %unordered_set.
607 size_type
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.
616 size_type
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.
625 size_type
626 bucket(const key_type& __key) const
627 { return _M_h.bucket(__key); }
629 //@{
631 * @brief Returns a read-only (constant) iterator pointing to the first
632 * bucket element.
633 * @param __n The bucket index.
634 * @return A read-only local iterator.
636 local_iterator
637 begin(size_type __n)
638 { return _M_h.begin(__n); }
640 const_local_iterator
641 begin(size_type __n) const
642 { return _M_h.begin(__n); }
644 const_local_iterator
645 cbegin(size_type __n) const
646 { return _M_h.cbegin(__n); }
647 //@}
649 //@{
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.
656 local_iterator
657 end(size_type __n)
658 { return _M_h.end(__n); }
660 const_local_iterator
661 end(size_type __n) const
662 { return _M_h.end(__n); }
664 const_local_iterator
665 cend(size_type __n) const
666 { return _M_h.cend(__n); }
667 //@}
669 // hash policy.
671 /// Returns the average number of elements per bucket.
672 float
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.
678 float
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.
686 void
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.
697 void
698 rehash(size_type __n)
699 { _M_h.rehash(__n); }
702 * @brief Prepare the %unordered_set for a specified number of
703 * elements.
704 * @param __n Number of elements required.
706 * Same as rehash(ceil(n / max_load_factor())).
708 void
709 reserve(size_type __n)
710 { _M_h.reserve(__n); }
712 template<typename _Value1, typename _Hash1, typename _Pred1,
713 typename _Alloc1>
714 friend bool
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;
745 _Hashtable _M_h;
747 public:
748 // typedefs:
749 //@{
750 /// Public typedefs.
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;
756 //@}
758 //@{
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;
770 //@}
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.
784 explicit
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,
797 const hasher& __hf,
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,
817 size_type __n = 0,
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,
842 size_type __n = 0,
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.
850 unordered_multiset&
851 operator=(const unordered_multiset&) = default;
853 /// Move assignment operator.
854 unordered_multiset&
855 operator=(unordered_multiset&&) = default;
858 * @brief Creates an %unordered_multiset with no elements.
859 * @param __a An allocator object.
861 explicit
862 unordered_multiset(const allocator_type& __a)
863 : _M_h(__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.
897 unordered_multiset&
898 operator=(initializer_list<value_type> __l)
900 _M_h = __l;
901 return *this;
904 /// Returns the allocator object with which the %unordered_multiset was
905 /// constructed.
906 allocator_type
907 get_allocator() const noexcept
908 { return _M_h.get_allocator(); }
910 // size and capacity:
912 /// Returns true if the %unordered_multiset is empty.
913 bool
914 empty() const noexcept
915 { return _M_h.empty(); }
917 /// Returns the size of the %unordered_multiset.
918 size_type
919 size() const noexcept
920 { return _M_h.size(); }
922 /// Returns the maximum size of the %unordered_multiset.
923 size_type
924 max_size() const noexcept
925 { return _M_h.max_size(); }
927 // iterators.
929 //@{
931 * Returns a read-only (constant) iterator that points to the first
932 * element in the %unordered_multiset.
934 iterator
935 begin() noexcept
936 { return _M_h.begin(); }
938 const_iterator
939 begin() const noexcept
940 { return _M_h.begin(); }
941 //@}
943 //@{
945 * Returns a read-only (constant) iterator that points one past the last
946 * element in the %unordered_multiset.
948 iterator
949 end() noexcept
950 { return _M_h.end(); }
952 const_iterator
953 end() const noexcept
954 { return _M_h.end(); }
955 //@}
958 * Returns a read-only (constant) iterator that points to the first
959 * element in the %unordered_multiset.
961 const_iterator
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.
969 const_iterator
970 cend() const noexcept
971 { return _M_h.end(); }
973 // modifiers.
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>
983 iterator
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
992 * inserted.
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>
1005 iterator
1006 emplace_hint(const_iterator __pos, _Args&&... __args)
1007 { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); }
1009 //@{
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.
1017 iterator
1018 insert(const value_type& __x)
1019 { return _M_h.insert(__x); }
1021 iterator
1022 insert(value_type&& __x)
1023 { return _M_h.insert(std::move(__x)); }
1024 //@}
1026 //@{
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.
1043 iterator
1044 insert(const_iterator __hint, const value_type& __x)
1045 { return _M_h.insert(__hint, __x); }
1047 iterator
1048 insert(const_iterator __hint, value_type&& __x)
1049 { return _M_h.insert(__hint, std::move(__x)); }
1050 //@}
1053 * @brief A template function that inserts a range of elements.
1054 * @param __first Iterator pointing to the start of the range to be
1055 * inserted.
1056 * @param __last Iterator pointing to the end of the range.
1058 * Complexity similar to that of the range constructor.
1060 template<typename _InputIterator>
1061 void
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
1068 * inserted.
1070 * Complexity similar to that of the range constructor.
1072 void
1073 insert(initializer_list<value_type> __l)
1074 { _M_h.insert(__l); }
1076 //@{
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.
1091 iterator
1092 erase(const_iterator __position)
1093 { return _M_h.erase(__position); }
1095 // LWG 2059.
1096 iterator
1097 erase(iterator __position)
1098 { return _M_h.erase(__position); }
1099 //@}
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.
1114 size_type
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
1122 * erased.
1123 * @param __last Iterator pointing to the end of the range to
1124 * be erased.
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.
1134 iterator
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.
1145 void
1146 clear() noexcept
1147 { _M_h.clear(); }
1150 * @brief Swaps data with another %unordered_multiset.
1151 * @param __x An %unordered_multiset of the same element and allocator
1152 * types.
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.
1158 void
1159 swap(unordered_multiset& __x)
1160 noexcept( noexcept(_M_h.swap(__x._M_h)) )
1161 { _M_h.swap(__x._M_h); }
1163 // observers.
1165 /// Returns the hash functor object with which the %unordered_multiset
1166 /// was constructed.
1167 hasher
1168 hash_function() const
1169 { return _M_h.hash_function(); }
1171 /// Returns the key comparison object with which the %unordered_multiset
1172 /// was constructed.
1173 key_equal
1174 key_eq() const
1175 { return _M_h.key_eq(); }
1177 // lookup.
1179 //@{
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
1184 * found.
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.
1191 iterator
1192 find(const key_type& __x)
1193 { return _M_h.find(__x); }
1195 const_iterator
1196 find(const key_type& __x) const
1197 { return _M_h.find(__x); }
1198 //@}
1201 * @brief Finds the number of elements.
1202 * @param __x Element to located.
1203 * @return Number of elements with specified key.
1205 size_type
1206 count(const key_type& __x) const
1207 { return _M_h.count(__x); }
1209 //@{
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); }
1223 //@}
1225 // bucket interface.
1227 /// Returns the number of buckets of the %unordered_multiset.
1228 size_type
1229 bucket_count() const noexcept
1230 { return _M_h.bucket_count(); }
1232 /// Returns the maximum number of buckets of the %unordered_multiset.
1233 size_type
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.
1242 size_type
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.
1251 size_type
1252 bucket(const key_type& __key) const
1253 { return _M_h.bucket(__key); }
1255 //@{
1257 * @brief Returns a read-only (constant) iterator pointing to the first
1258 * bucket element.
1259 * @param __n The bucket index.
1260 * @return A read-only local iterator.
1262 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); }
1273 //@}
1275 //@{
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.
1282 local_iterator
1283 end(size_type __n)
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); }
1293 //@}
1295 // hash policy.
1297 /// Returns the average number of elements per bucket.
1298 float
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.
1304 float
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.
1312 void
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.
1323 void
1324 rehash(size_type __n)
1325 { _M_h.rehash(__n); }
1328 * @brief Prepare the %unordered_multiset for a specified number of
1329 * elements.
1330 * @param __n Number of elements required.
1332 * Same as rehash(ceil(n / max_load_factor())).
1334 void
1335 reserve(size_type __n)
1336 { _M_h.reserve(__n); }
1338 template<typename _Value1, typename _Hash1, typename _Pred1,
1339 typename _Alloc1>
1340 friend bool
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>
1346 inline void
1347 swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
1348 unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
1349 { __x.swap(__y); }
1351 template<class _Value, class _Hash, class _Pred, class _Alloc>
1352 inline void
1353 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
1354 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
1355 { __x.swap(__y); }
1357 template<class _Value, class _Hash, class _Pred, class _Alloc>
1358 inline bool
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>
1364 inline bool
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>
1370 inline bool
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>
1376 inline bool
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
1382 } // namespace std
1384 #endif /* _UNORDERED_SET_H */