1 // Debugging map implementation -*- C++ -*-
3 // Copyright (C) 2003-2018 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
;
59 template<typename _ItT
, typename _SeqT
, typename _CatT
>
60 friend class ::__gnu_debug::_Safe_iterator
;
64 typedef _Key key_type
;
65 typedef _Tp mapped_type
;
66 typedef std::pair
<const _Key
, _Tp
> value_type
;
67 typedef _Compare key_compare
;
68 typedef _Allocator allocator_type
;
69 typedef typename
_Base::reference reference
;
70 typedef typename
_Base::const_reference const_reference
;
72 typedef __gnu_debug::_Safe_iterator
<_Base_iterator
, map
>
74 typedef __gnu_debug::_Safe_iterator
<_Base_const_iterator
, map
>
77 typedef typename
_Base::size_type size_type
;
78 typedef typename
_Base::difference_type difference_type
;
79 typedef typename
_Base::pointer pointer
;
80 typedef typename
_Base::const_pointer const_pointer
;
81 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
82 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
84 // 23.3.1.1 construct/copy/destroy:
86 #if __cplusplus < 201103L
95 map(const map
&) = default;
98 map(initializer_list
<value_type
> __l
,
99 const _Compare
& __c
= _Compare(),
100 const allocator_type
& __a
= allocator_type())
101 : _Base(__l
, __c
, __a
) { }
104 map(const allocator_type
& __a
)
107 map(const map
& __m
, const allocator_type
& __a
)
108 : _Base(__m
, __a
) { }
110 map(map
&& __m
, const allocator_type
& __a
)
111 noexcept( noexcept(_Base(std::move(__m
._M_base()), __a
)) )
112 : _Safe(std::move(__m
._M_safe()), __a
),
113 _Base(std::move(__m
._M_base()), __a
) { }
115 map(initializer_list
<value_type
> __l
, const allocator_type
& __a
)
116 : _Base(__l
, __a
) { }
118 template<typename _InputIterator
>
119 map(_InputIterator __first
, _InputIterator __last
,
120 const allocator_type
& __a
)
121 : _Base(__gnu_debug::__base(
122 __glibcxx_check_valid_constructor_range(__first
, __last
)),
123 __gnu_debug::__base(__last
), __a
)
129 map(const _Base
& __x
)
132 explicit map(const _Compare
& __comp
,
133 const _Allocator
& __a
= _Allocator())
134 : _Base(__comp
, __a
) { }
136 template<typename _InputIterator
>
137 map(_InputIterator __first
, _InputIterator __last
,
138 const _Compare
& __comp
= _Compare(),
139 const _Allocator
& __a
= _Allocator())
140 : _Base(__gnu_debug::__base(
141 __glibcxx_check_valid_constructor_range(__first
, __last
)),
142 __gnu_debug::__base(__last
),
145 #if __cplusplus < 201103L
147 operator=(const map
& __x
)
149 this->_M_safe() = __x
;
155 operator=(const map
&) = default;
158 operator=(map
&&) = default;
161 operator=(initializer_list
<value_type
> __l
)
164 this->_M_invalidate_all();
169 // _GLIBCXX_RESOLVE_LIB_DEFECTS
170 // 133. map missing get_allocator()
171 using _Base::get_allocator
;
175 begin() _GLIBCXX_NOEXCEPT
176 { return iterator(_Base::begin(), this); }
179 begin() const _GLIBCXX_NOEXCEPT
180 { return const_iterator(_Base::begin(), this); }
183 end() _GLIBCXX_NOEXCEPT
184 { return iterator(_Base::end(), this); }
187 end() const _GLIBCXX_NOEXCEPT
188 { return const_iterator(_Base::end(), this); }
191 rbegin() _GLIBCXX_NOEXCEPT
192 { return reverse_iterator(end()); }
194 const_reverse_iterator
195 rbegin() const _GLIBCXX_NOEXCEPT
196 { return const_reverse_iterator(end()); }
199 rend() _GLIBCXX_NOEXCEPT
200 { return reverse_iterator(begin()); }
202 const_reverse_iterator
203 rend() const _GLIBCXX_NOEXCEPT
204 { return const_reverse_iterator(begin()); }
206 #if __cplusplus >= 201103L
208 cbegin() const noexcept
209 { return const_iterator(_Base::begin(), this); }
212 cend() const noexcept
213 { return const_iterator(_Base::end(), this); }
215 const_reverse_iterator
216 crbegin() const noexcept
217 { return const_reverse_iterator(end()); }
219 const_reverse_iterator
220 crend() const noexcept
221 { return const_reverse_iterator(begin()); }
227 using _Base::max_size
;
229 // 23.3.1.2 element access:
230 using _Base::operator[];
232 // _GLIBCXX_RESOLVE_LIB_DEFECTS
233 // DR 464. Suggestion for new member functions in standard containers.
237 #if __cplusplus >= 201103L
238 template<typename
... _Args
>
239 std::pair
<iterator
, bool>
240 emplace(_Args
&&... __args
)
242 auto __res
= _Base::emplace(std::forward
<_Args
>(__args
)...);
243 return { { __res
.first
, this }, __res
.second
};
246 template<typename
... _Args
>
248 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
250 __glibcxx_check_insert(__pos
);
253 _Base::emplace_hint(__pos
.base(), std::forward
<_Args
>(__args
)...),
259 std::pair
<iterator
, bool>
260 insert(const value_type
& __x
)
262 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
263 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
267 #if __cplusplus >= 201103L
268 // _GLIBCXX_RESOLVE_LIB_DEFECTS
269 // 2354. Unnecessary copying when inserting into maps with braced-init
270 std::pair
<iterator
, bool>
271 insert(value_type
&& __x
)
273 auto __res
= _Base::insert(std::move(__x
));
274 return { { __res
.first
, this }, __res
.second
};
277 template<typename _Pair
, typename
= typename
278 std::enable_if
<std::is_constructible
<value_type
,
279 _Pair
&&>::value
>::type
>
280 std::pair
<iterator
, bool>
283 auto __res
= _Base::insert(std::forward
<_Pair
>(__x
));
284 return { { __res
.first
, this }, __res
.second
};
288 #if __cplusplus >= 201103L
290 insert(std::initializer_list
<value_type
> __list
)
291 { _Base::insert(__list
); }
295 #if __cplusplus >= 201103L
296 insert(const_iterator __position
, const value_type
& __x
)
298 insert(iterator __position
, const value_type
& __x
)
301 __glibcxx_check_insert(__position
);
302 return iterator(_Base::insert(__position
.base(), __x
), this);
305 #if __cplusplus >= 201103L
306 // _GLIBCXX_RESOLVE_LIB_DEFECTS
307 // 2354. Unnecessary copying when inserting into maps with braced-init
309 insert(const_iterator __position
, value_type
&& __x
)
311 __glibcxx_check_insert(__position
);
312 return { _Base::insert(__position
.base(), std::move(__x
)), this };
315 template<typename _Pair
, typename
= typename
316 std::enable_if
<std::is_constructible
<value_type
,
317 _Pair
&&>::value
>::type
>
319 insert(const_iterator __position
, _Pair
&& __x
)
321 __glibcxx_check_insert(__position
);
324 _Base::insert(__position
.base(), std::forward
<_Pair
>(__x
)),
330 template<typename _InputIterator
>
332 insert(_InputIterator __first
, _InputIterator __last
)
334 typename
__gnu_debug::_Distance_traits
<_InputIterator
>::__type __dist
;
335 __glibcxx_check_valid_range2(__first
, __last
, __dist
);
337 if (__dist
.second
>= __gnu_debug::__dp_sign
)
338 _Base::insert(__gnu_debug::__unsafe(__first
),
339 __gnu_debug::__unsafe(__last
));
341 _Base::insert(__first
, __last
);
345 #if __cplusplus > 201402L
346 template <typename
... _Args
>
348 try_emplace(const key_type
& __k
, _Args
&&... __args
)
350 auto __res
= _Base::try_emplace(__k
,
351 std::forward
<_Args
>(__args
)...);
352 return { { __res
.first
, this }, __res
.second
};
355 template <typename
... _Args
>
357 try_emplace(key_type
&& __k
, _Args
&&... __args
)
359 auto __res
= _Base::try_emplace(std::move(__k
),
360 std::forward
<_Args
>(__args
)...);
361 return { { __res
.first
, this }, __res
.second
};
364 template <typename
... _Args
>
366 try_emplace(const_iterator __hint
, const key_type
& __k
,
369 __glibcxx_check_insert(__hint
);
372 _Base::try_emplace(__hint
.base(), __k
,
373 std::forward
<_Args
>(__args
)...),
378 template <typename
... _Args
>
380 try_emplace(const_iterator __hint
, key_type
&& __k
, _Args
&&... __args
)
382 __glibcxx_check_insert(__hint
);
385 _Base::try_emplace(__hint
.base(), std::move(__k
),
386 std::forward
<_Args
>(__args
)...),
391 template <typename _Obj
>
392 std::pair
<iterator
, bool>
393 insert_or_assign(const key_type
& __k
, _Obj
&& __obj
)
395 auto __res
= _Base::insert_or_assign(__k
,
396 std::forward
<_Obj
>(__obj
));
397 return { { __res
.first
, this }, __res
.second
};
400 template <typename _Obj
>
401 std::pair
<iterator
, bool>
402 insert_or_assign(key_type
&& __k
, _Obj
&& __obj
)
404 auto __res
= _Base::insert_or_assign(std::move(__k
),
405 std::forward
<_Obj
>(__obj
));
406 return { { __res
.first
, this }, __res
.second
};
409 template <typename _Obj
>
411 insert_or_assign(const_iterator __hint
,
412 const key_type
& __k
, _Obj
&& __obj
)
414 __glibcxx_check_insert(__hint
);
417 _Base::insert_or_assign(__hint
.base(), __k
,
418 std::forward
<_Obj
>(__obj
)),
423 template <typename _Obj
>
425 insert_or_assign(const_iterator __hint
, key_type
&& __k
, _Obj
&& __obj
)
427 __glibcxx_check_insert(__hint
);
430 _Base::insert_or_assign(__hint
.base(), std::move(__k
),
431 std::forward
<_Obj
>(__obj
)),
437 #if __cplusplus > 201402L
438 using node_type
= typename
_Base::node_type
;
439 using insert_return_type
= _Node_insert_return
<iterator
, node_type
>;
442 extract(const_iterator __position
)
444 __glibcxx_check_erase(__position
);
445 this->_M_invalidate_if(_Equal(__position
.base()));
446 return _Base::extract(__position
.base());
450 extract(const key_type
& __key
)
452 const auto __position
= find(__key
);
453 if (__position
!= end())
454 return extract(__position
);
459 insert(node_type
&& __nh
)
461 auto __ret
= _Base::insert(std::move(__nh
));
463 { { __ret
.position
, this }, __ret
.inserted
, std::move(__ret
.node
) };
467 insert(const_iterator __hint
, node_type
&& __nh
)
469 __glibcxx_check_insert(__hint
);
470 return { _Base::insert(__hint
.base(), std::move(__nh
)), this };
476 #if __cplusplus >= 201103L
478 erase(const_iterator __position
)
480 __glibcxx_check_erase(__position
);
481 this->_M_invalidate_if(_Equal(__position
.base()));
482 return { _Base::erase(__position
.base()), this };
485 _GLIBCXX_ABI_TAG_CXX11
487 erase(iterator __position
)
488 { return erase(const_iterator(__position
)); }
491 erase(iterator __position
)
493 __glibcxx_check_erase(__position
);
494 this->_M_invalidate_if(_Equal(__position
.base()));
495 _Base::erase(__position
.base());
500 erase(const key_type
& __x
)
502 _Base_iterator __victim
= _Base::find(__x
);
503 if (__victim
== _Base::end())
507 this->_M_invalidate_if(_Equal(__victim
));
508 _Base::erase(__victim
);
513 #if __cplusplus >= 201103L
515 erase(const_iterator __first
, const_iterator __last
)
517 // _GLIBCXX_RESOLVE_LIB_DEFECTS
518 // 151. can't currently clear() empty container
519 __glibcxx_check_erase_range(__first
, __last
);
520 for (_Base_const_iterator __victim
= __first
.base();
521 __victim
!= __last
.base(); ++__victim
)
523 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::cend(),
524 _M_message(__gnu_debug::__msg_valid_range
)
525 ._M_iterator(__first
, "first")
526 ._M_iterator(__last
, "last"));
527 this->_M_invalidate_if(_Equal(__victim
));
530 return { _Base::erase(__first
.base(), __last
.base()), this };
534 erase(iterator __first
, iterator __last
)
536 // _GLIBCXX_RESOLVE_LIB_DEFECTS
537 // 151. can't currently clear() empty container
538 __glibcxx_check_erase_range(__first
, __last
);
539 for (_Base_iterator __victim
= __first
.base();
540 __victim
!= __last
.base(); ++__victim
)
542 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
543 _M_message(__gnu_debug::__msg_valid_range
)
544 ._M_iterator(__first
, "first")
545 ._M_iterator(__last
, "last"));
546 this->_M_invalidate_if(_Equal(__victim
));
548 _Base::erase(__first
.base(), __last
.base());
554 _GLIBCXX_NOEXCEPT_IF( noexcept(declval
<_Base
&>().swap(__x
)) )
561 clear() _GLIBCXX_NOEXCEPT
563 this->_M_invalidate_all();
568 using _Base::key_comp
;
569 using _Base::value_comp
;
571 // 23.3.1.3 map operations:
573 find(const key_type
& __x
)
574 { return iterator(_Base::find(__x
), this); }
576 #if __cplusplus > 201103L
577 template<typename _Kt
,
579 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
582 { return { _Base::find(__x
), this }; }
586 find(const key_type
& __x
) const
587 { return const_iterator(_Base::find(__x
), this); }
589 #if __cplusplus > 201103L
590 template<typename _Kt
,
592 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
594 find(const _Kt
& __x
) const
595 { return { _Base::find(__x
), this }; }
601 lower_bound(const key_type
& __x
)
602 { return iterator(_Base::lower_bound(__x
), this); }
604 #if __cplusplus > 201103L
605 template<typename _Kt
,
607 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
609 lower_bound(const _Kt
& __x
)
610 { return { _Base::lower_bound(__x
), this }; }
614 lower_bound(const key_type
& __x
) const
615 { return const_iterator(_Base::lower_bound(__x
), this); }
617 #if __cplusplus > 201103L
618 template<typename _Kt
,
620 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
622 lower_bound(const _Kt
& __x
) const
623 { return { _Base::lower_bound(__x
), this }; }
627 upper_bound(const key_type
& __x
)
628 { return iterator(_Base::upper_bound(__x
), this); }
630 #if __cplusplus > 201103L
631 template<typename _Kt
,
633 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
635 upper_bound(const _Kt
& __x
)
636 { return { _Base::upper_bound(__x
), this }; }
640 upper_bound(const key_type
& __x
) const
641 { return const_iterator(_Base::upper_bound(__x
), this); }
643 #if __cplusplus > 201103L
644 template<typename _Kt
,
646 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
648 upper_bound(const _Kt
& __x
) const
649 { return { _Base::upper_bound(__x
), this }; }
652 std::pair
<iterator
,iterator
>
653 equal_range(const key_type
& __x
)
655 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
656 _Base::equal_range(__x
);
657 return std::make_pair(iterator(__res
.first
, this),
658 iterator(__res
.second
, this));
661 #if __cplusplus > 201103L
662 template<typename _Kt
,
664 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
665 std::pair
<iterator
, iterator
>
666 equal_range(const _Kt
& __x
)
668 auto __res
= _Base::equal_range(__x
);
669 return { { __res
.first
, this }, { __res
.second
, this } };
673 std::pair
<const_iterator
,const_iterator
>
674 equal_range(const key_type
& __x
) const
676 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
677 _Base::equal_range(__x
);
678 return std::make_pair(const_iterator(__res
.first
, this),
679 const_iterator(__res
.second
, this));
682 #if __cplusplus > 201103L
683 template<typename _Kt
,
685 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
686 std::pair
<const_iterator
, const_iterator
>
687 equal_range(const _Kt
& __x
) const
689 auto __res
= _Base::equal_range(__x
);
690 return { { __res
.first
, this }, { __res
.second
, this } };
695 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
698 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
701 #if __cpp_deduction_guides >= 201606
703 template<typename _InputIterator
,
704 typename _Compare
= less
<__iter_key_t
<_InputIterator
>>,
705 typename _Allocator
= allocator
<__iter_to_alloc_t
<_InputIterator
>>,
706 typename
= _RequireInputIter
<_InputIterator
>,
707 typename
= _RequireAllocator
<_Allocator
>>
708 map(_InputIterator
, _InputIterator
,
709 _Compare
= _Compare(), _Allocator
= _Allocator())
710 -> map
<__iter_key_t
<_InputIterator
>, __iter_val_t
<_InputIterator
>,
711 _Compare
, _Allocator
>;
713 template<typename _Key
, typename _Tp
, typename _Compare
= less
<_Key
>,
714 typename _Allocator
= allocator
<pair
<const _Key
, _Tp
>>,
715 typename
= _RequireAllocator
<_Allocator
>>
716 map(initializer_list
<pair
<_Key
, _Tp
>>,
717 _Compare
= _Compare(), _Allocator
= _Allocator())
718 -> map
<_Key
, _Tp
, _Compare
, _Allocator
>;
720 template <typename _InputIterator
, typename _Allocator
,
721 typename
= _RequireInputIter
<_InputIterator
>,
722 typename
= _RequireAllocator
<_Allocator
>>
723 map(_InputIterator
, _InputIterator
, _Allocator
)
724 -> map
<__iter_key_t
<_InputIterator
>, __iter_val_t
<_InputIterator
>,
725 less
<__iter_key_t
<_InputIterator
>>, _Allocator
>;
727 template<typename _Key
, typename _Tp
, typename _Allocator
,
728 typename
= _RequireAllocator
<_Allocator
>>
729 map(initializer_list
<pair
<_Key
, _Tp
>>, _Allocator
)
730 -> map
<_Key
, _Tp
, less
<_Key
>, _Allocator
>;
734 template<typename _Key
, typename _Tp
,
735 typename _Compare
, typename _Allocator
>
737 operator==(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
738 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
739 { return __lhs
._M_base() == __rhs
._M_base(); }
741 template<typename _Key
, typename _Tp
,
742 typename _Compare
, typename _Allocator
>
744 operator!=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
745 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
746 { return __lhs
._M_base() != __rhs
._M_base(); }
748 template<typename _Key
, typename _Tp
,
749 typename _Compare
, typename _Allocator
>
751 operator<(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
752 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
753 { return __lhs
._M_base() < __rhs
._M_base(); }
755 template<typename _Key
, typename _Tp
,
756 typename _Compare
, typename _Allocator
>
758 operator<=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
759 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
760 { return __lhs
._M_base() <= __rhs
._M_base(); }
762 template<typename _Key
, typename _Tp
,
763 typename _Compare
, typename _Allocator
>
765 operator>=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
766 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
767 { return __lhs
._M_base() >= __rhs
._M_base(); }
769 template<typename _Key
, typename _Tp
,
770 typename _Compare
, typename _Allocator
>
772 operator>(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
773 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
774 { return __lhs
._M_base() > __rhs
._M_base(); }
776 template<typename _Key
, typename _Tp
,
777 typename _Compare
, typename _Allocator
>
779 swap(map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
780 map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
781 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
782 { __lhs
.swap(__rhs
); }
784 } // namespace __debug