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