1 // Debugging map implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 * This file is a GNU debug extension to the Standard C++ Library.
35 #ifndef _GLIBCXX_DEBUG_MAP_H
36 #define _GLIBCXX_DEBUG_MAP_H 1
38 #include <debug/safe_sequence.h>
39 #include <debug/safe_iterator.h>
46 template<typename _Key
, typename _Tp
, typename _Compare
= std::less
<_Key
>,
47 typename _Allocator
= std::allocator
<std::pair
<const _Key
, _Tp
> > >
49 : public _GLIBCXX_STD::map
<_Key
, _Tp
, _Compare
, _Allocator
>,
50 public __gnu_debug::_Safe_sequence
<map
<_Key
, _Tp
, _Compare
, _Allocator
> >
52 typedef _GLIBCXX_STD::map
<_Key
, _Tp
, _Compare
, _Allocator
> _Base
;
53 typedef __gnu_debug::_Safe_sequence
<map
> _Safe_base
;
57 typedef _Key key_type
;
58 typedef _Tp mapped_type
;
59 typedef std::pair
<const _Key
, _Tp
> value_type
;
60 typedef _Compare key_compare
;
61 typedef _Allocator allocator_type
;
62 typedef typename
_Base::reference reference
;
63 typedef typename
_Base::const_reference const_reference
;
65 typedef __gnu_debug::_Safe_iterator
<typename
_Base::iterator
, map
>
67 typedef __gnu_debug::_Safe_iterator
<typename
_Base::const_iterator
, map
>
70 typedef typename
_Base::size_type size_type
;
71 typedef typename
_Base::difference_type difference_type
;
72 typedef typename
_Base::pointer pointer
;
73 typedef typename
_Base::const_pointer const_pointer
;
74 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
75 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
77 using _Base::value_compare
;
79 // 23.3.1.1 construct/copy/destroy:
80 explicit map(const _Compare
& __comp
= _Compare(),
81 const _Allocator
& __a
= _Allocator())
82 : _Base(__comp
, __a
) { }
84 template<typename _InputIterator
>
85 map(_InputIterator __first
, _InputIterator __last
,
86 const _Compare
& __comp
= _Compare(),
87 const _Allocator
& __a
= _Allocator())
88 : _Base(__gnu_debug::__check_valid_range(__first
, __last
), __last
,
89 __comp
, __a
), _Safe_base() { }
91 map(const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __x
)
92 : _Base(__x
), _Safe_base() { }
94 map(const _Base
& __x
) : _Base(__x
), _Safe_base() { }
98 map
<_Key
,_Tp
,_Compare
,_Allocator
>&
99 operator=(const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __x
)
101 *static_cast<_Base
*>(this) = __x
;
102 this->_M_invalidate_all();
106 // _GLIBCXX_RESOLVE_LIB_DEFECTS
107 // 133. map missing get_allocator()
108 using _Base::get_allocator
;
113 { return iterator(_Base::begin(), this); }
117 { return const_iterator(_Base::begin(), this); }
121 { return iterator(_Base::end(), this); }
125 { return const_iterator(_Base::end(), this); }
129 { return reverse_iterator(end()); }
131 const_reverse_iterator
133 { return const_reverse_iterator(end()); }
137 { return reverse_iterator(begin()); }
139 const_reverse_iterator
141 { return const_reverse_iterator(begin()); }
146 using _Base::max_size
;
148 // 23.3.1.2 element access:
149 using _Base::operator[];
151 // _GLIBCXX_RESOLVE_LIB_DEFECTS
152 // DR 464. Suggestion for new member functions in standard containers.
156 std::pair
<iterator
, bool>
157 insert(const value_type
& __x
)
159 typedef typename
_Base::iterator _Base_iterator
;
160 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
161 return std::pair
<iterator
, bool>(iterator(__res
.first
, this),
166 insert(iterator __position
, const value_type
& __x
)
168 __glibcxx_check_insert(__position
);
169 return iterator(_Base::insert(__position
.base(), __x
), this);
172 template<typename _InputIterator
>
174 insert(_InputIterator __first
, _InputIterator __last
)
176 __glibcxx_check_valid_range(__first
, __last
);
177 _Base::insert(__first
, __last
);
181 erase(iterator __position
)
183 __glibcxx_check_erase(__position
);
184 __position
._M_invalidate();
185 _Base::erase(__position
.base());
189 erase(const key_type
& __x
)
191 iterator __victim
= find(__x
);
192 if (__victim
== end())
196 __victim
._M_invalidate();
197 _Base::erase(__victim
.base());
203 erase(iterator __first
, iterator __last
)
205 // _GLIBCXX_RESOLVE_LIB_DEFECTS
206 // 151. can't currently clear() empty container
207 __glibcxx_check_erase_range(__first
, __last
);
208 while (__first
!= __last
)
209 this->erase(__first
++);
213 swap(map
<_Key
,_Tp
,_Compare
,_Allocator
>& __x
)
221 { this->erase(begin(), end()); }
224 using _Base::key_comp
;
225 using _Base::value_comp
;
227 // 23.3.1.3 map operations:
229 find(const key_type
& __x
)
230 { return iterator(_Base::find(__x
), this); }
233 find(const key_type
& __x
) const
234 { return const_iterator(_Base::find(__x
), this); }
239 lower_bound(const key_type
& __x
)
240 { return iterator(_Base::lower_bound(__x
), this); }
243 lower_bound(const key_type
& __x
) const
244 { return const_iterator(_Base::lower_bound(__x
), this); }
247 upper_bound(const key_type
& __x
)
248 { return iterator(_Base::upper_bound(__x
), this); }
251 upper_bound(const key_type
& __x
) const
252 { return const_iterator(_Base::upper_bound(__x
), this); }
254 std::pair
<iterator
,iterator
>
255 equal_range(const key_type
& __x
)
257 typedef typename
_Base::iterator _Base_iterator
;
258 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
259 _Base::equal_range(__x
);
260 return std::make_pair(iterator(__res
.first
, this),
261 iterator(__res
.second
, this));
264 std::pair
<const_iterator
,const_iterator
>
265 equal_range(const key_type
& __x
) const
267 typedef typename
_Base::const_iterator _Base_const_iterator
;
268 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
269 _Base::equal_range(__x
);
270 return std::make_pair(const_iterator(__res
.first
, this),
271 const_iterator(__res
.second
, this));
275 _M_base() { return *this; }
278 _M_base() const { return *this; }
284 typedef typename
_Base::const_iterator _Base_const_iterator
;
285 typedef __gnu_debug::_Not_equal_to
<_Base_const_iterator
> _Not_equal
;
286 this->_M_invalidate_if(_Not_equal(_M_base().end()));
290 template<typename _Key
,typename _Tp
,typename _Compare
,typename _Allocator
>
292 operator==(const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __lhs
,
293 const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __rhs
)
294 { return __lhs
._M_base() == __rhs
._M_base(); }
296 template<typename _Key
,typename _Tp
,typename _Compare
,typename _Allocator
>
298 operator!=(const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __lhs
,
299 const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __rhs
)
300 { return __lhs
._M_base() != __rhs
._M_base(); }
302 template<typename _Key
,typename _Tp
,typename _Compare
,typename _Allocator
>
304 operator<(const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __lhs
,
305 const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __rhs
)
306 { return __lhs
._M_base() < __rhs
._M_base(); }
308 template<typename _Key
,typename _Tp
,typename _Compare
,typename _Allocator
>
310 operator<=(const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __lhs
,
311 const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __rhs
)
312 { return __lhs
._M_base() <= __rhs
._M_base(); }
314 template<typename _Key
,typename _Tp
,typename _Compare
,typename _Allocator
>
316 operator>=(const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __lhs
,
317 const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __rhs
)
318 { return __lhs
._M_base() >= __rhs
._M_base(); }
320 template<typename _Key
,typename _Tp
,typename _Compare
,typename _Allocator
>
322 operator>(const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __lhs
,
323 const map
<_Key
,_Tp
,_Compare
,_Allocator
>& __rhs
)
324 { return __lhs
._M_base() > __rhs
._M_base(); }
326 template<typename _Key
,typename _Tp
,typename _Compare
,typename _Allocator
>
328 swap(map
<_Key
,_Tp
,_Compare
,_Allocator
>& __lhs
,
329 map
<_Key
,_Tp
,_Compare
,_Allocator
>& __rhs
)
330 { __lhs
.swap(__rhs
); }
331 } // namespace __debug