1 // Debugging map 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_MAP_H
30 #define _GLIBCXX_DEBUG_MAP_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::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 __gnu_debug::_Safe_container
<
46 map
<_Key
, _Tp
, _Compare
, _Allocator
>, _Allocator
,
47 __gnu_debug::_Safe_node_sequence
>,
48 public _GLIBCXX_STD_C::map
<_Key
, _Tp
, _Compare
, _Allocator
>
50 typedef _GLIBCXX_STD_C::map
<
51 _Key
, _Tp
, _Compare
, _Allocator
> _Base
;
52 typedef __gnu_debug::_Safe_container
<
53 map
, _Allocator
, __gnu_debug::_Safe_node_sequence
> _Safe
;
55 typedef typename
_Base::const_iterator _Base_const_iterator
;
56 typedef typename
_Base::iterator _Base_iterator
;
57 typedef __gnu_debug::_Equal_to
<_Base_const_iterator
> _Equal
;
61 typedef _Key key_type
;
62 typedef _Tp mapped_type
;
63 typedef std::pair
<const _Key
, _Tp
> value_type
;
64 typedef _Compare key_compare
;
65 typedef _Allocator allocator_type
;
66 typedef typename
_Base::reference reference
;
67 typedef typename
_Base::const_reference const_reference
;
69 typedef __gnu_debug::_Safe_iterator
<_Base_iterator
, map
>
71 typedef __gnu_debug::_Safe_iterator
<_Base_const_iterator
, map
>
74 typedef typename
_Base::size_type size_type
;
75 typedef typename
_Base::difference_type difference_type
;
76 typedef typename
_Base::pointer pointer
;
77 typedef typename
_Base::const_pointer const_pointer
;
78 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
79 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
81 // 23.3.1.1 construct/copy/destroy:
83 #if __cplusplus < 201103L
92 map(const map
&) = default;
95 map(initializer_list
<value_type
> __l
,
96 const _Compare
& __c
= _Compare(),
97 const allocator_type
& __a
= allocator_type())
98 : _Base(__l
, __c
, __a
) { }
101 map(const allocator_type
& __a
)
104 map(const map
& __m
, const allocator_type
& __a
)
105 : _Base(__m
, __a
) { }
107 map(map
&& __m
, const allocator_type
& __a
)
108 : _Safe(std::move(__m
._M_safe()), __a
),
109 _Base(std::move(__m
._M_base()), __a
) { }
111 map(initializer_list
<value_type
> __l
, const allocator_type
& __a
)
112 : _Base(__l
, __a
) { }
114 template<typename _InputIterator
>
115 map(_InputIterator __first
, _InputIterator __last
,
116 const allocator_type
& __a
)
117 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
119 __gnu_debug::__base(__last
), __a
)
125 map(const _Base
& __x
)
128 explicit map(const _Compare
& __comp
,
129 const _Allocator
& __a
= _Allocator())
130 : _Base(__comp
, __a
) { }
132 template<typename _InputIterator
>
133 map(_InputIterator __first
, _InputIterator __last
,
134 const _Compare
& __comp
= _Compare(),
135 const _Allocator
& __a
= _Allocator())
136 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
138 __gnu_debug::__base(__last
),
141 #if __cplusplus < 201103L
143 operator=(const map
& __x
)
145 this->_M_safe() = __x
;
151 operator=(const map
&) = default;
154 operator=(map
&&) = default;
157 operator=(initializer_list
<value_type
> __l
)
160 this->_M_invalidate_all();
165 // _GLIBCXX_RESOLVE_LIB_DEFECTS
166 // 133. map missing get_allocator()
167 using _Base::get_allocator
;
171 begin() _GLIBCXX_NOEXCEPT
172 { return iterator(_Base::begin(), this); }
175 begin() const _GLIBCXX_NOEXCEPT
176 { return const_iterator(_Base::begin(), this); }
179 end() _GLIBCXX_NOEXCEPT
180 { return iterator(_Base::end(), this); }
183 end() const _GLIBCXX_NOEXCEPT
184 { return const_iterator(_Base::end(), this); }
187 rbegin() _GLIBCXX_NOEXCEPT
188 { return reverse_iterator(end()); }
190 const_reverse_iterator
191 rbegin() const _GLIBCXX_NOEXCEPT
192 { return const_reverse_iterator(end()); }
195 rend() _GLIBCXX_NOEXCEPT
196 { return reverse_iterator(begin()); }
198 const_reverse_iterator
199 rend() const _GLIBCXX_NOEXCEPT
200 { return const_reverse_iterator(begin()); }
202 #if __cplusplus >= 201103L
204 cbegin() const noexcept
205 { return const_iterator(_Base::begin(), this); }
208 cend() const noexcept
209 { return const_iterator(_Base::end(), this); }
211 const_reverse_iterator
212 crbegin() const noexcept
213 { return const_reverse_iterator(end()); }
215 const_reverse_iterator
216 crend() const noexcept
217 { return const_reverse_iterator(begin()); }
223 using _Base::max_size
;
225 // 23.3.1.2 element access:
226 using _Base::operator[];
228 // _GLIBCXX_RESOLVE_LIB_DEFECTS
229 // DR 464. Suggestion for new member functions in standard containers.
233 #if __cplusplus >= 201103L
234 template<typename
... _Args
>
235 std::pair
<iterator
, bool>
236 emplace(_Args
&&... __args
)
238 auto __res
= _Base::emplace(std::forward
<_Args
>(__args
)...);
239 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
243 template<typename
... _Args
>
245 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
247 __glibcxx_check_insert(__pos
);
248 return iterator(_Base::emplace_hint(__pos
.base(),
249 std::forward
<_Args
>(__args
)...),
254 std::pair
<iterator
, bool>
255 insert(const value_type
& __x
)
257 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
258 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
262 #if __cplusplus >= 201103L
263 template<typename _Pair
, typename
= typename
264 std::enable_if
<std::is_constructible
<value_type
,
265 _Pair
&&>::value
>::type
>
266 std::pair
<iterator
, bool>
269 std::pair
<_Base_iterator
, bool> __res
270 = _Base::insert(std::forward
<_Pair
>(__x
));
271 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
276 #if __cplusplus >= 201103L
278 insert(std::initializer_list
<value_type
> __list
)
279 { _Base::insert(__list
); }
283 #if __cplusplus >= 201103L
284 insert(const_iterator __position
, const value_type
& __x
)
286 insert(iterator __position
, const value_type
& __x
)
289 __glibcxx_check_insert(__position
);
290 return iterator(_Base::insert(__position
.base(), __x
), this);
293 #if __cplusplus >= 201103L
294 template<typename _Pair
, typename
= typename
295 std::enable_if
<std::is_constructible
<value_type
,
296 _Pair
&&>::value
>::type
>
298 insert(const_iterator __position
, _Pair
&& __x
)
300 __glibcxx_check_insert(__position
);
301 return iterator(_Base::insert(__position
.base(),
302 std::forward
<_Pair
>(__x
)), this);
306 template<typename _InputIterator
>
308 insert(_InputIterator __first
, _InputIterator __last
)
310 typename
__gnu_debug::_Distance_traits
<_InputIterator
>::__type __dist
;
311 __glibcxx_check_valid_range2(__first
, __last
, __dist
);
313 if (__dist
.second
>= __gnu_debug::__dp_sign
)
314 _Base::insert(__gnu_debug::__unsafe(__first
),
315 __gnu_debug::__unsafe(__last
));
317 _Base::insert(__first
, __last
);
320 #if __cplusplus >= 201103L
322 erase(const_iterator __position
)
324 __glibcxx_check_erase(__position
);
325 this->_M_invalidate_if(_Equal(__position
.base()));
326 return iterator(_Base::erase(__position
.base()), this);
330 erase(iterator __position
)
331 { return erase(const_iterator(__position
)); }
334 erase(iterator __position
)
336 __glibcxx_check_erase(__position
);
337 this->_M_invalidate_if(_Equal(__position
.base()));
338 _Base::erase(__position
.base());
343 erase(const key_type
& __x
)
345 _Base_iterator __victim
= _Base::find(__x
);
346 if (__victim
== _Base::end())
350 this->_M_invalidate_if(_Equal(__victim
));
351 _Base::erase(__victim
);
356 #if __cplusplus >= 201103L
358 erase(const_iterator __first
, const_iterator __last
)
360 // _GLIBCXX_RESOLVE_LIB_DEFECTS
361 // 151. can't currently clear() empty container
362 __glibcxx_check_erase_range(__first
, __last
);
363 for (_Base_const_iterator __victim
= __first
.base();
364 __victim
!= __last
.base(); ++__victim
)
366 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
367 _M_message(__gnu_debug::__msg_valid_range
)
368 ._M_iterator(__first
, "first")
369 ._M_iterator(__last
, "last"));
370 this->_M_invalidate_if(_Equal(__victim
));
372 return iterator(_Base::erase(__first
.base(), __last
.base()), this);
376 erase(iterator __first
, iterator __last
)
378 // _GLIBCXX_RESOLVE_LIB_DEFECTS
379 // 151. can't currently clear() empty container
380 __glibcxx_check_erase_range(__first
, __last
);
381 for (_Base_iterator __victim
= __first
.base();
382 __victim
!= __last
.base(); ++__victim
)
384 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
385 _M_message(__gnu_debug::__msg_valid_range
)
386 ._M_iterator(__first
, "first")
387 ._M_iterator(__last
, "last"));
388 this->_M_invalidate_if(_Equal(__victim
));
390 _Base::erase(__first
.base(), __last
.base());
396 _GLIBCXX_NOEXCEPT_IF( noexcept(declval
<_Base
&>().swap(__x
)) )
403 clear() _GLIBCXX_NOEXCEPT
405 this->_M_invalidate_all();
410 using _Base::key_comp
;
411 using _Base::value_comp
;
413 // 23.3.1.3 map operations:
415 find(const key_type
& __x
)
416 { return iterator(_Base::find(__x
), this); }
418 #if __cplusplus > 201103L
419 template<typename _Kt
,
421 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
424 { return { _Base::find(__x
), this }; }
428 find(const key_type
& __x
) const
429 { return const_iterator(_Base::find(__x
), this); }
431 #if __cplusplus > 201103L
432 template<typename _Kt
,
434 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
436 find(const _Kt
& __x
) const
437 { return { _Base::find(__x
), this }; }
443 lower_bound(const key_type
& __x
)
444 { return iterator(_Base::lower_bound(__x
), this); }
446 #if __cplusplus > 201103L
447 template<typename _Kt
,
449 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
451 lower_bound(const _Kt
& __x
)
452 { return { _Base::lower_bound(__x
), this }; }
456 lower_bound(const key_type
& __x
) const
457 { return const_iterator(_Base::lower_bound(__x
), this); }
459 #if __cplusplus > 201103L
460 template<typename _Kt
,
462 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
464 lower_bound(const _Kt
& __x
) const
465 { return { _Base::lower_bound(__x
), this }; }
469 upper_bound(const key_type
& __x
)
470 { return iterator(_Base::upper_bound(__x
), this); }
472 #if __cplusplus > 201103L
473 template<typename _Kt
,
475 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
477 upper_bound(const _Kt
& __x
)
478 { return { _Base::upper_bound(__x
), this }; }
482 upper_bound(const key_type
& __x
) const
483 { return const_iterator(_Base::upper_bound(__x
), this); }
485 #if __cplusplus > 201103L
486 template<typename _Kt
,
488 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
490 upper_bound(const _Kt
& __x
) const
491 { return { _Base::upper_bound(__x
), this }; }
494 std::pair
<iterator
,iterator
>
495 equal_range(const key_type
& __x
)
497 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
498 _Base::equal_range(__x
);
499 return std::make_pair(iterator(__res
.first
, this),
500 iterator(__res
.second
, this));
503 #if __cplusplus > 201103L
504 template<typename _Kt
,
506 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
507 std::pair
<iterator
, iterator
>
508 equal_range(const _Kt
& __x
)
510 auto __res
= _Base::equal_range(__x
);
511 return { { __res
.first
, this }, { __res
.second
, this } };
515 std::pair
<const_iterator
,const_iterator
>
516 equal_range(const key_type
& __x
) const
518 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
519 _Base::equal_range(__x
);
520 return std::make_pair(const_iterator(__res
.first
, this),
521 const_iterator(__res
.second
, this));
524 #if __cplusplus > 201103L
525 template<typename _Kt
,
527 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
528 std::pair
<const_iterator
, const_iterator
>
529 equal_range(const _Kt
& __x
) const
531 auto __res
= _Base::equal_range(__x
);
532 return { { __res
.first
, this }, { __res
.second
, this } };
537 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
540 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
543 template<typename _Key
, typename _Tp
,
544 typename _Compare
, typename _Allocator
>
546 operator==(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
547 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
548 { return __lhs
._M_base() == __rhs
._M_base(); }
550 template<typename _Key
, typename _Tp
,
551 typename _Compare
, typename _Allocator
>
553 operator!=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
554 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
555 { return __lhs
._M_base() != __rhs
._M_base(); }
557 template<typename _Key
, typename _Tp
,
558 typename _Compare
, typename _Allocator
>
560 operator<(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
561 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
562 { return __lhs
._M_base() < __rhs
._M_base(); }
564 template<typename _Key
, typename _Tp
,
565 typename _Compare
, typename _Allocator
>
567 operator<=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
568 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
569 { return __lhs
._M_base() <= __rhs
._M_base(); }
571 template<typename _Key
, typename _Tp
,
572 typename _Compare
, typename _Allocator
>
574 operator>=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
575 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
576 { return __lhs
._M_base() >= __rhs
._M_base(); }
578 template<typename _Key
, typename _Tp
,
579 typename _Compare
, typename _Allocator
>
581 operator>(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
582 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
583 { return __lhs
._M_base() > __rhs
._M_base(); }
585 template<typename _Key
, typename _Tp
,
586 typename _Compare
, typename _Allocator
>
588 swap(map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
589 map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
590 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
591 { __lhs
.swap(__rhs
); }
593 } // namespace __debug