1 // Debugging multiset implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009
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>
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_D::multiset<_Key, _Compare, _Allocator>,
46 public __gnu_debug::_Safe_sequence<multiset<_Key, _Compare, _Allocator> >
48 typedef _GLIBCXX_STD_D::multiset<_Key, _Compare, _Allocator> _Base;
49 typedef __gnu_debug::_Safe_sequence<multiset> _Safe_base;
53 typedef _Key key_type;
54 typedef _Key value_type;
55 typedef _Compare key_compare;
56 typedef _Compare value_compare;
57 typedef _Allocator allocator_type;
58 typedef typename _Base::reference reference;
59 typedef typename _Base::const_reference const_reference;
61 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multiset>
63 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
64 multiset> const_iterator;
66 typedef typename _Base::size_type size_type;
67 typedef typename _Base::difference_type difference_type;
68 typedef typename _Base::pointer pointer;
69 typedef typename _Base::const_pointer const_pointer;
70 typedef std::reverse_iterator<iterator> reverse_iterator;
71 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
73 // 23.3.3.1 construct/copy/destroy:
74 explicit multiset(const _Compare& __comp = _Compare(),
75 const _Allocator& __a = _Allocator())
76 : _Base(__comp, __a) { }
78 template<typename _InputIterator>
79 multiset(_InputIterator __first, _InputIterator __last,
80 const _Compare& __comp = _Compare(),
81 const _Allocator& __a = _Allocator())
82 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
85 multiset(const multiset& __x)
86 : _Base(__x), _Safe_base() { }
88 multiset(const _Base& __x)
89 : _Base(__x), _Safe_base() { }
91 #ifdef __GXX_EXPERIMENTAL_CXX0X__
92 multiset(multiset&& __x)
93 : _Base(std::forward<multiset>(__x)), _Safe_base()
94 { this->_M_swap(__x); }
96 multiset(initializer_list<value_type> __l,
97 const _Compare& __comp = _Compare(),
98 const allocator_type& __a = allocator_type())
99 : _Base(__l, __comp, __a), _Safe_base() { }
105 operator=(const multiset& __x)
107 *static_cast<_Base*>(this) = __x;
108 this->_M_invalidate_all();
112 #ifdef __GXX_EXPERIMENTAL_CXX0X__
114 operator=(multiset&& __x)
123 operator=(initializer_list<value_type> __l)
131 using _Base::get_allocator;
136 { return iterator(_Base::begin(), this); }
140 { return const_iterator(_Base::begin(), this); }
144 { return iterator(_Base::end(), this); }
148 { return const_iterator(_Base::end(), this); }
152 { return reverse_iterator(end()); }
154 const_reverse_iterator
156 { return const_reverse_iterator(end()); }
160 { return reverse_iterator(begin()); }
162 const_reverse_iterator
164 { return const_reverse_iterator(begin()); }
166 #ifdef __GXX_EXPERIMENTAL_CXX0X__
169 { return const_iterator(_Base::begin(), this); }
173 { return const_iterator(_Base::end(), this); }
175 const_reverse_iterator
177 { return const_reverse_iterator(end()); }
179 const_reverse_iterator
181 { return const_reverse_iterator(begin()); }
187 using _Base::max_size;
191 insert(const value_type& __x)
192 { return iterator(_Base::insert(__x), this); }
195 insert(iterator __position, const value_type& __x)
197 __glibcxx_check_insert(__position);
198 return iterator(_Base::insert(__position.base(), __x), this);
201 template<typename _InputIterator>
203 insert(_InputIterator __first, _InputIterator __last)
205 __glibcxx_check_valid_range(__first, __last);
206 _Base::insert(__first, __last);
209 #ifdef __GXX_EXPERIMENTAL_CXX0X__
211 insert(initializer_list<value_type> __l)
212 { _Base::insert(__l); }
216 erase(iterator __position)
218 __glibcxx_check_erase(__position);
219 __position._M_invalidate();
220 _Base::erase(__position.base());
224 erase(const key_type& __x)
226 std::pair<iterator, iterator> __victims = this->equal_range(__x);
227 size_type __count = 0;
228 while (__victims.first != __victims.second)
230 iterator __victim = __victims.first++;
231 __victim._M_invalidate();
232 _Base::erase(__victim.base());
239 erase(iterator __first, iterator __last)
241 // _GLIBCXX_RESOLVE_LIB_DEFECTS
242 // 151. can't currently clear() empty container
243 __glibcxx_check_erase_range(__first, __last);
244 while (__first != __last)
245 this->erase(__first++);
257 { this->erase(begin(), end()); }
260 using _Base::key_comp;
261 using _Base::value_comp;
263 // multiset operations:
265 find(const key_type& __x)
266 { return iterator(_Base::find(__x), this); }
268 // _GLIBCXX_RESOLVE_LIB_DEFECTS
269 // 214. set::find() missing const overload
271 find(const key_type& __x) const
272 { return const_iterator(_Base::find(__x), this); }
277 lower_bound(const key_type& __x)
278 { return iterator(_Base::lower_bound(__x), this); }
280 // _GLIBCXX_RESOLVE_LIB_DEFECTS
281 // 214. set::find() missing const overload
283 lower_bound(const key_type& __x) const
284 { return const_iterator(_Base::lower_bound(__x), this); }
287 upper_bound(const key_type& __x)
288 { return iterator(_Base::upper_bound(__x), this); }
290 // _GLIBCXX_RESOLVE_LIB_DEFECTS
291 // 214. set::find() missing const overload
293 upper_bound(const key_type& __x) const
294 { return const_iterator(_Base::upper_bound(__x), this); }
296 std::pair<iterator,iterator>
297 equal_range(const key_type& __x)
299 typedef typename _Base::iterator _Base_iterator;
300 std::pair<_Base_iterator, _Base_iterator> __res =
301 _Base::equal_range(__x);
302 return std::make_pair(iterator(__res.first, this),
303 iterator(__res.second, this));
306 // _GLIBCXX_RESOLVE_LIB_DEFECTS
307 // 214. set::find() missing const overload
308 std::pair<const_iterator,const_iterator>
309 equal_range(const key_type& __x) const
311 typedef typename _Base::const_iterator _Base_iterator;
312 std::pair<_Base_iterator, _Base_iterator> __res =
313 _Base::equal_range(__x);
314 return std::make_pair(const_iterator(__res.first, this),
315 const_iterator(__res.second, this));
319 _M_base() { return *this; }
322 _M_base() const { return *this; }
328 typedef typename _Base::const_iterator _Base_const_iterator;
329 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
330 this->_M_invalidate_if(_Not_equal(_M_base().end()));
334 template<typename _Key, typename _Compare, typename _Allocator>
336 operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
337 const multiset<_Key, _Compare, _Allocator>& __rhs)
338 { return __lhs._M_base() == __rhs._M_base(); }
340 template<typename _Key, typename _Compare, typename _Allocator>
342 operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
343 const multiset<_Key, _Compare, _Allocator>& __rhs)
344 { return __lhs._M_base() != __rhs._M_base(); }
346 template<typename _Key, typename _Compare, typename _Allocator>
348 operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
349 const multiset<_Key, _Compare, _Allocator>& __rhs)
350 { return __lhs._M_base() < __rhs._M_base(); }
352 template<typename _Key, typename _Compare, typename _Allocator>
354 operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
355 const multiset<_Key, _Compare, _Allocator>& __rhs)
356 { return __lhs._M_base() <= __rhs._M_base(); }
358 template<typename _Key, typename _Compare, typename _Allocator>
360 operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
361 const multiset<_Key, _Compare, _Allocator>& __rhs)
362 { return __lhs._M_base() >= __rhs._M_base(); }
364 template<typename _Key, typename _Compare, typename _Allocator>
366 operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
367 const multiset<_Key, _Compare, _Allocator>& __rhs)
368 { return __lhs._M_base() > __rhs._M_base(); }
370 template<typename _Key, typename _Compare, typename _Allocator>
372 swap(multiset<_Key, _Compare, _Allocator>& __x,
373 multiset<_Key, _Compare, _Allocator>& __y)
374 { return __x.swap(__y); }
376 } // namespace __debug