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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
34 # include <bits/c++0x_warning.h>
36 # include <unordered_set>
38 #include <debug/safe_sequence.h>
39 #include <debug/safe_iterator.h>
45 /// Class std::unordered_set with safety/checking/debug instrumentation.
46 template<typename _Value,
47 typename _Hash = std::hash<_Value>,
48 typename _Pred = std::equal_to<_Value>,
49 typename _Alloc = std::allocator<_Value> >
51 : public _GLIBCXX_STD_D::unordered_set<_Value, _Hash, _Pred, _Alloc>,
52 public __gnu_debug::_Safe_sequence<unordered_set<_Value, _Hash,
55 typedef _GLIBCXX_STD_D::unordered_set<_Value, _Hash,
57 typedef __gnu_debug::_Safe_sequence<unordered_set> _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_set> iterator;
70 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
71 unordered_set> const_iterator;
74 unordered_set(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_set(_InputIterator __f, _InputIterator __l,
83 const hasher& __hf = hasher(),
84 const key_equal& __eql = key_equal(),
85 const allocator_type& __a = allocator_type())
86 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
87 __hf, __eql, __a), _Safe_base() { }
89 unordered_set(const unordered_set& __x)
90 : _Base(__x), _Safe_base() { }
92 unordered_set(const _Base& __x)
93 : _Base(__x), _Safe_base() { }
95 unordered_set(unordered_set&& __x)
96 : _Base(std::forward<unordered_set>(__x)), _Safe_base() { }
98 unordered_set(initializer_list<value_type> __l,
100 const hasher& __hf = hasher(),
101 const key_equal& __eql = key_equal(),
102 const allocator_type& __a = allocator_type())
103 : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
106 operator=(const unordered_set& __x)
108 *static_cast<_Base*>(this) = __x;
109 this->_M_invalidate_all();
114 operator=(unordered_set&& __x)
124 operator=(initializer_list<value_type> __l)
132 swap(unordered_set& __x)
135 _Safe_base::_M_swap(__x);
142 this->_M_invalidate_all();
147 { return iterator(_Base::begin(), this); }
151 { return const_iterator(_Base::begin(), this); }
155 { return iterator(_Base::end(), this); }
159 { return const_iterator(_Base::end(), this); }
163 { return const_iterator(_Base::begin(), this); }
167 { return const_iterator(_Base::end(), this); }
175 std::pair<iterator, bool>
176 insert(const value_type& __obj)
178 typedef std::pair<typename _Base::iterator, bool> __pair_type;
179 __pair_type __res = _Base::insert(__obj);
180 return std::make_pair(iterator(__res.first, this), __res.second);
184 insert(const_iterator, const value_type& __obj)
186 typedef std::pair<typename _Base::iterator, bool> __pair_type;
187 __pair_type __res = _Base::insert(__obj);
188 return iterator(__res.first, this);
192 insert(std::initializer_list<value_type> __l)
193 { _Base::insert(__l); }
195 template<typename _InputIterator>
197 insert(_InputIterator __first, _InputIterator __last)
199 __glibcxx_check_valid_range(__first, __last);
200 _Base::insert(__first, __last);
204 find(const key_type& __key)
205 { return iterator(_Base::find(__key), this); }
208 find(const key_type& __key) const
209 { return const_iterator(_Base::find(__key), this); }
211 std::pair<iterator, iterator>
212 equal_range(const key_type& __key)
214 typedef typename _Base::iterator _Base_iterator;
215 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
216 __pair_type __res = _Base::equal_range(__key);
217 return std::make_pair(iterator(__res.first, this),
218 iterator(__res.second, this));
221 std::pair<const_iterator, const_iterator>
222 equal_range(const key_type& __key) const
224 typedef typename _Base::const_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(const_iterator(__res.first, this),
228 const_iterator(__res.second, this));
232 erase(const key_type& __key)
235 iterator __victim(_Base::find(__key), this);
236 if (__victim != end())
238 this->erase(__victim);
245 erase(const_iterator __it)
247 __glibcxx_check_erase(__it);
248 __it._M_invalidate();
249 return iterator(_Base::erase(__it.base()), this);
253 erase(const_iterator __first, const_iterator __last)
255 __glibcxx_check_erase_range(__first, __last);
256 for (const_iterator __tmp = __first; __tmp != __last;)
258 const_iterator __victim = __tmp++;
259 __victim._M_invalidate();
261 return iterator(_Base::erase(__first.base(),
262 __last.base()), this);
266 _M_base() { return *this; }
269 _M_base() const { return *this; }
275 typedef typename _Base::const_iterator _Base_const_iterator;
276 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
277 this->_M_invalidate_if(_Not_equal(_M_base().end()));
281 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
283 swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
284 unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
287 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
289 operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
290 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
291 { return __x._M_equal(__y); }
293 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
295 operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
296 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
297 { return !(__x == __y); }
300 /// Class std::unordered_multiset with safety/checking/debug instrumentation.
301 template<typename _Value,
302 typename _Hash = std::hash<_Value>,
303 typename _Pred = std::equal_to<_Value>,
304 typename _Alloc = std::allocator<_Value> >
305 class unordered_multiset
306 : public _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash, _Pred, _Alloc>,
307 public __gnu_debug::_Safe_sequence<unordered_multiset<_Value, _Hash,
310 typedef _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash,
311 _Pred, _Alloc> _Base;
312 typedef __gnu_debug::_Safe_sequence<unordered_multiset> _Safe_base;
315 typedef typename _Base::size_type size_type;
316 typedef typename _Base::hasher hasher;
317 typedef typename _Base::key_equal key_equal;
318 typedef typename _Base::allocator_type allocator_type;
320 typedef typename _Base::key_type key_type;
321 typedef typename _Base::value_type value_type;
323 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
324 unordered_multiset> iterator;
325 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
326 unordered_multiset> const_iterator;
329 unordered_multiset(size_type __n = 10,
330 const hasher& __hf = hasher(),
331 const key_equal& __eql = key_equal(),
332 const allocator_type& __a = allocator_type())
333 : _Base(__n, __hf, __eql, __a) { }
335 template<typename _InputIterator>
336 unordered_multiset(_InputIterator __f, _InputIterator __l,
338 const hasher& __hf = hasher(),
339 const key_equal& __eql = key_equal(),
340 const allocator_type& __a = allocator_type())
341 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n,
342 __hf, __eql, __a), _Safe_base() { }
344 unordered_multiset(const unordered_multiset& __x)
345 : _Base(__x), _Safe_base() { }
347 unordered_multiset(const _Base& __x)
348 : _Base(__x), _Safe_base() { }
350 unordered_multiset(unordered_multiset&& __x)
351 : _Base(std::forward<unordered_multiset>(__x)), _Safe_base() { }
353 unordered_multiset(initializer_list<value_type> __l,
355 const hasher& __hf = hasher(),
356 const key_equal& __eql = key_equal(),
357 const allocator_type& __a = allocator_type())
358 : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
361 operator=(const unordered_multiset& __x)
363 *static_cast<_Base*>(this) = __x;
364 this->_M_invalidate_all();
369 operator=(unordered_multiset&& __x)
379 operator=(initializer_list<value_type> __l)
387 swap(unordered_multiset& __x)
390 _Safe_base::_M_swap(__x);
397 this->_M_invalidate_all();
402 { return iterator(_Base::begin(), this); }
406 { return const_iterator(_Base::begin(), this); }
410 { return iterator(_Base::end(), this); }
414 { return const_iterator(_Base::end(), this); }
418 { return const_iterator(_Base::begin(), this); }
422 { return const_iterator(_Base::end(), this); }
431 insert(const value_type& __obj)
432 { return iterator(_Base::insert(__obj), this); }
435 insert(const_iterator, const value_type& __obj)
436 { return iterator(_Base::insert(__obj), this); }
439 insert(std::initializer_list<value_type> __l)
440 { _Base::insert(__l); }
442 template<typename _InputIterator>
444 insert(_InputIterator __first, _InputIterator __last)
446 __glibcxx_check_valid_range(__first, __last);
447 _Base::insert(__first, __last);
451 find(const key_type& __key)
452 { return iterator(_Base::find(__key), this); }
455 find(const key_type& __key) const
456 { return const_iterator(_Base::find(__key), this); }
458 std::pair<iterator, iterator>
459 equal_range(const key_type& __key)
461 typedef typename _Base::iterator _Base_iterator;
462 typedef std::pair<_Base_iterator, _Base_iterator> __pair_type;
463 __pair_type __res = _Base::equal_range(__key);
464 return std::make_pair(iterator(__res.first, this),
465 iterator(__res.second, this));
468 std::pair<const_iterator, const_iterator>
469 equal_range(const key_type& __key) const
471 typedef typename _Base::const_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(const_iterator(__res.first, this),
475 const_iterator(__res.second, this));
479 erase(const key_type& __key)
482 iterator __victim(_Base::find(__key), this);
483 if (__victim != end())
485 this->erase(__victim);
492 erase(const_iterator __it)
494 __glibcxx_check_erase(__it);
495 __it._M_invalidate();
496 return iterator(_Base::erase(__it.base()), this);
500 erase(const_iterator __first, const_iterator __last)
502 __glibcxx_check_erase_range(__first, __last);
503 for (const_iterator __tmp = __first; __tmp != __last;)
505 const_iterator __victim = __tmp++;
506 __victim._M_invalidate();
508 return iterator(_Base::erase(__first.base(),
509 __last.base()), this);
513 _M_base() { return *this; }
516 _M_base() const { return *this; }
522 typedef typename _Base::const_iterator _Base_const_iterator;
523 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
524 this->_M_invalidate_if(_Not_equal(_M_base().end()));
528 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
530 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
531 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
534 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
536 operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
537 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
538 { return __x._M_equal(__y); }
540 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
542 operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
543 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
544 { return !(__x == __y); }
546 } // namespace __debug
549 #endif // __GXX_EXPERIMENTAL_CXX0X__