1 // Debugging unordered_set/unordered_multiset implementation -*- C++ -*-
3 // Copyright (C) 2003-2015 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file debug/unordered_set
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_UNORDERED_SET
30 #define _GLIBCXX_DEBUG_UNORDERED_SET 1
32 #if __cplusplus < 201103L
33 # include <bits/c++0x_warning.h>
35 # include <unordered_set>
37 #include <debug/safe_unordered_container.h>
38 #include <debug/safe_container.h>
39 #include <debug/safe_iterator.h>
40 #include <debug/safe_local_iterator.h>
42 namespace std _GLIBCXX_VISIBILITY(default)
46 /// Class std::unordered_set with safety/checking/debug instrumentation.
47 template<typename _Value,
48 typename _Hash = std::hash<_Value>,
49 typename _Pred = std::equal_to<_Value>,
50 typename _Alloc = std::allocator<_Value> >
52 : public __gnu_debug::_Safe_container<
53 unordered_set<_Value, _Hash, _Pred, _Alloc>, _Alloc,
54 __gnu_debug::_Safe_unordered_container>,
55 public _GLIBCXX_STD_C::unordered_set<_Value, _Hash, _Pred, _Alloc>
57 typedef _GLIBCXX_STD_C::unordered_set<
58 _Value, _Hash, _Pred, _Alloc> _Base;
59 typedef __gnu_debug::_Safe_container<
60 unordered_set, _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
62 typedef typename _Base::const_iterator _Base_const_iterator;
63 typedef typename _Base::iterator _Base_iterator;
64 typedef typename _Base::const_local_iterator _Base_const_local_iterator;
65 typedef typename _Base::local_iterator _Base_local_iterator;
68 typedef typename _Base::size_type size_type;
69 typedef typename _Base::hasher hasher;
70 typedef typename _Base::key_equal key_equal;
71 typedef typename _Base::allocator_type allocator_type;
73 typedef typename _Base::key_type key_type;
74 typedef typename _Base::value_type value_type;
76 typedef __gnu_debug::_Safe_iterator<
77 _Base_iterator, unordered_set> iterator;
78 typedef __gnu_debug::_Safe_iterator<
79 _Base_const_iterator, unordered_set> const_iterator;
80 typedef __gnu_debug::_Safe_local_iterator<
81 _Base_local_iterator, unordered_set> local_iterator;
82 typedef __gnu_debug::_Safe_local_iterator<
83 _Base_const_local_iterator, unordered_set> const_local_iterator;
85 unordered_set() = default;
88 unordered_set(size_type __n,
89 const hasher& __hf = hasher(),
90 const key_equal& __eql = key_equal(),
91 const allocator_type& __a = allocator_type())
92 : _Base(__n, __hf, __eql, __a) { }
94 template<typename _InputIterator>
95 unordered_set(_InputIterator __first, _InputIterator __last,
97 const hasher& __hf = hasher(),
98 const key_equal& __eql = key_equal(),
99 const allocator_type& __a = allocator_type())
100 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
102 __gnu_debug::__base(__last), __n,
103 __hf, __eql, __a) { }
105 unordered_set(const unordered_set&) = default;
107 unordered_set(const _Base& __x)
110 unordered_set(unordered_set&&) = default;
113 unordered_set(const allocator_type& __a)
116 unordered_set(const unordered_set& __uset,
117 const allocator_type& __a)
118 : _Base(__uset, __a) { }
120 unordered_set(unordered_set&& __uset,
121 const allocator_type& __a)
122 : _Safe(std::move(__uset._M_safe()), __a),
123 _Base(std::move(__uset._M_base()), __a) { }
125 unordered_set(initializer_list<value_type> __l,
127 const hasher& __hf = hasher(),
128 const key_equal& __eql = key_equal(),
129 const allocator_type& __a = allocator_type())
130 : _Base(__l, __n, __hf, __eql, __a) { }
132 ~unordered_set() = default;
135 operator=(const unordered_set&) = default;
138 operator=(unordered_set&&) = default;
141 operator=(initializer_list<value_type> __l)
144 this->_M_invalidate_all();
149 swap(unordered_set& __x)
150 noexcept( noexcept(declval<_Base>().swap(__x)) )
160 this->_M_invalidate_all();
165 { return iterator(_Base::begin(), this); }
168 begin() const noexcept
169 { return const_iterator(_Base::begin(), this); }
173 { return iterator(_Base::end(), this); }
177 { return const_iterator(_Base::end(), this); }
180 cbegin() const noexcept
181 { return const_iterator(_Base::begin(), this); }
184 cend() const noexcept
185 { return const_iterator(_Base::end(), this); }
191 __glibcxx_check_bucket_index(__b);
192 return local_iterator(_Base::begin(__b), this);
198 __glibcxx_check_bucket_index(__b);
199 return local_iterator(_Base::end(__b), this);
203 begin(size_type __b) const
205 __glibcxx_check_bucket_index(__b);
206 return const_local_iterator(_Base::begin(__b), this);
210 end(size_type __b) const
212 __glibcxx_check_bucket_index(__b);
213 return const_local_iterator(_Base::end(__b), this);
217 cbegin(size_type __b) const
219 __glibcxx_check_bucket_index(__b);
220 return const_local_iterator(_Base::cbegin(__b), this);
224 cend(size_type __b) const
226 __glibcxx_check_bucket_index(__b);
227 return const_local_iterator(_Base::cend(__b), this);
231 bucket_size(size_type __b) const
233 __glibcxx_check_bucket_index(__b);
234 return _Base::bucket_size(__b);
238 max_load_factor() const noexcept
239 { return _Base::max_load_factor(); }
242 max_load_factor(float __f)
244 __glibcxx_check_max_load_factor(__f);
245 _Base::max_load_factor(__f);
248 template<typename... _Args>
249 std::pair<iterator, bool>
250 emplace(_Args&&... __args)
252 size_type __bucket_count = this->bucket_count();
253 std::pair<_Base_iterator, bool> __res
254 = _Base::emplace(std::forward<_Args>(__args)...);
255 _M_check_rehashed(__bucket_count);
256 return std::make_pair(iterator(__res.first, this), __res.second);
259 template<typename... _Args>
261 emplace_hint(const_iterator __hint, _Args&&... __args)
263 __glibcxx_check_insert(__hint);
264 size_type __bucket_count = this->bucket_count();
265 _Base_iterator __it = _Base::emplace_hint(__hint.base(),
266 std::forward<_Args>(__args)...);
267 _M_check_rehashed(__bucket_count);
268 return iterator(__it, this);
271 std::pair<iterator, bool>
272 insert(const value_type& __obj)
274 size_type __bucket_count = this->bucket_count();
275 std::pair<_Base_iterator, bool> __res
276 = _Base::insert(__obj);
277 _M_check_rehashed(__bucket_count);
278 return std::make_pair(iterator(__res.first, this), __res.second);
282 insert(const_iterator __hint, const value_type& __obj)
284 __glibcxx_check_insert(__hint);
285 size_type __bucket_count = this->bucket_count();
286 _Base_iterator __it = _Base::insert(__hint.base(), __obj);
287 _M_check_rehashed(__bucket_count);
288 return iterator(__it, this);
291 std::pair<iterator, bool>
292 insert(value_type&& __obj)
294 size_type __bucket_count = this->bucket_count();
295 std::pair<_Base_iterator, bool> __res
296 = _Base::insert(std::move(__obj));
297 _M_check_rehashed(__bucket_count);
298 return std::make_pair(iterator(__res.first, this), __res.second);
302 insert(const_iterator __hint, value_type&& __obj)
304 __glibcxx_check_insert(__hint);
305 size_type __bucket_count = this->bucket_count();
306 _Base_iterator __it = _Base::insert(__hint.base(), std::move(__obj));
307 _M_check_rehashed(__bucket_count);
308 return iterator(__it, this);
312 insert(std::initializer_list<value_type> __l)
314 size_type __bucket_count = this->bucket_count();
316 _M_check_rehashed(__bucket_count);
319 template<typename _InputIterator>
321 insert(_InputIterator __first, _InputIterator __last)
323 __glibcxx_check_valid_range(__first, __last);
324 size_type __bucket_count = this->bucket_count();
325 _Base::insert(__gnu_debug::__base(__first),
326 __gnu_debug::__base(__last));
327 _M_check_rehashed(__bucket_count);
331 find(const key_type& __key)
332 { return iterator(_Base::find(__key), this); }
335 find(const key_type& __key) const
336 { return const_iterator(_Base::find(__key), this); }
338 std::pair<iterator, iterator>
339 equal_range(const key_type& __key)
341 std::pair<_Base_iterator, _Base_iterator> __res
342 = _Base::equal_range(__key);
343 return std::make_pair(iterator(__res.first, this),
344 iterator(__res.second, this));
347 std::pair<const_iterator, const_iterator>
348 equal_range(const key_type& __key) const
350 std::pair<_Base_const_iterator, _Base_const_iterator>
351 __res = _Base::equal_range(__key);
352 return std::make_pair(const_iterator(__res.first, this),
353 const_iterator(__res.second, this));
357 erase(const key_type& __key)
360 _Base_iterator __victim(_Base::find(__key));
361 if (__victim != _Base::end())
363 this->_M_invalidate_if(
364 [__victim](_Base_const_iterator __it)
365 { return __it == __victim; });
366 this->_M_invalidate_local_if(
367 [__victim](_Base_const_local_iterator __it)
368 { return __it._M_curr() == __victim._M_cur; });
369 size_type __bucket_count = this->bucket_count();
370 _Base::erase(__victim);
371 _M_check_rehashed(__bucket_count);
378 erase(const_iterator __it)
380 __glibcxx_check_erase(__it);
381 _Base_const_iterator __victim = __it.base();
382 this->_M_invalidate_if(
383 [__victim](_Base_const_iterator __it)
384 { return __it == __victim; });
385 this->_M_invalidate_local_if(
386 [__victim](_Base_const_local_iterator __it)
387 { return __it._M_curr() == __victim._M_cur; });
388 size_type __bucket_count = this->bucket_count();
389 _Base_iterator __next = _Base::erase(__it.base());
390 _M_check_rehashed(__bucket_count);
391 return iterator(__next, this);
396 { return erase(const_iterator(__it)); }
399 erase(const_iterator __first, const_iterator __last)
401 __glibcxx_check_erase_range(__first, __last);
402 for (_Base_const_iterator __tmp = __first.base();
403 __tmp != __last.base(); ++__tmp)
405 _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::end(),
406 _M_message(__gnu_debug::__msg_valid_range)
407 ._M_iterator(__first, "first")
408 ._M_iterator(__last, "last"));
409 this->_M_invalidate_if(
410 [__tmp](_Base_const_iterator __it)
411 { return __it == __tmp; });
412 this->_M_invalidate_local_if(
413 [__tmp](_Base_const_local_iterator __it)
414 { return __it._M_curr() == __tmp._M_cur; });
416 size_type __bucket_count = this->bucket_count();
417 _Base_iterator __next = _Base::erase(__first.base(),
419 _M_check_rehashed(__bucket_count);
420 return iterator(__next, this);
424 _M_base() noexcept { return *this; }
427 _M_base() const noexcept { return *this; }
431 _M_check_rehashed(size_type __prev_count)
433 if (__prev_count != this->bucket_count())
434 this->_M_invalidate_locals();
438 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
440 swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
441 unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
444 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
446 operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
447 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
448 { return __x._M_base() == __y._M_base(); }
450 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
452 operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
453 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
454 { return !(__x == __y); }
457 /// Class std::unordered_multiset with safety/checking/debug instrumentation.
458 template<typename _Value,
459 typename _Hash = std::hash<_Value>,
460 typename _Pred = std::equal_to<_Value>,
461 typename _Alloc = std::allocator<_Value> >
462 class unordered_multiset
463 : public __gnu_debug::_Safe_container<
464 unordered_multiset<_Value, _Hash, _Pred, _Alloc>, _Alloc,
465 __gnu_debug::_Safe_unordered_container>,
466 public _GLIBCXX_STD_C::unordered_multiset<_Value, _Hash, _Pred, _Alloc>
468 typedef _GLIBCXX_STD_C::unordered_multiset<
469 _Value, _Hash, _Pred, _Alloc> _Base;
470 typedef __gnu_debug::_Safe_container<unordered_multiset,
471 _Alloc, __gnu_debug::_Safe_unordered_container> _Safe;
472 typedef typename _Base::const_iterator _Base_const_iterator;
473 typedef typename _Base::iterator _Base_iterator;
474 typedef typename _Base::const_local_iterator
475 _Base_const_local_iterator;
476 typedef typename _Base::local_iterator _Base_local_iterator;
479 typedef typename _Base::size_type size_type;
480 typedef typename _Base::hasher hasher;
481 typedef typename _Base::key_equal key_equal;
482 typedef typename _Base::allocator_type allocator_type;
484 typedef typename _Base::key_type key_type;
485 typedef typename _Base::value_type value_type;
487 typedef __gnu_debug::_Safe_iterator<
488 _Base_iterator, unordered_multiset> iterator;
489 typedef __gnu_debug::_Safe_iterator<
490 _Base_const_iterator, unordered_multiset> const_iterator;
491 typedef __gnu_debug::_Safe_local_iterator<
492 _Base_local_iterator, unordered_multiset> local_iterator;
493 typedef __gnu_debug::_Safe_local_iterator<
494 _Base_const_local_iterator, unordered_multiset> const_local_iterator;
496 unordered_multiset() = default;
499 unordered_multiset(size_type __n,
500 const hasher& __hf = hasher(),
501 const key_equal& __eql = key_equal(),
502 const allocator_type& __a = allocator_type())
503 : _Base(__n, __hf, __eql, __a) { }
505 template<typename _InputIterator>
506 unordered_multiset(_InputIterator __first, _InputIterator __last,
508 const hasher& __hf = hasher(),
509 const key_equal& __eql = key_equal(),
510 const allocator_type& __a = allocator_type())
511 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
513 __gnu_debug::__base(__last), __n,
514 __hf, __eql, __a) { }
516 unordered_multiset(const unordered_multiset&) = default;
518 unordered_multiset(const _Base& __x)
521 unordered_multiset(unordered_multiset&&) = default;
524 unordered_multiset(const allocator_type& __a)
527 unordered_multiset(const unordered_multiset& __uset,
528 const allocator_type& __a)
529 : _Base(__uset, __a) { }
531 unordered_multiset(unordered_multiset&& __uset,
532 const allocator_type& __a)
533 : _Safe(std::move(__uset._M_safe()), __a),
534 _Base(std::move(__uset._M_base()), __a) { }
536 unordered_multiset(initializer_list<value_type> __l,
538 const hasher& __hf = hasher(),
539 const key_equal& __eql = key_equal(),
540 const allocator_type& __a = allocator_type())
541 : _Base(__l, __n, __hf, __eql, __a) { }
543 ~unordered_multiset() = default;
546 operator=(const unordered_multiset&) = default;
549 operator=(unordered_multiset&&) = default;
552 operator=(initializer_list<value_type> __l)
554 this->_M_base() = __l;
555 this->_M_invalidate_all();
560 swap(unordered_multiset& __x)
561 noexcept( noexcept(declval<_Base>().swap(__x)) )
571 this->_M_invalidate_all();
576 { return iterator(_Base::begin(), this); }
579 begin() const noexcept
580 { return const_iterator(_Base::begin(), this); }
584 { return iterator(_Base::end(), this); }
588 { return const_iterator(_Base::end(), this); }
591 cbegin() const noexcept
592 { return const_iterator(_Base::begin(), this); }
595 cend() const noexcept
596 { return const_iterator(_Base::end(), this); }
602 __glibcxx_check_bucket_index(__b);
603 return local_iterator(_Base::begin(__b), this);
609 __glibcxx_check_bucket_index(__b);
610 return local_iterator(_Base::end(__b), this);
614 begin(size_type __b) const
616 __glibcxx_check_bucket_index(__b);
617 return const_local_iterator(_Base::begin(__b), this);
621 end(size_type __b) const
623 __glibcxx_check_bucket_index(__b);
624 return const_local_iterator(_Base::end(__b), this);
628 cbegin(size_type __b) const
630 __glibcxx_check_bucket_index(__b);
631 return const_local_iterator(_Base::cbegin(__b), this);
635 cend(size_type __b) const
637 __glibcxx_check_bucket_index(__b);
638 return const_local_iterator(_Base::cend(__b), this);
642 bucket_size(size_type __b) const
644 __glibcxx_check_bucket_index(__b);
645 return _Base::bucket_size(__b);
649 max_load_factor() const noexcept
650 { return _Base::max_load_factor(); }
653 max_load_factor(float __f)
655 __glibcxx_check_max_load_factor(__f);
656 _Base::max_load_factor(__f);
659 template<typename... _Args>
661 emplace(_Args&&... __args)
663 size_type __bucket_count = this->bucket_count();
665 = _Base::emplace(std::forward<_Args>(__args)...);
666 _M_check_rehashed(__bucket_count);
667 return iterator(__it, this);
670 template<typename... _Args>
672 emplace_hint(const_iterator __hint, _Args&&... __args)
674 __glibcxx_check_insert(__hint);
675 size_type __bucket_count = this->bucket_count();
676 _Base_iterator __it = _Base::emplace_hint(__hint.base(),
677 std::forward<_Args>(__args)...);
678 _M_check_rehashed(__bucket_count);
679 return iterator(__it, this);
683 insert(const value_type& __obj)
685 size_type __bucket_count = this->bucket_count();
686 _Base_iterator __it = _Base::insert(__obj);
687 _M_check_rehashed(__bucket_count);
688 return iterator(__it, this);
692 insert(const_iterator __hint, const value_type& __obj)
694 __glibcxx_check_insert(__hint);
695 size_type __bucket_count = this->bucket_count();
696 _Base_iterator __it = _Base::insert(__hint.base(), __obj);
697 _M_check_rehashed(__bucket_count);
698 return iterator(__it, this);
702 insert(value_type&& __obj)
704 size_type __bucket_count = this->bucket_count();
705 _Base_iterator __it = _Base::insert(std::move(__obj));
706 _M_check_rehashed(__bucket_count);
707 return iterator(__it, this);
711 insert(const_iterator __hint, value_type&& __obj)
713 __glibcxx_check_insert(__hint);
714 size_type __bucket_count = this->bucket_count();
715 _Base_iterator __it = _Base::insert(__hint.base(), std::move(__obj));
716 _M_check_rehashed(__bucket_count);
717 return iterator(__it, this);
721 insert(std::initializer_list<value_type> __l)
723 size_type __bucket_count = this->bucket_count();
725 _M_check_rehashed(__bucket_count);
728 template<typename _InputIterator>
730 insert(_InputIterator __first, _InputIterator __last)
732 __glibcxx_check_valid_range(__first, __last);
733 size_type __bucket_count = this->bucket_count();
734 _Base::insert(__gnu_debug::__base(__first),
735 __gnu_debug::__base(__last));
736 _M_check_rehashed(__bucket_count);
740 find(const key_type& __key)
741 { return iterator(_Base::find(__key), this); }
744 find(const key_type& __key) const
745 { return const_iterator(_Base::find(__key), this); }
747 std::pair<iterator, iterator>
748 equal_range(const key_type& __key)
750 std::pair<_Base_iterator, _Base_iterator> __res
751 = _Base::equal_range(__key);
752 return std::make_pair(iterator(__res.first, this),
753 iterator(__res.second, this));
756 std::pair<const_iterator, const_iterator>
757 equal_range(const key_type& __key) const
759 std::pair<_Base_const_iterator, _Base_const_iterator>
760 __res = _Base::equal_range(__key);
761 return std::make_pair(const_iterator(__res.first, this),
762 const_iterator(__res.second, this));
766 erase(const key_type& __key)
769 std::pair<_Base_iterator, _Base_iterator> __pair =
770 _Base::equal_range(__key);
771 for (_Base_iterator __victim = __pair.first; __victim != __pair.second;)
773 this->_M_invalidate_if([__victim](_Base_const_iterator __it)
774 { return __it == __victim; });
775 this->_M_invalidate_local_if(
776 [__victim](_Base_const_local_iterator __it)
777 { return __it._M_curr() == __victim._M_cur; });
778 _Base::erase(__victim++);
785 erase(const_iterator __it)
787 __glibcxx_check_erase(__it);
788 _Base_const_iterator __victim = __it.base();
789 this->_M_invalidate_if([__victim](_Base_const_iterator __it)
790 { return __it == __victim; });
791 this->_M_invalidate_local_if(
792 [__victim](_Base_const_local_iterator __it)
793 { return __it._M_curr() == __victim._M_cur; });
794 return iterator(_Base::erase(__it.base()), this);
799 { return erase(const_iterator(__it)); }
802 erase(const_iterator __first, const_iterator __last)
804 __glibcxx_check_erase_range(__first, __last);
805 for (_Base_const_iterator __tmp = __first.base();
806 __tmp != __last.base(); ++__tmp)
808 _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::end(),
809 _M_message(__gnu_debug::__msg_valid_range)
810 ._M_iterator(__first, "first")
811 ._M_iterator(__last, "last"));
812 this->_M_invalidate_if([__tmp](_Base_const_iterator __it)
813 { return __it == __tmp; });
814 this->_M_invalidate_local_if(
815 [__tmp](_Base_const_local_iterator __it)
816 { return __it._M_curr() == __tmp._M_cur; });
818 return iterator(_Base::erase(__first.base(),
819 __last.base()), this);
823 _M_base() noexcept { return *this; }
826 _M_base() const noexcept { return *this; }
830 _M_check_rehashed(size_type __prev_count)
832 if (__prev_count != this->bucket_count())
833 this->_M_invalidate_locals();
837 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
839 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
840 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
843 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
845 operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
846 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
847 { return __x._M_base() == __y._M_base(); }
849 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
851 operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
852 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
853 { return !(__x == __y); }
855 } // namespace __debug