1 // Debugging map implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011
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>
37 namespace std
_GLIBCXX_VISIBILITY(default)
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_C::map
<_Key
, _Tp
, _Compare
, _Allocator
>,
46 public __gnu_debug::_Safe_sequence
<map
<_Key
, _Tp
, _Compare
, _Allocator
> >
48 typedef _GLIBCXX_STD_C::map
<_Key
, _Tp
, _Compare
, _Allocator
> _Base
;
49 typedef __gnu_debug::_Safe_sequence
<map
> _Safe_base
;
51 typedef typename
_Base::const_iterator _Base_const_iterator
;
52 typedef typename
_Base::iterator _Base_iterator
;
53 typedef __gnu_debug::_Equal_to
<_Base_const_iterator
> _Equal
;
56 typedef _Key key_type
;
57 typedef _Tp mapped_type
;
58 typedef std::pair
<const _Key
, _Tp
> value_type
;
59 typedef _Compare key_compare
;
60 typedef _Allocator allocator_type
;
61 typedef typename
_Base::reference reference
;
62 typedef typename
_Base::const_reference const_reference
;
64 typedef __gnu_debug::_Safe_iterator
<typename
_Base::iterator
, map
>
66 typedef __gnu_debug::_Safe_iterator
<typename
_Base::const_iterator
, map
>
69 typedef typename
_Base::size_type size_type
;
70 typedef typename
_Base::difference_type difference_type
;
71 typedef typename
_Base::pointer pointer
;
72 typedef typename
_Base::const_pointer const_pointer
;
73 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
74 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
76 // 23.3.1.1 construct/copy/destroy:
77 explicit map(const _Compare
& __comp
= _Compare(),
78 const _Allocator
& __a
= _Allocator())
79 : _Base(__comp
, __a
) { }
81 template<typename _InputIterator
>
82 map(_InputIterator __first
, _InputIterator __last
,
83 const _Compare
& __comp
= _Compare(),
84 const _Allocator
& __a
= _Allocator())
85 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
87 __gnu_debug::__base(__last
),
88 __comp
, __a
), _Safe_base() { }
91 : _Base(__x
), _Safe_base() { }
94 : _Base(__x
), _Safe_base() { }
96 #ifdef __GXX_EXPERIMENTAL_CXX0X__
98 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
)
99 : _Base(std::move(__x
)), _Safe_base()
100 { this->_M_swap(__x
); }
102 map(initializer_list
<value_type
> __l
,
103 const _Compare
& __c
= _Compare(),
104 const allocator_type
& __a
= allocator_type())
105 : _Base(__l
, __c
, __a
), _Safe_base() { }
108 ~map() _GLIBCXX_NOEXCEPT
{ }
111 operator=(const map
& __x
)
113 *static_cast<_Base
*>(this) = __x
;
114 this->_M_invalidate_all();
118 #ifdef __GXX_EXPERIMENTAL_CXX0X__
130 operator=(initializer_list
<value_type
> __l
)
138 // _GLIBCXX_RESOLVE_LIB_DEFECTS
139 // 133. map missing get_allocator()
140 using _Base::get_allocator
;
144 begin() _GLIBCXX_NOEXCEPT
145 { return iterator(_Base::begin(), this); }
148 begin() const _GLIBCXX_NOEXCEPT
149 { return const_iterator(_Base::begin(), this); }
152 end() _GLIBCXX_NOEXCEPT
153 { return iterator(_Base::end(), this); }
156 end() const _GLIBCXX_NOEXCEPT
157 { return const_iterator(_Base::end(), this); }
160 rbegin() _GLIBCXX_NOEXCEPT
161 { return reverse_iterator(end()); }
163 const_reverse_iterator
164 rbegin() const _GLIBCXX_NOEXCEPT
165 { return const_reverse_iterator(end()); }
168 rend() _GLIBCXX_NOEXCEPT
169 { return reverse_iterator(begin()); }
171 const_reverse_iterator
172 rend() const _GLIBCXX_NOEXCEPT
173 { return const_reverse_iterator(begin()); }
175 #ifdef __GXX_EXPERIMENTAL_CXX0X__
177 cbegin() const noexcept
178 { return const_iterator(_Base::begin(), this); }
181 cend() const noexcept
182 { return const_iterator(_Base::end(), this); }
184 const_reverse_iterator
185 crbegin() const noexcept
186 { return const_reverse_iterator(end()); }
188 const_reverse_iterator
189 crend() const noexcept
190 { return const_reverse_iterator(begin()); }
196 using _Base::max_size
;
198 // 23.3.1.2 element access:
199 using _Base::operator[];
201 // _GLIBCXX_RESOLVE_LIB_DEFECTS
202 // DR 464. Suggestion for new member functions in standard containers.
206 std::pair
<iterator
, bool>
207 insert(const value_type
& __x
)
209 typedef typename
_Base::iterator _Base_iterator
;
210 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
211 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
215 #ifdef __GXX_EXPERIMENTAL_CXX0X__
216 template<typename _Pair
, typename
= typename
217 std::enable_if
<std::is_convertible
<_Pair
,
218 value_type
>::value
>::type
>
219 std::pair
<iterator
, bool>
222 typedef typename
_Base::iterator _Base_iterator
;
223 std::pair
<_Base_iterator
, bool> __res
224 = _Base::insert(std::forward
<_Pair
>(__x
));
225 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
230 #ifdef __GXX_EXPERIMENTAL_CXX0X__
232 insert(std::initializer_list
<value_type
> __list
)
233 { _Base::insert(__list
); }
237 #ifdef __GXX_EXPERIMENTAL_CXX0X__
238 insert(const_iterator __position
, const value_type
& __x
)
240 insert(iterator __position
, const value_type
& __x
)
243 __glibcxx_check_insert(__position
);
244 return iterator(_Base::insert(__position
.base(), __x
), this);
247 #ifdef __GXX_EXPERIMENTAL_CXX0X__
248 template<typename _Pair
, typename
= typename
249 std::enable_if
<std::is_convertible
<_Pair
,
250 value_type
>::value
>::type
>
252 insert(const_iterator __position
, _Pair
&& __x
)
254 __glibcxx_check_insert(__position
);
255 return iterator(_Base::insert(__position
.base(),
256 std::forward
<_Pair
>(__x
)), this);
260 template<typename _InputIterator
>
262 insert(_InputIterator __first
, _InputIterator __last
)
264 __glibcxx_check_valid_range(__first
, __last
);
265 _Base::insert(__gnu_debug::__base(__first
),
266 __gnu_debug::__base(__last
));
269 #ifdef __GXX_EXPERIMENTAL_CXX0X__
271 erase(const_iterator __position
)
273 __glibcxx_check_erase(__position
);
274 this->_M_invalidate_if(_Equal(__position
.base()));
275 return iterator(_Base::erase(__position
.base()), this);
279 erase(iterator __position
)
281 __glibcxx_check_erase(__position
);
282 this->_M_invalidate_if(_Equal(__position
.base()));
283 _Base::erase(__position
.base());
288 erase(const key_type
& __x
)
290 _Base_iterator __victim
= _Base::find(__x
);
291 if (__victim
== _Base::end())
295 this->_M_invalidate_if(_Equal(__victim
));
296 _Base::erase(__victim
);
301 #ifdef __GXX_EXPERIMENTAL_CXX0X__
303 erase(const_iterator __first
, const_iterator __last
)
305 // _GLIBCXX_RESOLVE_LIB_DEFECTS
306 // 151. can't currently clear() empty container
307 __glibcxx_check_erase_range(__first
, __last
);
308 for (_Base_const_iterator __victim
= __first
.base();
309 __victim
!= __last
.base(); ++__victim
)
311 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
312 _M_message(__gnu_debug::__msg_valid_range
)
313 ._M_iterator(__first
, "first")
314 ._M_iterator(__last
, "last"));
315 this->_M_invalidate_if(_Equal(__victim
));
317 return iterator(_Base::erase(__first
.base(), __last
.base()), this);
321 erase(iterator __first
, iterator __last
)
323 // _GLIBCXX_RESOLVE_LIB_DEFECTS
324 // 151. can't currently clear() empty container
325 __glibcxx_check_erase_range(__first
, __last
);
326 for (_Base_iterator __victim
= __first
.base();
327 __victim
!= __last
.base(); ++__victim
)
329 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
330 _M_message(__gnu_debug::__msg_valid_range
)
331 ._M_iterator(__first
, "first")
332 ._M_iterator(__last
, "last"));
333 this->_M_invalidate_if(_Equal(__victim
));
335 _Base::erase(__first
.base(), __last
.base());
347 clear() _GLIBCXX_NOEXCEPT
349 this->_M_invalidate_all();
354 using _Base::key_comp
;
355 using _Base::value_comp
;
357 // 23.3.1.3 map operations:
359 find(const key_type
& __x
)
360 { return iterator(_Base::find(__x
), this); }
363 find(const key_type
& __x
) const
364 { return const_iterator(_Base::find(__x
), this); }
369 lower_bound(const key_type
& __x
)
370 { return iterator(_Base::lower_bound(__x
), this); }
373 lower_bound(const key_type
& __x
) const
374 { return const_iterator(_Base::lower_bound(__x
), this); }
377 upper_bound(const key_type
& __x
)
378 { return iterator(_Base::upper_bound(__x
), this); }
381 upper_bound(const key_type
& __x
) const
382 { return const_iterator(_Base::upper_bound(__x
), this); }
384 std::pair
<iterator
,iterator
>
385 equal_range(const key_type
& __x
)
387 typedef typename
_Base::iterator _Base_iterator
;
388 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
389 _Base::equal_range(__x
);
390 return std::make_pair(iterator(__res
.first
, this),
391 iterator(__res
.second
, this));
394 std::pair
<const_iterator
,const_iterator
>
395 equal_range(const key_type
& __x
) const
397 typedef typename
_Base::const_iterator _Base_const_iterator
;
398 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
399 _Base::equal_range(__x
);
400 return std::make_pair(const_iterator(__res
.first
, this),
401 const_iterator(__res
.second
, this));
405 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
408 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
414 typedef typename
_Base::const_iterator _Base_const_iterator
;
415 typedef __gnu_debug::_Not_equal_to
<_Base_const_iterator
> _Not_equal
;
416 this->_M_invalidate_if(_Not_equal(_M_base().end()));
420 template<typename _Key
, typename _Tp
,
421 typename _Compare
, typename _Allocator
>
423 operator==(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
424 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
425 { return __lhs
._M_base() == __rhs
._M_base(); }
427 template<typename _Key
, typename _Tp
,
428 typename _Compare
, typename _Allocator
>
430 operator!=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
431 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
432 { return __lhs
._M_base() != __rhs
._M_base(); }
434 template<typename _Key
, typename _Tp
,
435 typename _Compare
, typename _Allocator
>
437 operator<(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
438 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
439 { return __lhs
._M_base() < __rhs
._M_base(); }
441 template<typename _Key
, typename _Tp
,
442 typename _Compare
, typename _Allocator
>
444 operator<=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
445 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
446 { return __lhs
._M_base() <= __rhs
._M_base(); }
448 template<typename _Key
, typename _Tp
,
449 typename _Compare
, typename _Allocator
>
451 operator>=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
452 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
453 { return __lhs
._M_base() >= __rhs
._M_base(); }
455 template<typename _Key
, typename _Tp
,
456 typename _Compare
, typename _Allocator
>
458 operator>(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
459 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
460 { return __lhs
._M_base() > __rhs
._M_base(); }
462 template<typename _Key
, typename _Tp
,
463 typename _Compare
, typename _Allocator
>
465 swap(map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
466 map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
467 { __lhs
.swap(__rhs
); }
469 } // namespace __debug