]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/debug/unordered_map
[multiple changes]
[thirdparty/gcc.git] / libstdc++-v3 / include / debug / unordered_map
1 // Debugging unordered_map/unordered_multimap implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 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/unordered_map
27 * This file is a GNU debug extension to the Standard C++ Library.
28 */
29
30 #ifndef _GLIBCXX_DEBUG_UNORDERED_MAP
31 #define _GLIBCXX_DEBUG_UNORDERED_MAP 1
32
33 #ifdef __GXX_EXPERIMENTAL_CXX0X__
34 # include <unordered_map>
35 #else
36 # include <bits/c++0x_warning.h>
37 #endif
38
39 #include <debug/safe_sequence.h>
40 #include <debug/safe_iterator.h>
41 #include <initializer_list>
42
43 namespace std
44 {
45 namespace __debug
46 {
47 /// Class std::unordered_map with safety/checking/debug instrumentation.
48 template<typename _Key, typename _Tp,
49 typename _Hash = std::hash<_Key>,
50 typename _Pred = std::equal_to<_Key>,
51 typename _Alloc = std::allocator<_Key> >
52 class unordered_map
53 : public _GLIBCXX_STD_D::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>,
54 public __gnu_debug::_Safe_sequence<unordered_map<_Key, _Tp, _Hash,
55 _Pred, _Alloc> >
56 {
57 typedef _GLIBCXX_STD_D::unordered_map<_Key, _Tp, _Hash,
58 _Pred, _Alloc> _Base;
59 typedef __gnu_debug::_Safe_sequence<unordered_map> _Safe_base;
60
61 public:
62 typedef typename _Base::size_type size_type;
63 typedef typename _Base::hasher hasher;
64 typedef typename _Base::key_equal key_equal;
65 typedef typename _Base::allocator_type allocator_type;
66
67 typedef typename _Base::key_type key_type;
68 typedef typename _Base::value_type value_type;
69
70 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
71 unordered_map> iterator;
72 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
73 unordered_map> const_iterator;
74
75 explicit
76 unordered_map(size_type __n = 10,
77 const hasher& __hf = hasher(),
78 const key_equal& __eql = key_equal(),
79 const allocator_type& __a = allocator_type())
80 : _Base(__n, __hf, __eql, __a) { }
81
82 template<typename _InputIterator>
83 unordered_map(_InputIterator __f, _InputIterator __l,
84 size_type __n = 10,
85 const hasher& __hf = hasher(),
86 const key_equal& __eql = key_equal(),
87 const allocator_type& __a = allocator_type())
88 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
89 __hf, __eql, __a), _Safe_base() { }
90
91 unordered_map(const unordered_map& __x)
92 : _Base(__x), _Safe_base() { }
93
94 unordered_map(const _Base& __x)
95 : _Base(__x), _Safe_base() { }
96
97 unordered_map(unordered_map&& __x)
98 : _Base(std::forward<unordered_map>(__x)), _Safe_base() { }
99
100 unordered_map(initializer_list<value_type> __l,
101 size_type __n = 10,
102 const hasher& __hf = hasher(),
103 const key_equal& __eql = key_equal(),
104 const allocator_type& __a = allocator_type())
105 : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
106
107 unordered_map&
108 operator=(const unordered_map& __x)
109 {
110 *static_cast<_Base*>(this) = __x;
111 this->_M_invalidate_all();
112 return *this;
113 }
114
115 unordered_map&
116 operator=(unordered_map&& __x)
117 {
118 // NB: DR 1204.
119 // NB: DR 675.
120 clear();
121 swap(__x);
122 return *this;
123 }
124
125 unordered_map&
126 operator=(initializer_list<value_type> __l)
127 {
128 this->clear();
129 this->insert(__l);
130 return *this;
131 }
132
133 void
134 swap(unordered_map& __x)
135 {
136 _Base::swap(__x);
137 _Safe_base::_M_swap(__x);
138 }
139
140 void
141 clear()
142 {
143 _Base::clear();
144 this->_M_invalidate_all();
145 }
146
147 iterator
148 begin()
149 { return iterator(_Base::begin(), this); }
150
151 const_iterator
152 begin() const
153 { return const_iterator(_Base::begin(), this); }
154
155 iterator
156 end()
157 { return iterator(_Base::end(), this); }
158
159 const_iterator
160 end() const
161 { return const_iterator(_Base::end(), this); }
162
163 const_iterator
164 cbegin() const
165 { return const_iterator(_Base::begin(), this); }
166
167 const_iterator
168 cend() const
169 { return const_iterator(_Base::end(), this); }
170
171 // local versions
172 using _Base::begin;
173 using _Base::end;
174 using _Base::cbegin;
175 using _Base::cend;
176
177 std::pair<iterator, bool>
178 insert(const value_type& __obj)
179 {
180 typedef std::pair<typename _Base::iterator, bool> __pair_type;
181 __pair_type __res = _Base::insert(__obj);
182 return std::make_pair(iterator(__res.first, this), __res.second);
183 }
184
185 iterator
186 insert(const_iterator, const value_type& __obj)
187 {
188 typedef std::pair<typename _Base::iterator, bool> __pair_type;
189 __pair_type __res = _Base::insert(__obj);
190 return iterator(__res.first, this);
191 }
192
193 void
194 insert(std::initializer_list<value_type> __l)
195 { _Base::insert(__l); }
196
197 template<typename _InputIterator>
198 void
199 insert(_InputIterator __first, _InputIterator __last)
200 {
201 __glibcxx_check_valid_range(__first, __last);
202 _Base::insert(__first, __last);
203 }
204
205 iterator
206 find(const key_type& __key)
207 { return iterator(_Base::find(__key), this); }
208
209 const_iterator
210 find(const key_type& __key) const
211 { return const_iterator(_Base::find(__key), this); }
212
213 std::pair<iterator, iterator>
214 equal_range(const key_type& __key)
215 {
216 typedef typename _Base::iterator _Base_iterator;
217 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
218 __pair_type __res = _Base::equal_range(__key);
219 return std::make_pair(iterator(__res.first, this),
220 iterator(__res.second, this));
221 }
222
223 std::pair<const_iterator, const_iterator>
224 equal_range(const key_type& __key) const
225 {
226 typedef typename _Base::const_iterator _Base_iterator;
227 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
228 __pair_type __res = _Base::equal_range(__key);
229 return std::make_pair(const_iterator(__res.first, this),
230 const_iterator(__res.second, this));
231 }
232
233 size_type
234 erase(const key_type& __key)
235 {
236 size_type __ret(0);
237 iterator __victim(_Base::find(__key), this);
238 if (__victim != end())
239 {
240 this->erase(__victim);
241 __ret = 1;
242 }
243 return __ret;
244 }
245
246 iterator
247 erase(const_iterator __it)
248 {
249 __glibcxx_check_erase(__it);
250 __it._M_invalidate();
251 return iterator(_Base::erase(__it.base()), this);
252 }
253
254 iterator
255 erase(const_iterator __first, const_iterator __last)
256 {
257 __glibcxx_check_erase_range(__first, __last);
258 for (const_iterator __tmp = __first; __tmp != __last;)
259 {
260 const_iterator __victim = __tmp++;
261 __victim._M_invalidate();
262 }
263 return iterator(_Base::erase(__first.base(),
264 __last.base()), this);
265 }
266
267 _Base&
268 _M_base() { return *this; }
269
270 const _Base&
271 _M_base() const { return *this; }
272
273 private:
274 void
275 _M_invalidate_all()
276 {
277 typedef typename _Base::const_iterator _Base_const_iterator;
278 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
279 this->_M_invalidate_if(_Not_equal(_M_base().end()));
280 }
281 };
282
283 template<typename _Key, typename _Tp, typename _Hash,
284 typename _Pred, typename _Alloc>
285 inline void
286 swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
287 unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
288 { __x.swap(__y); }
289
290 template<typename _Key, typename _Tp, typename _Hash,
291 typename _Pred, typename _Alloc>
292 inline bool
293 operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
294 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
295 { return __x._M_equal(__y); }
296
297 template<typename _Key, typename _Tp, typename _Hash,
298 typename _Pred, typename _Alloc>
299 inline bool
300 operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
301 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
302 { return !(__x == __y); }
303
304
305 /// Class std::unordered_multimap with safety/checking/debug instrumentation.
306 template<typename _Key, typename _Tp,
307 typename _Hash = std::hash<_Key>,
308 typename _Pred = std::equal_to<_Key>,
309 typename _Alloc = std::allocator<_Key> >
310 class unordered_multimap
311 : public _GLIBCXX_STD_D::unordered_multimap<_Key, _Tp, _Hash,
312 _Pred, _Alloc>,
313 public __gnu_debug::_Safe_sequence<unordered_multimap<_Key, _Tp, _Hash,
314 _Pred, _Alloc> >
315 {
316 typedef _GLIBCXX_STD_D::unordered_multimap<_Key, _Tp, _Hash,
317 _Pred, _Alloc> _Base;
318 typedef __gnu_debug::_Safe_sequence<unordered_multimap> _Safe_base;
319
320 public:
321 typedef typename _Base::size_type size_type;
322 typedef typename _Base::hasher hasher;
323 typedef typename _Base::key_equal key_equal;
324 typedef typename _Base::allocator_type allocator_type;
325
326 typedef typename _Base::key_type key_type;
327 typedef typename _Base::value_type value_type;
328
329 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
330 unordered_multimap> iterator;
331 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
332 unordered_multimap> const_iterator;
333
334 explicit
335 unordered_multimap(size_type __n = 10,
336 const hasher& __hf = hasher(),
337 const key_equal& __eql = key_equal(),
338 const allocator_type& __a = allocator_type())
339 : _Base(__n, __hf, __eql, __a) { }
340
341 template<typename _InputIterator>
342 unordered_multimap(_InputIterator __f, _InputIterator __l,
343 size_type __n = 10,
344 const hasher& __hf = hasher(),
345 const key_equal& __eql = key_equal(),
346 const allocator_type& __a = allocator_type())
347 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
348 __hf, __eql, __a), _Safe_base() { }
349
350 unordered_multimap(const unordered_multimap& __x)
351 : _Base(__x), _Safe_base() { }
352
353 unordered_multimap(const _Base& __x)
354 : _Base(__x), _Safe_base() { }
355
356 unordered_multimap(unordered_multimap&& __x)
357 : _Base(std::forward<unordered_multimap>(__x)), _Safe_base() { }
358
359 unordered_multimap(initializer_list<value_type> __l,
360 size_type __n = 10,
361 const hasher& __hf = hasher(),
362 const key_equal& __eql = key_equal(),
363 const allocator_type& __a = allocator_type())
364 : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
365
366 unordered_multimap&
367 operator=(const unordered_multimap& __x)
368 {
369 *static_cast<_Base*>(this) = __x;
370 this->_M_invalidate_all();
371 return *this;
372 }
373
374 unordered_multimap&
375 operator=(unordered_multimap&& __x)
376 {
377 // NB: DR 1204.
378 // NB: DR 675.
379 clear();
380 swap(__x);
381 return *this;
382 }
383
384 unordered_multimap&
385 operator=(initializer_list<value_type> __l)
386 {
387 this->clear();
388 this->insert(__l);
389 return *this;
390 }
391
392 void
393 swap(unordered_multimap& __x)
394 {
395 _Base::swap(__x);
396 _Safe_base::_M_swap(__x);
397 }
398
399 void
400 clear()
401 {
402 _Base::clear();
403 this->_M_invalidate_all();
404 }
405
406 iterator
407 begin()
408 { return iterator(_Base::begin(), this); }
409
410 const_iterator
411 begin() const
412 { return const_iterator(_Base::begin(), this); }
413
414 iterator
415 end()
416 { return iterator(_Base::end(), this); }
417
418 const_iterator
419 end() const
420 { return const_iterator(_Base::end(), this); }
421
422 const_iterator
423 cbegin() const
424 { return const_iterator(_Base::begin(), this); }
425
426 const_iterator
427 cend() const
428 { return const_iterator(_Base::end(), this); }
429
430 // local versions
431 using _Base::begin;
432 using _Base::end;
433 using _Base::cbegin;
434 using _Base::cend;
435
436 iterator
437 insert(const value_type& __obj)
438 { return iterator(_Base::insert(__obj), this); }
439
440 iterator
441 insert(const_iterator, const value_type& __obj)
442 { return iterator(_Base::insert(__obj), this); }
443
444 void
445 insert(std::initializer_list<value_type> __l)
446 { _Base::insert(__l); }
447
448 template<typename _InputIterator>
449 void
450 insert(_InputIterator __first, _InputIterator __last)
451 {
452 __glibcxx_check_valid_range(__first, __last);
453 _Base::insert(__first, __last);
454 }
455
456 iterator
457 find(const key_type& __key)
458 { return iterator(_Base::find(__key), this); }
459
460 const_iterator
461 find(const key_type& __key) const
462 { return const_iterator(_Base::find(__key), this); }
463
464 std::pair<iterator, iterator>
465 equal_range(const key_type& __key)
466 {
467 typedef typename _Base::iterator _Base_iterator;
468 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
469 __pair_type __res = _Base::equal_range(__key);
470 return std::make_pair(iterator(__res.first, this),
471 iterator(__res.second, this));
472 }
473
474 std::pair<const_iterator, const_iterator>
475 equal_range(const key_type& __key) const
476 {
477 typedef typename _Base::const_iterator _Base_iterator;
478 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
479 __pair_type __res = _Base::equal_range(__key);
480 return std::make_pair(const_iterator(__res.first, this),
481 const_iterator(__res.second, this));
482 }
483
484 size_type
485 erase(const key_type& __key)
486 {
487 size_type __ret(0);
488 iterator __victim(_Base::find(__key), this);
489 if (__victim != end())
490 {
491 this->erase(__victim);
492 __ret = 1;
493 }
494 return __ret;
495 }
496
497 iterator
498 erase(const_iterator __it)
499 {
500 __glibcxx_check_erase(__it);
501 __it._M_invalidate();
502 return iterator(_Base::erase(__it.base()), this);
503 }
504
505 iterator
506 erase(const_iterator __first, const_iterator __last)
507 {
508 __glibcxx_check_erase_range(__first, __last);
509 for (const_iterator __tmp = __first; __tmp != __last;)
510 {
511 const_iterator __victim = __tmp++;
512 __victim._M_invalidate();
513 }
514 return iterator(_Base::erase(__first.base(),
515 __last.base()), this);
516 }
517
518 _Base&
519 _M_base() { return *this; }
520
521 const _Base&
522 _M_base() const { return *this; }
523
524 private:
525 void
526 _M_invalidate_all()
527 {
528 typedef typename _Base::const_iterator _Base_const_iterator;
529 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
530 this->_M_invalidate_if(_Not_equal(_M_base().end()));
531 }
532 };
533
534 template<typename _Key, typename _Tp, typename _Hash,
535 typename _Pred, typename _Alloc>
536 inline void
537 swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
538 unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
539 { __x.swap(__y); }
540
541 template<typename _Key, typename _Tp, typename _Hash,
542 typename _Pred, typename _Alloc>
543 inline bool
544 operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
545 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
546 { return __x._M_equal(__y); }
547
548 template<typename _Key, typename _Tp, typename _Hash,
549 typename _Pred, typename _Alloc>
550 inline bool
551 operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
552 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
553 { return !(__x == __y); }
554
555 } // namespace __debug
556 } // namespace std
557
558 #endif