1 // Debugging map implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009
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 3, 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 // 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/>.
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_MAP_H
31 #define _GLIBCXX_DEBUG_MAP_H 1
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
41 /// Class std::map with safety/checking/debug instrumentation.
42 template<typename _Key
, typename _Tp
, typename _Compare
= std::less
<_Key
>,
43 typename _Allocator
= std::allocator
<std::pair
<const _Key
, _Tp
> > >
45 : public _GLIBCXX_STD_D::map
<_Key
, _Tp
, _Compare
, _Allocator
>,
46 public __gnu_debug::_Safe_sequence
<map
<_Key
, _Tp
, _Compare
, _Allocator
> >
48 typedef _GLIBCXX_STD_D::map
<_Key
, _Tp
, _Compare
, _Allocator
> _Base
;
49 typedef __gnu_debug::_Safe_sequence
<map
> _Safe_base
;
53 typedef _Key key_type
;
54 typedef _Tp mapped_type
;
55 typedef std::pair
<const _Key
, _Tp
> value_type
;
56 typedef _Compare key_compare
;
57 typedef _Allocator allocator_type
;
58 typedef typename
_Base::reference reference
;
59 typedef typename
_Base::const_reference const_reference
;
61 typedef __gnu_debug::_Safe_iterator
<typename
_Base::iterator
, map
>
63 typedef __gnu_debug::_Safe_iterator
<typename
_Base::const_iterator
, map
>
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
;
73 using _Base::value_compare
;
75 // 23.3.1.1 construct/copy/destroy:
76 explicit map(const _Compare
& __comp
= _Compare(),
77 const _Allocator
& __a
= _Allocator())
78 : _Base(__comp
, __a
) { }
80 template<typename _InputIterator
>
81 map(_InputIterator __first
, _InputIterator __last
,
82 const _Compare
& __comp
= _Compare(),
83 const _Allocator
& __a
= _Allocator())
84 : _Base(__gnu_debug::__check_valid_range(__first
, __last
), __last
,
85 __comp
, __a
), _Safe_base() { }
88 : _Base(__x
), _Safe_base() { }
91 : _Base(__x
), _Safe_base() { }
93 #ifdef __GXX_EXPERIMENTAL_CXX0X__
95 : _Base(std::forward
<map
>(__x
)), _Safe_base()
96 { this->_M_swap(__x
); }
98 map(initializer_list
<value_type
> __l
,
99 const _Compare
& __c
= _Compare(),
100 const allocator_type
& __a
= allocator_type())
101 : _Base(__l
, __c
, __a
), _Safe_base() { }
107 operator=(const map
& __x
)
109 *static_cast<_Base
*>(this) = __x
;
110 this->_M_invalidate_all();
114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
125 operator=(initializer_list
<value_type
> __l
)
133 // _GLIBCXX_RESOLVE_LIB_DEFECTS
134 // 133. map missing get_allocator()
135 using _Base::get_allocator
;
140 { return iterator(_Base::begin(), this); }
144 { return const_iterator(_Base::begin(), this); }
148 { return iterator(_Base::end(), this); }
152 { return const_iterator(_Base::end(), this); }
156 { return reverse_iterator(end()); }
158 const_reverse_iterator
160 { return const_reverse_iterator(end()); }
164 { return reverse_iterator(begin()); }
166 const_reverse_iterator
168 { return const_reverse_iterator(begin()); }
170 #ifdef __GXX_EXPERIMENTAL_CXX0X__
173 { return const_iterator(_Base::begin(), this); }
177 { return const_iterator(_Base::end(), this); }
179 const_reverse_iterator
181 { return const_reverse_iterator(end()); }
183 const_reverse_iterator
185 { return const_reverse_iterator(begin()); }
191 using _Base::max_size
;
193 // 23.3.1.2 element access:
194 using _Base::operator[];
196 // _GLIBCXX_RESOLVE_LIB_DEFECTS
197 // DR 464. Suggestion for new member functions in standard containers.
201 std::pair
<iterator
, bool>
202 insert(const value_type
& __x
)
204 typedef typename
_Base::iterator _Base_iterator
;
205 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
206 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
210 #ifdef __GXX_EXPERIMENTAL_CXX0X__
212 insert(std::initializer_list
<value_type
> __list
)
213 { _Base::insert(__list
); }
217 insert(iterator __position
, const value_type
& __x
)
219 __glibcxx_check_insert(__position
);
220 return iterator(_Base::insert(__position
.base(), __x
), this);
223 template<typename _InputIterator
>
225 insert(_InputIterator __first
, _InputIterator __last
)
227 __glibcxx_check_valid_range(__first
, __last
);
228 _Base::insert(__first
, __last
);
232 erase(iterator __position
)
234 __glibcxx_check_erase(__position
);
235 __position
._M_invalidate();
236 _Base::erase(__position
.base());
240 erase(const key_type
& __x
)
242 iterator __victim
= find(__x
);
243 if (__victim
== end())
247 __victim
._M_invalidate();
248 _Base::erase(__victim
.base());
254 erase(iterator __first
, iterator __last
)
256 // _GLIBCXX_RESOLVE_LIB_DEFECTS
257 // 151. can't currently clear() empty container
258 __glibcxx_check_erase_range(__first
, __last
);
259 while (__first
!= __last
)
260 this->erase(__first
++);
272 { this->erase(begin(), end()); }
275 using _Base::key_comp
;
276 using _Base::value_comp
;
278 // 23.3.1.3 map operations:
280 find(const key_type
& __x
)
281 { return iterator(_Base::find(__x
), this); }
284 find(const key_type
& __x
) const
285 { return const_iterator(_Base::find(__x
), this); }
290 lower_bound(const key_type
& __x
)
291 { return iterator(_Base::lower_bound(__x
), this); }
294 lower_bound(const key_type
& __x
) const
295 { return const_iterator(_Base::lower_bound(__x
), this); }
298 upper_bound(const key_type
& __x
)
299 { return iterator(_Base::upper_bound(__x
), this); }
302 upper_bound(const key_type
& __x
) const
303 { return const_iterator(_Base::upper_bound(__x
), this); }
305 std::pair
<iterator
,iterator
>
306 equal_range(const key_type
& __x
)
308 typedef typename
_Base::iterator _Base_iterator
;
309 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
310 _Base::equal_range(__x
);
311 return std::make_pair(iterator(__res
.first
, this),
312 iterator(__res
.second
, this));
315 std::pair
<const_iterator
,const_iterator
>
316 equal_range(const key_type
& __x
) const
318 typedef typename
_Base::const_iterator _Base_const_iterator
;
319 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
320 _Base::equal_range(__x
);
321 return std::make_pair(const_iterator(__res
.first
, this),
322 const_iterator(__res
.second
, this));
326 _M_base() { return *this; }
329 _M_base() const { return *this; }
335 typedef typename
_Base::const_iterator _Base_const_iterator
;
336 typedef __gnu_debug::_Not_equal_to
<_Base_const_iterator
> _Not_equal
;
337 this->_M_invalidate_if(_Not_equal(_M_base().end()));
341 template<typename _Key
, typename _Tp
,
342 typename _Compare
, typename _Allocator
>
344 operator==(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
345 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
346 { return __lhs
._M_base() == __rhs
._M_base(); }
348 template<typename _Key
, typename _Tp
,
349 typename _Compare
, typename _Allocator
>
351 operator!=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
352 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
353 { return __lhs
._M_base() != __rhs
._M_base(); }
355 template<typename _Key
, typename _Tp
,
356 typename _Compare
, typename _Allocator
>
358 operator<(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
359 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
360 { return __lhs
._M_base() < __rhs
._M_base(); }
362 template<typename _Key
, typename _Tp
,
363 typename _Compare
, typename _Allocator
>
365 operator<=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
366 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
367 { return __lhs
._M_base() <= __rhs
._M_base(); }
369 template<typename _Key
, typename _Tp
,
370 typename _Compare
, typename _Allocator
>
372 operator>=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
373 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
374 { return __lhs
._M_base() >= __rhs
._M_base(); }
376 template<typename _Key
, typename _Tp
,
377 typename _Compare
, typename _Allocator
>
379 operator>(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
380 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
381 { return __lhs
._M_base() > __rhs
._M_base(); }
383 template<typename _Key
, typename _Tp
,
384 typename _Compare
, typename _Allocator
>
386 swap(map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
387 map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
388 { __lhs
.swap(__rhs
); }
390 } // namespace __debug