]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_multiset.h
re PR libstdc++/81064 (Inline namespace regression)
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_multiset.h
1 // Multiset implementation -*- C++ -*-
2
3 // Copyright (C) 2001-2017 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.
10
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.
15
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.
19
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/>.
24
25 /*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51 /** @file bits/stl_multiset.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{set}
54 */
55
56 #ifndef _STL_MULTISET_H
57 #define _STL_MULTISET_H 1
58
59 #include <bits/concept_check.h>
60 #if __cplusplus >= 201103L
61 #include <initializer_list>
62 #endif
63
64 namespace std _GLIBCXX_VISIBILITY(default)
65 {
66 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
68
69 template<typename _Key, typename _Compare, typename _Alloc>
70 class set;
71
72 /**
73 * @brief A standard container made up of elements, which can be retrieved
74 * in logarithmic time.
75 *
76 * @ingroup associative_containers
77 *
78 *
79 * @tparam _Key Type of key objects.
80 * @tparam _Compare Comparison function object type, defaults to less<_Key>.
81 * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
82 *
83 * Meets the requirements of a <a href="tables.html#65">container</a>, a
84 * <a href="tables.html#66">reversible container</a>, and an
85 * <a href="tables.html#69">associative container</a> (using equivalent
86 * keys). For a @c multiset<Key> the key_type and value_type are Key.
87 *
88 * Multisets support bidirectional iterators.
89 *
90 * The private tree data is declared exactly the same way for set and
91 * multiset; the distinction is made entirely in how the tree functions are
92 * called (*_unique versus *_equal, same as the standard).
93 */
94 template <typename _Key, typename _Compare = std::less<_Key>,
95 typename _Alloc = std::allocator<_Key> >
96 class multiset
97 {
98 #ifdef _GLIBCXX_CONCEPT_CHECKS
99 // concept requirements
100 typedef typename _Alloc::value_type _Alloc_value_type;
101 # if __cplusplus < 201103L
102 __glibcxx_class_requires(_Key, _SGIAssignableConcept)
103 # endif
104 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
105 _BinaryFunctionConcept)
106 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
107 #endif
108
109 public:
110 // typedefs:
111 typedef _Key key_type;
112 typedef _Key value_type;
113 typedef _Compare key_compare;
114 typedef _Compare value_compare;
115 typedef _Alloc allocator_type;
116
117 private:
118 /// This turns a red-black tree into a [multi]set.
119 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
120 rebind<_Key>::other _Key_alloc_type;
121
122 typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
123 key_compare, _Key_alloc_type> _Rep_type;
124 /// The actual tree structure.
125 _Rep_type _M_t;
126
127 typedef __gnu_cxx::__alloc_traits<_Key_alloc_type> _Alloc_traits;
128
129 public:
130 typedef typename _Alloc_traits::pointer pointer;
131 typedef typename _Alloc_traits::const_pointer const_pointer;
132 typedef typename _Alloc_traits::reference reference;
133 typedef typename _Alloc_traits::const_reference const_reference;
134 // _GLIBCXX_RESOLVE_LIB_DEFECTS
135 // DR 103. set::iterator is required to be modifiable,
136 // but this allows modification of keys.
137 typedef typename _Rep_type::const_iterator iterator;
138 typedef typename _Rep_type::const_iterator const_iterator;
139 typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
140 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
141 typedef typename _Rep_type::size_type size_type;
142 typedef typename _Rep_type::difference_type difference_type;
143
144 #if __cplusplus > 201402L
145 using node_type = typename _Rep_type::node_type;
146 #endif
147
148 // allocation/deallocation
149 /**
150 * @brief Default constructor creates no elements.
151 */
152 #if __cplusplus < 201103L
153 multiset() : _M_t() { }
154 #else
155 multiset() = default;
156 #endif
157
158 /**
159 * @brief Creates a %multiset with no elements.
160 * @param __comp Comparator to use.
161 * @param __a An allocator object.
162 */
163 explicit
164 multiset(const _Compare& __comp,
165 const allocator_type& __a = allocator_type())
166 : _M_t(__comp, _Key_alloc_type(__a)) { }
167
168 /**
169 * @brief Builds a %multiset from a range.
170 * @param __first An input iterator.
171 * @param __last An input iterator.
172 *
173 * Create a %multiset consisting of copies of the elements from
174 * [first,last). This is linear in N if the range is already sorted,
175 * and NlogN otherwise (where N is distance(__first,__last)).
176 */
177 template<typename _InputIterator>
178 multiset(_InputIterator __first, _InputIterator __last)
179 : _M_t()
180 { _M_t._M_insert_equal(__first, __last); }
181
182 /**
183 * @brief Builds a %multiset from a range.
184 * @param __first An input iterator.
185 * @param __last An input iterator.
186 * @param __comp A comparison functor.
187 * @param __a An allocator object.
188 *
189 * Create a %multiset consisting of copies of the elements from
190 * [__first,__last). This is linear in N if the range is already sorted,
191 * and NlogN otherwise (where N is distance(__first,__last)).
192 */
193 template<typename _InputIterator>
194 multiset(_InputIterator __first, _InputIterator __last,
195 const _Compare& __comp,
196 const allocator_type& __a = allocator_type())
197 : _M_t(__comp, _Key_alloc_type(__a))
198 { _M_t._M_insert_equal(__first, __last); }
199
200 /**
201 * @brief %Multiset copy constructor.
202 *
203 * Whether the allocator is copied depends on the allocator traits.
204 */
205 #if __cplusplus < 201103L
206 multiset(const multiset& __x)
207 : _M_t(__x._M_t) { }
208 #else
209 multiset(const multiset&) = default;
210
211 /**
212 * @brief %Multiset move constructor.
213 *
214 * The newly-created %multiset contains the exact contents of the
215 * moved instance. The moved instance is a valid, but unspecified
216 * %multiset.
217 */
218 multiset(multiset&&) = default;
219
220 /**
221 * @brief Builds a %multiset from an initializer_list.
222 * @param __l An initializer_list.
223 * @param __comp A comparison functor.
224 * @param __a An allocator object.
225 *
226 * Create a %multiset consisting of copies of the elements from
227 * the list. This is linear in N if the list is already sorted,
228 * and NlogN otherwise (where N is @a __l.size()).
229 */
230 multiset(initializer_list<value_type> __l,
231 const _Compare& __comp = _Compare(),
232 const allocator_type& __a = allocator_type())
233 : _M_t(__comp, _Key_alloc_type(__a))
234 { _M_t._M_insert_equal(__l.begin(), __l.end()); }
235
236 /// Allocator-extended default constructor.
237 explicit
238 multiset(const allocator_type& __a)
239 : _M_t(_Compare(), _Key_alloc_type(__a)) { }
240
241 /// Allocator-extended copy constructor.
242 multiset(const multiset& __m, const allocator_type& __a)
243 : _M_t(__m._M_t, _Key_alloc_type(__a)) { }
244
245 /// Allocator-extended move constructor.
246 multiset(multiset&& __m, const allocator_type& __a)
247 noexcept(is_nothrow_copy_constructible<_Compare>::value
248 && _Alloc_traits::_S_always_equal())
249 : _M_t(std::move(__m._M_t), _Key_alloc_type(__a)) { }
250
251 /// Allocator-extended initialier-list constructor.
252 multiset(initializer_list<value_type> __l, const allocator_type& __a)
253 : _M_t(_Compare(), _Key_alloc_type(__a))
254 { _M_t._M_insert_equal(__l.begin(), __l.end()); }
255
256 /// Allocator-extended range constructor.
257 template<typename _InputIterator>
258 multiset(_InputIterator __first, _InputIterator __last,
259 const allocator_type& __a)
260 : _M_t(_Compare(), _Key_alloc_type(__a))
261 { _M_t._M_insert_equal(__first, __last); }
262
263 /**
264 * The dtor only erases the elements, and note that if the elements
265 * themselves are pointers, the pointed-to memory is not touched in any
266 * way. Managing the pointer is the user's responsibility.
267 */
268 ~multiset() = default;
269 #endif
270
271 /**
272 * @brief %Multiset assignment operator.
273 *
274 * Whether the allocator is copied depends on the allocator traits.
275 */
276 #if __cplusplus < 201103L
277 multiset&
278 operator=(const multiset& __x)
279 {
280 _M_t = __x._M_t;
281 return *this;
282 }
283 #else
284 multiset&
285 operator=(const multiset&) = default;
286
287 /// Move assignment operator.
288 multiset&
289 operator=(multiset&&) = default;
290
291 /**
292 * @brief %Multiset list assignment operator.
293 * @param __l An initializer_list.
294 *
295 * This function fills a %multiset with copies of the elements in the
296 * initializer list @a __l.
297 *
298 * Note that the assignment completely changes the %multiset and
299 * that the resulting %multiset's size is the same as the number
300 * of elements assigned.
301 */
302 multiset&
303 operator=(initializer_list<value_type> __l)
304 {
305 _M_t._M_assign_equal(__l.begin(), __l.end());
306 return *this;
307 }
308 #endif
309
310 // accessors:
311
312 /// Returns the comparison object.
313 key_compare
314 key_comp() const
315 { return _M_t.key_comp(); }
316 /// Returns the comparison object.
317 value_compare
318 value_comp() const
319 { return _M_t.key_comp(); }
320 /// Returns the memory allocation object.
321 allocator_type
322 get_allocator() const _GLIBCXX_NOEXCEPT
323 { return allocator_type(_M_t.get_allocator()); }
324
325 /**
326 * Returns a read-only (constant) iterator that points to the first
327 * element in the %multiset. Iteration is done in ascending order
328 * according to the keys.
329 */
330 iterator
331 begin() const _GLIBCXX_NOEXCEPT
332 { return _M_t.begin(); }
333
334 /**
335 * Returns a read-only (constant) iterator that points one past the last
336 * element in the %multiset. Iteration is done in ascending order
337 * according to the keys.
338 */
339 iterator
340 end() const _GLIBCXX_NOEXCEPT
341 { return _M_t.end(); }
342
343 /**
344 * Returns a read-only (constant) reverse iterator that points to the
345 * last element in the %multiset. Iteration is done in descending order
346 * according to the keys.
347 */
348 reverse_iterator
349 rbegin() const _GLIBCXX_NOEXCEPT
350 { return _M_t.rbegin(); }
351
352 /**
353 * Returns a read-only (constant) reverse iterator that points to the
354 * last element in the %multiset. Iteration is done in descending order
355 * according to the keys.
356 */
357 reverse_iterator
358 rend() const _GLIBCXX_NOEXCEPT
359 { return _M_t.rend(); }
360
361 #if __cplusplus >= 201103L
362 /**
363 * Returns a read-only (constant) iterator that points to the first
364 * element in the %multiset. Iteration is done in ascending order
365 * according to the keys.
366 */
367 iterator
368 cbegin() const noexcept
369 { return _M_t.begin(); }
370
371 /**
372 * Returns a read-only (constant) iterator that points one past the last
373 * element in the %multiset. Iteration is done in ascending order
374 * according to the keys.
375 */
376 iterator
377 cend() const noexcept
378 { return _M_t.end(); }
379
380 /**
381 * Returns a read-only (constant) reverse iterator that points to the
382 * last element in the %multiset. Iteration is done in descending order
383 * according to the keys.
384 */
385 reverse_iterator
386 crbegin() const noexcept
387 { return _M_t.rbegin(); }
388
389 /**
390 * Returns a read-only (constant) reverse iterator that points to the
391 * last element in the %multiset. Iteration is done in descending order
392 * according to the keys.
393 */
394 reverse_iterator
395 crend() const noexcept
396 { return _M_t.rend(); }
397 #endif
398
399 /// Returns true if the %set is empty.
400 bool
401 empty() const _GLIBCXX_NOEXCEPT
402 { return _M_t.empty(); }
403
404 /// Returns the size of the %set.
405 size_type
406 size() const _GLIBCXX_NOEXCEPT
407 { return _M_t.size(); }
408
409 /// Returns the maximum size of the %set.
410 size_type
411 max_size() const _GLIBCXX_NOEXCEPT
412 { return _M_t.max_size(); }
413
414 /**
415 * @brief Swaps data with another %multiset.
416 * @param __x A %multiset of the same element and allocator types.
417 *
418 * This exchanges the elements between two multisets in constant time.
419 * (It is only swapping a pointer, an integer, and an instance of the @c
420 * Compare type (which itself is often stateless and empty), so it should
421 * be quite fast.)
422 * Note that the global std::swap() function is specialized such that
423 * std::swap(s1,s2) will feed to this function.
424 *
425 * Whether the allocators are swapped depends on the allocator traits.
426 */
427 void
428 swap(multiset& __x)
429 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
430 { _M_t.swap(__x._M_t); }
431
432 // insert/erase
433 #if __cplusplus >= 201103L
434 /**
435 * @brief Builds and inserts an element into the %multiset.
436 * @param __args Arguments used to generate the element instance to be
437 * inserted.
438 * @return An iterator that points to the inserted element.
439 *
440 * This function inserts an element into the %multiset. Contrary
441 * to a std::set the %multiset does not rely on unique keys and thus
442 * multiple copies of the same element can be inserted.
443 *
444 * Insertion requires logarithmic time.
445 */
446 template<typename... _Args>
447 iterator
448 emplace(_Args&&... __args)
449 { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
450
451 /**
452 * @brief Builds and inserts an element into the %multiset.
453 * @param __pos An iterator that serves as a hint as to where the
454 * element should be inserted.
455 * @param __args Arguments used to generate the element instance to be
456 * inserted.
457 * @return An iterator that points to the inserted element.
458 *
459 * This function inserts an element into the %multiset. Contrary
460 * to a std::set the %multiset does not rely on unique keys and thus
461 * multiple copies of the same element can be inserted.
462 *
463 * Note that the first parameter is only a hint and can potentially
464 * improve the performance of the insertion process. A bad hint would
465 * cause no gains in efficiency.
466 *
467 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
468 * for more on @a hinting.
469 *
470 * Insertion requires logarithmic time (if the hint is not taken).
471 */
472 template<typename... _Args>
473 iterator
474 emplace_hint(const_iterator __pos, _Args&&... __args)
475 {
476 return _M_t._M_emplace_hint_equal(__pos,
477 std::forward<_Args>(__args)...);
478 }
479 #endif
480
481 /**
482 * @brief Inserts an element into the %multiset.
483 * @param __x Element to be inserted.
484 * @return An iterator that points to the inserted element.
485 *
486 * This function inserts an element into the %multiset. Contrary
487 * to a std::set the %multiset does not rely on unique keys and thus
488 * multiple copies of the same element can be inserted.
489 *
490 * Insertion requires logarithmic time.
491 */
492 iterator
493 insert(const value_type& __x)
494 { return _M_t._M_insert_equal(__x); }
495
496 #if __cplusplus >= 201103L
497 iterator
498 insert(value_type&& __x)
499 { return _M_t._M_insert_equal(std::move(__x)); }
500 #endif
501
502 /**
503 * @brief Inserts an element into the %multiset.
504 * @param __position An iterator that serves as a hint as to where the
505 * element should be inserted.
506 * @param __x Element to be inserted.
507 * @return An iterator that points to the inserted element.
508 *
509 * This function inserts an element into the %multiset. Contrary
510 * to a std::set the %multiset does not rely on unique keys and thus
511 * multiple copies of the same element can be inserted.
512 *
513 * Note that the first parameter is only a hint and can potentially
514 * improve the performance of the insertion process. A bad hint would
515 * cause no gains in efficiency.
516 *
517 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
518 * for more on @a hinting.
519 *
520 * Insertion requires logarithmic time (if the hint is not taken).
521 */
522 iterator
523 insert(const_iterator __position, const value_type& __x)
524 { return _M_t._M_insert_equal_(__position, __x); }
525
526 #if __cplusplus >= 201103L
527 iterator
528 insert(const_iterator __position, value_type&& __x)
529 { return _M_t._M_insert_equal_(__position, std::move(__x)); }
530 #endif
531
532 /**
533 * @brief A template function that tries to insert a range of elements.
534 * @param __first Iterator pointing to the start of the range to be
535 * inserted.
536 * @param __last Iterator pointing to the end of the range.
537 *
538 * Complexity similar to that of the range constructor.
539 */
540 template<typename _InputIterator>
541 void
542 insert(_InputIterator __first, _InputIterator __last)
543 { _M_t._M_insert_equal(__first, __last); }
544
545 #if __cplusplus >= 201103L
546 /**
547 * @brief Attempts to insert a list of elements into the %multiset.
548 * @param __l A std::initializer_list<value_type> of elements
549 * to be inserted.
550 *
551 * Complexity similar to that of the range constructor.
552 */
553 void
554 insert(initializer_list<value_type> __l)
555 { this->insert(__l.begin(), __l.end()); }
556 #endif
557
558 #if __cplusplus > 201402L
559 /// Extract a node.
560 node_type
561 extract(const_iterator __pos)
562 {
563 __glibcxx_assert(__pos != end());
564 return _M_t.extract(__pos);
565 }
566
567 /// Extract a node.
568 node_type
569 extract(const key_type& __x)
570 { return _M_t.extract(__x); }
571
572 /// Re-insert an extracted node.
573 iterator
574 insert(node_type&& __nh)
575 { return _M_t._M_reinsert_node_equal(std::move(__nh)); }
576
577 /// Re-insert an extracted node.
578 iterator
579 insert(const_iterator __hint, node_type&& __nh)
580 { return _M_t._M_reinsert_node_hint_equal(__hint, std::move(__nh)); }
581
582 template<typename, typename>
583 friend class _Rb_tree_merge_helper;
584
585 template<typename _Compare1>
586 void
587 merge(multiset<_Key, _Compare1, _Alloc>& __source)
588 {
589 using _Merge_helper = _Rb_tree_merge_helper<multiset, _Compare1>;
590 _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
591 }
592
593 template<typename _Compare1>
594 void
595 merge(multiset<_Key, _Compare1, _Alloc>&& __source)
596 { merge(__source); }
597
598 template<typename _Compare1>
599 void
600 merge(set<_Key, _Compare1, _Alloc>& __source)
601 {
602 using _Merge_helper = _Rb_tree_merge_helper<multiset, _Compare1>;
603 _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
604 }
605
606 template<typename _Compare1>
607 void
608 merge(set<_Key, _Compare1, _Alloc>&& __source)
609 { merge(__source); }
610 #endif // C++17
611
612 #if __cplusplus >= 201103L
613 // _GLIBCXX_RESOLVE_LIB_DEFECTS
614 // DR 130. Associative erase should return an iterator.
615 /**
616 * @brief Erases an element from a %multiset.
617 * @param __position An iterator pointing to the element to be erased.
618 * @return An iterator pointing to the element immediately following
619 * @a position prior to the element being erased. If no such
620 * element exists, end() is returned.
621 *
622 * This function erases an element, pointed to by the given iterator,
623 * from a %multiset. Note that this function only erases the element,
624 * and that if the element is itself a pointer, the pointed-to memory is
625 * not touched in any way. Managing the pointer is the user's
626 * responsibility.
627 */
628 _GLIBCXX_ABI_TAG_CXX11
629 iterator
630 erase(const_iterator __position)
631 { return _M_t.erase(__position); }
632 #else
633 /**
634 * @brief Erases an element from a %multiset.
635 * @param __position An iterator pointing to the element to be erased.
636 *
637 * This function erases an element, pointed to by the given iterator,
638 * from a %multiset. Note that this function only erases the element,
639 * and that if the element is itself a pointer, the pointed-to memory is
640 * not touched in any way. Managing the pointer is the user's
641 * responsibility.
642 */
643 void
644 erase(iterator __position)
645 { _M_t.erase(__position); }
646 #endif
647
648 /**
649 * @brief Erases elements according to the provided key.
650 * @param __x Key of element to be erased.
651 * @return The number of elements erased.
652 *
653 * This function erases all elements located by the given key from a
654 * %multiset.
655 * Note that this function only erases the element, and that if
656 * the element is itself a pointer, the pointed-to memory is not touched
657 * in any way. Managing the pointer is the user's responsibility.
658 */
659 size_type
660 erase(const key_type& __x)
661 { return _M_t.erase(__x); }
662
663 #if __cplusplus >= 201103L
664 // _GLIBCXX_RESOLVE_LIB_DEFECTS
665 // DR 130. Associative erase should return an iterator.
666 /**
667 * @brief Erases a [first,last) range of elements from a %multiset.
668 * @param __first Iterator pointing to the start of the range to be
669 * erased.
670 * @param __last Iterator pointing to the end of the range to
671 * be erased.
672 * @return The iterator @a last.
673 *
674 * This function erases a sequence of elements from a %multiset.
675 * Note that this function only erases the elements, and that if
676 * the elements themselves are pointers, the pointed-to memory is not
677 * touched in any way. Managing the pointer is the user's
678 * responsibility.
679 */
680 _GLIBCXX_ABI_TAG_CXX11
681 iterator
682 erase(const_iterator __first, const_iterator __last)
683 { return _M_t.erase(__first, __last); }
684 #else
685 /**
686 * @brief Erases a [first,last) range of elements from a %multiset.
687 * @param first Iterator pointing to the start of the range to be
688 * erased.
689 * @param last Iterator pointing to the end of the range to be erased.
690 *
691 * This function erases a sequence of elements from a %multiset.
692 * Note that this function only erases the elements, and that if
693 * the elements themselves are pointers, the pointed-to memory is not
694 * touched in any way. Managing the pointer is the user's
695 * responsibility.
696 */
697 void
698 erase(iterator __first, iterator __last)
699 { _M_t.erase(__first, __last); }
700 #endif
701
702 /**
703 * Erases all elements in a %multiset. Note that this function only
704 * erases the elements, and that if the elements themselves are pointers,
705 * the pointed-to memory is not touched in any way. Managing the pointer
706 * is the user's responsibility.
707 */
708 void
709 clear() _GLIBCXX_NOEXCEPT
710 { _M_t.clear(); }
711
712 // multiset operations:
713
714 //@{
715 /**
716 * @brief Finds the number of elements with given key.
717 * @param __x Key of elements to be located.
718 * @return Number of elements with specified key.
719 */
720 size_type
721 count(const key_type& __x) const
722 { return _M_t.count(__x); }
723
724 #if __cplusplus > 201103L
725 template<typename _Kt>
726 auto
727 count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
728 { return _M_t._M_count_tr(__x); }
729 #endif
730 //@}
731
732 // _GLIBCXX_RESOLVE_LIB_DEFECTS
733 // 214. set::find() missing const overload
734 //@{
735 /**
736 * @brief Tries to locate an element in a %set.
737 * @param __x Element to be located.
738 * @return Iterator pointing to sought-after element, or end() if not
739 * found.
740 *
741 * This function takes a key and tries to locate the element with which
742 * the key matches. If successful the function returns an iterator
743 * pointing to the sought after element. If unsuccessful it returns the
744 * past-the-end ( @c end() ) iterator.
745 */
746 iterator
747 find(const key_type& __x)
748 { return _M_t.find(__x); }
749
750 const_iterator
751 find(const key_type& __x) const
752 { return _M_t.find(__x); }
753
754 #if __cplusplus > 201103L
755 template<typename _Kt>
756 auto
757 find(const _Kt& __x)
758 -> decltype(iterator{_M_t._M_find_tr(__x)})
759 { return iterator{_M_t._M_find_tr(__x)}; }
760
761 template<typename _Kt>
762 auto
763 find(const _Kt& __x) const
764 -> decltype(const_iterator{_M_t._M_find_tr(__x)})
765 { return const_iterator{_M_t._M_find_tr(__x)}; }
766 #endif
767 //@}
768
769 //@{
770 /**
771 * @brief Finds the beginning of a subsequence matching given key.
772 * @param __x Key to be located.
773 * @return Iterator pointing to first element equal to or greater
774 * than key, or end().
775 *
776 * This function returns the first element of a subsequence of elements
777 * that matches the given key. If unsuccessful it returns an iterator
778 * pointing to the first element that has a greater value than given key
779 * or end() if no such element exists.
780 */
781 iterator
782 lower_bound(const key_type& __x)
783 { return _M_t.lower_bound(__x); }
784
785 const_iterator
786 lower_bound(const key_type& __x) const
787 { return _M_t.lower_bound(__x); }
788
789 #if __cplusplus > 201103L
790 template<typename _Kt>
791 auto
792 lower_bound(const _Kt& __x)
793 -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
794 { return iterator(_M_t._M_lower_bound_tr(__x)); }
795
796 template<typename _Kt>
797 auto
798 lower_bound(const _Kt& __x) const
799 -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
800 { return iterator(_M_t._M_lower_bound_tr(__x)); }
801 #endif
802 //@}
803
804 //@{
805 /**
806 * @brief Finds the end of a subsequence matching given key.
807 * @param __x Key to be located.
808 * @return Iterator pointing to the first element
809 * greater than key, or end().
810 */
811 iterator
812 upper_bound(const key_type& __x)
813 { return _M_t.upper_bound(__x); }
814
815 const_iterator
816 upper_bound(const key_type& __x) const
817 { return _M_t.upper_bound(__x); }
818
819 #if __cplusplus > 201103L
820 template<typename _Kt>
821 auto
822 upper_bound(const _Kt& __x)
823 -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
824 { return iterator(_M_t._M_upper_bound_tr(__x)); }
825
826 template<typename _Kt>
827 auto
828 upper_bound(const _Kt& __x) const
829 -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
830 { return iterator(_M_t._M_upper_bound_tr(__x)); }
831 #endif
832 //@}
833
834 //@{
835 /**
836 * @brief Finds a subsequence matching given key.
837 * @param __x Key to be located.
838 * @return Pair of iterators that possibly points to the subsequence
839 * matching given key.
840 *
841 * This function is equivalent to
842 * @code
843 * std::make_pair(c.lower_bound(val),
844 * c.upper_bound(val))
845 * @endcode
846 * (but is faster than making the calls separately).
847 *
848 * This function probably only makes sense for multisets.
849 */
850 std::pair<iterator, iterator>
851 equal_range(const key_type& __x)
852 { return _M_t.equal_range(__x); }
853
854 std::pair<const_iterator, const_iterator>
855 equal_range(const key_type& __x) const
856 { return _M_t.equal_range(__x); }
857
858 #if __cplusplus > 201103L
859 template<typename _Kt>
860 auto
861 equal_range(const _Kt& __x)
862 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
863 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
864
865 template<typename _Kt>
866 auto
867 equal_range(const _Kt& __x) const
868 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
869 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
870 #endif
871 //@}
872
873 template<typename _K1, typename _C1, typename _A1>
874 friend bool
875 operator==(const multiset<_K1, _C1, _A1>&,
876 const multiset<_K1, _C1, _A1>&);
877
878 template<typename _K1, typename _C1, typename _A1>
879 friend bool
880 operator< (const multiset<_K1, _C1, _A1>&,
881 const multiset<_K1, _C1, _A1>&);
882 };
883
884 /**
885 * @brief Multiset equality comparison.
886 * @param __x A %multiset.
887 * @param __y A %multiset of the same type as @a __x.
888 * @return True iff the size and elements of the multisets are equal.
889 *
890 * This is an equivalence relation. It is linear in the size of the
891 * multisets.
892 * Multisets are considered equivalent if their sizes are equal, and if
893 * corresponding elements compare equal.
894 */
895 template<typename _Key, typename _Compare, typename _Alloc>
896 inline bool
897 operator==(const multiset<_Key, _Compare, _Alloc>& __x,
898 const multiset<_Key, _Compare, _Alloc>& __y)
899 { return __x._M_t == __y._M_t; }
900
901 /**
902 * @brief Multiset ordering relation.
903 * @param __x A %multiset.
904 * @param __y A %multiset of the same type as @a __x.
905 * @return True iff @a __x is lexicographically less than @a __y.
906 *
907 * This is a total ordering relation. It is linear in the size of the
908 * sets. The elements must be comparable with @c <.
909 *
910 * See std::lexicographical_compare() for how the determination is made.
911 */
912 template<typename _Key, typename _Compare, typename _Alloc>
913 inline bool
914 operator<(const multiset<_Key, _Compare, _Alloc>& __x,
915 const multiset<_Key, _Compare, _Alloc>& __y)
916 { return __x._M_t < __y._M_t; }
917
918 /// Returns !(x == y).
919 template<typename _Key, typename _Compare, typename _Alloc>
920 inline bool
921 operator!=(const multiset<_Key, _Compare, _Alloc>& __x,
922 const multiset<_Key, _Compare, _Alloc>& __y)
923 { return !(__x == __y); }
924
925 /// Returns y < x.
926 template<typename _Key, typename _Compare, typename _Alloc>
927 inline bool
928 operator>(const multiset<_Key,_Compare,_Alloc>& __x,
929 const multiset<_Key,_Compare,_Alloc>& __y)
930 { return __y < __x; }
931
932 /// Returns !(y < x)
933 template<typename _Key, typename _Compare, typename _Alloc>
934 inline bool
935 operator<=(const multiset<_Key, _Compare, _Alloc>& __x,
936 const multiset<_Key, _Compare, _Alloc>& __y)
937 { return !(__y < __x); }
938
939 /// Returns !(x < y)
940 template<typename _Key, typename _Compare, typename _Alloc>
941 inline bool
942 operator>=(const multiset<_Key, _Compare, _Alloc>& __x,
943 const multiset<_Key, _Compare, _Alloc>& __y)
944 { return !(__x < __y); }
945
946 /// See std::multiset::swap().
947 template<typename _Key, typename _Compare, typename _Alloc>
948 inline void
949 swap(multiset<_Key, _Compare, _Alloc>& __x,
950 multiset<_Key, _Compare, _Alloc>& __y)
951 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
952 { __x.swap(__y); }
953
954 _GLIBCXX_END_NAMESPACE_CONTAINER
955
956 #if __cplusplus > 201402L
957 // Allow std::multiset access to internals of compatible sets.
958 template<typename _Val, typename _Cmp1, typename _Alloc, typename _Cmp2>
959 struct
960 _Rb_tree_merge_helper<_GLIBCXX_STD_C::multiset<_Val, _Cmp1, _Alloc>,
961 _Cmp2>
962 {
963 private:
964 friend class _GLIBCXX_STD_C::multiset<_Val, _Cmp1, _Alloc>;
965
966 static auto&
967 _S_get_tree(_GLIBCXX_STD_C::set<_Val, _Cmp2, _Alloc>& __set)
968 { return __set._M_t; }
969
970 static auto&
971 _S_get_tree(_GLIBCXX_STD_C::multiset<_Val, _Cmp2, _Alloc>& __set)
972 { return __set._M_t; }
973 };
974 #endif // C++17
975
976 _GLIBCXX_END_NAMESPACE_VERSION
977 } // namespace std
978
979 #endif /* _STL_MULTISET_H */