9c950a1d0c21386a7bfc5efd28886463b26eca5a
[gcc.git] / libstdc++-v3 / include / debug / multiset.h
1 // Debugging multiset implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
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.
11
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.
16
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.
20
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/>.
25
26 /** @file debug/multiset.h
27 * This file is a GNU debug extension to the Standard C++ Library.
28 */
29
30 #ifndef _GLIBCXX_DEBUG_MULTISET_H
31 #define _GLIBCXX_DEBUG_MULTISET_H 1
32
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
35 #include <utility>
36
37 namespace std
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
45 : public _GLIBCXX_STD_D::multiset<_Key, _Compare, _Allocator>,
46 public __gnu_debug::_Safe_sequence<multiset<_Key, _Compare, _Allocator> >
47 {
48 typedef _GLIBCXX_STD_D::multiset<_Key, _Compare, _Allocator> _Base;
49 typedef __gnu_debug::_Safe_sequence<multiset> _Safe_base;
50
51 public:
52 // types:
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;
60
61 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multiset>
62 iterator;
63 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
64 multiset> const_iterator;
65
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;
72
73 // 23.3.3.1 construct/copy/destroy:
74 explicit multiset(const _Compare& __comp = _Compare(),
75 const _Allocator& __a = _Allocator())
76 : _Base(__comp, __a) { }
77
78 template<typename _InputIterator>
79 multiset(_InputIterator __first, _InputIterator __last,
80 const _Compare& __comp = _Compare(),
81 const _Allocator& __a = _Allocator())
82 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
83 __last)),
84 __gnu_debug::__base(__last),
85 __comp, __a) { }
86
87 multiset(const multiset& __x)
88 : _Base(__x), _Safe_base() { }
89
90 multiset(const _Base& __x)
91 : _Base(__x), _Safe_base() { }
92
93 #ifdef __GXX_EXPERIMENTAL_CXX0X__
94 multiset(multiset&& __x)
95 : _Base(std::move(__x)), _Safe_base()
96 { this->_M_swap(__x); }
97
98 multiset(initializer_list<value_type> __l,
99 const _Compare& __comp = _Compare(),
100 const allocator_type& __a = allocator_type())
101 : _Base(__l, __comp, __a), _Safe_base() { }
102 #endif
103
104 ~multiset() { }
105
106 multiset&
107 operator=(const multiset& __x)
108 {
109 *static_cast<_Base*>(this) = __x;
110 this->_M_invalidate_all();
111 return *this;
112 }
113
114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
115 multiset&
116 operator=(multiset&& __x)
117 {
118 // NB: DR 1204.
119 // NB: DR 675.
120 clear();
121 swap(__x);
122 return *this;
123 }
124
125 multiset&
126 operator=(initializer_list<value_type> __l)
127 {
128 this->clear();
129 this->insert(__l);
130 return *this;
131 }
132 #endif
133
134 using _Base::get_allocator;
135
136 // iterators:
137 iterator
138 begin()
139 { return iterator(_Base::begin(), this); }
140
141 const_iterator
142 begin() const
143 { return const_iterator(_Base::begin(), this); }
144
145 iterator
146 end()
147 { return iterator(_Base::end(), this); }
148
149 const_iterator
150 end() const
151 { return const_iterator(_Base::end(), this); }
152
153 reverse_iterator
154 rbegin()
155 { return reverse_iterator(end()); }
156
157 const_reverse_iterator
158 rbegin() const
159 { return const_reverse_iterator(end()); }
160
161 reverse_iterator
162 rend()
163 { return reverse_iterator(begin()); }
164
165 const_reverse_iterator
166 rend() const
167 { return const_reverse_iterator(begin()); }
168
169 #ifdef __GXX_EXPERIMENTAL_CXX0X__
170 const_iterator
171 cbegin() const
172 { return const_iterator(_Base::begin(), this); }
173
174 const_iterator
175 cend() const
176 { return const_iterator(_Base::end(), this); }
177
178 const_reverse_iterator
179 crbegin() const
180 { return const_reverse_iterator(end()); }
181
182 const_reverse_iterator
183 crend() const
184 { return const_reverse_iterator(begin()); }
185 #endif
186
187 // capacity:
188 using _Base::empty;
189 using _Base::size;
190 using _Base::max_size;
191
192 // modifiers:
193 iterator
194 insert(const value_type& __x)
195 { return iterator(_Base::insert(__x), this); }
196
197 #ifdef __GXX_EXPERIMENTAL_CXX0X__
198 iterator
199 insert(value_type&& __x)
200 { return iterator(_Base::insert(std::move(__x)), this); }
201 #endif
202
203 iterator
204 insert(const_iterator __position, const value_type& __x)
205 {
206 __glibcxx_check_insert(__position);
207 return iterator(_Base::insert(__position.base(), __x), this);
208 }
209
210 #ifdef __GXX_EXPERIMENTAL_CXX0X__
211 iterator
212 insert(const_iterator __position, value_type&& __x)
213 {
214 __glibcxx_check_insert(__position);
215 return iterator(_Base::insert(__position.base(), std::move(__x)),
216 this);
217 }
218 #endif
219
220 template<typename _InputIterator>
221 void
222 insert(_InputIterator __first, _InputIterator __last)
223 {
224 __glibcxx_check_valid_range(__first, __last);
225 _Base::insert(__gnu_debug::__base(__first),
226 __gnu_debug::__base(__last));
227 }
228
229 #ifdef __GXX_EXPERIMENTAL_CXX0X__
230 void
231 insert(initializer_list<value_type> __l)
232 { _Base::insert(__l); }
233 #endif
234
235 #ifdef __GXX_EXPERIMENTAL_CXX0X__
236 iterator
237 erase(const_iterator __position)
238 {
239 __glibcxx_check_erase(__position);
240 __position._M_invalidate();
241 return iterator(_Base::erase(__position.base()), this);
242 }
243 #else
244 void
245 erase(iterator __position)
246 {
247 __glibcxx_check_erase(__position);
248 __position._M_invalidate();
249 _Base::erase(__position.base());
250 }
251 #endif
252
253 size_type
254 erase(const key_type& __x)
255 {
256 std::pair<iterator, iterator> __victims = this->equal_range(__x);
257 size_type __count = 0;
258 while (__victims.first != __victims.second)
259 {
260 iterator __victim = __victims.first++;
261 __victim._M_invalidate();
262 _Base::erase(__victim.base());
263 ++__count;
264 }
265 return __count;
266 }
267
268 #ifdef __GXX_EXPERIMENTAL_CXX0X__
269 iterator
270 erase(const_iterator __first, const_iterator __last)
271 {
272 // _GLIBCXX_RESOLVE_LIB_DEFECTS
273 // 151. can't currently clear() empty container
274 __glibcxx_check_erase_range(__first, __last);
275 while (__first != __last)
276 this->erase(__first++);
277 return __last; // iterator == const_iterator
278 }
279 #else
280 void
281 erase(iterator __first, iterator __last)
282 {
283 // _GLIBCXX_RESOLVE_LIB_DEFECTS
284 // 151. can't currently clear() empty container
285 __glibcxx_check_erase_range(__first, __last);
286 while (__first != __last)
287 this->erase(__first++);
288 }
289 #endif
290
291 void
292 swap(multiset& __x)
293 {
294 _Base::swap(__x);
295 this->_M_swap(__x);
296 }
297
298 void
299 clear()
300 { this->erase(begin(), end()); }
301
302 // observers:
303 using _Base::key_comp;
304 using _Base::value_comp;
305
306 // multiset operations:
307 iterator
308 find(const key_type& __x)
309 { return iterator(_Base::find(__x), this); }
310
311 // _GLIBCXX_RESOLVE_LIB_DEFECTS
312 // 214. set::find() missing const overload
313 const_iterator
314 find(const key_type& __x) const
315 { return const_iterator(_Base::find(__x), this); }
316
317 using _Base::count;
318
319 iterator
320 lower_bound(const key_type& __x)
321 { return iterator(_Base::lower_bound(__x), this); }
322
323 // _GLIBCXX_RESOLVE_LIB_DEFECTS
324 // 214. set::find() missing const overload
325 const_iterator
326 lower_bound(const key_type& __x) const
327 { return const_iterator(_Base::lower_bound(__x), this); }
328
329 iterator
330 upper_bound(const key_type& __x)
331 { return iterator(_Base::upper_bound(__x), this); }
332
333 // _GLIBCXX_RESOLVE_LIB_DEFECTS
334 // 214. set::find() missing const overload
335 const_iterator
336 upper_bound(const key_type& __x) const
337 { return const_iterator(_Base::upper_bound(__x), this); }
338
339 std::pair<iterator,iterator>
340 equal_range(const key_type& __x)
341 {
342 typedef typename _Base::iterator _Base_iterator;
343 std::pair<_Base_iterator, _Base_iterator> __res =
344 _Base::equal_range(__x);
345 return std::make_pair(iterator(__res.first, this),
346 iterator(__res.second, this));
347 }
348
349 // _GLIBCXX_RESOLVE_LIB_DEFECTS
350 // 214. set::find() missing const overload
351 std::pair<const_iterator,const_iterator>
352 equal_range(const key_type& __x) const
353 {
354 typedef typename _Base::const_iterator _Base_iterator;
355 std::pair<_Base_iterator, _Base_iterator> __res =
356 _Base::equal_range(__x);
357 return std::make_pair(const_iterator(__res.first, this),
358 const_iterator(__res.second, this));
359 }
360
361 _Base&
362 _M_base() { return *this; }
363
364 const _Base&
365 _M_base() const { return *this; }
366
367 private:
368 void
369 _M_invalidate_all()
370 {
371 typedef typename _Base::const_iterator _Base_const_iterator;
372 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
373 this->_M_invalidate_if(_Not_equal(_M_base().end()));
374 }
375 };
376
377 template<typename _Key, typename _Compare, typename _Allocator>
378 inline bool
379 operator==(const multiset<_Key, _Compare, _Allocator>& __lhs,
380 const multiset<_Key, _Compare, _Allocator>& __rhs)
381 { return __lhs._M_base() == __rhs._M_base(); }
382
383 template<typename _Key, typename _Compare, typename _Allocator>
384 inline bool
385 operator!=(const multiset<_Key, _Compare, _Allocator>& __lhs,
386 const multiset<_Key, _Compare, _Allocator>& __rhs)
387 { return __lhs._M_base() != __rhs._M_base(); }
388
389 template<typename _Key, typename _Compare, typename _Allocator>
390 inline bool
391 operator<(const multiset<_Key, _Compare, _Allocator>& __lhs,
392 const multiset<_Key, _Compare, _Allocator>& __rhs)
393 { return __lhs._M_base() < __rhs._M_base(); }
394
395 template<typename _Key, typename _Compare, typename _Allocator>
396 inline bool
397 operator<=(const multiset<_Key, _Compare, _Allocator>& __lhs,
398 const multiset<_Key, _Compare, _Allocator>& __rhs)
399 { return __lhs._M_base() <= __rhs._M_base(); }
400
401 template<typename _Key, typename _Compare, typename _Allocator>
402 inline bool
403 operator>=(const multiset<_Key, _Compare, _Allocator>& __lhs,
404 const multiset<_Key, _Compare, _Allocator>& __rhs)
405 { return __lhs._M_base() >= __rhs._M_base(); }
406
407 template<typename _Key, typename _Compare, typename _Allocator>
408 inline bool
409 operator>(const multiset<_Key, _Compare, _Allocator>& __lhs,
410 const multiset<_Key, _Compare, _Allocator>& __rhs)
411 { return __lhs._M_base() > __rhs._M_base(); }
412
413 template<typename _Key, typename _Compare, typename _Allocator>
414 void
415 swap(multiset<_Key, _Compare, _Allocator>& __x,
416 multiset<_Key, _Compare, _Allocator>& __y)
417 { return __x.swap(__y); }
418
419 } // namespace __debug
420 } // namespace std
421
422 #endif