]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/debug/unordered_map
re PR fortran/42309 (Problem with a pointer array passed to a subroutine)
[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 <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(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 const_iterator
194 insert(const_iterator, const value_type& __obj)
195 {
196 typedef std::pair<typename _Base::iterator, bool> __pair_type;
197 __pair_type __res = _Base::insert(__obj);
198 return const_iterator(__res.first, this);
199 }
200
201 void
202 insert(std::initializer_list<value_type> __l)
203 { _Base::insert(__l); }
204
205 template<typename _InputIterator>
206 void
207 insert(_InputIterator __first, _InputIterator __last)
208 {
209 __glibcxx_check_valid_range(__first, __last);
210 _Base::insert(__first, __last);
211 }
212
213 iterator
214 find(const key_type& __key)
215 { return iterator(_Base::find(__key), this); }
216
217 const_iterator
218 find(const key_type& __key) const
219 { return const_iterator(_Base::find(__key), this); }
220
221 std::pair<iterator, iterator>
222 equal_range(const key_type& __key)
223 {
224 typedef typename _Base::iterator _Base_iterator;
225 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
226 __pair_type __res = _Base::equal_range(__key);
227 return std::make_pair(iterator(__res.first, this),
228 iterator(__res.second, this));
229 }
230
231 std::pair<const_iterator, const_iterator>
232 equal_range(const key_type& __key) const
233 {
234 typedef typename _Base::const_iterator _Base_iterator;
235 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
236 __pair_type __res = _Base::equal_range(__key);
237 return std::make_pair(const_iterator(__res.first, this),
238 const_iterator(__res.second, this));
239 }
240
241 size_type
242 erase(const key_type& __key)
243 {
244 size_type __ret(0);
245 iterator __victim(_Base::find(__key), this);
246 if (__victim != end())
247 {
248 this->erase(__victim);
249 __ret = 1;
250 }
251 return __ret;
252 }
253
254 iterator
255 erase(iterator __it)
256 {
257 __glibcxx_check_erase(__it);
258 __it._M_invalidate();
259 return iterator(_Base::erase(__it.base()), this);
260 }
261
262 const_iterator
263 erase(const_iterator __it)
264 {
265 __glibcxx_check_erase(__it);
266 __it._M_invalidate();
267 return const_iterator(_Base::erase(__it.base()), this);
268 }
269
270 iterator
271 erase(iterator __first, iterator __last)
272 {
273 __glibcxx_check_erase_range(__first, __last);
274 for (iterator __tmp = __first; __tmp != __last;)
275 {
276 iterator __victim = __tmp++;
277 __victim._M_invalidate();
278 }
279 return iterator(_Base::erase(__first.base(),
280 __last.base()), this);
281 }
282
283 const_iterator
284 erase(const_iterator __first, const_iterator __last)
285 {
286 __glibcxx_check_erase_range(__first, __last);
287 for (const_iterator __tmp = __first; __tmp != __last;)
288 {
289 const_iterator __victim = __tmp++;
290 __victim._M_invalidate();
291 }
292 return const_iterator(_Base::erase(__first.base(),
293 __last.base()), this);
294 }
295
296 _Base&
297 _M_base() { return *this; }
298
299 const _Base&
300 _M_base() const { return *this; }
301
302 private:
303 void
304 _M_invalidate_all()
305 {
306 typedef typename _Base::const_iterator _Base_const_iterator;
307 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
308 this->_M_invalidate_if(_Not_equal(_M_base().end()));
309 }
310 };
311
312 template<typename _Key, typename _Tp, typename _Hash,
313 typename _Pred, typename _Alloc>
314 inline void
315 swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
316 unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
317 { __x.swap(__y); }
318
319
320 /// Class std::unordered_multimap with safety/checking/debug instrumentation.
321 template<typename _Key, typename _Tp,
322 typename _Hash = std::hash<_Key>,
323 typename _Pred = std::equal_to<_Key>,
324 typename _Alloc = std::allocator<_Key> >
325 class unordered_multimap
326 : public _GLIBCXX_STD_D::unordered_multimap<_Key, _Tp, _Hash,
327 _Pred, _Alloc>,
328 public __gnu_debug::_Safe_sequence<unordered_multimap<_Key, _Tp, _Hash,
329 _Pred, _Alloc> >
330 {
331 typedef _GLIBCXX_STD_D::unordered_multimap<_Key, _Tp, _Hash,
332 _Pred, _Alloc> _Base;
333 typedef __gnu_debug::_Safe_sequence<unordered_multimap> _Safe_base;
334
335 public:
336 typedef typename _Base::size_type size_type;
337 typedef typename _Base::hasher hasher;
338 typedef typename _Base::key_equal key_equal;
339 typedef typename _Base::allocator_type allocator_type;
340
341 typedef typename _Base::key_type key_type;
342 typedef typename _Base::value_type value_type;
343
344 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
345 unordered_multimap> iterator;
346 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
347 unordered_multimap> const_iterator;
348
349 explicit
350 unordered_multimap(size_type __n = 10,
351 const hasher& __hf = hasher(),
352 const key_equal& __eql = key_equal(),
353 const allocator_type& __a = allocator_type())
354 : _Base(__n, __hf, __eql, __a) { }
355
356 template<typename _InputIterator>
357 unordered_multimap(_InputIterator __f, _InputIterator __l,
358 size_type __n = 10,
359 const hasher& __hf = hasher(),
360 const key_equal& __eql = key_equal(),
361 const allocator_type& __a = allocator_type())
362 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
363 __hf, __eql, __a), _Safe_base() { }
364
365 unordered_multimap(const unordered_multimap& __x)
366 : _Base(__x), _Safe_base() { }
367
368 unordered_multimap(const _Base& __x)
369 : _Base(__x), _Safe_base() { }
370
371 unordered_multimap(unordered_multimap&& __x)
372 : _Base(std::forward<unordered_multimap>(__x)), _Safe_base() { }
373
374 unordered_multimap(initializer_list<value_type> __l,
375 size_type __n = 10,
376 const hasher& __hf = hasher(),
377 const key_equal& __eql = key_equal(),
378 const allocator_type& __a = allocator_type())
379 : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
380
381 unordered_multimap&
382 operator=(const unordered_multimap& __x)
383 {
384 *static_cast<_Base*>(this) = __x;
385 this->_M_invalidate_all();
386 return *this;
387 }
388
389 unordered_multimap&
390 operator=(unordered_multimap&& __x)
391 {
392 // NB: DR 1204.
393 // NB: DR 675.
394 clear();
395 swap(__x);
396 return *this;
397 }
398
399 unordered_multimap&
400 operator=(initializer_list<value_type> __l)
401 {
402 this->clear();
403 this->insert(__l);
404 return *this;
405 }
406
407 void
408 swap(unordered_multimap& __x)
409 {
410 _Base::swap(__x);
411 _Safe_base::_M_swap(__x);
412 }
413
414 void
415 clear()
416 {
417 _Base::clear();
418 this->_M_invalidate_all();
419 }
420
421 iterator
422 begin()
423 { return iterator(_Base::begin(), this); }
424
425 const_iterator
426 begin() const
427 { return const_iterator(_Base::begin(), this); }
428
429 iterator
430 end()
431 { return iterator(_Base::end(), this); }
432
433 const_iterator
434 end() const
435 { return const_iterator(_Base::end(), this); }
436
437 const_iterator
438 cbegin() const
439 { return const_iterator(_Base::begin(), this); }
440
441 const_iterator
442 cend() const
443 { return const_iterator(_Base::end(), this); }
444
445 // local versions
446 using _Base::begin;
447 using _Base::end;
448 using _Base::cbegin;
449 using _Base::cend;
450
451 iterator
452 insert(const value_type& __obj)
453 { return iterator(_Base::insert(__obj), this); }
454
455 iterator
456 insert(iterator, const value_type& __obj)
457 { return iterator(_Base::insert(__obj), this); }
458
459 const_iterator
460 insert(const_iterator, const value_type& __obj)
461 { return const_iterator(_Base::insert(__obj), this); }
462
463 void
464 insert(std::initializer_list<value_type> __l)
465 { _Base::insert(__l); }
466
467 template<typename _InputIterator>
468 void
469 insert(_InputIterator __first, _InputIterator __last)
470 {
471 __glibcxx_check_valid_range(__first, __last);
472 _Base::insert(__first, __last);
473 }
474
475 iterator
476 find(const key_type& __key)
477 { return iterator(_Base::find(__key), this); }
478
479 const_iterator
480 find(const key_type& __key) const
481 { return const_iterator(_Base::find(__key), this); }
482
483 std::pair<iterator, iterator>
484 equal_range(const key_type& __key)
485 {
486 typedef typename _Base::iterator _Base_iterator;
487 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
488 __pair_type __res = _Base::equal_range(__key);
489 return std::make_pair(iterator(__res.first, this),
490 iterator(__res.second, this));
491 }
492
493 std::pair<const_iterator, const_iterator>
494 equal_range(const key_type& __key) const
495 {
496 typedef typename _Base::const_iterator _Base_iterator;
497 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
498 __pair_type __res = _Base::equal_range(__key);
499 return std::make_pair(const_iterator(__res.first, this),
500 const_iterator(__res.second, this));
501 }
502
503 size_type
504 erase(const key_type& __key)
505 {
506 size_type __ret(0);
507 iterator __victim(_Base::find(__key), this);
508 if (__victim != end())
509 {
510 this->erase(__victim);
511 __ret = 1;
512 }
513 return __ret;
514 }
515
516 iterator
517 erase(iterator __it)
518 {
519 __glibcxx_check_erase(__it);
520 __it._M_invalidate();
521 return iterator(_Base::erase(__it.base()), this);
522 }
523
524 const_iterator
525 erase(const_iterator __it)
526 {
527 __glibcxx_check_erase(__it);
528 __it._M_invalidate();
529 return const_iterator(_Base::erase(__it.base()), this);
530 }
531
532 iterator
533 erase(iterator __first, iterator __last)
534 {
535 __glibcxx_check_erase_range(__first, __last);
536 for (iterator __tmp = __first; __tmp != __last;)
537 {
538 iterator __victim = __tmp++;
539 __victim._M_invalidate();
540 }
541 return iterator(_Base::erase(__first.base(),
542 __last.base()), this);
543 }
544
545 const_iterator
546 erase(const_iterator __first, const_iterator __last)
547 {
548 __glibcxx_check_erase_range(__first, __last);
549 for (const_iterator __tmp = __first; __tmp != __last;)
550 {
551 const_iterator __victim = __tmp++;
552 __victim._M_invalidate();
553 }
554 return const_iterator(_Base::erase(__first.base(),
555 __last.base()), this);
556 }
557
558 _Base&
559 _M_base() { return *this; }
560
561 const _Base&
562 _M_base() const { return *this; }
563
564 private:
565 void
566 _M_invalidate_all()
567 {
568 typedef typename _Base::const_iterator _Base_const_iterator;
569 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
570 this->_M_invalidate_if(_Not_equal(_M_base().end()));
571 }
572 };
573
574 template<typename _Key, typename _Tp, typename _Hash,
575 typename _Pred, typename _Alloc>
576 inline void
577 swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
578 unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
579 { __x.swap(__y); }
580
581 } // namespace __debug
582 } // namespace std
583
584 #endif