1 // Debugging unordered_set/unordered_multiset 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_set
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_UNORDERED_SET
31 #define _GLIBCXX_DEBUG_UNORDERED_SET 1
33 #ifdef __GXX_EXPERIMENTAL_CXX0X__
34 # include <unordered_set>
36 # include <bits/c++0x_warning.h>
39 #include <debug/safe_sequence.h>
40 #include <debug/safe_iterator.h>
41 #include <initializer_list>
47 /// Class std::unordered_set with safety/checking/debug instrumentation.
48 template<typename _Value,
49 typename _Hash = std::hash<_Value>,
50 typename _Pred = std::equal_to<_Value>,
51 typename _Alloc = std::allocator<_Value> >
53 : public _GLIBCXX_STD_D::unordered_set<_Value, _Hash, _Pred, _Alloc>,
54 public __gnu_debug::_Safe_sequence<unordered_set<_Value, _Hash,
57 typedef _GLIBCXX_STD_D::unordered_set<_Value, _Hash,
59 typedef __gnu_debug::_Safe_sequence<unordered_set> _Safe_base;
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;
67 typedef typename _Base::key_type key_type;
68 typedef typename _Base::value_type value_type;
70 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
71 unordered_set> iterator;
72 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
73 unordered_set> const_iterator;
76 unordered_set(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) { }
82 template<typename _InputIterator>
83 unordered_set(_InputIterator __f, _InputIterator __l,
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() { }
91 unordered_set(const unordered_set& __x)
92 : _Base(__x), _Safe_base() { }
94 unordered_set(const _Base& __x)
95 : _Base(__x), _Safe_base() { }
97 unordered_set(unordered_set&& __x)
98 : _Base(std::forward<unordered_set>(__x)), _Safe_base() { }
100 unordered_set(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_set& __x)
110 *static_cast<_Base*>(this) = __x;
111 this->_M_invalidate_all();
116 operator=(unordered_set&& __x)
126 operator=(initializer_list<value_type> __l)
134 swap(unordered_set& __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(__first, __last);
206 find(const key_type& __key)
207 { return iterator(_Base::find(__key), this); }
210 find(const key_type& __key) const
211 { return const_iterator(_Base::find(__key), this); }
213 std::pair<iterator, iterator>
214 equal_range(const key_type& __key)
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));
223 std::pair<const_iterator, const_iterator>
224 equal_range(const key_type& __key) const
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));
234 erase(const key_type& __key)
237 iterator __victim(_Base::find(__key), this);
238 if (__victim != end())
240 this->erase(__victim);
247 erase(const_iterator __it)
249 __glibcxx_check_erase(__it);
250 __it._M_invalidate();
251 return iterator(_Base::erase(__it.base()), this);
255 erase(const_iterator __first, const_iterator __last)
257 __glibcxx_check_erase_range(__first, __last);
258 for (const_iterator __tmp = __first; __tmp != __last;)
260 const_iterator __victim = __tmp++;
261 __victim._M_invalidate();
263 return iterator(_Base::erase(__first.base(),
264 __last.base()), this);
268 _M_base() { return *this; }
271 _M_base() const { return *this; }
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()));
283 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
285 swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
286 unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
289 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
291 operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
292 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
293 { return __x._M_equal(__y); }
295 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
297 operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
298 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
299 { return !(__x == __y); }
302 /// Class std::unordered_multiset with safety/checking/debug instrumentation.
303 template<typename _Value,
304 typename _Hash = std::hash<_Value>,
305 typename _Pred = std::equal_to<_Value>,
306 typename _Alloc = std::allocator<_Value> >
307 class unordered_multiset
308 : public _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash, _Pred, _Alloc>,
309 public __gnu_debug::_Safe_sequence<unordered_multiset<_Value, _Hash,
312 typedef _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash,
313 _Pred, _Alloc> _Base;
314 typedef __gnu_debug::_Safe_sequence<unordered_multiset> _Safe_base;
317 typedef typename _Base::size_type size_type;
318 typedef typename _Base::hasher hasher;
319 typedef typename _Base::key_equal key_equal;
320 typedef typename _Base::allocator_type allocator_type;
322 typedef typename _Base::key_type key_type;
323 typedef typename _Base::value_type value_type;
325 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
326 unordered_multiset> iterator;
327 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
328 unordered_multiset> const_iterator;
331 unordered_multiset(size_type __n = 10,
332 const hasher& __hf = hasher(),
333 const key_equal& __eql = key_equal(),
334 const allocator_type& __a = allocator_type())
335 : _Base(__n, __hf, __eql, __a) { }
337 template<typename _InputIterator>
338 unordered_multiset(_InputIterator __f, _InputIterator __l,
340 const hasher& __hf = hasher(),
341 const key_equal& __eql = key_equal(),
342 const allocator_type& __a = allocator_type())
343 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
344 __hf, __eql, __a), _Safe_base() { }
346 unordered_multiset(const unordered_multiset& __x)
347 : _Base(__x), _Safe_base() { }
349 unordered_multiset(const _Base& __x)
350 : _Base(__x), _Safe_base() { }
352 unordered_multiset(unordered_multiset&& __x)
353 : _Base(std::forward<unordered_multiset>(__x)), _Safe_base() { }
355 unordered_multiset(initializer_list<value_type> __l,
357 const hasher& __hf = hasher(),
358 const key_equal& __eql = key_equal(),
359 const allocator_type& __a = allocator_type())
360 : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
363 operator=(const unordered_multiset& __x)
365 *static_cast<_Base*>(this) = __x;
366 this->_M_invalidate_all();
371 operator=(unordered_multiset&& __x)
381 operator=(initializer_list<value_type> __l)
389 swap(unordered_multiset& __x)
392 _Safe_base::_M_swap(__x);
399 this->_M_invalidate_all();
404 { return iterator(_Base::begin(), this); }
408 { return const_iterator(_Base::begin(), this); }
412 { return iterator(_Base::end(), this); }
416 { return const_iterator(_Base::end(), this); }
420 { return const_iterator(_Base::begin(), this); }
424 { return const_iterator(_Base::end(), this); }
433 insert(const value_type& __obj)
434 { return iterator(_Base::insert(__obj), this); }
437 insert(const_iterator, const value_type& __obj)
438 { return iterator(_Base::insert(__obj), this); }
441 insert(std::initializer_list<value_type> __l)
442 { _Base::insert(__l); }
444 template<typename _InputIterator>
446 insert(_InputIterator __first, _InputIterator __last)
448 __glibcxx_check_valid_range(__first, __last);
449 _Base::insert(__first, __last);
453 find(const key_type& __key)
454 { return iterator(_Base::find(__key), this); }
457 find(const key_type& __key) const
458 { return const_iterator(_Base::find(__key), this); }
460 std::pair<iterator, iterator>
461 equal_range(const key_type& __key)
463 typedef typename _Base::iterator _Base_iterator;
464 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
465 __pair_type __res = _Base::equal_range(__key);
466 return std::make_pair(iterator(__res.first, this),
467 iterator(__res.second, this));
470 std::pair<const_iterator, const_iterator>
471 equal_range(const key_type& __key) const
473 typedef typename _Base::const_iterator _Base_iterator;
474 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
475 __pair_type __res = _Base::equal_range(__key);
476 return std::make_pair(const_iterator(__res.first, this),
477 const_iterator(__res.second, this));
481 erase(const key_type& __key)
484 iterator __victim(_Base::find(__key), this);
485 if (__victim != end())
487 this->erase(__victim);
494 erase(const_iterator __it)
496 __glibcxx_check_erase(__it);
497 __it._M_invalidate();
498 return iterator(_Base::erase(__it.base()), this);
502 erase(const_iterator __first, const_iterator __last)
504 __glibcxx_check_erase_range(__first, __last);
505 for (const_iterator __tmp = __first; __tmp != __last;)
507 const_iterator __victim = __tmp++;
508 __victim._M_invalidate();
510 return iterator(_Base::erase(__first.base(),
511 __last.base()), this);
515 _M_base() { return *this; }
518 _M_base() const { return *this; }
524 typedef typename _Base::const_iterator _Base_const_iterator;
525 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
526 this->_M_invalidate_if(_Not_equal(_M_base().end()));
530 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
532 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
533 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
536 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
538 operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
539 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
540 { return __x._M_equal(__y); }
542 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
544 operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
545 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
546 { return !(__x == __y); }
548 } // namespace __debug