1 // Debugging multiset implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file debug/multiset.h
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_MULTISET_H
31 #define _GLIBCXX_DEBUG_MULTISET_H 1
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
37 namespace std
_GLIBCXX_VISIBILITY(default)
41 /// Class std::multiset with safety/checking/debug instrumentation.
42 template<typename _Key
, typename _Compare
= std::less
<_Key
>,
43 typename _Allocator
= std::allocator
<_Key
> >
45 : public _GLIBCXX_STD_C::multiset
<_Key
, _Compare
, _Allocator
>,
46 public __gnu_debug::_Safe_sequence
<multiset
<_Key
, _Compare
, _Allocator
> >
48 typedef _GLIBCXX_STD_C::multiset
<_Key
, _Compare
, _Allocator
> _Base
;
50 typedef typename
_Base::const_iterator _Base_const_iterator
;
51 typedef typename
_Base::iterator _Base_iterator
;
52 typedef __gnu_debug::_Equal_to
<_Base_const_iterator
> _Equal
;
55 typedef _Key key_type
;
56 typedef _Key value_type
;
57 typedef _Compare key_compare
;
58 typedef _Compare value_compare
;
59 typedef _Allocator allocator_type
;
60 typedef typename
_Base::reference reference
;
61 typedef typename
_Base::const_reference const_reference
;
63 typedef __gnu_debug::_Safe_iterator
<_Base_iterator
, multiset
>
65 typedef __gnu_debug::_Safe_iterator
<_Base_const_iterator
,
66 multiset
> const_iterator
;
68 typedef typename
_Base::size_type size_type
;
69 typedef typename
_Base::difference_type difference_type
;
70 typedef typename
_Base::pointer pointer
;
71 typedef typename
_Base::const_pointer const_pointer
;
72 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
73 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
75 // 23.3.3.1 construct/copy/destroy:
76 explicit multiset(const _Compare
& __comp
= _Compare(),
77 const _Allocator
& __a
= _Allocator())
78 : _Base(__comp
, __a
) { }
80 template<typename _InputIterator
>
81 multiset(_InputIterator __first
, _InputIterator __last
,
82 const _Compare
& __comp
= _Compare(),
83 const _Allocator
& __a
= _Allocator())
84 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
86 __gnu_debug::__base(__last
),
89 multiset(const multiset
& __x
)
92 multiset(const _Base
& __x
)
95 #if __cplusplus >= 201103L
96 multiset(multiset
&& __x
)
97 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
)
98 : _Base(std::move(__x
))
99 { this->_M_swap(__x
); }
101 multiset(initializer_list
<value_type
> __l
,
102 const _Compare
& __comp
= _Compare(),
103 const allocator_type
& __a
= allocator_type())
104 : _Base(__l
, __comp
, __a
) { }
107 ~multiset() _GLIBCXX_NOEXCEPT
{ }
110 operator=(const multiset
& __x
)
112 *static_cast<_Base
*>(this) = __x
;
113 this->_M_invalidate_all();
117 #if __cplusplus >= 201103L
119 operator=(multiset
&& __x
)
123 __glibcxx_check_self_move_assign(__x
);
130 operator=(initializer_list
<value_type
> __l
)
138 using _Base::get_allocator
;
142 begin() _GLIBCXX_NOEXCEPT
143 { return iterator(_Base::begin(), this); }
146 begin() const _GLIBCXX_NOEXCEPT
147 { return const_iterator(_Base::begin(), this); }
150 end() _GLIBCXX_NOEXCEPT
151 { return iterator(_Base::end(), this); }
154 end() const _GLIBCXX_NOEXCEPT
155 { return const_iterator(_Base::end(), this); }
158 rbegin() _GLIBCXX_NOEXCEPT
159 { return reverse_iterator(end()); }
161 const_reverse_iterator
162 rbegin() const _GLIBCXX_NOEXCEPT
163 { return const_reverse_iterator(end()); }
166 rend() _GLIBCXX_NOEXCEPT
167 { return reverse_iterator(begin()); }
169 const_reverse_iterator
170 rend() const _GLIBCXX_NOEXCEPT
171 { return const_reverse_iterator(begin()); }
173 #if __cplusplus >= 201103L
175 cbegin() const noexcept
176 { return const_iterator(_Base::begin(), this); }
179 cend() const noexcept
180 { return const_iterator(_Base::end(), this); }
182 const_reverse_iterator
183 crbegin() const noexcept
184 { return const_reverse_iterator(end()); }
186 const_reverse_iterator
187 crend() const noexcept
188 { return const_reverse_iterator(begin()); }
194 using _Base::max_size
;
197 #if __cplusplus >= 201103L
198 template<typename
... _Args
>
200 emplace(_Args
&&... __args
)
202 return iterator(_Base::emplace(std::forward
<_Args
>(__args
)...), this);
205 template<typename
... _Args
>
207 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
209 __glibcxx_check_insert(__pos
);
210 return iterator(_Base::emplace_hint(__pos
.base(),
211 std::forward
<_Args
>(__args
)...),
217 insert(const value_type
& __x
)
218 { return iterator(_Base::insert(__x
), this); }
220 #if __cplusplus >= 201103L
222 insert(value_type
&& __x
)
223 { return iterator(_Base::insert(std::move(__x
)), this); }
227 insert(const_iterator __position
, const value_type
& __x
)
229 __glibcxx_check_insert(__position
);
230 return iterator(_Base::insert(__position
.base(), __x
), this);
233 #if __cplusplus >= 201103L
235 insert(const_iterator __position
, value_type
&& __x
)
237 __glibcxx_check_insert(__position
);
238 return iterator(_Base::insert(__position
.base(), std::move(__x
)),
243 template<typename _InputIterator
>
245 insert(_InputIterator __first
, _InputIterator __last
)
247 __glibcxx_check_valid_range(__first
, __last
);
248 _Base::insert(__gnu_debug::__base(__first
),
249 __gnu_debug::__base(__last
));
252 #if __cplusplus >= 201103L
254 insert(initializer_list
<value_type
> __l
)
255 { _Base::insert(__l
); }
258 #if __cplusplus >= 201103L
260 erase(const_iterator __position
)
262 __glibcxx_check_erase(__position
);
263 this->_M_invalidate_if(_Equal(__position
.base()));
264 return iterator(_Base::erase(__position
.base()), this);
268 erase(iterator __position
)
270 __glibcxx_check_erase(__position
);
271 this->_M_invalidate_if(_Equal(__position
.base()));
272 _Base::erase(__position
.base());
277 erase(const key_type
& __x
)
279 std::pair
<_Base_iterator
, _Base_iterator
> __victims
=
280 _Base::equal_range(__x
);
281 size_type __count
= 0;
282 _Base_iterator __victim
= __victims
.first
;
283 while (__victim
!= __victims
.second
)
285 this->_M_invalidate_if(_Equal(__victim
));
286 _Base::erase(__victim
++);
292 #if __cplusplus >= 201103L
294 erase(const_iterator __first
, const_iterator __last
)
296 // _GLIBCXX_RESOLVE_LIB_DEFECTS
297 // 151. can't currently clear() empty container
298 __glibcxx_check_erase_range(__first
, __last
);
299 for (_Base_const_iterator __victim
= __first
.base();
300 __victim
!= __last
.base(); ++__victim
)
302 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
303 _M_message(__gnu_debug::__msg_valid_range
)
304 ._M_iterator(__first
, "first")
305 ._M_iterator(__last
, "last"));
306 this->_M_invalidate_if(_Equal(__victim
));
308 return iterator(_Base::erase(__first
.base(), __last
.base()), this);
312 erase(iterator __first
, iterator __last
)
314 // _GLIBCXX_RESOLVE_LIB_DEFECTS
315 // 151. can't currently clear() empty container
316 __glibcxx_check_erase_range(__first
, __last
);
317 for (_Base_iterator __victim
= __first
.base();
318 __victim
!= __last
.base(); ++__victim
)
320 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
321 _M_message(__gnu_debug::__msg_valid_range
)
322 ._M_iterator(__first
, "first")
323 ._M_iterator(__last
, "last"));
324 this->_M_invalidate_if(_Equal(__victim
));
326 _Base::erase(__first
.base(), __last
.base());
338 clear() _GLIBCXX_NOEXCEPT
340 this->_M_invalidate_all();
345 using _Base::key_comp
;
346 using _Base::value_comp
;
348 // multiset operations:
350 find(const key_type
& __x
)
351 { return iterator(_Base::find(__x
), this); }
353 // _GLIBCXX_RESOLVE_LIB_DEFECTS
354 // 214. set::find() missing const overload
356 find(const key_type
& __x
) const
357 { return const_iterator(_Base::find(__x
), this); }
362 lower_bound(const key_type
& __x
)
363 { return iterator(_Base::lower_bound(__x
), this); }
365 // _GLIBCXX_RESOLVE_LIB_DEFECTS
366 // 214. set::find() missing const overload
368 lower_bound(const key_type
& __x
) const
369 { return const_iterator(_Base::lower_bound(__x
), this); }
372 upper_bound(const key_type
& __x
)
373 { return iterator(_Base::upper_bound(__x
), this); }
375 // _GLIBCXX_RESOLVE_LIB_DEFECTS
376 // 214. set::find() missing const overload
378 upper_bound(const key_type
& __x
) const
379 { return const_iterator(_Base::upper_bound(__x
), this); }
381 std::pair
<iterator
,iterator
>
382 equal_range(const key_type
& __x
)
384 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
385 _Base::equal_range(__x
);
386 return std::make_pair(iterator(__res
.first
, this),
387 iterator(__res
.second
, this));
390 // _GLIBCXX_RESOLVE_LIB_DEFECTS
391 // 214. set::find() missing const overload
392 std::pair
<const_iterator
,const_iterator
>
393 equal_range(const key_type
& __x
) const
395 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
396 _Base::equal_range(__x
);
397 return std::make_pair(const_iterator(__res
.first
, this),
398 const_iterator(__res
.second
, this));
402 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
405 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
411 typedef __gnu_debug::_Not_equal_to
<_Base_const_iterator
> _Not_equal
;
412 this->_M_invalidate_if(_Not_equal(_Base::end()));
416 template<typename _Key
, typename _Compare
, typename _Allocator
>
418 operator==(const multiset
<_Key
, _Compare
, _Allocator
>& __lhs
,
419 const multiset
<_Key
, _Compare
, _Allocator
>& __rhs
)
420 { return __lhs
._M_base() == __rhs
._M_base(); }
422 template<typename _Key
, typename _Compare
, typename _Allocator
>
424 operator!=(const multiset
<_Key
, _Compare
, _Allocator
>& __lhs
,
425 const multiset
<_Key
, _Compare
, _Allocator
>& __rhs
)
426 { return __lhs
._M_base() != __rhs
._M_base(); }
428 template<typename _Key
, typename _Compare
, typename _Allocator
>
430 operator<(const multiset
<_Key
, _Compare
, _Allocator
>& __lhs
,
431 const multiset
<_Key
, _Compare
, _Allocator
>& __rhs
)
432 { return __lhs
._M_base() < __rhs
._M_base(); }
434 template<typename _Key
, typename _Compare
, typename _Allocator
>
436 operator<=(const multiset
<_Key
, _Compare
, _Allocator
>& __lhs
,
437 const multiset
<_Key
, _Compare
, _Allocator
>& __rhs
)
438 { return __lhs
._M_base() <= __rhs
._M_base(); }
440 template<typename _Key
, typename _Compare
, typename _Allocator
>
442 operator>=(const multiset
<_Key
, _Compare
, _Allocator
>& __lhs
,
443 const multiset
<_Key
, _Compare
, _Allocator
>& __rhs
)
444 { return __lhs
._M_base() >= __rhs
._M_base(); }
446 template<typename _Key
, typename _Compare
, typename _Allocator
>
448 operator>(const multiset
<_Key
, _Compare
, _Allocator
>& __lhs
,
449 const multiset
<_Key
, _Compare
, _Allocator
>& __rhs
)
450 { return __lhs
._M_base() > __rhs
._M_base(); }
452 template<typename _Key
, typename _Compare
, typename _Allocator
>
454 swap(multiset
<_Key
, _Compare
, _Allocator
>& __x
,
455 multiset
<_Key
, _Compare
, _Allocator
>& __y
)
456 { return __x
.swap(__y
); }
458 } // namespace __debug