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 : _Base(std::move(__x)), _Safe_base()
99 { this->_M_swap(__x); }
101 map(initializer_list<value_type> __l,
102 const _Compare& __c = _Compare(),
103 const allocator_type& __a = allocator_type())
104 : _Base(__l, __c, __a), _Safe_base() { }
110 operator=(const map& __x)
112 *static_cast<_Base*>(this) = __x;
113 this->_M_invalidate_all();
117 #ifdef __GXX_EXPERIMENTAL_CXX0X__
129 operator=(initializer_list<value_type> __l)
137 // _GLIBCXX_RESOLVE_LIB_DEFECTS
138 // 133. map missing get_allocator()
139 using _Base::get_allocator;
144 { return iterator(_Base::begin(), this); }
148 { return const_iterator(_Base::begin(), this); }
152 { return iterator(_Base::end(), this); }
156 { return const_iterator(_Base::end(), this); }
160 { return reverse_iterator(end()); }
162 const_reverse_iterator
164 { return const_reverse_iterator(end()); }
168 { return reverse_iterator(begin()); }
170 const_reverse_iterator
172 { return const_reverse_iterator(begin()); }
174 #ifdef __GXX_EXPERIMENTAL_CXX0X__
177 { return const_iterator(_Base::begin(), this); }
181 { return const_iterator(_Base::end(), this); }
183 const_reverse_iterator
185 { return const_reverse_iterator(end()); }
187 const_reverse_iterator
189 { return const_reverse_iterator(begin()); }
195 using _Base::max_size;
197 // 23.3.1.2 element access:
198 using _Base::operator[];
200 // _GLIBCXX_RESOLVE_LIB_DEFECTS
201 // DR 464. Suggestion for new member functions in standard containers.
205 std::pair<iterator, bool>
206 insert(const value_type& __x)
208 typedef typename _Base::iterator _Base_iterator;
209 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
210 return std::pair<iterator, bool>(iterator(__res.first, this),
214 #ifdef __GXX_EXPERIMENTAL_CXX0X__
215 template<typename _Pair, typename = typename
216 std::enable_if<std::is_convertible<_Pair,
217 value_type>::value>::type>
218 std::pair<iterator, bool>
221 typedef typename _Base::iterator _Base_iterator;
222 std::pair<_Base_iterator, bool> __res
223 = _Base::insert(std::forward<_Pair>(__x));
224 return std::pair<iterator, bool>(iterator(__res.first, this),
229 #ifdef __GXX_EXPERIMENTAL_CXX0X__
231 insert(std::initializer_list<value_type> __list)
232 { _Base::insert(__list); }
236 #ifdef __GXX_EXPERIMENTAL_CXX0X__
237 insert(const_iterator __position, const value_type& __x)
239 insert(iterator __position, const value_type& __x)
242 __glibcxx_check_insert(__position);
243 return iterator(_Base::insert(__position.base(), __x), this);
246 #ifdef __GXX_EXPERIMENTAL_CXX0X__
247 template<typename _Pair, typename = typename
248 std::enable_if<std::is_convertible<_Pair,
249 value_type>::value>::type>
251 insert(const_iterator __position, _Pair&& __x)
253 __glibcxx_check_insert(__position);
254 return iterator(_Base::insert(__position.base(),
255 std::forward<_Pair>(__x)), this);
259 template<typename _InputIterator>
261 insert(_InputIterator __first, _InputIterator __last)
263 __glibcxx_check_valid_range(__first, __last);
264 _Base::insert(__gnu_debug::__base(__first),
265 __gnu_debug::__base(__last));
268 #ifdef __GXX_EXPERIMENTAL_CXX0X__
270 erase(const_iterator __position)
272 __glibcxx_check_erase(__position);
273 this->_M_invalidate_if(_Equal(__position.base()));
274 return iterator(_Base::erase(__position.base()), this);
278 erase(iterator __position)
280 __glibcxx_check_erase(__position);
281 this->_M_invalidate_if(_Equal(__position.base()));
282 _Base::erase(__position.base());
287 erase(const key_type& __x)
289 _Base_iterator __victim = _Base::find(__x);
290 if (__victim == _Base::end())
294 this->_M_invalidate_if(_Equal(__victim));
295 _Base::erase(__victim);
300 #ifdef __GXX_EXPERIMENTAL_CXX0X__
302 erase(const_iterator __first, const_iterator __last)
304 // _GLIBCXX_RESOLVE_LIB_DEFECTS
305 // 151. can't currently clear() empty container
306 __glibcxx_check_erase_range(__first, __last);
307 for (_Base_const_iterator __victim = __first.base();
308 __victim != __last.base(); ++__victim)
310 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
311 _M_message(__gnu_debug::__msg_valid_range)
312 ._M_iterator(__first, "first")
313 ._M_iterator(__last, "last"));
314 this->_M_invalidate_if(_Equal(__victim));
316 return iterator(_Base::erase(__first.base(), __last.base()), this);
320 erase(iterator __first, iterator __last)
322 // _GLIBCXX_RESOLVE_LIB_DEFECTS
323 // 151. can't currently clear() empty container
324 __glibcxx_check_erase_range(__first, __last);
325 for (_Base_iterator __victim = __first.base();
326 __victim != __last.base(); ++__victim)
328 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
329 _M_message(__gnu_debug::__msg_valid_range)
330 ._M_iterator(__first, "first")
331 ._M_iterator(__last, "last"));
332 this->_M_invalidate_if(_Equal(__victim));
334 _Base::erase(__first.base(), __last.base());
348 this->_M_invalidate_all();
353 using _Base::key_comp;
354 using _Base::value_comp;
356 // 23.3.1.3 map operations:
358 find(const key_type& __x)
359 { return iterator(_Base::find(__x), this); }
362 find(const key_type& __x) const
363 { return const_iterator(_Base::find(__x), this); }
368 lower_bound(const key_type& __x)
369 { return iterator(_Base::lower_bound(__x), this); }
372 lower_bound(const key_type& __x) const
373 { return const_iterator(_Base::lower_bound(__x), this); }
376 upper_bound(const key_type& __x)
377 { return iterator(_Base::upper_bound(__x), this); }
380 upper_bound(const key_type& __x) const
381 { return const_iterator(_Base::upper_bound(__x), this); }
383 std::pair<iterator,iterator>
384 equal_range(const key_type& __x)
386 typedef typename _Base::iterator _Base_iterator;
387 std::pair<_Base_iterator, _Base_iterator> __res =
388 _Base::equal_range(__x);
389 return std::make_pair(iterator(__res.first, this),
390 iterator(__res.second, this));
393 std::pair<const_iterator,const_iterator>
394 equal_range(const key_type& __x) const
396 typedef typename _Base::const_iterator _Base_const_iterator;
397 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
398 _Base::equal_range(__x);
399 return std::make_pair(const_iterator(__res.first, this),
400 const_iterator(__res.second, this));
404 _M_base() { return *this; }
407 _M_base() const { return *this; }
413 typedef typename _Base::const_iterator _Base_const_iterator;
414 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
415 this->_M_invalidate_if(_Not_equal(_M_base().end()));
419 template<typename _Key, typename _Tp,
420 typename _Compare, typename _Allocator>
422 operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
423 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
424 { return __lhs._M_base() == __rhs._M_base(); }
426 template<typename _Key, typename _Tp,
427 typename _Compare, typename _Allocator>
429 operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
430 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
431 { return __lhs._M_base() != __rhs._M_base(); }
433 template<typename _Key, typename _Tp,
434 typename _Compare, typename _Allocator>
436 operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
437 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
438 { return __lhs._M_base() < __rhs._M_base(); }
440 template<typename _Key, typename _Tp,
441 typename _Compare, typename _Allocator>
443 operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
444 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
445 { return __lhs._M_base() <= __rhs._M_base(); }
447 template<typename _Key, typename _Tp,
448 typename _Compare, typename _Allocator>
450 operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
451 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
452 { return __lhs._M_base() >= __rhs._M_base(); }
454 template<typename _Key, typename _Tp,
455 typename _Compare, typename _Allocator>
457 operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
458 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
459 { return __lhs._M_base() > __rhs._M_base(); }
461 template<typename _Key, typename _Tp,
462 typename _Compare, typename _Allocator>
464 swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
465 map<_Key, _Tp, _Compare, _Allocator>& __rhs)
466 { __lhs.swap(__rhs); }
468 } // namespace __debug