In libobjc/: 2011-06-02 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / libstdc++-v3 / include / debug / multiset.h
blobee4c04735dd81e93a41cd6015b38ac59988db9e9
1 // Debugging multiset implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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>
35 #include <utility>
37 namespace std _GLIBCXX_VISIBILITY(default)
39 namespace __debug
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
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;
49 typedef __gnu_debug::_Safe_sequence<multiset> _Safe_base;
51 typedef typename _Base::const_iterator _Base_const_iterator;
52 typedef typename _Base::iterator _Base_iterator;
53 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
54 public:
55 // types:
56 typedef _Key key_type;
57 typedef _Key value_type;
58 typedef _Compare key_compare;
59 typedef _Compare value_compare;
60 typedef _Allocator allocator_type;
61 typedef typename _Base::reference reference;
62 typedef typename _Base::const_reference const_reference;
64 typedef __gnu_debug::_Safe_iterator<_Base_iterator, multiset>
65 iterator;
66 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,
67 multiset> const_iterator;
69 typedef typename _Base::size_type size_type;
70 typedef typename _Base::difference_type difference_type;
71 typedef typename _Base::pointer pointer;
72 typedef typename _Base::const_pointer const_pointer;
73 typedef std::reverse_iterator<iterator> reverse_iterator;
74 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
76 // 23.3.3.1 construct/copy/destroy:
77 explicit multiset(const _Compare& __comp = _Compare(),
78 const _Allocator& __a = _Allocator())
79 : _Base(__comp, __a) { }
81 template<typename _InputIterator>
82 multiset(_InputIterator __first, _InputIterator __last,
83 const _Compare& __comp = _Compare(),
84 const _Allocator& __a = _Allocator())
85 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
86 __last)),
87 __gnu_debug::__base(__last),
88 __comp, __a) { }
90 multiset(const multiset& __x)
91 : _Base(__x), _Safe_base() { }
93 multiset(const _Base& __x)
94 : _Base(__x), _Safe_base() { }
96 #ifdef __GXX_EXPERIMENTAL_CXX0X__
97 multiset(multiset&& __x)
98 noexcept(is_nothrow_copy_constructible<_Compare>::value)
99 : _Base(std::move(__x)), _Safe_base()
100 { this->_M_swap(__x); }
102 multiset(initializer_list<value_type> __l,
103 const _Compare& __comp = _Compare(),
104 const allocator_type& __a = allocator_type())
105 : _Base(__l, __comp, __a), _Safe_base() { }
106 #endif
108 ~multiset() _GLIBCXX_NOEXCEPT { }
110 multiset&
111 operator=(const multiset& __x)
113 *static_cast<_Base*>(this) = __x;
114 this->_M_invalidate_all();
115 return *this;
118 #ifdef __GXX_EXPERIMENTAL_CXX0X__
119 multiset&
120 operator=(multiset&& __x)
122 // NB: DR 1204.
123 // NB: DR 675.
124 clear();
125 swap(__x);
126 return *this;
129 multiset&
130 operator=(initializer_list<value_type> __l)
132 this->clear();
133 this->insert(__l);
134 return *this;
136 #endif
138 using _Base::get_allocator;
140 // iterators:
141 iterator
142 begin() _GLIBCXX_NOEXCEPT
143 { return iterator(_Base::begin(), this); }
145 const_iterator
146 begin() const _GLIBCXX_NOEXCEPT
147 { return const_iterator(_Base::begin(), this); }
149 iterator
150 end() _GLIBCXX_NOEXCEPT
151 { return iterator(_Base::end(), this); }
153 const_iterator
154 end() const _GLIBCXX_NOEXCEPT
155 { return const_iterator(_Base::end(), this); }
157 reverse_iterator
158 rbegin() _GLIBCXX_NOEXCEPT
159 { return reverse_iterator(end()); }
161 const_reverse_iterator
162 rbegin() const _GLIBCXX_NOEXCEPT
163 { return const_reverse_iterator(end()); }
165 reverse_iterator
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 #ifdef __GXX_EXPERIMENTAL_CXX0X__
174 const_iterator
175 cbegin() const noexcept
176 { return const_iterator(_Base::begin(), this); }
178 const_iterator
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()); }
189 #endif
191 // capacity:
192 using _Base::empty;
193 using _Base::size;
194 using _Base::max_size;
196 // modifiers:
197 iterator
198 insert(const value_type& __x)
199 { return iterator(_Base::insert(__x), this); }
201 #ifdef __GXX_EXPERIMENTAL_CXX0X__
202 iterator
203 insert(value_type&& __x)
204 { return iterator(_Base::insert(std::move(__x)), this); }
205 #endif
207 iterator
208 insert(const_iterator __position, const value_type& __x)
210 __glibcxx_check_insert(__position);
211 return iterator(_Base::insert(__position.base(), __x), this);
214 #ifdef __GXX_EXPERIMENTAL_CXX0X__
215 iterator
216 insert(const_iterator __position, value_type&& __x)
218 __glibcxx_check_insert(__position);
219 return iterator(_Base::insert(__position.base(), std::move(__x)),
220 this);
222 #endif
224 template<typename _InputIterator>
225 void
226 insert(_InputIterator __first, _InputIterator __last)
228 __glibcxx_check_valid_range(__first, __last);
229 _Base::insert(__gnu_debug::__base(__first),
230 __gnu_debug::__base(__last));
233 #ifdef __GXX_EXPERIMENTAL_CXX0X__
234 void
235 insert(initializer_list<value_type> __l)
236 { _Base::insert(__l); }
237 #endif
239 #ifdef __GXX_EXPERIMENTAL_CXX0X__
240 iterator
241 erase(const_iterator __position)
243 __glibcxx_check_erase(__position);
244 this->_M_invalidate_if(_Equal(__position.base()));
245 return iterator(_Base::erase(__position.base()), this);
247 #else
248 void
249 erase(iterator __position)
251 __glibcxx_check_erase(__position);
252 this->_M_invalidate_if(_Equal(__position.base()));
253 _Base::erase(__position.base());
255 #endif
257 size_type
258 erase(const key_type& __x)
260 std::pair<_Base_iterator, _Base_iterator> __victims =
261 _Base::equal_range(__x);
262 size_type __count = 0;
263 _Base_iterator __victim = __victims.first;
264 while (__victim != __victims.second)
266 this->_M_invalidate_if(_Equal(__victim));
267 _Base::erase(__victim++);
268 ++__count;
270 return __count;
273 #ifdef __GXX_EXPERIMENTAL_CXX0X__
274 iterator
275 erase(const_iterator __first, const_iterator __last)
277 // _GLIBCXX_RESOLVE_LIB_DEFECTS
278 // 151. can't currently clear() empty container
279 __glibcxx_check_erase_range(__first, __last);
280 for (_Base_const_iterator __victim = __first.base();
281 __victim != __last.base(); ++__victim)
283 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
284 _M_message(__gnu_debug::__msg_valid_range)
285 ._M_iterator(__first, "first")
286 ._M_iterator(__last, "last"));
287 this->_M_invalidate_if(_Equal(__victim));
289 return iterator(_Base::erase(__first.base(), __last.base()), this);
291 #else
292 void
293 erase(iterator __first, iterator __last)
295 // _GLIBCXX_RESOLVE_LIB_DEFECTS
296 // 151. can't currently clear() empty container
297 __glibcxx_check_erase_range(__first, __last);
298 for (_Base_iterator __victim = __first.base();
299 __victim != __last.base(); ++__victim)
301 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
302 _M_message(__gnu_debug::__msg_valid_range)
303 ._M_iterator(__first, "first")
304 ._M_iterator(__last, "last"));
305 this->_M_invalidate_if(_Equal(__victim));
307 _Base::erase(__first.base(), __last.base());
309 #endif
311 void
312 swap(multiset& __x)
314 _Base::swap(__x);
315 this->_M_swap(__x);
318 void
319 clear() _GLIBCXX_NOEXCEPT
321 this->_M_invalidate_all();
322 _Base::clear();
325 // observers:
326 using _Base::key_comp;
327 using _Base::value_comp;
329 // multiset operations:
330 iterator
331 find(const key_type& __x)
332 { return iterator(_Base::find(__x), this); }
334 // _GLIBCXX_RESOLVE_LIB_DEFECTS
335 // 214. set::find() missing const overload
336 const_iterator
337 find(const key_type& __x) const
338 { return const_iterator(_Base::find(__x), this); }
340 using _Base::count;
342 iterator
343 lower_bound(const key_type& __x)
344 { return iterator(_Base::lower_bound(__x), this); }
346 // _GLIBCXX_RESOLVE_LIB_DEFECTS
347 // 214. set::find() missing const overload
348 const_iterator
349 lower_bound(const key_type& __x) const
350 { return const_iterator(_Base::lower_bound(__x), this); }
352 iterator
353 upper_bound(const key_type& __x)
354 { return iterator(_Base::upper_bound(__x), this); }
356 // _GLIBCXX_RESOLVE_LIB_DEFECTS
357 // 214. set::find() missing const overload
358 const_iterator
359 upper_bound(const key_type& __x) const
360 { return const_iterator(_Base::upper_bound(__x), this); }
362 std::pair<iterator,iterator>
363 equal_range(const key_type& __x)
365 std::pair<_Base_iterator, _Base_iterator> __res =
366 _Base::equal_range(__x);
367 return std::make_pair(iterator(__res.first, this),
368 iterator(__res.second, this));
371 // _GLIBCXX_RESOLVE_LIB_DEFECTS
372 // 214. set::find() missing const overload
373 std::pair<const_iterator,const_iterator>
374 equal_range(const key_type& __x) const
376 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
377 _Base::equal_range(__x);
378 return std::make_pair(const_iterator(__res.first, this),
379 const_iterator(__res.second, this));
382 _Base&
383 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
385 const _Base&
386 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
388 private:
389 void
390 _M_invalidate_all()
392 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
393 this->_M_invalidate_if(_Not_equal(_Base::end()));
397 template<typename _Key, typename _Compare, typename _Allocator>
398 inline bool
399 operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
400 const multiset<_Key, _Compare, _Allocator>& __rhs)
401 { return __lhs._M_base() == __rhs._M_base(); }
403 template<typename _Key, typename _Compare, typename _Allocator>
404 inline bool
405 operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
406 const multiset<_Key, _Compare, _Allocator>& __rhs)
407 { return __lhs._M_base() != __rhs._M_base(); }
409 template<typename _Key, typename _Compare, typename _Allocator>
410 inline bool
411 operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
412 const multiset<_Key, _Compare, _Allocator>& __rhs)
413 { return __lhs._M_base() < __rhs._M_base(); }
415 template<typename _Key, typename _Compare, typename _Allocator>
416 inline bool
417 operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
418 const multiset<_Key, _Compare, _Allocator>& __rhs)
419 { return __lhs._M_base() <= __rhs._M_base(); }
421 template<typename _Key, typename _Compare, typename _Allocator>
422 inline bool
423 operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
424 const multiset<_Key, _Compare, _Allocator>& __rhs)
425 { return __lhs._M_base() >= __rhs._M_base(); }
427 template<typename _Key, typename _Compare, typename _Allocator>
428 inline bool
429 operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
430 const multiset<_Key, _Compare, _Allocator>& __rhs)
431 { return __lhs._M_base() > __rhs._M_base(); }
433 template<typename _Key, typename _Compare, typename _Allocator>
434 void
435 swap(multiset<_Key, _Compare, _Allocator>& __x,
436 multiset<_Key, _Compare, _Allocator>& __y)
437 { return __x.swap(__y); }
439 } // namespace __debug
440 } // namespace std
442 #endif