]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/debug/map.h
bitset: Add doxygen markup.
[thirdparty/gcc.git] / libstdc++-v3 / include / debug / map.h
1 // Debugging map implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009
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 3, or (at your option)
10 // any later version.
11
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.
16
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.
20
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/>.
25
26 /** @file debug/map.h
27 * This file is a GNU debug extension to the Standard C++ Library.
28 */
29
30 #ifndef _GLIBCXX_DEBUG_MAP_H
31 #define _GLIBCXX_DEBUG_MAP_H 1
32
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
35 #include <utility>
36
37 namespace std
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
45 : public _GLIBCXX_STD_D::map<_Key, _Tp, _Compare, _Allocator>,
46 public __gnu_debug::_Safe_sequence<map<_Key, _Tp, _Compare, _Allocator> >
47 {
48 typedef _GLIBCXX_STD_D::map<_Key, _Tp, _Compare, _Allocator> _Base;
49 typedef __gnu_debug::_Safe_sequence<map> _Safe_base;
50
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;
60
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;
65
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;
72
73 using _Base::value_compare;
74
75 // 23.3.1.1 construct/copy/destroy:
76 explicit map(const _Compare& __comp = _Compare(),
77 const _Allocator& __a = _Allocator())
78 : _Base(__comp, __a) { }
79
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() { }
86
87 map(const map& __x)
88 : _Base(__x), _Safe_base() { }
89
90 map(const _Base& __x)
91 : _Base(__x), _Safe_base() { }
92
93 #ifdef __GXX_EXPERIMENTAL_CXX0X__
94 map(map&& __x)
95 : _Base(std::forward<map>(__x)), _Safe_base()
96 { this->_M_swap(__x); }
97
98 map(initializer_list<value_type> __l,
99 const _Compare& __c = _Compare(),
100 const allocator_type& __a = allocator_type())
101 : _Base(__l, __c, __a), _Safe_base() { }
102 #endif
103
104 ~map() { }
105
106 map&
107 operator=(const map& __x)
108 {
109 *static_cast<_Base*>(this) = __x;
110 this->_M_invalidate_all();
111 return *this;
112 }
113
114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
115 map&
116 operator=(map&& __x)
117 {
118 // NB: DR 675.
119 clear();
120 swap(__x);
121 return *this;
122 }
123
124 map&
125 operator=(initializer_list<value_type> __l)
126 {
127 this->clear();
128 this->insert(__l);
129 return *this;
130 }
131 #endif
132
133 // _GLIBCXX_RESOLVE_LIB_DEFECTS
134 // 133. map missing get_allocator()
135 using _Base::get_allocator;
136
137 // iterators:
138 iterator
139 begin()
140 { return iterator(_Base::begin(), this); }
141
142 const_iterator
143 begin() const
144 { return const_iterator(_Base::begin(), this); }
145
146 iterator
147 end()
148 { return iterator(_Base::end(), this); }
149
150 const_iterator
151 end() const
152 { return const_iterator(_Base::end(), this); }
153
154 reverse_iterator
155 rbegin()
156 { return reverse_iterator(end()); }
157
158 const_reverse_iterator
159 rbegin() const
160 { return const_reverse_iterator(end()); }
161
162 reverse_iterator
163 rend()
164 { return reverse_iterator(begin()); }
165
166 const_reverse_iterator
167 rend() const
168 { return const_reverse_iterator(begin()); }
169
170 #ifdef __GXX_EXPERIMENTAL_CXX0X__
171 const_iterator
172 cbegin() const
173 { return const_iterator(_Base::begin(), this); }
174
175 const_iterator
176 cend() const
177 { return const_iterator(_Base::end(), this); }
178
179 const_reverse_iterator
180 crbegin() const
181 { return const_reverse_iterator(end()); }
182
183 const_reverse_iterator
184 crend() const
185 { return const_reverse_iterator(begin()); }
186 #endif
187
188 // capacity:
189 using _Base::empty;
190 using _Base::size;
191 using _Base::max_size;
192
193 // 23.3.1.2 element access:
194 using _Base::operator[];
195
196 // _GLIBCXX_RESOLVE_LIB_DEFECTS
197 // DR 464. Suggestion for new member functions in standard containers.
198 using _Base::at;
199
200 // modifiers:
201 std::pair<iterator, bool>
202 insert(const value_type& __x)
203 {
204 typedef typename _Base::iterator _Base_iterator;
205 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
206 return std::pair<iterator, bool>(iterator(__res.first, this),
207 __res.second);
208 }
209
210 #ifdef __GXX_EXPERIMENTAL_CXX0X__
211 void
212 insert(std::initializer_list<value_type> __list)
213 { _Base::insert(__list); }
214 #endif
215
216 iterator
217 insert(iterator __position, const value_type& __x)
218 {
219 __glibcxx_check_insert(__position);
220 return iterator(_Base::insert(__position.base(), __x), this);
221 }
222
223 template<typename _InputIterator>
224 void
225 insert(_InputIterator __first, _InputIterator __last)
226 {
227 __glibcxx_check_valid_range(__first, __last);
228 _Base::insert(__first, __last);
229 }
230
231 void
232 erase(iterator __position)
233 {
234 __glibcxx_check_erase(__position);
235 __position._M_invalidate();
236 _Base::erase(__position.base());
237 }
238
239 size_type
240 erase(const key_type& __x)
241 {
242 iterator __victim = find(__x);
243 if (__victim == end())
244 return 0;
245 else
246 {
247 __victim._M_invalidate();
248 _Base::erase(__victim.base());
249 return 1;
250 }
251 }
252
253 void
254 erase(iterator __first, iterator __last)
255 {
256 // _GLIBCXX_RESOLVE_LIB_DEFECTS
257 // 151. can't currently clear() empty container
258 __glibcxx_check_erase_range(__first, __last);
259 while (__first != __last)
260 this->erase(__first++);
261 }
262
263 void
264 swap(map& __x)
265 {
266 _Base::swap(__x);
267 this->_M_swap(__x);
268 }
269
270 void
271 clear()
272 { this->erase(begin(), end()); }
273
274 // observers:
275 using _Base::key_comp;
276 using _Base::value_comp;
277
278 // 23.3.1.3 map operations:
279 iterator
280 find(const key_type& __x)
281 { return iterator(_Base::find(__x), this); }
282
283 const_iterator
284 find(const key_type& __x) const
285 { return const_iterator(_Base::find(__x), this); }
286
287 using _Base::count;
288
289 iterator
290 lower_bound(const key_type& __x)
291 { return iterator(_Base::lower_bound(__x), this); }
292
293 const_iterator
294 lower_bound(const key_type& __x) const
295 { return const_iterator(_Base::lower_bound(__x), this); }
296
297 iterator
298 upper_bound(const key_type& __x)
299 { return iterator(_Base::upper_bound(__x), this); }
300
301 const_iterator
302 upper_bound(const key_type& __x) const
303 { return const_iterator(_Base::upper_bound(__x), this); }
304
305 std::pair<iterator,iterator>
306 equal_range(const key_type& __x)
307 {
308 typedef typename _Base::iterator _Base_iterator;
309 std::pair<_Base_iterator, _Base_iterator> __res =
310 _Base::equal_range(__x);
311 return std::make_pair(iterator(__res.first, this),
312 iterator(__res.second, this));
313 }
314
315 std::pair<const_iterator,const_iterator>
316 equal_range(const key_type& __x) const
317 {
318 typedef typename _Base::const_iterator _Base_const_iterator;
319 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
320 _Base::equal_range(__x);
321 return std::make_pair(const_iterator(__res.first, this),
322 const_iterator(__res.second, this));
323 }
324
325 _Base&
326 _M_base() { return *this; }
327
328 const _Base&
329 _M_base() const { return *this; }
330
331 private:
332 void
333 _M_invalidate_all()
334 {
335 typedef typename _Base::const_iterator _Base_const_iterator;
336 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
337 this->_M_invalidate_if(_Not_equal(_M_base().end()));
338 }
339 };
340
341 template<typename _Key, typename _Tp,
342 typename _Compare, typename _Allocator>
343 inline bool
344 operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
345 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
346 { return __lhs._M_base() == __rhs._M_base(); }
347
348 template<typename _Key, typename _Tp,
349 typename _Compare, typename _Allocator>
350 inline bool
351 operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
352 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
353 { return __lhs._M_base() != __rhs._M_base(); }
354
355 template<typename _Key, typename _Tp,
356 typename _Compare, typename _Allocator>
357 inline bool
358 operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
359 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
360 { return __lhs._M_base() < __rhs._M_base(); }
361
362 template<typename _Key, typename _Tp,
363 typename _Compare, typename _Allocator>
364 inline bool
365 operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
366 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
367 { return __lhs._M_base() <= __rhs._M_base(); }
368
369 template<typename _Key, typename _Tp,
370 typename _Compare, typename _Allocator>
371 inline bool
372 operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
373 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
374 { return __lhs._M_base() >= __rhs._M_base(); }
375
376 template<typename _Key, typename _Tp,
377 typename _Compare, typename _Allocator>
378 inline bool
379 operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
380 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
381 { return __lhs._M_base() > __rhs._M_base(); }
382
383 template<typename _Key, typename _Tp,
384 typename _Compare, typename _Allocator>
385 inline void
386 swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
387 map<_Key, _Tp, _Compare, _Allocator>& __rhs)
388 { __lhs.swap(__rhs); }
389
390 } // namespace __debug
391 } // namespace std
392
393 #endif