1 // Debugging unordered_map/unordered_multimap implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
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)
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 // 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.
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/>.
26 /** @file debug/unordered_map
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_UNORDERED_MAP
31 #define _GLIBCXX_DEBUG_UNORDERED_MAP 1
33 #ifndef __GXX_EXPERIMENTAL_CXX0X__
34 # include <bits/c++0x_warning.h>
36 # include <unordered_map>
38 #include <debug/safe_sequence.h>
39 #include <debug/safe_iterator.h>
45 /// Class std::unordered_map with safety/checking/debug instrumentation.
46 template<typename _Key, typename _Tp,
47 typename _Hash = std::hash<_Key>,
48 typename _Pred = std::equal_to<_Key>,
49 typename _Alloc = std::allocator<_Key> >
51 : public _GLIBCXX_STD_D::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>,
52 public __gnu_debug::_Safe_sequence<unordered_map<_Key, _Tp, _Hash,
55 typedef _GLIBCXX_STD_D::unordered_map<_Key, _Tp, _Hash,
57 typedef __gnu_debug::_Safe_sequence<unordered_map> _Safe_base;
60 typedef typename _Base::size_type size_type;
61 typedef typename _Base::hasher hasher;
62 typedef typename _Base::key_equal key_equal;
63 typedef typename _Base::allocator_type allocator_type;
65 typedef typename _Base::key_type key_type;
66 typedef typename _Base::value_type value_type;
68 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
69 unordered_map> iterator;
70 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
71 unordered_map> const_iterator;
74 unordered_map(size_type __n = 10,
75 const hasher& __hf = hasher(),
76 const key_equal& __eql = key_equal(),
77 const allocator_type& __a = allocator_type())
78 : _Base(__n, __hf, __eql, __a) { }
80 template<typename _InputIterator>
81 unordered_map(_InputIterator __first, _InputIterator __last,
83 const hasher& __hf = hasher(),
84 const key_equal& __eql = key_equal(),
85 const allocator_type& __a = allocator_type())
86 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
88 __gnu_debug::__base(__last), __n,
89 __hf, __eql, __a), _Safe_base() { }
91 unordered_map(const unordered_map& __x)
92 : _Base(__x), _Safe_base() { }
94 unordered_map(const _Base& __x)
95 : _Base(__x), _Safe_base() { }
97 unordered_map(unordered_map&& __x)
98 : _Base(std::move(__x)), _Safe_base() { }
100 unordered_map(initializer_list<value_type> __l,
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() { }
108 operator=(const unordered_map& __x)
110 *static_cast<_Base*>(this) = __x;
111 this->_M_invalidate_all();
116 operator=(unordered_map&& __x)
126 operator=(initializer_list<value_type> __l)
134 swap(unordered_map& __x)
137 _Safe_base::_M_swap(__x);
144 this->_M_invalidate_all();
149 { return iterator(_Base::begin(), this); }
153 { return const_iterator(_Base::begin(), this); }
157 { return iterator(_Base::end(), this); }
161 { return const_iterator(_Base::end(), this); }
165 { return const_iterator(_Base::begin(), this); }
169 { return const_iterator(_Base::end(), this); }
177 std::pair<iterator, bool>
178 insert(const value_type& __obj)
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);
186 insert(const_iterator, const value_type& __obj)
188 typedef std::pair<typename _Base::iterator, bool> __pair_type;
189 __pair_type __res = _Base::insert(__obj);
190 return iterator(__res.first, this);
194 insert(std::initializer_list<value_type> __l)
195 { _Base::insert(__l); }
197 template<typename _InputIterator>
199 insert(_InputIterator __first, _InputIterator __last)
201 __glibcxx_check_valid_range(__first, __last);
202 _Base::insert(__gnu_debug::__base(__first),
203 __gnu_debug::__base(__last));
207 find(const key_type& __key)
208 { return iterator(_Base::find(__key), this); }
211 find(const key_type& __key) const
212 { return const_iterator(_Base::find(__key), this); }
214 std::pair<iterator, iterator>
215 equal_range(const key_type& __key)
217 typedef typename _Base::iterator _Base_iterator;
218 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
219 __pair_type __res = _Base::equal_range(__key);
220 return std::make_pair(iterator(__res.first, this),
221 iterator(__res.second, this));
224 std::pair<const_iterator, const_iterator>
225 equal_range(const key_type& __key) const
227 typedef typename _Base::const_iterator _Base_iterator;
228 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
229 __pair_type __res = _Base::equal_range(__key);
230 return std::make_pair(const_iterator(__res.first, this),
231 const_iterator(__res.second, this));
235 erase(const key_type& __key)
238 iterator __victim(_Base::find(__key), this);
239 if (__victim != end())
241 this->erase(__victim);
248 erase(const_iterator __it)
250 __glibcxx_check_erase(__it);
251 __it._M_invalidate();
252 return iterator(_Base::erase(__it.base()), this);
256 erase(const_iterator __first, const_iterator __last)
258 __glibcxx_check_erase_range(__first, __last);
259 for (const_iterator __tmp = __first; __tmp != __last;)
261 const_iterator __victim = __tmp++;
262 __victim._M_invalidate();
264 return iterator(_Base::erase(__first.base(),
265 __last.base()), this);
269 _M_base() { return *this; }
272 _M_base() const { return *this; }
278 typedef typename _Base::const_iterator _Base_const_iterator;
279 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
280 this->_M_invalidate_if(_Not_equal(_M_base().end()));
284 template<typename _Key, typename _Tp, typename _Hash,
285 typename _Pred, typename _Alloc>
287 swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
288 unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
291 template<typename _Key, typename _Tp, typename _Hash,
292 typename _Pred, typename _Alloc>
294 operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
295 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
296 { return __x._M_equal(__y); }
298 template<typename _Key, typename _Tp, typename _Hash,
299 typename _Pred, typename _Alloc>
301 operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
302 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
303 { return !(__x == __y); }
306 /// Class std::unordered_multimap with safety/checking/debug instrumentation.
307 template<typename _Key, typename _Tp,
308 typename _Hash = std::hash<_Key>,
309 typename _Pred = std::equal_to<_Key>,
310 typename _Alloc = std::allocator<_Key> >
311 class unordered_multimap
312 : public _GLIBCXX_STD_D::unordered_multimap<_Key, _Tp, _Hash,
314 public __gnu_debug::_Safe_sequence<unordered_multimap<_Key, _Tp, _Hash,
317 typedef _GLIBCXX_STD_D::unordered_multimap<_Key, _Tp, _Hash,
318 _Pred, _Alloc> _Base;
319 typedef __gnu_debug::_Safe_sequence<unordered_multimap> _Safe_base;
322 typedef typename _Base::size_type size_type;
323 typedef typename _Base::hasher hasher;
324 typedef typename _Base::key_equal key_equal;
325 typedef typename _Base::allocator_type allocator_type;
327 typedef typename _Base::key_type key_type;
328 typedef typename _Base::value_type value_type;
330 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
331 unordered_multimap> iterator;
332 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
333 unordered_multimap> const_iterator;
336 unordered_multimap(size_type __n = 10,
337 const hasher& __hf = hasher(),
338 const key_equal& __eql = key_equal(),
339 const allocator_type& __a = allocator_type())
340 : _Base(__n, __hf, __eql, __a) { }
342 template<typename _InputIterator>
343 unordered_multimap(_InputIterator __first, _InputIterator __last,
345 const hasher& __hf = hasher(),
346 const key_equal& __eql = key_equal(),
347 const allocator_type& __a = allocator_type())
348 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
350 __gnu_debug::__base(__last), __n,
351 __hf, __eql, __a), _Safe_base() { }
353 unordered_multimap(const unordered_multimap& __x)
354 : _Base(__x), _Safe_base() { }
356 unordered_multimap(const _Base& __x)
357 : _Base(__x), _Safe_base() { }
359 unordered_multimap(unordered_multimap&& __x)
360 : _Base(std::move(__x)), _Safe_base() { }
362 unordered_multimap(initializer_list<value_type> __l,
364 const hasher& __hf = hasher(),
365 const key_equal& __eql = key_equal(),
366 const allocator_type& __a = allocator_type())
367 : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
370 operator=(const unordered_multimap& __x)
372 *static_cast<_Base*>(this) = __x;
373 this->_M_invalidate_all();
378 operator=(unordered_multimap&& __x)
388 operator=(initializer_list<value_type> __l)
396 swap(unordered_multimap& __x)
399 _Safe_base::_M_swap(__x);
406 this->_M_invalidate_all();
411 { return iterator(_Base::begin(), this); }
415 { return const_iterator(_Base::begin(), this); }
419 { return iterator(_Base::end(), this); }
423 { return const_iterator(_Base::end(), this); }
427 { return const_iterator(_Base::begin(), this); }
431 { return const_iterator(_Base::end(), this); }
440 insert(const value_type& __obj)
441 { return iterator(_Base::insert(__obj), this); }
444 insert(const_iterator, const value_type& __obj)
445 { return iterator(_Base::insert(__obj), this); }
448 insert(std::initializer_list<value_type> __l)
449 { _Base::insert(__l); }
451 template<typename _InputIterator>
453 insert(_InputIterator __first, _InputIterator __last)
455 __glibcxx_check_valid_range(__first, __last);
456 _Base::insert(__gnu_debug::__base(__first),
457 __gnu_debug::__base(__last));
461 find(const key_type& __key)
462 { return iterator(_Base::find(__key), this); }
465 find(const key_type& __key) const
466 { return const_iterator(_Base::find(__key), this); }
468 std::pair<iterator, iterator>
469 equal_range(const key_type& __key)
471 typedef typename _Base::iterator _Base_iterator;
472 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
473 __pair_type __res = _Base::equal_range(__key);
474 return std::make_pair(iterator(__res.first, this),
475 iterator(__res.second, this));
478 std::pair<const_iterator, const_iterator>
479 equal_range(const key_type& __key) const
481 typedef typename _Base::const_iterator _Base_iterator;
482 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
483 __pair_type __res = _Base::equal_range(__key);
484 return std::make_pair(const_iterator(__res.first, this),
485 const_iterator(__res.second, this));
489 erase(const key_type& __key)
492 iterator __victim(_Base::find(__key), this);
493 if (__victim != end())
495 this->erase(__victim);
502 erase(const_iterator __it)
504 __glibcxx_check_erase(__it);
505 __it._M_invalidate();
506 return iterator(_Base::erase(__it.base()), this);
510 erase(const_iterator __first, const_iterator __last)
512 __glibcxx_check_erase_range(__first, __last);
513 for (const_iterator __tmp = __first; __tmp != __last;)
515 const_iterator __victim = __tmp++;
516 __victim._M_invalidate();
518 return iterator(_Base::erase(__first.base(),
519 __last.base()), this);
523 _M_base() { return *this; }
526 _M_base() const { return *this; }
532 typedef typename _Base::const_iterator _Base_const_iterator;
533 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
534 this->_M_invalidate_if(_Not_equal(_M_base().end()));
538 template<typename _Key, typename _Tp, typename _Hash,
539 typename _Pred, typename _Alloc>
541 swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
542 unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
545 template<typename _Key, typename _Tp, typename _Hash,
546 typename _Pred, typename _Alloc>
548 operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
549 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
550 { return __x._M_equal(__y); }
552 template<typename _Key, typename _Tp, typename _Hash,
553 typename _Pred, typename _Alloc>
555 operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
556 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
557 { return !(__x == __y); }
559 } // namespace __debug
562 #endif // __GXX_EXPERIMENTAL_CXX0X__