1 // Debugging set implementation -*- C++ -*-
3 // Copyright (C) 2003-2015 Free Software Foundation, Inc.
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)
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/>.
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_container.h>
34 #include <debug/safe_iterator.h>
37 namespace std
_GLIBCXX_VISIBILITY(default)
41 /// Class std::set with safety/checking/debug instrumentation.
42 template<typename _Key
, typename _Compare
= std::less
<_Key
>,
43 typename _Allocator
= std::allocator
<_Key
> >
45 : public __gnu_debug::_Safe_container
<
46 set
<_Key
, _Compare
, _Allocator
>, _Allocator
,
47 __gnu_debug::_Safe_node_sequence
>,
48 public _GLIBCXX_STD_C::set
<_Key
,_Compare
,_Allocator
>
50 typedef _GLIBCXX_STD_C::set
<_Key
, _Compare
, _Allocator
> _Base
;
51 typedef __gnu_debug::_Safe_container
<
52 set
, _Allocator
, __gnu_debug::_Safe_node_sequence
> _Safe
;
54 typedef typename
_Base::const_iterator _Base_const_iterator
;
55 typedef typename
_Base::iterator _Base_iterator
;
56 typedef __gnu_debug::_Equal_to
<_Base_const_iterator
> _Equal
;
60 typedef _Key key_type
;
61 typedef _Key value_type
;
62 typedef _Compare key_compare
;
63 typedef _Compare value_compare
;
64 typedef _Allocator allocator_type
;
65 typedef typename
_Base::reference reference
;
66 typedef typename
_Base::const_reference const_reference
;
68 typedef __gnu_debug::_Safe_iterator
<_Base_iterator
, set
>
70 typedef __gnu_debug::_Safe_iterator
<_Base_const_iterator
, set
>
73 typedef typename
_Base::size_type size_type
;
74 typedef typename
_Base::difference_type difference_type
;
75 typedef typename
_Base::pointer pointer
;
76 typedef typename
_Base::const_pointer const_pointer
;
77 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
78 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
80 // 23.3.3.1 construct/copy/destroy:
82 #if __cplusplus < 201103L
91 set(const set
&) = default;
94 set(initializer_list
<value_type
> __l
,
95 const _Compare
& __comp
= _Compare(),
96 const allocator_type
& __a
= allocator_type())
97 : _Base(__l
, __comp
, __a
) { }
100 set(const allocator_type
& __a
)
103 set(const set
& __x
, const allocator_type
& __a
)
104 : _Base(__x
, __a
) { }
106 set(set
&& __x
, const allocator_type
& __a
)
107 : _Safe(std::move(__x
._M_safe()), __a
),
108 _Base(std::move(__x
._M_base()), __a
) { }
110 set(initializer_list
<value_type
> __l
, const allocator_type
& __a
)
111 : _Base(__l
, __a
) { }
113 template<typename _InputIterator
>
114 set(_InputIterator __first
, _InputIterator __last
,
115 const allocator_type
& __a
)
116 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
118 __gnu_debug::__base(__last
), __a
) { }
123 explicit set(const _Compare
& __comp
,
124 const _Allocator
& __a
= _Allocator())
125 : _Base(__comp
, __a
) { }
127 template<typename _InputIterator
>
128 set(_InputIterator __first
, _InputIterator __last
,
129 const _Compare
& __comp
= _Compare(),
130 const _Allocator
& __a
= _Allocator())
131 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
133 __gnu_debug::__base(__last
),
136 set(const _Base
& __x
)
139 #if __cplusplus < 201103L
141 operator=(const set
& __x
)
143 this->_M_safe() = __x
;
149 operator=(const set
&) = default;
152 operator=(set
&&) = default;
155 operator=(initializer_list
<value_type
> __l
)
158 this->_M_invalidate_all();
163 using _Base::get_allocator
;
167 begin() _GLIBCXX_NOEXCEPT
168 { return iterator(_Base::begin(), this); }
171 begin() const _GLIBCXX_NOEXCEPT
172 { return const_iterator(_Base::begin(), this); }
175 end() _GLIBCXX_NOEXCEPT
176 { return iterator(_Base::end(), this); }
179 end() const _GLIBCXX_NOEXCEPT
180 { return const_iterator(_Base::end(), this); }
183 rbegin() _GLIBCXX_NOEXCEPT
184 { return reverse_iterator(end()); }
186 const_reverse_iterator
187 rbegin() const _GLIBCXX_NOEXCEPT
188 { return const_reverse_iterator(end()); }
191 rend() _GLIBCXX_NOEXCEPT
192 { return reverse_iterator(begin()); }
194 const_reverse_iterator
195 rend() const _GLIBCXX_NOEXCEPT
196 { return const_reverse_iterator(begin()); }
198 #if __cplusplus >= 201103L
200 cbegin() const noexcept
201 { return const_iterator(_Base::begin(), this); }
204 cend() const noexcept
205 { return const_iterator(_Base::end(), this); }
207 const_reverse_iterator
208 crbegin() const noexcept
209 { return const_reverse_iterator(end()); }
211 const_reverse_iterator
212 crend() const noexcept
213 { return const_reverse_iterator(begin()); }
219 using _Base::max_size
;
222 #if __cplusplus >= 201103L
223 template<typename
... _Args
>
224 std::pair
<iterator
, bool>
225 emplace(_Args
&&... __args
)
227 auto __res
= _Base::emplace(std::forward
<_Args
>(__args
)...);
228 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
232 template<typename
... _Args
>
234 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
236 __glibcxx_check_insert(__pos
);
237 return iterator(_Base::emplace_hint(__pos
.base(),
238 std::forward
<_Args
>(__args
)...),
243 std::pair
<iterator
, bool>
244 insert(const value_type
& __x
)
246 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
247 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
251 #if __cplusplus >= 201103L
252 std::pair
<iterator
, bool>
253 insert(value_type
&& __x
)
255 std::pair
<_Base_iterator
, bool> __res
256 = _Base::insert(std::move(__x
));
257 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
263 insert(const_iterator __position
, const value_type
& __x
)
265 __glibcxx_check_insert(__position
);
266 return iterator(_Base::insert(__position
.base(), __x
), this);
269 #if __cplusplus >= 201103L
271 insert(const_iterator __position
, value_type
&& __x
)
273 __glibcxx_check_insert(__position
);
274 return iterator(_Base::insert(__position
.base(), std::move(__x
)),
279 template <typename _InputIterator
>
281 insert(_InputIterator __first
, _InputIterator __last
)
283 typename
__gnu_debug::_Distance_traits
<_InputIterator
>::__type __dist
;
284 __glibcxx_check_valid_range2(__first
, __last
, __dist
);
286 if (__dist
.second
>= __gnu_debug::__dp_sign
)
287 _Base::insert(__gnu_debug::__unsafe(__first
),
288 __gnu_debug::__unsafe(__last
));
290 _Base::insert(__first
, __last
);
293 #if __cplusplus >= 201103L
295 insert(initializer_list
<value_type
> __l
)
296 { _Base::insert(__l
); }
299 #if __cplusplus >= 201103L
301 erase(const_iterator __position
)
303 __glibcxx_check_erase(__position
);
304 this->_M_invalidate_if(_Equal(__position
.base()));
305 return iterator(_Base::erase(__position
.base()), this);
309 erase(iterator __position
)
311 __glibcxx_check_erase(__position
);
312 this->_M_invalidate_if(_Equal(__position
.base()));
313 _Base::erase(__position
.base());
318 erase(const key_type
& __x
)
320 _Base_iterator __victim
= _Base::find(__x
);
321 if (__victim
== _Base::end())
325 this->_M_invalidate_if(_Equal(__victim
));
326 _Base::erase(__victim
);
331 #if __cplusplus >= 201103L
333 erase(const_iterator __first
, const_iterator __last
)
335 // _GLIBCXX_RESOLVE_LIB_DEFECTS
336 // 151. can't currently clear() empty container
337 __glibcxx_check_erase_range(__first
, __last
);
338 for (_Base_const_iterator __victim
= __first
.base();
339 __victim
!= __last
.base(); ++__victim
)
341 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
342 _M_message(__gnu_debug::__msg_valid_range
)
343 ._M_iterator(__first
, "first")
344 ._M_iterator(__last
, "last"));
345 this->_M_invalidate_if(_Equal(__victim
));
347 return iterator(_Base::erase(__first
.base(), __last
.base()), this);
351 erase(iterator __first
, iterator __last
)
353 // _GLIBCXX_RESOLVE_LIB_DEFECTS
354 // 151. can't currently clear() empty container
355 __glibcxx_check_erase_range(__first
, __last
);
356 for (_Base_iterator __victim
= __first
.base();
357 __victim
!= __last
.base(); ++__victim
)
359 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
360 _M_message(__gnu_debug::__msg_valid_range
)
361 ._M_iterator(__first
, "first")
362 ._M_iterator(__last
, "last"));
363 this->_M_invalidate_if(_Equal(__victim
));
365 _Base::erase(__first
.base(), __last
.base());
371 _GLIBCXX_NOEXCEPT_IF( noexcept(declval
<_Base
&>().swap(__x
)) )
378 clear() _GLIBCXX_NOEXCEPT
380 this->_M_invalidate_all();
385 using _Base::key_comp
;
386 using _Base::value_comp
;
390 find(const key_type
& __x
)
391 { return iterator(_Base::find(__x
), this); }
393 // _GLIBCXX_RESOLVE_LIB_DEFECTS
394 // 214. set::find() missing const overload
396 find(const key_type
& __x
) const
397 { return const_iterator(_Base::find(__x
), this); }
399 #if __cplusplus > 201103L
400 template<typename _Kt
,
402 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
405 { return { _Base::find(__x
), this }; }
407 template<typename _Kt
,
409 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
411 find(const _Kt
& __x
) const
412 { return { _Base::find(__x
), this }; }
418 lower_bound(const key_type
& __x
)
419 { return iterator(_Base::lower_bound(__x
), this); }
421 // _GLIBCXX_RESOLVE_LIB_DEFECTS
422 // 214. set::find() missing const overload
424 lower_bound(const key_type
& __x
) const
425 { return const_iterator(_Base::lower_bound(__x
), this); }
427 #if __cplusplus > 201103L
428 template<typename _Kt
,
430 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
432 lower_bound(const _Kt
& __x
)
433 { return { _Base::lower_bound(__x
), this }; }
435 template<typename _Kt
,
437 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
439 lower_bound(const _Kt
& __x
) const
440 { return { _Base::lower_bound(__x
), this }; }
444 upper_bound(const key_type
& __x
)
445 { return iterator(_Base::upper_bound(__x
), this); }
447 // _GLIBCXX_RESOLVE_LIB_DEFECTS
448 // 214. set::find() missing const overload
450 upper_bound(const key_type
& __x
) const
451 { return const_iterator(_Base::upper_bound(__x
), this); }
453 #if __cplusplus > 201103L
454 template<typename _Kt
,
456 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
458 upper_bound(const _Kt
& __x
)
459 { return { _Base::upper_bound(__x
), this }; }
461 template<typename _Kt
,
463 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
465 upper_bound(const _Kt
& __x
) const
466 { return { _Base::upper_bound(__x
), this }; }
469 std::pair
<iterator
, iterator
>
470 equal_range(const key_type
& __x
)
472 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
473 _Base::equal_range(__x
);
474 return std::make_pair(iterator(__res
.first
, this),
475 iterator(__res
.second
, this));
478 // _GLIBCXX_RESOLVE_LIB_DEFECTS
479 // 214. set::find() missing const overload
480 std::pair
<const_iterator
, const_iterator
>
481 equal_range(const key_type
& __x
) const
483 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
484 _Base::equal_range(__x
);
485 return std::make_pair(const_iterator(__res
.first
, this),
486 const_iterator(__res
.second
, this));
489 #if __cplusplus > 201103L
490 template<typename _Kt
,
492 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
493 std::pair
<iterator
, iterator
>
494 equal_range(const _Kt
& __x
)
496 auto __res
= _Base::equal_range(__x
);
497 return { { __res
.first
, this }, { __res
.second
, this } };
500 template<typename _Kt
,
502 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
503 std::pair
<const_iterator
, const_iterator
>
504 equal_range(const _Kt
& __x
) const
506 auto __res
= _Base::equal_range(__x
);
507 return { { __res
.first
, this }, { __res
.second
, this } };
512 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
515 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
518 template<typename _Key
, typename _Compare
, typename _Allocator
>
520 operator==(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
521 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
522 { return __lhs
._M_base() == __rhs
._M_base(); }
524 template<typename _Key
, typename _Compare
, typename _Allocator
>
526 operator!=(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
527 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
528 { return __lhs
._M_base() != __rhs
._M_base(); }
530 template<typename _Key
, typename _Compare
, typename _Allocator
>
532 operator<(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
533 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
534 { return __lhs
._M_base() < __rhs
._M_base(); }
536 template<typename _Key
, typename _Compare
, typename _Allocator
>
538 operator<=(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
539 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
540 { return __lhs
._M_base() <= __rhs
._M_base(); }
542 template<typename _Key
, typename _Compare
, typename _Allocator
>
544 operator>=(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
545 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
546 { return __lhs
._M_base() >= __rhs
._M_base(); }
548 template<typename _Key
, typename _Compare
, typename _Allocator
>
550 operator>(const set
<_Key
, _Compare
, _Allocator
>& __lhs
,
551 const set
<_Key
, _Compare
, _Allocator
>& __rhs
)
552 { return __lhs
._M_base() > __rhs
._M_base(); }
554 template<typename _Key
, typename _Compare
, typename _Allocator
>
556 swap(set
<_Key
, _Compare
, _Allocator
>& __x
,
557 set
<_Key
, _Compare
, _Allocator
>& __y
)
558 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
559 { return __x
.swap(__y
); }
561 } // namespace __debug