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 __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_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::move(__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(__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 _Value, typename _Hash, typename _Pred, typename _Alloc>
286 swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
287 unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
290 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
292 operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
293 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
294 { return __x._M_equal(__y); }
296 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
298 operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
299 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
300 { return !(__x == __y); }
303 /// Class std::unordered_multiset with safety/checking/debug instrumentation.
304 template<typename _Value,
305 typename _Hash = std::hash<_Value>,
306 typename _Pred = std::equal_to<_Value>,
307 typename _Alloc = std::allocator<_Value> >
308 class unordered_multiset
309 : public _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash, _Pred, _Alloc>,
310 public __gnu_debug::_Safe_sequence<unordered_multiset<_Value, _Hash,
313 typedef _GLIBCXX_STD_D::unordered_multiset<_Value, _Hash,
314 _Pred, _Alloc> _Base;
315 typedef __gnu_debug::_Safe_sequence<unordered_multiset> _Safe_base;
318 typedef typename _Base::size_type size_type;
319 typedef typename _Base::hasher hasher;
320 typedef typename _Base::key_equal key_equal;
321 typedef typename _Base::allocator_type allocator_type;
323 typedef typename _Base::key_type key_type;
324 typedef typename _Base::value_type value_type;
326 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,
327 unordered_multiset> iterator;
328 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
329 unordered_multiset> const_iterator;
332 unordered_multiset(size_type __n = 10,
333 const hasher& __hf = hasher(),
334 const key_equal& __eql = key_equal(),
335 const allocator_type& __a = allocator_type())
336 : _Base(__n, __hf, __eql, __a) { }
338 template<typename _InputIterator>
339 unordered_multiset(_InputIterator __first, _InputIterator __last,
341 const hasher& __hf = hasher(),
342 const key_equal& __eql = key_equal(),
343 const allocator_type& __a = allocator_type())
344 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
346 __gnu_debug::__base(__last), __n,
347 __hf, __eql, __a), _Safe_base() { }
349 unordered_multiset(const unordered_multiset& __x)
350 : _Base(__x), _Safe_base() { }
352 unordered_multiset(const _Base& __x)
353 : _Base(__x), _Safe_base() { }
355 unordered_multiset(unordered_multiset&& __x)
356 : _Base(std::move(__x)), _Safe_base() { }
358 unordered_multiset(initializer_list<value_type> __l,
360 const hasher& __hf = hasher(),
361 const key_equal& __eql = key_equal(),
362 const allocator_type& __a = allocator_type())
363 : _Base(__l, __n, __hf, __eql, __a), _Safe_base() { }
366 operator=(const unordered_multiset& __x)
368 *static_cast<_Base*>(this) = __x;
369 this->_M_invalidate_all();
374 operator=(unordered_multiset&& __x)
384 operator=(initializer_list<value_type> __l)
392 swap(unordered_multiset& __x)
395 _Safe_base::_M_swap(__x);
402 this->_M_invalidate_all();
407 { return iterator(_Base::begin(), this); }
411 { return const_iterator(_Base::begin(), this); }
415 { return iterator(_Base::end(), this); }
419 { return const_iterator(_Base::end(), this); }
423 { return const_iterator(_Base::begin(), this); }
427 { return const_iterator(_Base::end(), this); }
436 insert(const value_type& __obj)
437 { return iterator(_Base::insert(__obj), this); }
440 insert(const_iterator, const value_type& __obj)
441 { return iterator(_Base::insert(__obj), this); }
444 insert(std::initializer_list<value_type> __l)
445 { _Base::insert(__l); }
447 template<typename _InputIterator>
449 insert(_InputIterator __first, _InputIterator __last)
451 __glibcxx_check_valid_range(__first, __last);
452 _Base::insert(__gnu_debug::__base(__first),
453 __gnu_debug::__base(__last));
457 find(const key_type& __key)
458 { return iterator(_Base::find(__key), this); }
461 find(const key_type& __key) const
462 { return const_iterator(_Base::find(__key), this); }
464 std::pair<iterator, iterator>
465 equal_range(const key_type& __key)
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));
474 std::pair<const_iterator, const_iterator>
475 equal_range(const key_type& __key) const
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));
485 erase(const key_type& __key)
488 iterator __victim(_Base::find(__key), this);
489 if (__victim != end())
491 this->erase(__victim);
498 erase(const_iterator __it)
500 __glibcxx_check_erase(__it);
501 __it._M_invalidate();
502 return iterator(_Base::erase(__it.base()), this);
506 erase(const_iterator __first, const_iterator __last)
508 __glibcxx_check_erase_range(__first, __last);
509 for (const_iterator __tmp = __first; __tmp != __last;)
511 const_iterator __victim = __tmp++;
512 __victim._M_invalidate();
514 return iterator(_Base::erase(__first.base(),
515 __last.base()), this);
519 _M_base() { return *this; }
522 _M_base() const { return *this; }
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()));
534 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
536 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
537 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __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._M_equal(__y); }
546 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
548 operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
549 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
550 { return !(__x == __y); }
552 } // namespace __debug
555 #endif // __GXX_EXPERIMENTAL_CXX0X__