1 // Debugging multiset implementation -*- C++ -*-
3 // Copyright (C) 2003-2014 Free Software Foundation, Inc.
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)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file debug/multiset.h
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_MULTISET_H
30 #define _GLIBCXX_DEBUG_MULTISET_H 1
32 #include <debug/safe_sequence.h>
33 #include <debug/safe_iterator.h>
36 namespace std _GLIBCXX_VISIBILITY(default)
40 /// Class std::multiset with safety/checking/debug instrumentation.
41 template<typename _Key, typename _Compare = std::less<_Key>,
42 typename _Allocator = std::allocator<_Key> >
44 : public _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator>,
45 public __gnu_debug::_Safe_sequence<multiset<_Key, _Compare, _Allocator> >
47 typedef _GLIBCXX_STD_C::multiset<_Key, _Compare, _Allocator> _Base;
49 typedef typename _Base::const_iterator _Base_const_iterator;
50 typedef typename _Base::iterator _Base_iterator;
51 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
53 #if __cplusplus >= 201103L
54 typedef __gnu_cxx::__alloc_traits<typename
55 _Base::allocator_type> _Alloc_traits;
59 typedef _Key key_type;
60 typedef _Key value_type;
61 typedef _Compare key_compare;
62 typedef _Compare value_compare;
63 typedef _Allocator allocator_type;
64 typedef typename _Base::reference reference;
65 typedef typename _Base::const_reference const_reference;
67 typedef __gnu_debug::_Safe_iterator<_Base_iterator, multiset>
69 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,
70 multiset> const_iterator;
72 typedef typename _Base::size_type size_type;
73 typedef typename _Base::difference_type difference_type;
74 typedef typename _Base::pointer pointer;
75 typedef typename _Base::const_pointer const_pointer;
76 typedef std::reverse_iterator<iterator> reverse_iterator;
77 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
79 // 23.3.3.1 construct/copy/destroy:
81 multiset() : _Base() { }
83 explicit multiset(const _Compare& __comp,
84 const _Allocator& __a = _Allocator())
85 : _Base(__comp, __a) { }
87 template<typename _InputIterator>
88 multiset(_InputIterator __first, _InputIterator __last,
89 const _Compare& __comp = _Compare(),
90 const _Allocator& __a = _Allocator())
91 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
93 __gnu_debug::__base(__last),
96 multiset(const multiset& __x)
99 multiset(const _Base& __x)
102 #if __cplusplus >= 201103L
103 multiset(multiset&& __x)
104 noexcept(is_nothrow_copy_constructible<_Compare>::value)
105 : _Base(std::move(__x))
106 { this->_M_swap(__x); }
108 multiset(initializer_list<value_type> __l,
109 const _Compare& __comp = _Compare(),
110 const allocator_type& __a = allocator_type())
111 : _Base(__l, __comp, __a) { }
114 multiset(const allocator_type& __a)
117 multiset(const multiset& __m, const allocator_type& __a)
118 : _Base(__m, __a) { }
120 multiset(multiset&& __m, const allocator_type& __a)
121 : _Base(std::move(__m._M_base()), __a) { }
123 multiset(initializer_list<value_type> __l, const allocator_type& __a)
127 template<typename _InputIterator>
128 multiset(_InputIterator __first, _InputIterator __last,
129 const allocator_type& __a)
130 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
132 __gnu_debug::__base(__last), __a)
136 ~multiset() _GLIBCXX_NOEXCEPT { }
139 operator=(const multiset& __x)
142 this->_M_invalidate_all();
146 #if __cplusplus >= 201103L
148 operator=(multiset&& __x)
149 noexcept(_Alloc_traits::_S_nothrow_move())
151 __glibcxx_check_self_move_assign(__x);
152 bool __xfer_memory = _Alloc_traits::_S_propagate_on_move_assign()
153 || __x.get_allocator() == this->get_allocator();
154 _M_base() = std::move(__x._M_base());
158 this->_M_invalidate_all();
159 __x._M_invalidate_all();
164 operator=(initializer_list<value_type> __l)
167 this->_M_invalidate_all();
172 using _Base::get_allocator;
176 begin() _GLIBCXX_NOEXCEPT
177 { return iterator(_Base::begin(), this); }
180 begin() const _GLIBCXX_NOEXCEPT
181 { return const_iterator(_Base::begin(), this); }
184 end() _GLIBCXX_NOEXCEPT
185 { return iterator(_Base::end(), this); }
188 end() const _GLIBCXX_NOEXCEPT
189 { return const_iterator(_Base::end(), this); }
192 rbegin() _GLIBCXX_NOEXCEPT
193 { return reverse_iterator(end()); }
195 const_reverse_iterator
196 rbegin() const _GLIBCXX_NOEXCEPT
197 { return const_reverse_iterator(end()); }
200 rend() _GLIBCXX_NOEXCEPT
201 { return reverse_iterator(begin()); }
203 const_reverse_iterator
204 rend() const _GLIBCXX_NOEXCEPT
205 { return const_reverse_iterator(begin()); }
207 #if __cplusplus >= 201103L
209 cbegin() const noexcept
210 { return const_iterator(_Base::begin(), this); }
213 cend() const noexcept
214 { return const_iterator(_Base::end(), this); }
216 const_reverse_iterator
217 crbegin() const noexcept
218 { return const_reverse_iterator(end()); }
220 const_reverse_iterator
221 crend() const noexcept
222 { return const_reverse_iterator(begin()); }
228 using _Base::max_size;
231 #if __cplusplus >= 201103L
232 template<typename... _Args>
234 emplace(_Args&&... __args)
236 return iterator(_Base::emplace(std::forward<_Args>(__args)...), this);
239 template<typename... _Args>
241 emplace_hint(const_iterator __pos, _Args&&... __args)
243 __glibcxx_check_insert(__pos);
244 return iterator(_Base::emplace_hint(__pos.base(),
245 std::forward<_Args>(__args)...),
251 insert(const value_type& __x)
252 { return iterator(_Base::insert(__x), this); }
254 #if __cplusplus >= 201103L
256 insert(value_type&& __x)
257 { return iterator(_Base::insert(std::move(__x)), this); }
261 insert(const_iterator __position, const value_type& __x)
263 __glibcxx_check_insert(__position);
264 return iterator(_Base::insert(__position.base(), __x), this);
267 #if __cplusplus >= 201103L
269 insert(const_iterator __position, value_type&& __x)
271 __glibcxx_check_insert(__position);
272 return iterator(_Base::insert(__position.base(), std::move(__x)),
277 template<typename _InputIterator>
279 insert(_InputIterator __first, _InputIterator __last)
281 __glibcxx_check_valid_range(__first, __last);
282 _Base::insert(__gnu_debug::__base(__first),
283 __gnu_debug::__base(__last));
286 #if __cplusplus >= 201103L
288 insert(initializer_list<value_type> __l)
289 { _Base::insert(__l); }
292 #if __cplusplus >= 201103L
294 erase(const_iterator __position)
296 __glibcxx_check_erase(__position);
297 this->_M_invalidate_if(_Equal(__position.base()));
298 return iterator(_Base::erase(__position.base()), this);
302 erase(iterator __position)
304 __glibcxx_check_erase(__position);
305 this->_M_invalidate_if(_Equal(__position.base()));
306 _Base::erase(__position.base());
311 erase(const key_type& __x)
313 std::pair<_Base_iterator, _Base_iterator> __victims =
314 _Base::equal_range(__x);
315 size_type __count = 0;
316 _Base_iterator __victim = __victims.first;
317 while (__victim != __victims.second)
319 this->_M_invalidate_if(_Equal(__victim));
320 _Base::erase(__victim++);
326 #if __cplusplus >= 201103L
328 erase(const_iterator __first, const_iterator __last)
330 // _GLIBCXX_RESOLVE_LIB_DEFECTS
331 // 151. can't currently clear() empty container
332 __glibcxx_check_erase_range(__first, __last);
333 for (_Base_const_iterator __victim = __first.base();
334 __victim != __last.base(); ++__victim)
336 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
337 _M_message(__gnu_debug::__msg_valid_range)
338 ._M_iterator(__first, "first")
339 ._M_iterator(__last, "last"));
340 this->_M_invalidate_if(_Equal(__victim));
342 return iterator(_Base::erase(__first.base(), __last.base()), this);
346 erase(iterator __first, iterator __last)
348 // _GLIBCXX_RESOLVE_LIB_DEFECTS
349 // 151. can't currently clear() empty container
350 __glibcxx_check_erase_range(__first, __last);
351 for (_Base_iterator __victim = __first.base();
352 __victim != __last.base(); ++__victim)
354 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
355 _M_message(__gnu_debug::__msg_valid_range)
356 ._M_iterator(__first, "first")
357 ._M_iterator(__last, "last"));
358 this->_M_invalidate_if(_Equal(__victim));
360 _Base::erase(__first.base(), __last.base());
366 #if __cplusplus >= 201103L
367 noexcept(_Alloc_traits::_S_nothrow_swap())
370 #if __cplusplus >= 201103L
371 if (!_Alloc_traits::_S_propagate_on_swap())
372 __glibcxx_check_equal_allocs(__x);
379 clear() _GLIBCXX_NOEXCEPT
381 this->_M_invalidate_all();
386 using _Base::key_comp;
387 using _Base::value_comp;
389 // multiset operations:
391 find(const key_type& __x)
392 { return iterator(_Base::find(__x), this); }
394 // _GLIBCXX_RESOLVE_LIB_DEFECTS
395 // 214. set::find() missing const overload
397 find(const key_type& __x) const
398 { return const_iterator(_Base::find(__x), this); }
403 lower_bound(const key_type& __x)
404 { return iterator(_Base::lower_bound(__x), this); }
406 // _GLIBCXX_RESOLVE_LIB_DEFECTS
407 // 214. set::find() missing const overload
409 lower_bound(const key_type& __x) const
410 { return const_iterator(_Base::lower_bound(__x), this); }
413 upper_bound(const key_type& __x)
414 { return iterator(_Base::upper_bound(__x), this); }
416 // _GLIBCXX_RESOLVE_LIB_DEFECTS
417 // 214. set::find() missing const overload
419 upper_bound(const key_type& __x) const
420 { return const_iterator(_Base::upper_bound(__x), this); }
422 std::pair<iterator,iterator>
423 equal_range(const key_type& __x)
425 std::pair<_Base_iterator, _Base_iterator> __res =
426 _Base::equal_range(__x);
427 return std::make_pair(iterator(__res.first, this),
428 iterator(__res.second, this));
431 // _GLIBCXX_RESOLVE_LIB_DEFECTS
432 // 214. set::find() missing const overload
433 std::pair<const_iterator,const_iterator>
434 equal_range(const key_type& __x) const
436 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
437 _Base::equal_range(__x);
438 return std::make_pair(const_iterator(__res.first, this),
439 const_iterator(__res.second, this));
443 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
446 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
452 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
453 this->_M_invalidate_if(_Not_equal(_Base::end()));
457 template<typename _Key, typename _Compare, typename _Allocator>
459 operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
460 const multiset<_Key, _Compare, _Allocator>& __rhs)
461 { return __lhs._M_base() == __rhs._M_base(); }
463 template<typename _Key, typename _Compare, typename _Allocator>
465 operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
466 const multiset<_Key, _Compare, _Allocator>& __rhs)
467 { return __lhs._M_base() != __rhs._M_base(); }
469 template<typename _Key, typename _Compare, typename _Allocator>
471 operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
472 const multiset<_Key, _Compare, _Allocator>& __rhs)
473 { return __lhs._M_base() < __rhs._M_base(); }
475 template<typename _Key, typename _Compare, typename _Allocator>
477 operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
478 const multiset<_Key, _Compare, _Allocator>& __rhs)
479 { return __lhs._M_base() <= __rhs._M_base(); }
481 template<typename _Key, typename _Compare, typename _Allocator>
483 operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
484 const multiset<_Key, _Compare, _Allocator>& __rhs)
485 { return __lhs._M_base() >= __rhs._M_base(); }
487 template<typename _Key, typename _Compare, typename _Allocator>
489 operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
490 const multiset<_Key, _Compare, _Allocator>& __rhs)
491 { return __lhs._M_base() > __rhs._M_base(); }
493 template<typename _Key, typename _Compare, typename _Allocator>
495 swap(multiset<_Key, _Compare, _Allocator>& __x,
496 multiset<_Key, _Compare, _Allocator>& __y)
497 { return __x.swap(__y); }
499 } // namespace __debug