libstdc++
debug/map.h
Go to the documentation of this file.
1 // Debugging map implementation -*- C++ -*-
2 
3 // Copyright (C) 2003-2017 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>
33 #include <debug/safe_container.h>
34 #include <debug/safe_iterator.h>
35 #include <utility>
36 
37 namespace std _GLIBCXX_VISIBILITY(default)
38 {
39 namespace __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 
70  iterator;
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  : _Safe(std::move(__m._M_safe()), __a),
109  _Base(std::move(__m._M_base()), __a) { }
110 
111  map(initializer_list<value_type> __l, const allocator_type& __a)
112  : _Base(__l, __a) { }
113 
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,
118  __last)),
119  __gnu_debug::__base(__last), __a)
120  { }
121 
122  ~map() = default;
123 #endif
124 
125  map(const _Base& __x)
126  : _Base(__x) { }
127 
128  explicit map(const _Compare& __comp,
129  const _Allocator& __a = _Allocator())
130  : _Base(__comp, __a) { }
131 
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,
137  __last)),
138  __gnu_debug::__base(__last),
139  __comp, __a) { }
140 
141 #if __cplusplus < 201103L
142  map&
143  operator=(const map& __x)
144  {
145  this->_M_safe() = __x;
146  _M_base() = __x;
147  return *this;
148  }
149 #else
150  map&
151  operator=(const map&) = default;
152 
153  map&
154  operator=(map&&) = default;
155 
156  map&
157  operator=(initializer_list<value_type> __l)
158  {
159  _M_base() = __l;
160  this->_M_invalidate_all();
161  return *this;
162  }
163 #endif
164 
165  // _GLIBCXX_RESOLVE_LIB_DEFECTS
166  // 133. map missing get_allocator()
167  using _Base::get_allocator;
168 
169  // iterators:
170  iterator
171  begin() _GLIBCXX_NOEXCEPT
172  { return iterator(_Base::begin(), this); }
173 
174  const_iterator
175  begin() const _GLIBCXX_NOEXCEPT
176  { return const_iterator(_Base::begin(), this); }
177 
178  iterator
179  end() _GLIBCXX_NOEXCEPT
180  { return iterator(_Base::end(), this); }
181 
182  const_iterator
183  end() const _GLIBCXX_NOEXCEPT
184  { return const_iterator(_Base::end(), this); }
185 
186  reverse_iterator
187  rbegin() _GLIBCXX_NOEXCEPT
188  { return reverse_iterator(end()); }
189 
190  const_reverse_iterator
191  rbegin() const _GLIBCXX_NOEXCEPT
192  { return const_reverse_iterator(end()); }
193 
194  reverse_iterator
195  rend() _GLIBCXX_NOEXCEPT
196  { return reverse_iterator(begin()); }
197 
198  const_reverse_iterator
199  rend() const _GLIBCXX_NOEXCEPT
200  { return const_reverse_iterator(begin()); }
201 
202 #if __cplusplus >= 201103L
203  const_iterator
204  cbegin() const noexcept
205  { return const_iterator(_Base::begin(), this); }
206 
207  const_iterator
208  cend() const noexcept
209  { return const_iterator(_Base::end(), this); }
210 
211  const_reverse_iterator
212  crbegin() const noexcept
213  { return const_reverse_iterator(end()); }
214 
215  const_reverse_iterator
216  crend() const noexcept
217  { return const_reverse_iterator(begin()); }
218 #endif
219 
220  // capacity:
221  using _Base::empty;
222  using _Base::size;
223  using _Base::max_size;
224 
225  // 23.3.1.2 element access:
226  using _Base::operator[];
227 
228  // _GLIBCXX_RESOLVE_LIB_DEFECTS
229  // DR 464. Suggestion for new member functions in standard containers.
230  using _Base::at;
231 
232  // modifiers:
233 #if __cplusplus >= 201103L
234  template<typename... _Args>
236  emplace(_Args&&... __args)
237  {
238  auto __res = _Base::emplace(std::forward<_Args>(__args)...);
239  return std::pair<iterator, bool>(iterator(__res.first, this),
240  __res.second);
241  }
242 
243  template<typename... _Args>
244  iterator
245  emplace_hint(const_iterator __pos, _Args&&... __args)
246  {
247  __glibcxx_check_insert(__pos);
248  return iterator(_Base::emplace_hint(__pos.base(),
249  std::forward<_Args>(__args)...),
250  this);
251  }
252 #endif
253 
255  insert(const value_type& __x)
256  {
257  std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
258  return std::pair<iterator, bool>(iterator(__res.first, this),
259  __res.second);
260  }
261 
262 #if __cplusplus >= 201103L
263  template<typename _Pair, typename = typename
264  std::enable_if<std::is_constructible<value_type,
265  _Pair&&>::value>::type>
267  insert(_Pair&& __x)
268  {
270  = _Base::insert(std::forward<_Pair>(__x));
271  return std::pair<iterator, bool>(iterator(__res.first, this),
272  __res.second);
273  }
274 #endif
275 
276 #if __cplusplus >= 201103L
277  void
278  insert(std::initializer_list<value_type> __list)
279  { _Base::insert(__list); }
280 #endif
281 
282  iterator
283 #if __cplusplus >= 201103L
284  insert(const_iterator __position, const value_type& __x)
285 #else
286  insert(iterator __position, const value_type& __x)
287 #endif
288  {
289  __glibcxx_check_insert(__position);
290  return iterator(_Base::insert(__position.base(), __x), this);
291  }
292 
293 #if __cplusplus >= 201103L
294  template<typename _Pair, typename = typename
295  std::enable_if<std::is_constructible<value_type,
296  _Pair&&>::value>::type>
297  iterator
298  insert(const_iterator __position, _Pair&& __x)
299  {
300  __glibcxx_check_insert(__position);
301  return iterator(_Base::insert(__position.base(),
302  std::forward<_Pair>(__x)), this);
303  }
304 #endif
305 
306  template<typename _InputIterator>
307  void
308  insert(_InputIterator __first, _InputIterator __last)
309  {
311  __glibcxx_check_valid_range2(__first, __last, __dist);
312 
313  if (__dist.second >= __gnu_debug::__dp_sign)
314  _Base::insert(__gnu_debug::__unsafe(__first),
315  __gnu_debug::__unsafe(__last));
316  else
317  _Base::insert(__first, __last);
318  }
319 
320 
321 #if __cplusplus > 201402L
322  template <typename... _Args>
324  try_emplace(const key_type& __k, _Args&&... __args)
325  {
326  auto __res = _Base::try_emplace(__k,
327  std::forward<_Args>(__args)...);
328  return { iterator(__res.first, this), __res.second };
329  }
330 
331  template <typename... _Args>
333  try_emplace(key_type&& __k, _Args&&... __args)
334  {
335  auto __res = _Base::try_emplace(std::move(__k),
336  std::forward<_Args>(__args)...);
337  return { iterator(__res.first, this), __res.second };
338  }
339 
340  template <typename... _Args>
341  iterator
342  try_emplace(const_iterator __hint, const key_type& __k,
343  _Args&&... __args)
344  {
345  __glibcxx_check_insert(__hint);
346  return iterator(_Base::try_emplace(__hint.base(), __k,
347  std::forward<_Args>(__args)...),
348  this);
349  }
350 
351  template <typename... _Args>
352  iterator
353  try_emplace(const_iterator __hint, key_type&& __k, _Args&&... __args)
354  {
355  __glibcxx_check_insert(__hint);
356  return iterator(_Base::try_emplace(__hint.base(), std::move(__k),
357  std::forward<_Args>(__args)...),
358  this);
359  }
360 
361  template <typename _Obj>
363  insert_or_assign(const key_type& __k, _Obj&& __obj)
364  {
365  auto __res = _Base::insert_or_assign(__k,
366  std::forward<_Obj>(__obj));
367  return { iterator(__res.first, this), __res.second };
368  }
369 
370  template <typename _Obj>
372  insert_or_assign(key_type&& __k, _Obj&& __obj)
373  {
374  auto __res = _Base::insert_or_assign(std::move(__k),
375  std::forward<_Obj>(__obj));
376  return { iterator(__res.first, this), __res.second };
377  }
378 
379  template <typename _Obj>
380  iterator
381  insert_or_assign(const_iterator __hint,
382  const key_type& __k, _Obj&& __obj)
383  {
384  __glibcxx_check_insert(__hint);
385  return iterator(_Base::insert_or_assign(__hint.base(), __k,
386  std::forward<_Obj>(__obj)),
387  this);
388  }
389 
390  template <typename _Obj>
391  iterator
392  insert_or_assign(const_iterator __hint, key_type&& __k, _Obj&& __obj)
393  {
394  __glibcxx_check_insert(__hint);
395  return iterator(_Base::insert_or_assign(__hint.base(),
396  std::move(__k),
397  std::forward<_Obj>(__obj)),
398  this);
399  }
400 #endif // C++17
401 
402 #if __cplusplus > 201402L
403  using node_type = typename _Base::node_type;
404 
405  struct insert_return_type
406  {
407  bool inserted;
408  iterator position;
409  node_type node;
410  };
411 
412  node_type
413  extract(const_iterator __position)
414  {
415  __glibcxx_check_erase(__position);
416  this->_M_invalidate_if(_Equal(__position.base()));
417  return _Base::extract(__position.base());
418  }
419 
420  node_type
421  extract(const key_type& __key)
422  {
423  const auto __position = find(__key);
424  if (__position != end())
425  return extract(__position);
426  return {};
427  }
428 
429  insert_return_type
430  insert(node_type&& __nh)
431  {
432  auto __ret = _Base::insert(std::move(__nh));
433  iterator __pos = iterator(__ret.position, this);
434  return { __ret.inserted, __pos, std::move(__ret.node) };
435  }
436 
437  iterator
438  insert(const_iterator __hint, node_type&& __nh)
439  {
440  __glibcxx_check_insert(__hint);
441  return iterator(_Base::insert(__hint.base(), std::move(__nh)), this);
442  }
443 
444  using _Base::merge;
445 #endif // C++17
446 
447 #if __cplusplus >= 201103L
448  iterator
449  erase(const_iterator __position)
450  {
451  __glibcxx_check_erase(__position);
452  this->_M_invalidate_if(_Equal(__position.base()));
453  return iterator(_Base::erase(__position.base()), this);
454  }
455 
456  iterator
457  erase(iterator __position)
458  { return erase(const_iterator(__position)); }
459 #else
460  void
461  erase(iterator __position)
462  {
463  __glibcxx_check_erase(__position);
464  this->_M_invalidate_if(_Equal(__position.base()));
465  _Base::erase(__position.base());
466  }
467 #endif
468 
469  size_type
470  erase(const key_type& __x)
471  {
472  _Base_iterator __victim = _Base::find(__x);
473  if (__victim == _Base::end())
474  return 0;
475  else
476  {
477  this->_M_invalidate_if(_Equal(__victim));
478  _Base::erase(__victim);
479  return 1;
480  }
481  }
482 
483 #if __cplusplus >= 201103L
484  iterator
485  erase(const_iterator __first, const_iterator __last)
486  {
487  // _GLIBCXX_RESOLVE_LIB_DEFECTS
488  // 151. can't currently clear() empty container
489  __glibcxx_check_erase_range(__first, __last);
490  for (_Base_const_iterator __victim = __first.base();
491  __victim != __last.base(); ++__victim)
492  {
493  _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
494  _M_message(__gnu_debug::__msg_valid_range)
495  ._M_iterator(__first, "first")
496  ._M_iterator(__last, "last"));
497  this->_M_invalidate_if(_Equal(__victim));
498  }
499  return iterator(_Base::erase(__first.base(), __last.base()), this);
500  }
501 #else
502  void
503  erase(iterator __first, iterator __last)
504  {
505  // _GLIBCXX_RESOLVE_LIB_DEFECTS
506  // 151. can't currently clear() empty container
507  __glibcxx_check_erase_range(__first, __last);
508  for (_Base_iterator __victim = __first.base();
509  __victim != __last.base(); ++__victim)
510  {
511  _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
512  _M_message(__gnu_debug::__msg_valid_range)
513  ._M_iterator(__first, "first")
514  ._M_iterator(__last, "last"));
515  this->_M_invalidate_if(_Equal(__victim));
516  }
517  _Base::erase(__first.base(), __last.base());
518  }
519 #endif
520 
521  void
522  swap(map& __x)
523  _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
524  {
525  _Safe::_M_swap(__x);
526  _Base::swap(__x);
527  }
528 
529  void
530  clear() _GLIBCXX_NOEXCEPT
531  {
532  this->_M_invalidate_all();
533  _Base::clear();
534  }
535 
536  // observers:
537  using _Base::key_comp;
538  using _Base::value_comp;
539 
540  // 23.3.1.3 map operations:
541  iterator
542  find(const key_type& __x)
543  { return iterator(_Base::find(__x), this); }
544 
545 #if __cplusplus > 201103L
546  template<typename _Kt,
547  typename _Req =
548  typename __has_is_transparent<_Compare, _Kt>::type>
549  iterator
550  find(const _Kt& __x)
551  { return { _Base::find(__x), this }; }
552 #endif
553 
554  const_iterator
555  find(const key_type& __x) const
556  { return const_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>
562  const_iterator
563  find(const _Kt& __x) const
564  { return { _Base::find(__x), this }; }
565 #endif
566 
567  using _Base::count;
568 
569  iterator
570  lower_bound(const key_type& __x)
571  { return iterator(_Base::lower_bound(__x), this); }
572 
573 #if __cplusplus > 201103L
574  template<typename _Kt,
575  typename _Req =
576  typename __has_is_transparent<_Compare, _Kt>::type>
577  iterator
578  lower_bound(const _Kt& __x)
579  { return { _Base::lower_bound(__x), this }; }
580 #endif
581 
582  const_iterator
583  lower_bound(const key_type& __x) const
584  { return const_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>
590  const_iterator
591  lower_bound(const _Kt& __x) const
592  { return { _Base::lower_bound(__x), this }; }
593 #endif
594 
595  iterator
596  upper_bound(const key_type& __x)
597  { return iterator(_Base::upper_bound(__x), this); }
598 
599 #if __cplusplus > 201103L
600  template<typename _Kt,
601  typename _Req =
602  typename __has_is_transparent<_Compare, _Kt>::type>
603  iterator
604  upper_bound(const _Kt& __x)
605  { return { _Base::upper_bound(__x), this }; }
606 #endif
607 
608  const_iterator
609  upper_bound(const key_type& __x) const
610  { return const_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>
616  const_iterator
617  upper_bound(const _Kt& __x) const
618  { return { _Base::upper_bound(__x), this }; }
619 #endif
620 
622  equal_range(const key_type& __x)
623  {
625  _Base::equal_range(__x);
626  return std::make_pair(iterator(__res.first, this),
627  iterator(__res.second, this));
628  }
629 
630 #if __cplusplus > 201103L
631  template<typename _Kt,
632  typename _Req =
633  typename __has_is_transparent<_Compare, _Kt>::type>
635  equal_range(const _Kt& __x)
636  {
637  auto __res = _Base::equal_range(__x);
638  return { { __res.first, this }, { __res.second, this } };
639  }
640 #endif
641 
643  equal_range(const key_type& __x) const
644  {
646  _Base::equal_range(__x);
647  return std::make_pair(const_iterator(__res.first, this),
648  const_iterator(__res.second, this));
649  }
650 
651 #if __cplusplus > 201103L
652  template<typename _Kt,
653  typename _Req =
654  typename __has_is_transparent<_Compare, _Kt>::type>
656  equal_range(const _Kt& __x) const
657  {
658  auto __res = _Base::equal_range(__x);
659  return { { __res.first, this }, { __res.second, this } };
660  }
661 #endif
662 
663  _Base&
664  _M_base() _GLIBCXX_NOEXCEPT { return *this; }
665 
666  const _Base&
667  _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
668  };
669 
670  template<typename _Key, typename _Tp,
671  typename _Compare, typename _Allocator>
672  inline bool
673  operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
675  { return __lhs._M_base() == __rhs._M_base(); }
676 
677  template<typename _Key, typename _Tp,
678  typename _Compare, typename _Allocator>
679  inline bool
680  operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
682  { return __lhs._M_base() != __rhs._M_base(); }
683 
684  template<typename _Key, typename _Tp,
685  typename _Compare, typename _Allocator>
686  inline bool
687  operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
689  { return __lhs._M_base() < __rhs._M_base(); }
690 
691  template<typename _Key, typename _Tp,
692  typename _Compare, typename _Allocator>
693  inline bool
694  operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
696  { return __lhs._M_base() <= __rhs._M_base(); }
697 
698  template<typename _Key, typename _Tp,
699  typename _Compare, typename _Allocator>
700  inline bool
701  operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
703  { return __lhs._M_base() >= __rhs._M_base(); }
704 
705  template<typename _Key, typename _Tp,
706  typename _Compare, typename _Allocator>
707  inline bool
708  operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
710  { return __lhs._M_base() > __rhs._M_base(); }
711 
712  template<typename _Key, typename _Tp,
713  typename _Compare, typename _Allocator>
714  inline void
717  _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
718  { __lhs.swap(__rhs); }
719 
720 } // namespace __debug
721 } // namespace std
722 
723 #endif
constexpr const _Tp * end(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to one past the last element of the initializer_list. ...
initializer_list
ISO C++ entities toplevel namespace is std.
Like _Safe_sequence but with a special _M_invalidate_all implementation not invalidating past-the-end...
#define __glibcxx_check_insert(_Position)
Definition: macros.h:79
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:519
Class std::map with safety/checking/debug instrumentation.
Definition: debug/map.h:44
_T1 first
second_type is the second bound type
Definition: stl_pair.h:203
Safe class dealing with some allocator dependent operations.
_T2 second
first is a copy of the first object
Definition: stl_pair.h:204
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:198
#define __glibcxx_check_erase(_Position)
Definition: macros.h:145
#define __glibcxx_check_erase_range(_First, _Last)
Definition: macros.h:173
Safe iterator wrapper.
Definition: formatter.h:56
_Iterator & base() noexcept
Return the underlying iterator.