]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/debug/map.h
set.h: Revert last change, per DR 1204 [Ready]; mention the latter in comment.
[thirdparty/gcc.git] / libstdc++-v3 / include / debug / map.h
1 // Debugging map implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
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 1204.
119 // NB: DR 675.
120 clear();
121 swap(__x);
122 return *this;
123 }
124
125 map&
126 operator=(initializer_list<value_type> __l)
127 {
128 this->clear();
129 this->insert(__l);
130 return *this;
131 }
132 #endif
133
134 // _GLIBCXX_RESOLVE_LIB_DEFECTS
135 // 133. map missing get_allocator()
136 using _Base::get_allocator;
137
138 // iterators:
139 iterator
140 begin()
141 { return iterator(_Base::begin(), this); }
142
143 const_iterator
144 begin() const
145 { return const_iterator(_Base::begin(), this); }
146
147 iterator
148 end()
149 { return iterator(_Base::end(), this); }
150
151 const_iterator
152 end() const
153 { return const_iterator(_Base::end(), this); }
154
155 reverse_iterator
156 rbegin()
157 { return reverse_iterator(end()); }
158
159 const_reverse_iterator
160 rbegin() const
161 { return const_reverse_iterator(end()); }
162
163 reverse_iterator
164 rend()
165 { return reverse_iterator(begin()); }
166
167 const_reverse_iterator
168 rend() const
169 { return const_reverse_iterator(begin()); }
170
171 #ifdef __GXX_EXPERIMENTAL_CXX0X__
172 const_iterator
173 cbegin() const
174 { return const_iterator(_Base::begin(), this); }
175
176 const_iterator
177 cend() const
178 { return const_iterator(_Base::end(), this); }
179
180 const_reverse_iterator
181 crbegin() const
182 { return const_reverse_iterator(end()); }
183
184 const_reverse_iterator
185 crend() const
186 { return const_reverse_iterator(begin()); }
187 #endif
188
189 // capacity:
190 using _Base::empty;
191 using _Base::size;
192 using _Base::max_size;
193
194 // 23.3.1.2 element access:
195 using _Base::operator[];
196
197 // _GLIBCXX_RESOLVE_LIB_DEFECTS
198 // DR 464. Suggestion for new member functions in standard containers.
199 using _Base::at;
200
201 // modifiers:
202 std::pair<iterator, bool>
203 insert(const value_type& __x)
204 {
205 typedef typename _Base::iterator _Base_iterator;
206 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
207 return std::pair<iterator, bool>(iterator(__res.first, this),
208 __res.second);
209 }
210
211 #ifdef __GXX_EXPERIMENTAL_CXX0X__
212 void
213 insert(std::initializer_list<value_type> __list)
214 { _Base::insert(__list); }
215 #endif
216
217 iterator
218 insert(iterator __position, const value_type& __x)
219 {
220 __glibcxx_check_insert(__position);
221 return iterator(_Base::insert(__position.base(), __x), this);
222 }
223
224 template<typename _InputIterator>
225 void
226 insert(_InputIterator __first, _InputIterator __last)
227 {
228 __glibcxx_check_valid_range(__first, __last);
229 _Base::insert(__first, __last);
230 }
231
232 #ifdef __GXX_EXPERIMENTAL_CXX0X__
233 iterator
234 erase(iterator __position)
235 {
236 __glibcxx_check_erase(__position);
237 __position._M_invalidate();
238 return iterator(_Base::erase(__position.base()), this);
239 }
240 #else
241 void
242 erase(iterator __position)
243 {
244 __glibcxx_check_erase(__position);
245 __position._M_invalidate();
246 _Base::erase(__position.base());
247 }
248 #endif
249
250 size_type
251 erase(const key_type& __x)
252 {
253 iterator __victim = find(__x);
254 if (__victim == end())
255 return 0;
256 else
257 {
258 __victim._M_invalidate();
259 _Base::erase(__victim.base());
260 return 1;
261 }
262 }
263
264 #ifdef __GXX_EXPERIMENTAL_CXX0X__
265 iterator
266 erase(iterator __first, iterator __last)
267 {
268 // _GLIBCXX_RESOLVE_LIB_DEFECTS
269 // 151. can't currently clear() empty container
270 __glibcxx_check_erase_range(__first, __last);
271 while (__first != __last)
272 this->erase(__first++);
273 return __last;
274 }
275 #else
276 void
277 erase(iterator __first, iterator __last)
278 {
279 // _GLIBCXX_RESOLVE_LIB_DEFECTS
280 // 151. can't currently clear() empty container
281 __glibcxx_check_erase_range(__first, __last);
282 while (__first != __last)
283 this->erase(__first++);
284 }
285 #endif
286
287 void
288 swap(map& __x)
289 {
290 _Base::swap(__x);
291 this->_M_swap(__x);
292 }
293
294 void
295 clear()
296 { this->erase(begin(), end()); }
297
298 // observers:
299 using _Base::key_comp;
300 using _Base::value_comp;
301
302 // 23.3.1.3 map operations:
303 iterator
304 find(const key_type& __x)
305 { return iterator(_Base::find(__x), this); }
306
307 const_iterator
308 find(const key_type& __x) const
309 { return const_iterator(_Base::find(__x), this); }
310
311 using _Base::count;
312
313 iterator
314 lower_bound(const key_type& __x)
315 { return iterator(_Base::lower_bound(__x), this); }
316
317 const_iterator
318 lower_bound(const key_type& __x) const
319 { return const_iterator(_Base::lower_bound(__x), this); }
320
321 iterator
322 upper_bound(const key_type& __x)
323 { return iterator(_Base::upper_bound(__x), this); }
324
325 const_iterator
326 upper_bound(const key_type& __x) const
327 { return const_iterator(_Base::upper_bound(__x), this); }
328
329 std::pair<iterator,iterator>
330 equal_range(const key_type& __x)
331 {
332 typedef typename _Base::iterator _Base_iterator;
333 std::pair<_Base_iterator, _Base_iterator> __res =
334 _Base::equal_range(__x);
335 return std::make_pair(iterator(__res.first, this),
336 iterator(__res.second, this));
337 }
338
339 std::pair<const_iterator,const_iterator>
340 equal_range(const key_type& __x) const
341 {
342 typedef typename _Base::const_iterator _Base_const_iterator;
343 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
344 _Base::equal_range(__x);
345 return std::make_pair(const_iterator(__res.first, this),
346 const_iterator(__res.second, this));
347 }
348
349 _Base&
350 _M_base() { return *this; }
351
352 const _Base&
353 _M_base() const { return *this; }
354
355 private:
356 void
357 _M_invalidate_all()
358 {
359 typedef typename _Base::const_iterator _Base_const_iterator;
360 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
361 this->_M_invalidate_if(_Not_equal(_M_base().end()));
362 }
363 };
364
365 template<typename _Key, typename _Tp,
366 typename _Compare, typename _Allocator>
367 inline bool
368 operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
369 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
370 { return __lhs._M_base() == __rhs._M_base(); }
371
372 template<typename _Key, typename _Tp,
373 typename _Compare, typename _Allocator>
374 inline bool
375 operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
376 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
377 { return __lhs._M_base() != __rhs._M_base(); }
378
379 template<typename _Key, typename _Tp,
380 typename _Compare, typename _Allocator>
381 inline bool
382 operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
383 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
384 { return __lhs._M_base() < __rhs._M_base(); }
385
386 template<typename _Key, typename _Tp,
387 typename _Compare, typename _Allocator>
388 inline bool
389 operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
390 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
391 { return __lhs._M_base() <= __rhs._M_base(); }
392
393 template<typename _Key, typename _Tp,
394 typename _Compare, typename _Allocator>
395 inline bool
396 operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
397 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
398 { return __lhs._M_base() >= __rhs._M_base(); }
399
400 template<typename _Key, typename _Tp,
401 typename _Compare, typename _Allocator>
402 inline bool
403 operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
404 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
405 { return __lhs._M_base() > __rhs._M_base(); }
406
407 template<typename _Key, typename _Tp,
408 typename _Compare, typename _Allocator>
409 inline void
410 swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
411 map<_Key, _Tp, _Compare, _Allocator>& __rhs)
412 { __lhs.swap(__rhs); }
413
414 } // namespace __debug
415 } // namespace std
416
417 #endif