Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / libstdc++-v3 / include / debug / set.h
blobc83e2afa96cc382bac954f7509ce26ce9d354f0f
1 // Debugging set implementation -*- C++ -*-
3 // Copyright (C) 2003-2014 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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/set.h
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_SET_H
30 #define _GLIBCXX_DEBUG_SET_H 1
32 #include <debug/safe_sequence.h>
33 #include <debug/safe_iterator.h>
34 #include <utility>
36 namespace std _GLIBCXX_VISIBILITY(default)
38 namespace __debug
40 /// Class std::set with safety/checking/debug instrumentation.
41 template<typename _Key, typename _Compare = std::less<_Key>,
42 typename _Allocator = std::allocator<_Key> >
43 class set
44 : public _GLIBCXX_STD_C::set<_Key,_Compare,_Allocator>,
45 public __gnu_debug::_Safe_sequence<set<_Key, _Compare, _Allocator> >
47 typedef _GLIBCXX_STD_C::set<_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;
52 #if __cplusplus >= 201103L
53 typedef __gnu_cxx::__alloc_traits<typename
54 _Base::allocator_type> _Alloc_traits;
55 #endif
56 public:
57 // types:
58 typedef _Key key_type;
59 typedef _Key value_type;
60 typedef _Compare key_compare;
61 typedef _Compare value_compare;
62 typedef _Allocator allocator_type;
63 typedef typename _Base::reference reference;
64 typedef typename _Base::const_reference const_reference;
66 typedef __gnu_debug::_Safe_iterator<_Base_iterator, set>
67 iterator;
68 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, set>
69 const_iterator;
71 typedef typename _Base::size_type size_type;
72 typedef typename _Base::difference_type difference_type;
73 typedef typename _Base::pointer pointer;
74 typedef typename _Base::const_pointer const_pointer;
75 typedef std::reverse_iterator<iterator> reverse_iterator;
76 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
78 // 23.3.3.1 construct/copy/destroy:
80 set() : _Base() { }
82 explicit set(const _Compare& __comp,
83 const _Allocator& __a = _Allocator())
84 : _Base(__comp, __a) { }
86 template<typename _InputIterator>
87 set(_InputIterator __first, _InputIterator __last,
88 const _Compare& __comp = _Compare(),
89 const _Allocator& __a = _Allocator())
90 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
91 __last)),
92 __gnu_debug::__base(__last),
93 __comp, __a) { }
95 set(const set& __x)
96 : _Base(__x) { }
98 set(const _Base& __x)
99 : _Base(__x) { }
101 #if __cplusplus >= 201103L
102 set(set&& __x)
103 noexcept(is_nothrow_copy_constructible<_Compare>::value)
104 : _Base(std::move(__x))
105 { this->_M_swap(__x); }
107 set(initializer_list<value_type> __l,
108 const _Compare& __comp = _Compare(),
109 const allocator_type& __a = allocator_type())
110 : _Base(__l, __comp, __a) { }
112 explicit
113 set(const allocator_type& __a)
114 : _Base(__a) { }
116 set(const set& __x, const allocator_type& __a)
117 : _Base(__x, __a) { }
119 set(set&& __x, const allocator_type& __a)
120 : _Base(std::move(__x._M_base()), __a) { }
122 set(initializer_list<value_type> __l, const allocator_type& __a)
123 : _Base(__l, __a)
126 template<typename _InputIterator>
127 set(_InputIterator __first, _InputIterator __last,
128 const allocator_type& __a)
129 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
130 __last)),
131 __gnu_debug::__base(__last), __a)
133 #endif
135 ~set() _GLIBCXX_NOEXCEPT { }
137 set&
138 operator=(const set& __x)
140 _M_base() = __x;
141 this->_M_invalidate_all();
142 return *this;
145 #if __cplusplus >= 201103L
146 set&
147 operator=(set&& __x)
148 noexcept(_Alloc_traits::_S_nothrow_move())
150 __glibcxx_check_self_move_assign(__x);
151 bool __xfer_memory = _Alloc_traits::_S_propagate_on_move_assign()
152 || __x.get_allocator() == this->get_allocator();
153 _M_base() = std::move(__x._M_base());
154 if (__xfer_memory)
155 this->_M_swap(__x);
156 else
157 this->_M_invalidate_all();
158 __x._M_invalidate_all();
159 return *this;
162 set&
163 operator=(initializer_list<value_type> __l)
165 _M_base() = __l;
166 this->_M_invalidate_all();
167 return *this;
169 #endif
171 using _Base::get_allocator;
173 // iterators:
174 iterator
175 begin() _GLIBCXX_NOEXCEPT
176 { return iterator(_Base::begin(), this); }
178 const_iterator
179 begin() const _GLIBCXX_NOEXCEPT
180 { return const_iterator(_Base::begin(), this); }
182 iterator
183 end() _GLIBCXX_NOEXCEPT
184 { return iterator(_Base::end(), this); }
186 const_iterator
187 end() const _GLIBCXX_NOEXCEPT
188 { return const_iterator(_Base::end(), this); }
190 reverse_iterator
191 rbegin() _GLIBCXX_NOEXCEPT
192 { return reverse_iterator(end()); }
194 const_reverse_iterator
195 rbegin() const _GLIBCXX_NOEXCEPT
196 { return const_reverse_iterator(end()); }
198 reverse_iterator
199 rend() _GLIBCXX_NOEXCEPT
200 { return reverse_iterator(begin()); }
202 const_reverse_iterator
203 rend() const _GLIBCXX_NOEXCEPT
204 { return const_reverse_iterator(begin()); }
206 #if __cplusplus >= 201103L
207 const_iterator
208 cbegin() const noexcept
209 { return const_iterator(_Base::begin(), this); }
211 const_iterator
212 cend() const noexcept
213 { return const_iterator(_Base::end(), this); }
215 const_reverse_iterator
216 crbegin() const noexcept
217 { return const_reverse_iterator(end()); }
219 const_reverse_iterator
220 crend() const noexcept
221 { return const_reverse_iterator(begin()); }
222 #endif
224 // capacity:
225 using _Base::empty;
226 using _Base::size;
227 using _Base::max_size;
229 // modifiers:
230 #if __cplusplus >= 201103L
231 template<typename... _Args>
232 std::pair<iterator, bool>
233 emplace(_Args&&... __args)
235 auto __res = _Base::emplace(std::forward<_Args>(__args)...);
236 return std::pair<iterator, bool>(iterator(__res.first, this),
237 __res.second);
240 template<typename... _Args>
241 iterator
242 emplace_hint(const_iterator __pos, _Args&&... __args)
244 __glibcxx_check_insert(__pos);
245 return iterator(_Base::emplace_hint(__pos.base(),
246 std::forward<_Args>(__args)...),
247 this);
249 #endif
251 std::pair<iterator, bool>
252 insert(const value_type& __x)
254 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
255 return std::pair<iterator, bool>(iterator(__res.first, this),
256 __res.second);
259 #if __cplusplus >= 201103L
260 std::pair<iterator, bool>
261 insert(value_type&& __x)
263 std::pair<_Base_iterator, bool> __res
264 = _Base::insert(std::move(__x));
265 return std::pair<iterator, bool>(iterator(__res.first, this),
266 __res.second);
268 #endif
270 iterator
271 insert(const_iterator __position, const value_type& __x)
273 __glibcxx_check_insert(__position);
274 return iterator(_Base::insert(__position.base(), __x), this);
277 #if __cplusplus >= 201103L
278 iterator
279 insert(const_iterator __position, value_type&& __x)
281 __glibcxx_check_insert(__position);
282 return iterator(_Base::insert(__position.base(), std::move(__x)),
283 this);
285 #endif
287 template <typename _InputIterator>
288 void
289 insert(_InputIterator __first, _InputIterator __last)
291 __glibcxx_check_valid_range(__first, __last);
292 _Base::insert(__gnu_debug::__base(__first),
293 __gnu_debug::__base(__last));
296 #if __cplusplus >= 201103L
297 void
298 insert(initializer_list<value_type> __l)
299 { _Base::insert(__l); }
300 #endif
302 #if __cplusplus >= 201103L
303 iterator
304 erase(const_iterator __position)
306 __glibcxx_check_erase(__position);
307 this->_M_invalidate_if(_Equal(__position.base()));
308 return iterator(_Base::erase(__position.base()), this);
310 #else
311 void
312 erase(iterator __position)
314 __glibcxx_check_erase(__position);
315 this->_M_invalidate_if(_Equal(__position.base()));
316 _Base::erase(__position.base());
318 #endif
320 size_type
321 erase(const key_type& __x)
323 _Base_iterator __victim = _Base::find(__x);
324 if (__victim == _Base::end())
325 return 0;
326 else
328 this->_M_invalidate_if(_Equal(__victim));
329 _Base::erase(__victim);
330 return 1;
334 #if __cplusplus >= 201103L
335 iterator
336 erase(const_iterator __first, const_iterator __last)
338 // _GLIBCXX_RESOLVE_LIB_DEFECTS
339 // 151. can't currently clear() empty container
340 __glibcxx_check_erase_range(__first, __last);
341 for (_Base_const_iterator __victim = __first.base();
342 __victim != __last.base(); ++__victim)
344 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
345 _M_message(__gnu_debug::__msg_valid_range)
346 ._M_iterator(__first, "first")
347 ._M_iterator(__last, "last"));
348 this->_M_invalidate_if(_Equal(__victim));
350 return iterator(_Base::erase(__first.base(), __last.base()), this);
352 #else
353 void
354 erase(iterator __first, iterator __last)
356 // _GLIBCXX_RESOLVE_LIB_DEFECTS
357 // 151. can't currently clear() empty container
358 __glibcxx_check_erase_range(__first, __last);
359 for (_Base_iterator __victim = __first.base();
360 __victim != __last.base(); ++__victim)
362 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
363 _M_message(__gnu_debug::__msg_valid_range)
364 ._M_iterator(__first, "first")
365 ._M_iterator(__last, "last"));
366 this->_M_invalidate_if(_Equal(__victim));
368 _Base::erase(__first.base(), __last.base());
370 #endif
372 void
373 swap(set& __x)
374 #if __cplusplus >= 201103L
375 noexcept(_Alloc_traits::_S_nothrow_swap())
376 #endif
378 #if __cplusplus >= 201103L
379 if (!_Alloc_traits::_S_propagate_on_swap())
380 __glibcxx_check_equal_allocs(__x);
381 #endif
382 _Base::swap(__x);
383 this->_M_swap(__x);
386 void
387 clear() _GLIBCXX_NOEXCEPT
389 this->_M_invalidate_all();
390 _Base::clear();
393 // observers:
394 using _Base::key_comp;
395 using _Base::value_comp;
397 // set operations:
398 iterator
399 find(const key_type& __x)
400 { return iterator(_Base::find(__x), this); }
402 // _GLIBCXX_RESOLVE_LIB_DEFECTS
403 // 214. set::find() missing const overload
404 const_iterator
405 find(const key_type& __x) const
406 { return const_iterator(_Base::find(__x), this); }
408 using _Base::count;
410 iterator
411 lower_bound(const key_type& __x)
412 { return iterator(_Base::lower_bound(__x), this); }
414 // _GLIBCXX_RESOLVE_LIB_DEFECTS
415 // 214. set::find() missing const overload
416 const_iterator
417 lower_bound(const key_type& __x) const
418 { return const_iterator(_Base::lower_bound(__x), this); }
420 iterator
421 upper_bound(const key_type& __x)
422 { return iterator(_Base::upper_bound(__x), this); }
424 // _GLIBCXX_RESOLVE_LIB_DEFECTS
425 // 214. set::find() missing const overload
426 const_iterator
427 upper_bound(const key_type& __x) const
428 { return const_iterator(_Base::upper_bound(__x), this); }
430 std::pair<iterator,iterator>
431 equal_range(const key_type& __x)
433 std::pair<_Base_iterator, _Base_iterator> __res =
434 _Base::equal_range(__x);
435 return std::make_pair(iterator(__res.first, this),
436 iterator(__res.second, this));
439 // _GLIBCXX_RESOLVE_LIB_DEFECTS
440 // 214. set::find() missing const overload
441 std::pair<const_iterator,const_iterator>
442 equal_range(const key_type& __x) const
444 std::pair<_Base_iterator, _Base_iterator> __res =
445 _Base::equal_range(__x);
446 return std::make_pair(const_iterator(__res.first, this),
447 const_iterator(__res.second, this));
450 _Base&
451 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
453 const _Base&
454 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
456 private:
457 void
458 _M_invalidate_all()
460 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
461 this->_M_invalidate_if(_Not_equal(_M_base().end()));
465 template<typename _Key, typename _Compare, typename _Allocator>
466 inline bool
467 operator==(const set<_Key, _Compare, _Allocator>& __lhs,
468 const set<_Key, _Compare, _Allocator>& __rhs)
469 { return __lhs._M_base() == __rhs._M_base(); }
471 template<typename _Key, typename _Compare, typename _Allocator>
472 inline bool
473 operator!=(const set<_Key, _Compare, _Allocator>& __lhs,
474 const set<_Key, _Compare, _Allocator>& __rhs)
475 { return __lhs._M_base() != __rhs._M_base(); }
477 template<typename _Key, typename _Compare, typename _Allocator>
478 inline bool
479 operator<(const set<_Key, _Compare, _Allocator>& __lhs,
480 const set<_Key, _Compare, _Allocator>& __rhs)
481 { return __lhs._M_base() < __rhs._M_base(); }
483 template<typename _Key, typename _Compare, typename _Allocator>
484 inline bool
485 operator<=(const set<_Key, _Compare, _Allocator>& __lhs,
486 const set<_Key, _Compare, _Allocator>& __rhs)
487 { return __lhs._M_base() <= __rhs._M_base(); }
489 template<typename _Key, typename _Compare, typename _Allocator>
490 inline bool
491 operator>=(const set<_Key, _Compare, _Allocator>& __lhs,
492 const set<_Key, _Compare, _Allocator>& __rhs)
493 { return __lhs._M_base() >= __rhs._M_base(); }
495 template<typename _Key, typename _Compare, typename _Allocator>
496 inline bool
497 operator>(const set<_Key, _Compare, _Allocator>& __lhs,
498 const set<_Key, _Compare, _Allocator>& __rhs)
499 { return __lhs._M_base() > __rhs._M_base(); }
501 template<typename _Key, typename _Compare, typename _Allocator>
502 void
503 swap(set<_Key, _Compare, _Allocator>& __x,
504 set<_Key, _Compare, _Allocator>& __y)
505 { return __x.swap(__y); }
507 } // namespace __debug
508 } // namespace std
510 #endif