libstdc++
multiset.h
Go to the documentation of this file.
1 // Debugging multiset implementation -*- C++ -*-
2 
3 // Copyright (C) 2003-2026 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 /** @file debug/multiset.h
26  * This file is a GNU debug extension to the Standard C++ Library.
27  */
28 
29 #ifndef _GLIBCXX_DEBUG_MULTISET_H
30 #define _GLIBCXX_DEBUG_MULTISET_H 1
31 
32 #include <debug/safe_sequence.h>
33 #include <debug/safe_container.h>
34 #include <debug/safe_iterator.h>
35 #include <bits/stl_pair.h>
36 
37 namespace std _GLIBCXX_VISIBILITY(default)
38 {
39 namespace __debug
40 {
41  /// Class std::multiset with safety/checking/debug instrumentation.
42  template<typename _Key, typename _Compare = std::less<_Key>,
43  typename _Allocator = std::allocator<_Key> >
44  class multiset
46  multiset<_Key, _Compare, _Allocator>, _Allocator,
47  __gnu_debug::_Safe_node_sequence>,
48  public _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator>
49  {
50  typedef _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator> _Base;
53 
55  typedef typename _Base::iterator _Base_iterator;
57 
58  template<typename _ItT, typename _SeqT, typename _CatT>
59  friend class ::__gnu_debug::_Safe_iterator;
60 
61  // Reference wrapper for base class. Disambiguates multiset(const _Base&)
62  // from copy constructor by requiring a user-defined conversion.
63  // See PR libstdc++/90102.
64  struct _Base_ref
65  {
66  _Base_ref(const _Base& __r) : _M_ref(__r) { }
67 
68  const _Base& _M_ref;
69  };
70 
71  public:
72  // types:
73  typedef _Key key_type;
74  typedef _Key value_type;
75  typedef _Compare key_compare;
76  typedef _Compare value_compare;
77  typedef _Allocator allocator_type;
78  typedef typename _Base::reference reference;
79  typedef typename _Base::const_reference const_reference;
80 
82  iterator;
83  typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,
84  multiset> const_iterator;
85 
86  typedef typename _Base::size_type size_type;
87  typedef typename _Base::difference_type difference_type;
88  typedef typename _Base::pointer pointer;
89  typedef typename _Base::const_pointer const_pointer;
92 
93  // 23.3.3.1 construct/copy/destroy:
94 
95 #if __cplusplus < 201103L
96  multiset() : _Base() { }
97 
98  multiset(const multiset& __x)
99  : _Base(__x) { }
100 
101  ~multiset() { }
102 #else
103  multiset() = default;
104  multiset(const multiset&) = default;
105  multiset(multiset&&) = default;
106 
107  multiset(initializer_list<value_type> __l,
108  const _Compare& __comp = _Compare(),
109  const allocator_type& __a = allocator_type())
110  : _Base(__l, __comp, __a) { }
111 
112  explicit
113  multiset(const allocator_type& __a)
114  : _Base(__a) { }
115 
116  multiset(const multiset& __m,
117  const __type_identity_t<allocator_type>& __a)
118  : _Base(__m, __a) { }
119 
120  multiset(multiset&& __m, const __type_identity_t<allocator_type>& __a)
121  noexcept( noexcept(_Base(std::move(__m), __a)) )
122  : _Safe(std::move(__m), __a),
123  _Base(std::move(__m), __a) { }
124 
125  multiset(initializer_list<value_type> __l, const allocator_type& __a)
126  : _Base(__l, __a)
127  { }
128 
129  template<typename _InputIterator>
130  multiset(_InputIterator __first, _InputIterator __last,
131  const allocator_type& __a)
132  : _Base(__gnu_debug::__base(
133  __glibcxx_check_valid_constructor_range(__first, __last)),
134  __gnu_debug::__base(__last), __a) { }
135 
136 #if __glibcxx_containers_ranges // C++ >= 23
137  /**
138  * @brief Construct a multiset from a range.
139  * @since C++23
140  */
141  template<std::__detail::__container_compatible_range<value_type> _Rg>
142  multiset(std::from_range_t __t, _Rg&& __rg,
143  const _Compare& __c,
144  const allocator_type& __a = allocator_type())
145  : _Base(__t, std::forward<_Rg>(__rg), __c, __a)
146  { }
147 
148  template<std::__detail::__container_compatible_range<value_type> _Rg>
149  multiset(std::from_range_t __t, _Rg&& __rg,
150  const allocator_type& __a = allocator_type())
151  : _Base(__t, std::forward<_Rg>(__rg), __a)
152  { }
153 #endif
154 
155  ~multiset() = default;
156 #endif
157 
158  explicit multiset(const _Compare& __comp,
159  const _Allocator& __a = _Allocator())
160  : _Base(__comp, __a) { }
161 
162  template<typename _InputIterator>
163  multiset(_InputIterator __first, _InputIterator __last,
164  const _Compare& __comp = _Compare(),
165  const _Allocator& __a = _Allocator())
166  : _Base(__gnu_debug::__base(
167  __glibcxx_check_valid_constructor_range(__first, __last)),
168  __gnu_debug::__base(__last),
169  __comp, __a) { }
170 
171  multiset(_Base_ref __x)
172  : _Base(__x._M_ref) { }
173 
174 #if __cplusplus >= 201103L
175  multiset&
176  operator=(const multiset&) = default;
177 
178  multiset&
179  operator=(multiset&&) = default;
180 
181  multiset&
182  operator=(initializer_list<value_type> __l)
183  {
184  _Base::operator=(__l);
185  this->_M_invalidate_all();
186  return *this;
187  }
188 #endif
189 
190  using _Base::get_allocator;
191 
192  // iterators:
193  iterator
194  begin() _GLIBCXX_NOEXCEPT
195  { return iterator(_Base::begin(), this); }
196 
197  const_iterator
198  begin() const _GLIBCXX_NOEXCEPT
199  { return const_iterator(_Base::begin(), this); }
200 
201  iterator
202  end() _GLIBCXX_NOEXCEPT
203  { return iterator(_Base::end(), this); }
204 
205  const_iterator
206  end() const _GLIBCXX_NOEXCEPT
207  { return const_iterator(_Base::end(), this); }
208 
209  reverse_iterator
210  rbegin() _GLIBCXX_NOEXCEPT
211  { return reverse_iterator(end()); }
212 
213  const_reverse_iterator
214  rbegin() const _GLIBCXX_NOEXCEPT
215  { return const_reverse_iterator(end()); }
216 
217  reverse_iterator
218  rend() _GLIBCXX_NOEXCEPT
219  { return reverse_iterator(begin()); }
220 
221  const_reverse_iterator
222  rend() const _GLIBCXX_NOEXCEPT
223  { return const_reverse_iterator(begin()); }
224 
225 #if __cplusplus >= 201103L
226  const_iterator
227  cbegin() const noexcept
228  { return const_iterator(_Base::begin(), this); }
229 
230  const_iterator
231  cend() const noexcept
232  { return const_iterator(_Base::end(), this); }
233 
234  const_reverse_iterator
235  crbegin() const noexcept
236  { return const_reverse_iterator(end()); }
237 
238  const_reverse_iterator
239  crend() const noexcept
240  { return const_reverse_iterator(begin()); }
241 #endif
242 
243  // capacity:
244  using _Base::empty;
245  using _Base::size;
246  using _Base::max_size;
247 
248  // modifiers:
249 #if __cplusplus >= 201103L
250  template<typename... _Args>
251  iterator
252  emplace(_Args&&... __args)
253  { return { _Base::emplace(std::forward<_Args>(__args)...), this }; }
254 
255  template<typename... _Args>
256  iterator
257  emplace_hint(const_iterator __pos, _Args&&... __args)
258  {
259  __glibcxx_check_insert(__pos);
260  return
261  {
262  _Base::emplace_hint(__pos.base(), std::forward<_Args>(__args)...),
263  this
264  };
265  }
266 #endif
267 
268  iterator
269  insert(const value_type& __x)
270  { return iterator(_Base::insert(__x), this); }
271 
272 #if __cplusplus >= 201103L
273  iterator
274  insert(value_type&& __x)
275  { return { _Base::insert(std::move(__x)), this }; }
276 #endif
277 
278  iterator
279  insert(const_iterator __position, const value_type& __x)
280  {
281  __glibcxx_check_insert(__position);
282  return iterator(_Base::insert(__position.base(), __x), this);
283  }
284 
285 #if __cplusplus >= 201103L
286  iterator
287  insert(const_iterator __position, value_type&& __x)
288  {
289  __glibcxx_check_insert(__position);
290  return { _Base::insert(__position.base(), std::move(__x)), this };
291  }
292 #endif
293 
294  template<typename _InputIterator>
295  void
296  insert(_InputIterator __first, _InputIterator __last)
297  {
299  __glibcxx_check_valid_range2(__first, __last, __dist);
300 
301  if (__dist.second >= __gnu_debug::__dp_sign)
302  _Base::insert(__gnu_debug::__unsafe(__first),
303  __gnu_debug::__unsafe(__last));
304  else
305  _Base::insert(__first, __last);
306  }
307 
308 #if __cplusplus >= 201103L
309  void
310  insert(initializer_list<value_type> __l)
311  { _Base::insert(__l); }
312 #endif
313 
314 #ifdef __glibcxx_node_extract // >= C++17 && HOSTED
315  using node_type = typename _Base::node_type;
316 
317  node_type
318  extract(const_iterator __position)
319  {
320  __glibcxx_check_erase(__position);
321  this->_M_invalidate_if(_Equal(__position.base()));
322  return _Base::extract(__position.base());
323  }
324 
325  node_type
326  extract(const key_type& __key)
327  {
328  const auto __position = find(__key);
329  if (__position != end())
330  return extract(__position);
331  return {};
332  }
333 
334 # ifdef __glibcxx_associative_heterogeneous_erasure
335  template <__heterogeneous_tree_key<multiset> _Kt>
336  node_type
337  extract(_Kt&& __key)
338  {
339  const auto __position = find(__key);
340  if (__position != end())
341  return extract(__position);
342  return {};
343  }
344 #endif
345 
346  iterator
347  insert(node_type&& __nh)
348  { return { _Base::insert(std::move(__nh)), this }; }
349 
350  iterator
351  insert(const_iterator __hint, node_type&& __nh)
352  {
353  __glibcxx_check_insert(__hint);
354  return { _Base::insert(__hint.base(), std::move(__nh)), this };
355  }
356 
357  using _Base::merge;
358 #endif // C++17
359 
360 #if __cplusplus >= 201103L
361  _GLIBCXX_ABI_TAG_CXX11
362  iterator
363  erase(const_iterator __position)
364  {
365  __glibcxx_check_erase(__position);
366  return { erase(__position.base()), this };
367  }
368 
369  _Base_iterator
370  erase(_Base_const_iterator __position)
371  {
372  __glibcxx_check_erase2(__position);
373  this->_M_invalidate_if(_Equal(__position));
374  return _Base::erase(__position);
375  }
376 #else
377  void
378  erase(iterator __position)
379  {
380  __glibcxx_check_erase(__position);
381  this->_M_invalidate_if(_Equal(__position.base()));
382  _Base::erase(__position.base());
383  }
384 #endif
385 
386  size_type
387  erase(const key_type& __x)
388  {
390  _Base::equal_range(__x);
391  size_type __count = 0;
392  _Base_iterator __victim = __victims.first;
393  while (__victim != __victims.second)
394  {
395  this->_M_invalidate_if(_Equal(__victim));
396  _Base::erase(__victim++);
397  ++__count;
398  }
399  return __count;
400  }
401 
402 # ifdef __glibcxx_associative_heterogeneous_erasure
403  template <__heterogeneous_tree_key<multiset> _Kt>
404  size_type
405  erase(_Kt&& __x)
406  {
407  auto __victims = _Base::equal_range(__x);
408  size_type __count = 0;
409  for (auto __victim = __victims.first; __victim != __victims.second;)
410  {
411  this->_M_invalidate_if(_Equal(__victim));
412  _Base::erase(__victim++);
413  ++__count;
414  }
415  return __count;
416  }
417 # endif
418 
419 #if __cplusplus >= 201103L
420  _GLIBCXX_ABI_TAG_CXX11
421  iterator
422  erase(const_iterator __first, const_iterator __last)
423  {
424  // _GLIBCXX_RESOLVE_LIB_DEFECTS
425  // 151. can't currently clear() empty container
426  __glibcxx_check_erase_range(__first, __last);
427  for (_Base_const_iterator __victim = __first.base();
428  __victim != __last.base(); ++__victim)
429  {
430  _GLIBCXX_DEBUG_VERIFY(__victim != _Base::cend(),
431  _M_message(__gnu_debug::__msg_valid_range)
432  ._M_iterator(__first, "first")
433  ._M_iterator(__last, "last"));
434  this->_M_invalidate_if(_Equal(__victim));
435  }
436 
437  return { _Base::erase(__first.base(), __last.base()), this };
438  }
439 #else
440  void
441  erase(iterator __first, iterator __last)
442  {
443  // _GLIBCXX_RESOLVE_LIB_DEFECTS
444  // 151. can't currently clear() empty container
445  __glibcxx_check_erase_range(__first, __last);
446  for (_Base_iterator __victim = __first.base();
447  __victim != __last.base(); ++__victim)
448  {
449  _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
450  _M_message(__gnu_debug::__msg_valid_range)
451  ._M_iterator(__first, "first")
452  ._M_iterator(__last, "last"));
453  this->_M_invalidate_if(_Equal(__victim));
454  }
455  _Base::erase(__first.base(), __last.base());
456  }
457 #endif
458 
459  void
460  swap(multiset& __x)
461  _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
462  {
463  _Safe::_M_swap(__x);
464  _Base::swap(__x);
465  }
466 
467  void
468  clear() _GLIBCXX_NOEXCEPT
469  {
470  this->_M_invalidate_all();
471  _Base::clear();
472  }
473 
474  // observers:
475  using _Base::key_comp;
476  using _Base::value_comp;
477 
478  // multiset operations:
479  iterator
480  find(const key_type& __x)
481  { return iterator(_Base::find(__x), this); }
482 
483  // _GLIBCXX_RESOLVE_LIB_DEFECTS
484  // 214. set::find() missing const overload
485  const_iterator
486  find(const key_type& __x) const
487  { return const_iterator(_Base::find(__x), this); }
488 
489 #ifdef __glibcxx_generic_associative_lookup // C++ >= 14
490  template<typename _Kt,
491  typename _Req =
492  typename __has_is_transparent<_Compare, _Kt>::type>
493  iterator
494  find(const _Kt& __x)
495  { return { _Base::find(__x), this }; }
496 
497  template<typename _Kt,
498  typename _Req =
499  typename __has_is_transparent<_Compare, _Kt>::type>
500  const_iterator
501  find(const _Kt& __x) const
502  { return { _Base::find(__x), this }; }
503 #endif
504 
505  using _Base::count;
506 
507  iterator
508  lower_bound(const key_type& __x)
509  { return iterator(_Base::lower_bound(__x), this); }
510 
511  // _GLIBCXX_RESOLVE_LIB_DEFECTS
512  // 214. set::find() missing const overload
513  const_iterator
514  lower_bound(const key_type& __x) const
515  { return const_iterator(_Base::lower_bound(__x), this); }
516 
517 #ifdef __glibcxx_generic_associative_lookup // C++ >= 14
518  template<typename _Kt,
519  typename _Req =
520  typename __has_is_transparent<_Compare, _Kt>::type>
521  iterator
522  lower_bound(const _Kt& __x)
523  { return { _Base::lower_bound(__x), this }; }
524 
525  template<typename _Kt,
526  typename _Req =
527  typename __has_is_transparent<_Compare, _Kt>::type>
528  const_iterator
529  lower_bound(const _Kt& __x) const
530  { return { _Base::lower_bound(__x), this }; }
531 #endif
532 
533  iterator
534  upper_bound(const key_type& __x)
535  { return iterator(_Base::upper_bound(__x), this); }
536 
537  // _GLIBCXX_RESOLVE_LIB_DEFECTS
538  // 214. set::find() missing const overload
539  const_iterator
540  upper_bound(const key_type& __x) const
541  { return const_iterator(_Base::upper_bound(__x), this); }
542 
543 #ifdef __glibcxx_generic_associative_lookup // C++ >= 14
544  template<typename _Kt,
545  typename _Req =
546  typename __has_is_transparent<_Compare, _Kt>::type>
547  iterator
548  upper_bound(const _Kt& __x)
549  { return { _Base::upper_bound(__x), this }; }
550 
551  template<typename _Kt,
552  typename _Req =
553  typename __has_is_transparent<_Compare, _Kt>::type>
554  const_iterator
555  upper_bound(const _Kt& __x) const
556  { return { _Base::upper_bound(__x), this }; }
557 #endif
558 
560  equal_range(const key_type& __x)
561  {
563  _Base::equal_range(__x);
564  return std::make_pair(iterator(__res.first, this),
565  iterator(__res.second, this));
566  }
567 
568  // _GLIBCXX_RESOLVE_LIB_DEFECTS
569  // 214. set::find() missing const overload
571  equal_range(const key_type& __x) const
572  {
574  _Base::equal_range(__x);
575  return std::make_pair(const_iterator(__res.first, this),
576  const_iterator(__res.second, this));
577  }
578 
579 #ifdef __glibcxx_generic_associative_lookup // C++ >= 14
580  template<typename _Kt,
581  typename _Req =
582  typename __has_is_transparent<_Compare, _Kt>::type>
584  equal_range(const _Kt& __x)
585  {
586  auto __res = _Base::equal_range(__x);
587  return { { __res.first, this }, { __res.second, this } };
588  }
589 
590  template<typename _Kt,
591  typename _Req =
592  typename __has_is_transparent<_Compare, _Kt>::type>
594  equal_range(const _Kt& __x) const
595  {
596  auto __res = _Base::equal_range(__x);
597  return { { __res.first, this }, { __res.second, this } };
598  }
599 #endif
600 
601  _Base&
602  _M_base() _GLIBCXX_NOEXCEPT { return *this; }
603 
604  const _Base&
605  _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
606  };
607 
608 #if __cpp_deduction_guides >= 201606
609 
610  template<typename _InputIterator,
611  typename _Compare =
613  typename _Allocator =
615  typename = _RequireInputIter<_InputIterator>,
616  typename = _RequireNotAllocator<_Compare>,
617  typename = _RequireAllocator<_Allocator>>
618  multiset(_InputIterator, _InputIterator,
619  _Compare = _Compare(), _Allocator = _Allocator())
621  _Compare, _Allocator>;
622 
623  template<typename _Key,
624  typename _Compare = less<_Key>,
625  typename _Allocator = allocator<_Key>,
626  typename = _RequireNotAllocator<_Compare>,
627  typename = _RequireAllocator<_Allocator>>
629  _Compare = _Compare(), _Allocator = _Allocator())
631 
632  template<typename _InputIterator, typename _Allocator,
633  typename = _RequireInputIter<_InputIterator>,
634  typename = _RequireAllocator<_Allocator>>
635  multiset(_InputIterator, _InputIterator, _Allocator)
638  _Allocator>;
639 
640  template<typename _Key, typename _Allocator,
641  typename = _RequireAllocator<_Allocator>>
642  multiset(initializer_list<_Key>, _Allocator)
643  -> multiset<_Key, less<_Key>, _Allocator>;
644 
645 #if __glibcxx_containers_ranges // C++ >= 23
646  template<ranges::input_range _Rg,
647  __not_allocator_like _Compare = less<ranges::range_value_t<_Rg>>,
648  __allocator_like _Alloc = std::allocator<ranges::range_value_t<_Rg>>>
649  multiset(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc())
650  -> multiset<ranges::range_value_t<_Rg>, _Compare, _Alloc>;
651 
652  template<ranges::input_range _Rg, __allocator_like _Alloc>
653  multiset(from_range_t, _Rg&&, _Alloc)
655 #endif
656 #endif // deduction guides
657 
658  template<typename _Key, typename _Compare, typename _Allocator>
659  inline bool
660  operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
662  { return __lhs._M_base() == __rhs._M_base(); }
663 
664 #if __cpp_lib_three_way_comparison
665  template<typename _Key, typename _Compare, typename _Alloc>
666  inline __detail::__synth3way_t<_Key>
667  operator<=>(const multiset<_Key, _Compare, _Alloc>& __lhs,
669  { return __lhs._M_base() <=> __rhs._M_base(); }
670 #else
671  template<typename _Key, typename _Compare, typename _Allocator>
672  inline bool
673  operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
675  { return __lhs._M_base() != __rhs._M_base(); }
676 
677  template<typename _Key, typename _Compare, typename _Allocator>
678  inline bool
679  operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
681  { return __lhs._M_base() < __rhs._M_base(); }
682 
683  template<typename _Key, typename _Compare, typename _Allocator>
684  inline bool
685  operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
687  { return __lhs._M_base() <= __rhs._M_base(); }
688 
689  template<typename _Key, typename _Compare, typename _Allocator>
690  inline bool
691  operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
693  { return __lhs._M_base() >= __rhs._M_base(); }
694 
695  template<typename _Key, typename _Compare, typename _Allocator>
696  inline bool
697  operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
699  { return __lhs._M_base() > __rhs._M_base(); }
700 #endif // three-way comparison
701 
702  template<typename _Key, typename _Compare, typename _Allocator>
703  void
706  _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
707  { return __x.swap(__y); }
708 
709 } // namespace __debug
710 } // namespace std
711 
712 #endif
#define __glibcxx_check_erase_range(_First, _Last)
Definition: macros.h:245
#define __glibcxx_check_erase(_Position)
Definition: macros.h:209
Class std::multiset with safety/checking/debug instrumentation.
Definition: multiset.h:44
One of the comparison functors.
Definition: stl_function.h:359
#define __glibcxx_check_insert(_Position)
Definition: macros.h:143
ISO C++ entities toplevel namespace is std.
Safe iterator wrapper.
Struct holding two objects of arbitrary type.
Safe class dealing with some allocator dependent operations.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:138
constexpr _Iterator & base() noexcept
Return the underlying iterator.
Like _Safe_sequence but with a special _M_invalidate_all implementation not invalidating past-the-end...
The standard allocator, as per C++03 [20.4.1].
Definition: allocator.h:133
_T2 second
The second member.
Definition: stl_pair.h:309
_T1 first
The first member.
Definition: stl_pair.h:308
constexpr _Iterator __base(_Iterator __it)
initializer_list