1 // Debugging set implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 * This file is a GNU debug extension to the Standard C++ Library.
35 #ifndef _GLIBCXX_DEBUG_SET_H
36 #define _GLIBCXX_DEBUG_SET_H 1
38 #include <debug/safe_sequence.h>
39 #include <debug/safe_iterator.h>
46 template<typename _Key
, typename _Compare
= std::less
<_Key
>,
47 typename _Allocator
= std::allocator
<_Key
> >
49 : public _GLIBCXX_STD_D::set
<_Key
,_Compare
,_Allocator
>,
50 public __gnu_debug::_Safe_sequence
<set
<_Key
, _Compare
, _Allocator
> >
52 typedef _GLIBCXX_STD_D::set
<_Key
,_Compare
,_Allocator
> _Base
;
53 typedef __gnu_debug::_Safe_sequence
<set
> _Safe_base
;
57 typedef _Key key_type
;
58 typedef _Key value_type
;
59 typedef _Compare key_compare
;
60 typedef _Compare value_compare
;
61 typedef _Allocator allocator_type
;
62 typedef typename
_Base::reference reference
;
63 typedef typename
_Base::const_reference const_reference
;
65 typedef __gnu_debug::_Safe_iterator
<typename
_Base::iterator
, set
>
67 typedef __gnu_debug::_Safe_iterator
<typename
_Base::const_iterator
, set
>
70 typedef typename
_Base::size_type size_type
;
71 typedef typename
_Base::difference_type difference_type
;
72 typedef typename
_Base::pointer pointer
;
73 typedef typename
_Base::const_pointer const_pointer
;
74 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
75 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
77 // 23.3.3.1 construct/copy/destroy:
78 explicit set(const _Compare
& __comp
= _Compare(),
79 const _Allocator
& __a
= _Allocator())
80 : _Base(__comp
, __a
) { }
82 template<typename _InputIterator
>
83 set(_InputIterator __first
, _InputIterator __last
,
84 const _Compare
& __comp
= _Compare(),
85 const _Allocator
& __a
= _Allocator())
86 : _Base(__gnu_debug::__check_valid_range(__first
, __last
), __last
,
89 set(const set
<_Key
,_Compare
,_Allocator
>& __x
)
90 : _Base(__x
), _Safe_base() { }
92 set(const _Base
& __x
) : _Base(__x
), _Safe_base() { }
96 set
<_Key
,_Compare
,_Allocator
>&
97 operator=(const set
<_Key
,_Compare
,_Allocator
>& __x
)
99 *static_cast<_Base
*>(this) = __x
;
100 this->_M_invalidate_all();
104 using _Base::get_allocator
;
109 { return iterator(_Base::begin(), this); }
113 { return const_iterator(_Base::begin(), this); }
117 { return iterator(_Base::end(), this); }
121 { return const_iterator(_Base::end(), this); }
125 { return reverse_iterator(end()); }
127 const_reverse_iterator
129 { return const_reverse_iterator(end()); }
133 { return reverse_iterator(begin()); }
135 const_reverse_iterator
137 { return const_reverse_iterator(begin()); }
142 using _Base::max_size
;
145 std::pair
<iterator
, bool>
146 insert(const value_type
& __x
)
148 typedef typename
_Base::iterator _Base_iterator
;
149 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
150 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
155 insert(iterator __position
, const value_type
& __x
)
157 __glibcxx_check_insert(__position
);
158 return iterator(_Base::insert(__position
.base(), __x
), this);
161 template <typename _InputIterator
>
163 insert(_InputIterator __first
, _InputIterator __last
)
165 __glibcxx_check_valid_range(__first
, __last
);
166 _Base::insert(__first
, __last
);
170 erase(iterator __position
)
172 __glibcxx_check_erase(__position
);
173 __position
._M_invalidate();
174 _Base::erase(__position
.base());
178 erase(const key_type
& __x
)
180 iterator __victim
= find(__x
);
181 if (__victim
== end())
185 __victim
._M_invalidate();
186 _Base::erase(__victim
.base());
192 erase(iterator __first
, iterator __last
)
194 // _GLIBCXX_RESOLVE_LIB_DEFECTS
195 // 151. can't currently clear() empty container
196 __glibcxx_check_erase_range(__first
, __last
);
198 while (__first
!= __last
)
199 this->erase(__first
++);
203 swap(set
<_Key
,_Compare
,_Allocator
>& __x
)
211 { this->erase(begin(), end()); }
214 using _Base::key_comp
;
215 using _Base::value_comp
;
219 find(const key_type
& __x
)
220 { return iterator(_Base::find(__x
), this); }
222 // _GLIBCXX_RESOLVE_LIB_DEFECTS
223 // 214. set::find() missing const overload
225 find(const key_type
& __x
) const
226 { return const_iterator(_Base::find(__x
), this); }
231 lower_bound(const key_type
& __x
)
232 { return iterator(_Base::lower_bound(__x
), this); }
234 // _GLIBCXX_RESOLVE_LIB_DEFECTS
235 // 214. set::find() missing const overload
237 lower_bound(const key_type
& __x
) const
238 { return const_iterator(_Base::lower_bound(__x
), this); }
241 upper_bound(const key_type
& __x
)
242 { return iterator(_Base::upper_bound(__x
), this); }
244 // _GLIBCXX_RESOLVE_LIB_DEFECTS
245 // 214. set::find() missing const overload
247 upper_bound(const key_type
& __x
) const
248 { return const_iterator(_Base::upper_bound(__x
), this); }
250 std::pair
<iterator
,iterator
>
251 equal_range(const key_type
& __x
)
253 typedef typename
_Base::iterator _Base_iterator
;
254 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
255 _Base::equal_range(__x
);
256 return std::make_pair(iterator(__res
.first
, this),
257 iterator(__res
.second
, this));
260 // _GLIBCXX_RESOLVE_LIB_DEFECTS
261 // 214. set::find() missing const overload
262 std::pair
<const_iterator
,const_iterator
>
263 equal_range(const key_type
& __x
) const
265 typedef typename
_Base::const_iterator _Base_iterator
;
266 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
267 _Base::equal_range(__x
);
268 return std::make_pair(const_iterator(__res
.first
, this),
269 const_iterator(__res
.second
, this));
273 _M_base() { return *this; }
276 _M_base() const { return *this; }
282 typedef typename
_Base::const_iterator _Base_const_iterator
;
283 typedef __gnu_debug::_Not_equal_to
<_Base_const_iterator
> _Not_equal
;
284 this->_M_invalidate_if(_Not_equal(_M_base().end()));
288 template<typename _Key
, typename _Compare
, typename _Allocator
>
290 operator==(const set
<_Key
,_Compare
,_Allocator
>& __lhs
,
291 const set
<_Key
,_Compare
,_Allocator
>& __rhs
)
292 { return __lhs
._M_base() == __rhs
._M_base(); }
294 template<typename _Key
, typename _Compare
, typename _Allocator
>
296 operator!=(const set
<_Key
,_Compare
,_Allocator
>& __lhs
,
297 const set
<_Key
,_Compare
,_Allocator
>& __rhs
)
298 { return __lhs
._M_base() != __rhs
._M_base(); }
300 template<typename _Key
, typename _Compare
, typename _Allocator
>
302 operator<(const set
<_Key
,_Compare
,_Allocator
>& __lhs
,
303 const set
<_Key
,_Compare
,_Allocator
>& __rhs
)
304 { return __lhs
._M_base() < __rhs
._M_base(); }
306 template<typename _Key
, typename _Compare
, typename _Allocator
>
308 operator<=(const set
<_Key
,_Compare
,_Allocator
>& __lhs
,
309 const set
<_Key
,_Compare
,_Allocator
>& __rhs
)
310 { return __lhs
._M_base() <= __rhs
._M_base(); }
312 template<typename _Key
, typename _Compare
, typename _Allocator
>
314 operator>=(const set
<_Key
,_Compare
,_Allocator
>& __lhs
,
315 const set
<_Key
,_Compare
,_Allocator
>& __rhs
)
316 { return __lhs
._M_base() >= __rhs
._M_base(); }
318 template<typename _Key
, typename _Compare
, typename _Allocator
>
320 operator>(const set
<_Key
,_Compare
,_Allocator
>& __lhs
,
321 const set
<_Key
,_Compare
,_Allocator
>& __rhs
)
322 { return __lhs
._M_base() > __rhs
._M_base(); }
324 template<typename _Key
, typename _Compare
, typename _Allocator
>
326 swap(set
<_Key
,_Compare
,_Allocator
>& __x
,
327 set
<_Key
,_Compare
,_Allocator
>& __y
)
328 { return __x
.swap(__y
); }
329 } // namespace __debug