2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / debug / map.h
blob75dd340b56f27861d473c7c186e21f71f87038ac
1 // Debugging map implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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.
31 #ifndef _GLIBCXX_DEBUG_MAP_H
32 #define _GLIBCXX_DEBUG_MAP_H 1
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
36 #include <utility>
38 namespace std
40 namespace __gnu_debug_def
42 template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
43 typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
44 class map
45 : public _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator>,
46 public __gnu_debug::_Safe_sequence<map<_Key, _Tp, _Compare, _Allocator> >
48 typedef _GLIBCXX_STD::map<_Key, _Tp, _Compare, _Allocator> _Base;
49 typedef __gnu_debug::_Safe_sequence<map> _Safe_base;
51 public:
52 // types:
53 typedef _Key key_type;
54 typedef _Tp mapped_type;
55 typedef std::pair<const _Key, _Tp> value_type;
56 typedef _Compare key_compare;
57 typedef _Allocator allocator_type;
58 typedef typename _Base::reference reference;
59 typedef typename _Base::const_reference const_reference;
61 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, map>
62 iterator;
63 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, map>
64 const_iterator;
66 typedef typename _Base::size_type size_type;
67 typedef typename _Base::difference_type difference_type;
68 typedef typename _Base::pointer pointer;
69 typedef typename _Base::const_pointer const_pointer;
70 typedef std::reverse_iterator<iterator> reverse_iterator;
71 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
73 using _Base::value_compare;
75 // 23.3.1.1 construct/copy/destroy:
76 explicit map(const _Compare& __comp = _Compare(),
77 const _Allocator& __a = _Allocator())
78 : _Base(__comp, __a) { }
80 template<typename _InputIterator>
81 map(_InputIterator __first, _InputIterator __last,
82 const _Compare& __comp = _Compare(),
83 const _Allocator& __a = _Allocator())
84 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
85 __comp, __a), _Safe_base() { }
87 map(const map<_Key,_Tp,_Compare,_Allocator>& __x)
88 : _Base(__x), _Safe_base() { }
90 map(const _Base& __x) : _Base(__x), _Safe_base() { }
92 ~map() { }
94 map<_Key,_Tp,_Compare,_Allocator>&
95 operator=(const map<_Key,_Tp,_Compare,_Allocator>& __x)
97 *static_cast<_Base*>(this) = __x;
98 this->_M_invalidate_all();
99 return *this;
102 // _GLIBCXX_RESOLVE_LIB_DEFECTS
103 // 133. map missing get_allocator()
104 using _Base::get_allocator;
106 // iterators:
107 iterator
108 begin()
109 { return iterator(_Base::begin(), this); }
111 const_iterator
112 begin() const
113 { return const_iterator(_Base::begin(), this); }
115 iterator
116 end()
117 { return iterator(_Base::end(), this); }
119 const_iterator
120 end() const
121 { return const_iterator(_Base::end(), this); }
123 reverse_iterator
124 rbegin()
125 { return reverse_iterator(end()); }
127 const_reverse_iterator
128 rbegin() const
129 { return const_reverse_iterator(end()); }
131 reverse_iterator
132 rend()
133 { return reverse_iterator(begin()); }
135 const_reverse_iterator
136 rend() const
137 { return const_reverse_iterator(begin()); }
139 // capacity:
140 using _Base::empty;
141 using _Base::size;
142 using _Base::max_size;
144 // 23.3.1.2 element access:
145 using _Base::operator[];
147 // _GLIBCXX_RESOLVE_LIB_DEFECTS
148 // DR 464. Suggestion for new member functions in standard containers.
149 using _Base::at;
151 // modifiers:
152 std::pair<iterator, bool>
153 insert(const value_type& __x)
155 typedef typename _Base::iterator _Base_iterator;
156 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
157 return std::pair<iterator, bool>(iterator(__res.first, this),
158 __res.second);
161 iterator
162 insert(iterator __position, const value_type& __x)
164 __glibcxx_check_insert(__position);
165 return iterator(_Base::insert(__position.base(), __x), this);
168 template<typename _InputIterator>
169 void
170 insert(_InputIterator __first, _InputIterator __last)
172 __glibcxx_check_valid_range(__first, __last);
173 _Base::insert(__first, __last);
176 void
177 erase(iterator __position)
179 __glibcxx_check_erase(__position);
180 __position._M_invalidate();
181 _Base::erase(__position.base());
184 size_type
185 erase(const key_type& __x)
187 iterator __victim = find(__x);
188 if (__victim == end())
189 return 0;
190 else
192 __victim._M_invalidate();
193 _Base::erase(__victim.base());
194 return 1;
198 void
199 erase(iterator __first, iterator __last)
201 // _GLIBCXX_RESOLVE_LIB_DEFECTS
202 // 151. can't currently clear() empty container
203 __glibcxx_check_erase_range(__first, __last);
204 while (__first != __last)
205 this->erase(__first++);
208 void
209 swap(map<_Key,_Tp,_Compare,_Allocator>& __x)
211 _Base::swap(__x);
212 this->_M_swap(__x);
215 void
216 clear()
217 { this->erase(begin(), end()); }
219 // observers:
220 using _Base::key_comp;
221 using _Base::value_comp;
223 // 23.3.1.3 map operations:
224 iterator
225 find(const key_type& __x)
226 { return iterator(_Base::find(__x), this); }
228 const_iterator
229 find(const key_type& __x) const
230 { return const_iterator(_Base::find(__x), this); }
232 using _Base::count;
234 iterator
235 lower_bound(const key_type& __x)
236 { return iterator(_Base::lower_bound(__x), this); }
238 const_iterator
239 lower_bound(const key_type& __x) const
240 { return const_iterator(_Base::lower_bound(__x), this); }
242 iterator
243 upper_bound(const key_type& __x)
244 { return iterator(_Base::upper_bound(__x), this); }
246 const_iterator
247 upper_bound(const key_type& __x) const
248 { return const_iterator(_Base::upper_bound(__x), this); }
250 std::pair<iterator,iterator>
251 equal_range(const key_type& __x)
253 typedef typename _Base::iterator _Base_iterator;
254 std::pair<_Base_iterator, _Base_iterator> __res =
255 _Base::equal_range(__x);
256 return std::make_pair(iterator(__res.first, this),
257 iterator(__res.second, this));
260 std::pair<const_iterator,const_iterator>
261 equal_range(const key_type& __x) const
263 typedef typename _Base::const_iterator _Base_const_iterator;
264 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
265 _Base::equal_range(__x);
266 return std::make_pair(const_iterator(__res.first, this),
267 const_iterator(__res.second, this));
270 _Base&
271 _M_base() { return *this; }
273 const _Base&
274 _M_base() const { return *this; }
276 private:
277 void
278 _M_invalidate_all()
280 typedef typename _Base::const_iterator _Base_const_iterator;
281 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
282 this->_M_invalidate_if(_Not_equal(_M_base().end()));
286 template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
287 inline bool
288 operator==(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
289 const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
290 { return __lhs._M_base() == __rhs._M_base(); }
292 template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
293 inline bool
294 operator!=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
295 const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
296 { return __lhs._M_base() != __rhs._M_base(); }
298 template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
299 inline bool
300 operator<(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
301 const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
302 { return __lhs._M_base() < __rhs._M_base(); }
304 template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
305 inline bool
306 operator<=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
307 const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
308 { return __lhs._M_base() <= __rhs._M_base(); }
310 template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
311 inline bool
312 operator>=(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
313 const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
314 { return __lhs._M_base() >= __rhs._M_base(); }
316 template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
317 inline bool
318 operator>(const map<_Key,_Tp,_Compare,_Allocator>& __lhs,
319 const map<_Key,_Tp,_Compare,_Allocator>& __rhs)
320 { return __lhs._M_base() > __rhs._M_base(); }
322 template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
323 inline void
324 swap(map<_Key,_Tp,_Compare,_Allocator>& __lhs,
325 map<_Key,_Tp,_Compare,_Allocator>& __rhs)
326 { __lhs.swap(__rhs); }
327 } // namespace __gnu_debug_def
328 } // namespace std
330 #endif