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