libstdc++
debug/map.h
Go to the documentation of this file.
1// Debugging map implementation -*- C++ -*-
2
3// Copyright (C) 2003-2018 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/** @file debug/map.h
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
29#ifndef _GLIBCXX_DEBUG_MAP_H
30#define _GLIBCXX_DEBUG_MAP_H 1
31
32#include <debug/safe_sequence.h>
34#include <debug/safe_iterator.h>
35#include <utility>
36
37namespace std _GLIBCXX_VISIBILITY(default)
38{
39namespace __debug
40{
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> > >
44 class map
46 map<_Key, _Tp, _Compare, _Allocator>, _Allocator,
47 __gnu_debug::_Safe_node_sequence>,
48 public _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator>
49 {
50 typedef _GLIBCXX_STD_C::map<
51 _Key, _Tp, _Compare, _Allocator> _Base;
54
56 typedef typename _Base::iterator _Base_iterator;
58
59 public:
60 // types:
61 typedef _Key key_type;
62 typedef _Tp mapped_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;
68
73
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;
80
81 // 23.3.1.1 construct/copy/destroy:
82
83#if __cplusplus < 201103L
84 map() : _Base() { }
85
86 map(const map& __x)
87 : _Base(__x) { }
88
89 ~map() { }
90#else
91 map() = default;
92 map(const map&) = default;
93 map(map&&) = default;
94
96 const _Compare& __c = _Compare(),
97 const allocator_type& __a = allocator_type())
98 : _Base(__l, __c, __a) { }
99
100 explicit
101 map(const allocator_type& __a)
102 : _Base(__a) { }
103
104 map(const map& __m, const allocator_type& __a)
105 : _Base(__m, __a) { }
106
107 map(map&& __m, const allocator_type& __a)
108 noexcept( noexcept(_Base(std::move(__m._M_base()), __a)) )
109 : _Safe(std::move(__m._M_safe()), __a),
110 _Base(std::move(__m._M_base()), __a) { }
111
112 map(initializer_list<value_type> __l, const allocator_type& __a)
113 : _Base(__l, __a) { }
114
115 template<typename _InputIterator>
116 map(_InputIterator __first, _InputIterator __last,
117 const allocator_type& __a)
118 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
119 __last)),
120 __gnu_debug::__base(__last), __a)
121 { }
122
123 ~map() = default;
124#endif
125
126 map(const _Base& __x)
127 : _Base(__x) { }
128
129 explicit map(const _Compare& __comp,
130 const _Allocator& __a = _Allocator())
131 : _Base(__comp, __a) { }
132
133 template<typename _InputIterator>
134 map(_InputIterator __first, _InputIterator __last,
135 const _Compare& __comp = _Compare(),
136 const _Allocator& __a = _Allocator())
137 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
138 __last)),
139 __gnu_debug::__base(__last),
140 __comp, __a) { }
141
142#if __cplusplus < 201103L
143 map&
144 operator=(const map& __x)
145 {
146 this->_M_safe() = __x;
147 _M_base() = __x;
148 return *this;
149 }
150#else
151 map&
152 operator=(const map&) = default;
153
154 map&
155 operator=(map&&) = default;
156
157 map&
158 operator=(initializer_list<value_type> __l)
159 {
160 _M_base() = __l;
161 this->_M_invalidate_all();
162 return *this;
163 }
164#endif
165
166 // _GLIBCXX_RESOLVE_LIB_DEFECTS
167 // 133. map missing get_allocator()
168 using _Base::get_allocator;
169
170 // iterators:
172 begin() _GLIBCXX_NOEXCEPT
173 { return iterator(_Base::begin(), this); }
174
176 begin() const _GLIBCXX_NOEXCEPT
177 { return const_iterator(_Base::begin(), this); }
178
180 end() _GLIBCXX_NOEXCEPT
181 { return iterator(_Base::end(), this); }
182
184 end() const _GLIBCXX_NOEXCEPT
185 { return const_iterator(_Base::end(), this); }
186
188 rbegin() _GLIBCXX_NOEXCEPT
189 { return reverse_iterator(end()); }
190
192 rbegin() const _GLIBCXX_NOEXCEPT
193 { return const_reverse_iterator(end()); }
194
196 rend() _GLIBCXX_NOEXCEPT
197 { return reverse_iterator(begin()); }
198
200 rend() const _GLIBCXX_NOEXCEPT
201 { return const_reverse_iterator(begin()); }
202
203#if __cplusplus >= 201103L
205 cbegin() const noexcept
206 { return const_iterator(_Base::begin(), this); }
207
209 cend() const noexcept
210 { return const_iterator(_Base::end(), this); }
211
213 crbegin() const noexcept
214 { return const_reverse_iterator(end()); }
215
217 crend() const noexcept
218 { return const_reverse_iterator(begin()); }
219#endif
220
221 // capacity:
222 using _Base::empty;
223 using _Base::size;
224 using _Base::max_size;
225
226 // 23.3.1.2 element access:
227 using _Base::operator[];
228
229 // _GLIBCXX_RESOLVE_LIB_DEFECTS
230 // DR 464. Suggestion for new member functions in standard containers.
231 using _Base::at;
232
233 // modifiers:
234#if __cplusplus >= 201103L
235 template<typename... _Args>
237 emplace(_Args&&... __args)
238 {
239 auto __res = _Base::emplace(std::forward<_Args>(__args)...);
240 return std::pair<iterator, bool>(iterator(__res.first, this),
241 __res.second);
242 }
243
244 template<typename... _Args>
246 emplace_hint(const_iterator __pos, _Args&&... __args)
247 {
249 return iterator(_Base::emplace_hint(__pos.base(),
250 std::forward<_Args>(__args)...),
251 this);
252 }
253#endif
254
256 insert(const value_type& __x)
257 {
258 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
259 return std::pair<iterator, bool>(iterator(__res.first, this),
260 __res.second);
261 }
262
263#if __cplusplus >= 201103L
264 // _GLIBCXX_RESOLVE_LIB_DEFECTS
265 // 2354. Unnecessary copying when inserting into maps with braced-init
267 insert(value_type&& __x)
268 {
269 auto __res = _Base::insert(std::move(__x));
270 return { iterator(__res.first, this), __res.second };
271 }
272
273 template<typename _Pair, typename = typename
275 _Pair&&>::value>::type>
277 insert(_Pair&& __x)
278 {
280 = _Base::insert(std::forward<_Pair>(__x));
281 return std::pair<iterator, bool>(iterator(__res.first, this),
282 __res.second);
283 }
284#endif
285
286#if __cplusplus >= 201103L
287 void
289 { _Base::insert(__list); }
290#endif
291
293#if __cplusplus >= 201103L
294 insert(const_iterator __position, const value_type& __x)
295#else
296 insert(iterator __position, const value_type& __x)
297#endif
298 {
299 __glibcxx_check_insert(__position);
300 return iterator(_Base::insert(__position.base(), __x), this);
301 }
302
303#if __cplusplus >= 201103L
304 // _GLIBCXX_RESOLVE_LIB_DEFECTS
305 // 2354. Unnecessary copying when inserting into maps with braced-init
307 insert(const_iterator __position, value_type&& __x)
308 {
309 __glibcxx_check_insert(__position);
310 return { _Base::insert(__position.base(), std::move(__x)), this };
311 }
312
313 template<typename _Pair, typename = typename
315 _Pair&&>::value>::type>
317 insert(const_iterator __position, _Pair&& __x)
318 {
319 __glibcxx_check_insert(__position);
320 return iterator(_Base::insert(__position.base(),
321 std::forward<_Pair>(__x)), this);
322 }
323#endif
324
325 template<typename _InputIterator>
326 void
327 insert(_InputIterator __first, _InputIterator __last)
328 {
330 __glibcxx_check_valid_range2(__first, __last, __dist);
331
332 if (__dist.second >= __gnu_debug::__dp_sign)
333 _Base::insert(__gnu_debug::__unsafe(__first),
334 __gnu_debug::__unsafe(__last));
335 else
336 _Base::insert(__first, __last);
337 }
338
339
340#if __cplusplus > 201402L
341 template <typename... _Args>
343 try_emplace(const key_type& __k, _Args&&... __args)
344 {
345 auto __res = _Base::try_emplace(__k,
346 std::forward<_Args>(__args)...);
347 return { iterator(__res.first, this), __res.second };
348 }
349
350 template <typename... _Args>
352 try_emplace(key_type&& __k, _Args&&... __args)
353 {
354 auto __res = _Base::try_emplace(std::move(__k),
355 std::forward<_Args>(__args)...);
356 return { iterator(__res.first, this), __res.second };
357 }
358
359 template <typename... _Args>
361 try_emplace(const_iterator __hint, const key_type& __k,
362 _Args&&... __args)
363 {
365 return iterator(_Base::try_emplace(__hint.base(), __k,
366 std::forward<_Args>(__args)...),
367 this);
368 }
369
370 template <typename... _Args>
372 try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
373 {
375 return iterator(_Base::try_emplace(__hint.base(), std::move(__k),
376 std::forward<_Args>(__args)...),
377 this);
378 }
379
380 template <typename _Obj>
382 insert_or_assign(const key_type& __k, _Obj&& __obj)
383 {
384 auto __res = _Base::insert_or_assign(__k,
385 std::forward<_Obj>(__obj));
386 return { iterator(__res.first, this), __res.second };
387 }
388
389 template <typename _Obj>
391 insert_or_assign(key_type&& __k, _Obj&& __obj)
392 {
393 auto __res = _Base::insert_or_assign(std::move(__k),
394 std::forward<_Obj>(__obj));
395 return { iterator(__res.first, this), __res.second };
396 }
397
398 template <typename _Obj>
400 insert_or_assign(const_iterator __hint,
401 const key_type& __k, _Obj&& __obj)
402 {
404 return iterator(_Base::insert_or_assign(__hint.base(), __k,
405 std::forward<_Obj>(__obj)),
406 this);
407 }
408
409 template <typename _Obj>
411 insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
412 {
414 return iterator(_Base::insert_or_assign(__hint.base(),
415 std::move(__k),
416 std::forward<_Obj>(__obj)),
417 this);
418 }
419#endif // C++17
420
421#if __cplusplus > 201402L
422 using node_type = typename _Base::node_type;
423 using insert_return_type = _Node_insert_return<iterator, node_type>;
424
425 node_type
426 extract(const_iterator __position)
427 {
428 __glibcxx_check_erase(__position);
429 this->_M_invalidate_if(_Equal(__position.base()));
430 return _Base::extract(__position.base());
431 }
432
433 node_type
434 extract(const key_type& __key)
435 {
436 const auto __position = find(__key);
437 if (__position != end())
438 return extract(__position);
439 return {};
440 }
441
442 insert_return_type
443 insert(node_type&& __nh)
444 {
445 auto __ret = _Base::insert(std::move(__nh));
446 iterator __pos = iterator(__ret.position, this);
447 return { __pos, __ret.inserted, std::move(__ret.node) };
448 }
449
451 insert(const_iterator __hint, node_type&& __nh)
452 {
454 return iterator(_Base::insert(__hint.base(), std::move(__nh)), this);
455 }
456
457 using _Base::merge;
458#endif // C++17
459
460#if __cplusplus >= 201103L
462 erase(const_iterator __position)
463 {
464 __glibcxx_check_erase(__position);
465 this->_M_invalidate_if(_Equal(__position.base()));
466 return iterator(_Base::erase(__position.base()), this);
467 }
468
470 erase(iterator __position)
471 { return erase(const_iterator(__position)); }
472#else
473 void
474 erase(iterator __position)
475 {
476 __glibcxx_check_erase(__position);
477 this->_M_invalidate_if(_Equal(__position.base()));
478 _Base::erase(__position.base());
479 }
480#endif
481
482 size_type
483 erase(const key_type& __x)
484 {
485 _Base_iterator __victim = _Base::find(__x);
486 if (__victim == _Base::end())
487 return 0;
488 else
489 {
490 this->_M_invalidate_if(_Equal(__victim));
491 _Base::erase(__victim);
492 return 1;
493 }
494 }
495
496#if __cplusplus >= 201103L
498 erase(const_iterator __first, const_iterator __last)
499 {
500 // _GLIBCXX_RESOLVE_LIB_DEFECTS
501 // 151. can't currently clear() empty container
502 __glibcxx_check_erase_range(__first, __last);
503 for (_Base_const_iterator __victim = __first.base();
504 __victim != __last.base(); ++__victim)
505 {
506 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
507 _M_message(__gnu_debug::__msg_valid_range)
508 ._M_iterator(__first, "first")
509 ._M_iterator(__last, "last"));
510 this->_M_invalidate_if(_Equal(__victim));
511 }
512 return iterator(_Base::erase(__first.base(), __last.base()), this);
513 }
514#else
515 void
516 erase(iterator __first, iterator __last)
517 {
518 // _GLIBCXX_RESOLVE_LIB_DEFECTS
519 // 151. can't currently clear() empty container
520 __glibcxx_check_erase_range(__first, __last);
521 for (_Base_iterator __victim = __first.base();
522 __victim != __last.base(); ++__victim)
523 {
524 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
525 _M_message(__gnu_debug::__msg_valid_range)
526 ._M_iterator(__first, "first")
527 ._M_iterator(__last, "last"));
528 this->_M_invalidate_if(_Equal(__victim));
529 }
530 _Base::erase(__first.base(), __last.base());
531 }
532#endif
533
534 void
535 swap(map& __x)
536 _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
537 {
538 _Safe::_M_swap(__x);
539 _Base::swap(__x);
540 }
541
542 void
543 clear() _GLIBCXX_NOEXCEPT
544 {
545 this->_M_invalidate_all();
546 _Base::clear();
547 }
548
549 // observers:
550 using _Base::key_comp;
551 using _Base::value_comp;
552
553 // 23.3.1.3 map operations:
555 find(const key_type& __x)
556 { return iterator(_Base::find(__x), this); }
557
558#if __cplusplus > 201103L
559 template<typename _Kt,
560 typename _Req =
561 typename __has_is_transparent<_Compare, _Kt>::type>
563 find(const _Kt& __x)
564 { return { _Base::find(__x), this }; }
565#endif
566
568 find(const key_type& __x) const
569 { return const_iterator(_Base::find(__x), this); }
570
571#if __cplusplus > 201103L
572 template<typename _Kt,
573 typename _Req =
574 typename __has_is_transparent<_Compare, _Kt>::type>
576 find(const _Kt& __x) const
577 { return { _Base::find(__x), this }; }
578#endif
579
580 using _Base::count;
581
583 lower_bound(const key_type& __x)
584 { return iterator(_Base::lower_bound(__x), this); }
585
586#if __cplusplus > 201103L
587 template<typename _Kt,
588 typename _Req =
589 typename __has_is_transparent<_Compare, _Kt>::type>
591 lower_bound(const _Kt& __x)
592 { return { _Base::lower_bound(__x), this }; }
593#endif
594
596 lower_bound(const key_type& __x) const
597 { return const_iterator(_Base::lower_bound(__x), this); }
598
599#if __cplusplus > 201103L
600 template<typename _Kt,
601 typename _Req =
602 typename __has_is_transparent<_Compare, _Kt>::type>
604 lower_bound(const _Kt& __x) const
605 { return { _Base::lower_bound(__x), this }; }
606#endif
607
609 upper_bound(const key_type& __x)
610 { return iterator(_Base::upper_bound(__x), this); }
611
612#if __cplusplus > 201103L
613 template<typename _Kt,
614 typename _Req =
615 typename __has_is_transparent<_Compare, _Kt>::type>
617 upper_bound(const _Kt& __x)
618 { return { _Base::upper_bound(__x), this }; }
619#endif
620
622 upper_bound(const key_type& __x) const
623 { return const_iterator(_Base::upper_bound(__x), this); }
624
625#if __cplusplus > 201103L
626 template<typename _Kt,
627 typename _Req =
628 typename __has_is_transparent<_Compare, _Kt>::type>
630 upper_bound(const _Kt& __x) const
631 { return { _Base::upper_bound(__x), this }; }
632#endif
633
635 equal_range(const key_type& __x)
636 {
638 _Base::equal_range(__x);
639 return std::make_pair(iterator(__res.first, this),
640 iterator(__res.second, this));
641 }
642
643#if __cplusplus > 201103L
644 template<typename _Kt,
645 typename _Req =
646 typename __has_is_transparent<_Compare, _Kt>::type>
648 equal_range(const _Kt& __x)
649 {
650 auto __res = _Base::equal_range(__x);
651 return { { __res.first, this }, { __res.second, this } };
652 }
653#endif
654
656 equal_range(const key_type& __x) const
657 {
659 _Base::equal_range(__x);
660 return std::make_pair(const_iterator(__res.first, this),
661 const_iterator(__res.second, this));
662 }
663
664#if __cplusplus > 201103L
665 template<typename _Kt,
666 typename _Req =
667 typename __has_is_transparent<_Compare, _Kt>::type>
669 equal_range(const _Kt& __x) const
670 {
671 auto __res = _Base::equal_range(__x);
672 return { { __res.first, this }, { __res.second, this } };
673 }
674#endif
675
676 _Base&
677 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
678
679 const _Base&
680 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
681 };
682
683#if __cpp_deduction_guides >= 201606
684
685 template<typename _InputIterator,
686 typename _Compare = less<__iter_key_t<_InputIterator>>,
687 typename _Allocator = allocator<__iter_to_alloc_t<_InputIterator>>,
688 typename = _RequireInputIter<_InputIterator>,
689 typename = _RequireAllocator<_Allocator>>
690 map(_InputIterator, _InputIterator,
691 _Compare = _Compare(), _Allocator = _Allocator())
692 -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
693 _Compare, _Allocator>;
694
695 template<typename _Key, typename _Tp, typename _Compare = less<_Key>,
696 typename _Allocator = allocator<pair<const _Key, _Tp>>,
697 typename = _RequireAllocator<_Allocator>>
699 _Compare = _Compare(), _Allocator = _Allocator())
701
702 template <typename _InputIterator, typename _Allocator,
703 typename = _RequireInputIter<_InputIterator>,
704 typename = _RequireAllocator<_Allocator>>
705 map(_InputIterator, _InputIterator, _Allocator)
706 -> map<__iter_key_t<_InputIterator>, __iter_val_t<_InputIterator>,
708
709 template<typename _Key, typename _Tp, typename _Allocator,
710 typename = _RequireAllocator<_Allocator>>
712 -> map<_Key, _Tp, less<_Key>, _Allocator>;
713
714#endif
715
716 template<typename _Key, typename _Tp,
717 typename _Compare, typename _Allocator>
718 inline bool
719 operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
721 { return __lhs._M_base() == __rhs._M_base(); }
722
723 template<typename _Key, typename _Tp,
724 typename _Compare, typename _Allocator>
725 inline bool
726 operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
728 { return __lhs._M_base() != __rhs._M_base(); }
729
730 template<typename _Key, typename _Tp,
731 typename _Compare, typename _Allocator>
732 inline bool
733 operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
734 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
735 { return __lhs._M_base() < __rhs._M_base(); }
736
737 template<typename _Key, typename _Tp,
738 typename _Compare, typename _Allocator>
739 inline bool
740 operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
741 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
742 { return __lhs._M_base() <= __rhs._M_base(); }
743
744 template<typename _Key, typename _Tp,
745 typename _Compare, typename _Allocator>
746 inline bool
747 operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
748 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
749 { return __lhs._M_base() >= __rhs._M_base(); }
750
751 template<typename _Key, typename _Tp,
752 typename _Compare, typename _Allocator>
753 inline bool
754 operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
755 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
756 { return __lhs._M_base() > __rhs._M_base(); }
757
758 template<typename _Key, typename _Tp,
759 typename _Compare, typename _Allocator>
760 inline void
761 swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
762 map<_Key, _Tp, _Compare, _Allocator>& __rhs)
763 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
764 { __lhs.swap(__rhs); }
765
766} // namespace __debug
767} // namespace std
768
769#endif
#define __glibcxx_check_insert(_Position)
Definition: macros.h:79
#define __glibcxx_check_erase_range(_First, _Last)
Definition: macros.h:173
#define __glibcxx_check_erase(_Position)
Definition: macros.h:145
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition: stl_pair.h:524
ISO C++ entities toplevel namespace is std.
initializer_list
is_constructible
Definition: type_traits:874
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:1907
The standard allocator, as per [20.4].
Definition: allocator.h:109
One of the comparison functors.
Definition: stl_function.h:382
A standard container made up of (key,value) pairs, which can be retrieved based on a key,...
Definition: stl_map.h:101
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:210
_T1 first
second_type is the second bound type
Definition: stl_pair.h:214
_T2 second
first is a copy of the first object
Definition: stl_pair.h:215
Safe iterator wrapper.
Definition: safe_iterator.h:89
_Iterator & base() noexcept
Return the underlying iterator.
Class std::map with safety/checking/debug instrumentation.
Definition: debug/map.h:49
Safe class dealing with some allocator dependent operations.
Like _Safe_sequence but with a special _M_invalidate_all implementation not invalidating past-the-end...