diff options
Diffstat (limited to 'system/include/libcxx/__hash_table')
-rw-r--r-- | system/include/libcxx/__hash_table | 620 |
1 files changed, 566 insertions, 54 deletions
diff --git a/system/include/libcxx/__hash_table b/system/include/libcxx/__hash_table index 6f6050d3..6157fcd9 100644 --- a/system/include/libcxx/__hash_table +++ b/system/include/libcxx/__hash_table @@ -20,6 +20,12 @@ #include <__undef_min_max> +#ifdef _LIBCPP_DEBUG2 +# include <__debug> +#else +# define _LIBCPP_ASSERT(x, m) ((void)0) +#endif + #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #pragma GCC system_header #endif @@ -33,7 +39,6 @@ template <class _NodePtr> struct __hash_node_base { typedef __hash_node_base __first_node; - // typedef _NodePtr pointer; _NodePtr __next_; @@ -106,16 +111,70 @@ public: #endif pointer; - _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT {} + _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT +#if _LIBCPP_STD_VER > 11 + : __node_(nullptr) +#endif + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + __get_db()->__insert_i(this); +#endif + } + +#if _LIBCPP_DEBUG_LEVEL >= 2 + + _LIBCPP_INLINE_VISIBILITY + __hash_iterator(const __hash_iterator& __i) + : __node_(__i.__node_) + { + __get_db()->__iterator_copy(this, &__i); + } + + _LIBCPP_INLINE_VISIBILITY + ~__hash_iterator() + { + __get_db()->__erase_i(this); + } + + _LIBCPP_INLINE_VISIBILITY + __hash_iterator& operator=(const __hash_iterator& __i) + { + if (this != &__i) + { + __get_db()->__iterator_copy(this, &__i); + __node_ = __i.__node_; + } + return *this; + } + +#endif // _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_INLINE_VISIBILITY - reference operator*() const {return __node_->__value_;} + reference operator*() const + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to dereference a non-dereferenceable unordered container iterator"); +#endif + return __node_->__value_; + } _LIBCPP_INLINE_VISIBILITY - pointer operator->() const {return _VSTD::addressof(__node_->__value_);} + pointer operator->() const + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to dereference a non-dereferenceable unordered container iterator"); +#endif + return pointer_traits<pointer>::pointer_to(__node_->__value_); + } _LIBCPP_INLINE_VISIBILITY __hash_iterator& operator++() { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to increment non-incrementable unordered container iterator"); +#endif __node_ = __node_->__next_; return *this; } @@ -130,16 +189,27 @@ public: friend _LIBCPP_INLINE_VISIBILITY bool operator==(const __hash_iterator& __x, const __hash_iterator& __y) - {return __x.__node_ == __y.__node_;} + { + return __x.__node_ == __y.__node_; + } friend _LIBCPP_INLINE_VISIBILITY bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y) - {return __x.__node_ != __y.__node_;} + {return !(__x == __y);} private: +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_INLINE_VISIBILITY + __hash_iterator(__node_pointer __node, const void* __c) _NOEXCEPT + : __node_(__node) + { + __get_db()->__insert_ic(this, __c); + } +#else _LIBCPP_INLINE_VISIBILITY __hash_iterator(__node_pointer __node) _NOEXCEPT : __node_(__node) {} +#endif template <class, class, class, class> friend class __hash_table; template <class> friend class _LIBCPP_TYPE_VIS __hash_const_iterator; @@ -180,20 +250,78 @@ public: __non_const_node_pointer; typedef __hash_iterator<__non_const_node_pointer> __non_const_iterator; - _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT {} + _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT +#if _LIBCPP_STD_VER > 11 + : __node_(nullptr) +#endif + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + __get_db()->__insert_i(this); +#endif + } _LIBCPP_INLINE_VISIBILITY __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT : __node_(__x.__node_) - {} + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + __get_db()->__iterator_copy(this, &__x); +#endif + } + +#if _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_INLINE_VISIBILITY - reference operator*() const {return __node_->__value_;} + __hash_const_iterator(const __hash_const_iterator& __i) + : __node_(__i.__node_) + { + __get_db()->__iterator_copy(this, &__i); + } + + _LIBCPP_INLINE_VISIBILITY + ~__hash_const_iterator() + { + __get_db()->__erase_i(this); + } + + _LIBCPP_INLINE_VISIBILITY + __hash_const_iterator& operator=(const __hash_const_iterator& __i) + { + if (this != &__i) + { + __get_db()->__iterator_copy(this, &__i); + __node_ = __i.__node_; + } + return *this; + } + +#endif // _LIBCPP_DEBUG_LEVEL >= 2 + + _LIBCPP_INLINE_VISIBILITY + reference operator*() const + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to dereference a non-dereferenceable unordered container const_iterator"); +#endif + return __node_->__value_; + } _LIBCPP_INLINE_VISIBILITY - pointer operator->() const {return _VSTD::addressof(__node_->__value_);} + pointer operator->() const + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to dereference a non-dereferenceable unordered container const_iterator"); +#endif + return pointer_traits<pointer>::pointer_to(__node_->__value_); + } _LIBCPP_INLINE_VISIBILITY __hash_const_iterator& operator++() { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to increment non-incrementable unordered container const_iterator"); +#endif __node_ = __node_->__next_; return *this; } @@ -208,16 +336,27 @@ public: friend _LIBCPP_INLINE_VISIBILITY bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y) - {return __x.__node_ == __y.__node_;} + { + return __x.__node_ == __y.__node_; + } friend _LIBCPP_INLINE_VISIBILITY bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y) - {return __x.__node_ != __y.__node_;} + {return !(__x == __y);} private: +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_INLINE_VISIBILITY + __hash_const_iterator(__node_pointer __node, const void* __c) _NOEXCEPT + : __node_(__node) + { + __get_db()->__insert_ic(this, __c); + } +#else _LIBCPP_INLINE_VISIBILITY __hash_const_iterator(__node_pointer __node) _NOEXCEPT : __node_(__node) {} +#endif template <class, class, class, class> friend class __hash_table; template <class> friend class _LIBCPP_TYPE_VIS __hash_map_const_iterator; @@ -250,16 +389,71 @@ public: #endif pointer; - _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT {} + _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + __get_db()->__insert_i(this); +#endif + } + +#if _LIBCPP_DEBUG_LEVEL >= 2 + + _LIBCPP_INLINE_VISIBILITY + __hash_local_iterator(const __hash_local_iterator& __i) + : __node_(__i.__node_), + __bucket_(__i.__bucket_), + __bucket_count_(__i.__bucket_count_) + { + __get_db()->__iterator_copy(this, &__i); + } _LIBCPP_INLINE_VISIBILITY - reference operator*() const {return __node_->__value_;} + ~__hash_local_iterator() + { + __get_db()->__erase_i(this); + } + _LIBCPP_INLINE_VISIBILITY - pointer operator->() const {return &__node_->__value_;} + __hash_local_iterator& operator=(const __hash_local_iterator& __i) + { + if (this != &__i) + { + __get_db()->__iterator_copy(this, &__i); + __node_ = __i.__node_; + __bucket_ = __i.__bucket_; + __bucket_count_ = __i.__bucket_count_; + } + return *this; + } + +#endif // _LIBCPP_DEBUG_LEVEL >= 2 + + _LIBCPP_INLINE_VISIBILITY + reference operator*() const + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to dereference a non-dereferenceable unordered container local_iterator"); +#endif + return __node_->__value_; + } + _LIBCPP_INLINE_VISIBILITY + pointer operator->() const + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to dereference a non-dereferenceable unordered container local_iterator"); +#endif + return pointer_traits<pointer>::pointer_to(__node_->__value_); + } _LIBCPP_INLINE_VISIBILITY __hash_local_iterator& operator++() { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to increment non-incrementable unordered container local_iterator"); +#endif __node_ = __node_->__next_; if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_) __node_ = nullptr; @@ -276,12 +470,27 @@ public: friend _LIBCPP_INLINE_VISIBILITY bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y) - {return __x.__node_ == __y.__node_;} + { + return __x.__node_ == __y.__node_; + } friend _LIBCPP_INLINE_VISIBILITY bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y) - {return __x.__node_ != __y.__node_;} + {return !(__x == __y);} private: +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_INLINE_VISIBILITY + __hash_local_iterator(__node_pointer __node, size_t __bucket, + size_t __bucket_count, const void* __c) _NOEXCEPT + : __node_(__node), + __bucket_(__bucket), + __bucket_count_(__bucket_count) + { + __get_db()->__insert_ic(this, __c); + if (__node_ != nullptr) + __node_ = __node_->__next_; + } +#else _LIBCPP_INLINE_VISIBILITY __hash_local_iterator(__node_pointer __node, size_t __bucket, size_t __bucket_count) _NOEXCEPT @@ -292,7 +501,7 @@ private: if (__node_ != nullptr) __node_ = __node_->__next_; } - +#endif template <class, class, class, class> friend class __hash_table; template <class> friend class _LIBCPP_TYPE_VIS __hash_const_local_iterator; template <class> friend class _LIBCPP_TYPE_VIS __hash_map_iterator; @@ -334,22 +543,82 @@ public: #endif pointer; - _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT {} + _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + __get_db()->__insert_i(this); +#endif + } + _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT : __node_(__x.__node_), __bucket_(__x.__bucket_), __bucket_count_(__x.__bucket_count_) - {} + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + __get_db()->__iterator_copy(this, &__x); +#endif + } + +#if _LIBCPP_DEBUG_LEVEL >= 2 + + _LIBCPP_INLINE_VISIBILITY + __hash_const_local_iterator(const __hash_const_local_iterator& __i) + : __node_(__i.__node_), + __bucket_(__i.__bucket_), + __bucket_count_(__i.__bucket_count_) + { + __get_db()->__iterator_copy(this, &__i); + } + + _LIBCPP_INLINE_VISIBILITY + ~__hash_const_local_iterator() + { + __get_db()->__erase_i(this); + } _LIBCPP_INLINE_VISIBILITY - reference operator*() const {return __node_->__value_;} + __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i) + { + if (this != &__i) + { + __get_db()->__iterator_copy(this, &__i); + __node_ = __i.__node_; + __bucket_ = __i.__bucket_; + __bucket_count_ = __i.__bucket_count_; + } + return *this; + } + +#endif // _LIBCPP_DEBUG_LEVEL >= 2 + + _LIBCPP_INLINE_VISIBILITY + reference operator*() const + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to dereference a non-dereferenceable unordered container const_local_iterator"); +#endif + return __node_->__value_; + } _LIBCPP_INLINE_VISIBILITY - pointer operator->() const {return &__node_->__value_;} + pointer operator->() const + { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to dereference a non-dereferenceable unordered container const_local_iterator"); +#endif + return pointer_traits<pointer>::pointer_to(__node_->__value_); + } _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator& operator++() { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), + "Attempted to increment non-incrementable unordered container const_local_iterator"); +#endif __node_ = __node_->__next_; if (__node_ != nullptr && __constrain_hash(__node_->__hash_, __bucket_count_) != __bucket_) __node_ = nullptr; @@ -366,12 +635,27 @@ public: friend _LIBCPP_INLINE_VISIBILITY bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) - {return __x.__node_ == __y.__node_;} + { + return __x.__node_ == __y.__node_; + } friend _LIBCPP_INLINE_VISIBILITY bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) - {return __x.__node_ != __y.__node_;} + {return !(__x == __y);} private: +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_INLINE_VISIBILITY + __hash_const_local_iterator(__node_pointer __node, size_t __bucket, + size_t __bucket_count, const void* __c) _NOEXCEPT + : __node_(__node), + __bucket_(__bucket), + __bucket_count_(__bucket_count) + { + __get_db()->__insert_ic(this, __c); + if (__node_ != nullptr) + __node_ = __node_->__next_; + } +#else _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator(__node_pointer __node, size_t __bucket, size_t __bucket_count) _NOEXCEPT @@ -382,7 +666,7 @@ private: if (__node_ != nullptr) __node_ = __node_->__next_; } - +#endif template <class, class, class, class> friend class __hash_table; template <class> friend class _LIBCPP_TYPE_VIS __hash_map_const_iterator; }; @@ -505,8 +789,15 @@ public: __node_allocator; typedef allocator_traits<__node_allocator> __node_traits; typedef typename __node_traits::pointer __node_pointer; - typedef typename __node_traits::const_pointer __node_const_pointer; + typedef typename __node_traits::pointer __node_const_pointer; typedef __hash_node_base<__node_pointer> __first_node; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<__first_node> +#else + rebind<__first_node>::other +#endif + __node_base_pointer; private: @@ -558,9 +849,9 @@ public: public: typedef __hash_iterator<__node_pointer> iterator; - typedef __hash_const_iterator<__node_const_pointer> const_iterator; + typedef __hash_const_iterator<__node_pointer> const_iterator; typedef __hash_local_iterator<__node_pointer> local_iterator; - typedef __hash_const_local_iterator<__node_const_pointer> const_local_iterator; + typedef __hash_const_local_iterator<__node_pointer> const_local_iterator; __hash_table() _NOEXCEPT_( @@ -658,7 +949,11 @@ public: template <class _Key> _LIBCPP_INLINE_VISIBILITY size_type bucket(const _Key& __k) const - {return __constrain_hash(hash_function()(__k), bucket_count());} + { + _LIBCPP_ASSERT(bucket_count() > 0, + "unordered container::bucket(key) called when bucket_count() == 0"); + return __constrain_hash(hash_function()(__k), bucket_count()); + } template <class _Key> iterator find(const _Key& __x); @@ -706,7 +1001,7 @@ public: _LIBCPP_INLINE_VISIBILITY size_type max_bucket_count() const _NOEXCEPT - {return __bucket_list_.get_deleter().__alloc().max_size();} + {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());} size_type bucket_size(size_type __n) const; _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT { @@ -714,16 +1009,73 @@ public: return __bc != 0 ? (float)size() / __bc : 0.f; } _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT - {max_load_factor() = _VSTD::max(__mlf, load_factor());} - - _LIBCPP_INLINE_VISIBILITY local_iterator begin(size_type __n) - {return local_iterator(__bucket_list_[__n], __n, bucket_count());} - _LIBCPP_INLINE_VISIBILITY local_iterator end(size_type __n) - {return local_iterator(nullptr, __n, bucket_count());} - _LIBCPP_INLINE_VISIBILITY const_local_iterator cbegin(size_type __n) const - {return const_local_iterator(__bucket_list_[__n], __n, bucket_count());} - _LIBCPP_INLINE_VISIBILITY const_local_iterator cend(size_type __n) const - {return const_local_iterator(nullptr, __n, bucket_count());} + { + _LIBCPP_ASSERT(__mlf > 0, + "unordered container::max_load_factor(lf) called with lf <= 0"); + max_load_factor() = _VSTD::max(__mlf, load_factor()); + } + + _LIBCPP_INLINE_VISIBILITY + local_iterator + begin(size_type __n) + { + _LIBCPP_ASSERT(__n < bucket_count(), + "unordered container::begin(n) called with n >= bucket_count()"); +#if _LIBCPP_DEBUG_LEVEL >= 2 + return local_iterator(__bucket_list_[__n], __n, bucket_count(), this); +#else + return local_iterator(__bucket_list_[__n], __n, bucket_count()); +#endif + } + + _LIBCPP_INLINE_VISIBILITY + local_iterator + end(size_type __n) + { + _LIBCPP_ASSERT(__n < bucket_count(), + "unordered container::end(n) called with n >= bucket_count()"); +#if _LIBCPP_DEBUG_LEVEL >= 2 + return local_iterator(nullptr, __n, bucket_count(), this); +#else + return local_iterator(nullptr, __n, bucket_count()); +#endif + } + + _LIBCPP_INLINE_VISIBILITY + const_local_iterator + cbegin(size_type __n) const + { + _LIBCPP_ASSERT(__n < bucket_count(), + "unordered container::cbegin(n) called with n >= bucket_count()"); +#if _LIBCPP_DEBUG_LEVEL >= 2 + return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this); +#else + return const_local_iterator(__bucket_list_[__n], __n, bucket_count()); +#endif + } + + _LIBCPP_INLINE_VISIBILITY + const_local_iterator + cend(size_type __n) const + { + _LIBCPP_ASSERT(__n < bucket_count(), + "unordered container::cend(n) called with n >= bucket_count()"); +#if _LIBCPP_DEBUG_LEVEL >= 2 + return const_local_iterator(nullptr, __n, bucket_count(), this); +#else + return const_local_iterator(nullptr, __n, bucket_count()); +#endif + } + +#if _LIBCPP_DEBUG_LEVEL >= 2 + + bool __dereferenceable(const const_iterator* __i) const; + bool __decrementable(const const_iterator* __i) const; + bool __addable(const const_iterator* __i, ptrdiff_t __n) const; + bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const; + +#endif // _LIBCPP_DEBUG_LEVEL >= 2 + private: void __rehash(size_type __n); @@ -807,6 +1159,9 @@ private: void __deallocate(__node_pointer __np) _NOEXCEPT; __node_pointer __detach() _NOEXCEPT; + + template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS unordered_map; + template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS unordered_multimap; }; template <class _Tp, class _Hash, class _Equal, class _Alloc> @@ -893,7 +1248,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u) if (size() > 0) { __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] = - static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())); __u.__p1_.first().__next_ = nullptr; __u.size() = 0; } @@ -917,7 +1272,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u, __p1_.first().__next_ = __u.__p1_.first().__next_; __u.__p1_.first().__next_ = nullptr; __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] = - static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())); size() = __u.size(); __u.size() = 0; } @@ -930,6 +1285,9 @@ template <class _Tp, class _Hash, class _Equal, class _Alloc> __hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table() { __deallocate(__p1_.first().__next_); +#if _LIBCPP_DEBUG_LEVEL >= 2 + __get_db()->__erase_c(this); +#endif } template <class _Tp, class _Hash, class _Equal, class _Alloc> @@ -971,6 +1329,21 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np) while (__np != nullptr) { __node_pointer __next = __np->__next_; +#if _LIBCPP_DEBUG_LEVEL >= 2 + __c_node* __c = __get_db()->__find_c_and_lock(this); + for (__i_node** __p = __c->end_; __p != __c->beg_; ) + { + --__p; + iterator* __i = static_cast<iterator*>((*__p)->__i_); + if (__i->__node_ == __np) + { + (*__p)->__c_ = nullptr; + if (--__c->end_ != __p) + memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); + } + } + __get_db()->unlock(); +#endif __node_traits::destroy(__na, _VSTD::addressof(__np->__value_)); __node_traits::deallocate(__na, __np, 1); __np = __next; @@ -1014,10 +1387,13 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( if (size() > 0) { __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] = - static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())); __u.__p1_.first().__next_ = nullptr; __u.size() = 0; } +#if _LIBCPP_DEBUG_LEVEL >= 2 + __get_db()->swap(this, &__u); +#endif } template <class _Tp, class _Hash, class _Equal, class _Alloc> @@ -1158,7 +1534,11 @@ inline _LIBCPP_INLINE_VISIBILITY typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 + return iterator(__p1_.first().__next_, this); +#else return iterator(__p1_.first().__next_); +#endif } template <class _Tp, class _Hash, class _Equal, class _Alloc> @@ -1166,7 +1546,11 @@ inline _LIBCPP_INLINE_VISIBILITY typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 + return iterator(nullptr, this); +#else return iterator(nullptr); +#endif } template <class _Tp, class _Hash, class _Equal, class _Alloc> @@ -1174,7 +1558,11 @@ inline _LIBCPP_INLINE_VISIBILITY typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 + return const_iterator(__p1_.first().__next_, this); +#else return const_iterator(__p1_.first().__next_); +#endif } template <class _Tp, class _Hash, class _Equal, class _Alloc> @@ -1182,7 +1570,11 @@ inline _LIBCPP_INLINE_VISIBILITY typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT { +#if _LIBCPP_DEBUG_LEVEL >= 2 + return const_iterator(nullptr, this); +#else return const_iterator(nullptr); +#endif } template <class _Tp, class _Hash, class _Equal, class _Alloc> @@ -1236,7 +1628,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __ __node_pointer __pn = __bucket_list_[__chash]; if (__pn == nullptr) { - __pn = static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())); __nd->__next_ = __pn->__next_; __pn->__next_ = __nd; // fix up __bucket_list_ @@ -1255,7 +1647,11 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __ __inserted = true; } __done: +#if _LIBCPP_DEBUG_LEVEL >= 2 + return pair<iterator, bool>(iterator(__ndptr, this), __inserted); +#else return pair<iterator, bool>(iterator(__ndptr), __inserted); +#endif } template <class _Tp, class _Hash, class _Equal, class _Alloc> @@ -1274,7 +1670,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __c __node_pointer __pn = __bucket_list_[__chash]; if (__pn == nullptr) { - __pn = static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())); __cp->__next_ = __pn->__next_; __pn->__next_ = __cp; // fix up __bucket_list_ @@ -1312,7 +1708,11 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __c } } ++size(); +#if _LIBCPP_DEBUG_LEVEL >= 2 + return iterator(__cp, this); +#else return iterator(__cp); +#endif } template <class _Tp, class _Hash, class _Equal, class _Alloc> @@ -1320,9 +1720,14 @@ typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi( const_iterator __p, __node_pointer __cp) { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + "unordered container::emplace_hint(const_iterator, args...) called with an iterator not" + " referring to this unordered container"); +#endif if (__p != end() && key_eq()(*__p, __cp->__value_)) { - __node_pointer __np = const_cast<__node_pointer>(__p.__node_); + __node_pointer __np = __p.__node_; __cp->__hash_ = __np->__hash_; size_type __bc = bucket_count(); if (size()+1 > __bc * max_load_factor() || __bc == 0) @@ -1338,7 +1743,11 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi( __cp->__next_ = __np; __pp->__next_ = __cp; ++size(); +#if _LIBCPP_DEBUG_LEVEL >= 2 + return iterator(__cp, this); +#else return iterator(__cp); +#endif } return __node_insert_multi(__cp); } @@ -1380,7 +1789,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(const value_type& __x) __node_pointer __pn = __bucket_list_[__chash]; if (__pn == nullptr) { - __pn = static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())); __h->__next_ = __pn->__next_; __pn->__next_ = __h.get(); // fix up __bucket_list_ @@ -1399,7 +1808,11 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(const value_type& __x) __inserted = true; } __done: +#if _LIBCPP_DEBUG_LEVEL >= 2 + return pair<iterator, bool>(iterator(__nd, this), __inserted); +#else return pair<iterator, bool>(iterator(__nd), __inserted); +#endif } #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES @@ -1434,6 +1847,11 @@ typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi( const_iterator __p, _Args&&... __args) { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + "unordered container::emplace_hint(const_iterator, args...) called with an iterator not" + " referring to this unordered container"); +#endif __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); iterator __r = __node_insert_multi(__p, __h.get()); __h.release(); @@ -1475,6 +1893,11 @@ typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p, _Pp&& __x) { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + "unordered container::insert(const_iterator, rvalue) called with an iterator not" + " referring to this unordered container"); +#endif __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x)); iterator __r = __node_insert_multi(__p, __h.get()); __h.release(); @@ -1498,6 +1921,11 @@ typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p, const value_type& __x) { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + "unordered container::insert(const_iterator, lvalue) called with an iterator not" + " referring to this unordered container"); +#endif __node_holder __h = __construct_node(__x); iterator __r = __node_insert_multi(__p, __h.get()); __h.release(); @@ -1534,6 +1962,9 @@ template <class _Tp, class _Hash, class _Equal, class _Alloc> void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc) { +#if _LIBCPP_DEBUG_LEVEL >= 2 + __get_db()->__invalidate_all(this); +#endif // _LIBCPP_DEBUG_LEVEL >= 2 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc(); __bucket_list_.reset(__nbc > 0 ? __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr); @@ -1542,7 +1973,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc) { for (size_type __i = 0; __i < __nbc; ++__i) __bucket_list_[__i] = nullptr; - __node_pointer __pp(static_cast<__node_pointer>(_VSTD::addressof(__p1_.first()))); + __node_pointer __pp(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()))); __node_pointer __cp = __pp->__next_; if (__cp != nullptr) { @@ -1599,7 +2030,11 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) __nd = __nd->__next_) { if (key_eq()(__nd->__value_, __k)) +#if _LIBCPP_DEBUG_LEVEL >= 2 + return iterator(__nd, this); +#else return iterator(__nd); +#endif } } } @@ -1624,7 +2059,11 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const __nd = __nd->__next_) { if (key_eq()(__nd->__value_, __k)) +#if _LIBCPP_DEBUG_LEVEL >= 2 + return const_iterator(__nd, this); +#else return const_iterator(__nd); +#endif } } @@ -1700,8 +2139,17 @@ template <class _Tp, class _Hash, class _Equal, class _Alloc> typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p) { - __node_pointer __np = const_cast<__node_pointer>(__p.__node_); + __node_pointer __np = __p.__node_; +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, + "unordered container erase(iterator) called with an iterator not" + " referring to this container"); + _LIBCPP_ASSERT(__p != end(), + "unordered container erase(iterator) called with a non-dereferenceable iterator"); + iterator __r(__np, this); +#else iterator __r(__np); +#endif ++__r; remove(__p); return __r; @@ -1712,13 +2160,25 @@ typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first, const_iterator __last) { +#if _LIBCPP_DEBUG_LEVEL >= 2 + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this, + "unodered container::erase(iterator, iterator) called with an iterator not" + " referring to this unodered container"); + _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this, + "unodered container::erase(iterator, iterator) called with an iterator not" + " referring to this unodered container"); +#endif for (const_iterator __p = __first; __first != __last; __p = __first) { ++__first; erase(__p); } - __node_pointer __np = const_cast<__node_pointer>(__last.__node_); + __node_pointer __np = __last.__node_; +#if _LIBCPP_DEBUG_LEVEL >= 2 + return iterator (__np, this); +#else return iterator (__np); +#endif } template <class _Tp, class _Hash, class _Equal, class _Alloc> @@ -1757,7 +2217,7 @@ typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT { // current node - __node_pointer __cn = const_cast<__node_pointer>(__p.__node_); + __node_pointer __cn = __p.__node_; size_type __bc = bucket_count(); size_t __chash = __constrain_hash(__cn->__hash_, __bc); // find previous node @@ -1767,7 +2227,8 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT // Fix up __bucket_list_ // if __pn is not in same bucket (before begin is not in same bucket) && // if __cn->__next_ is not in same bucket (nullptr is not in same bucket) - if (__pn == _VSTD::addressof(__p1_.first()) || __constrain_hash(__pn->__hash_, __bc) != __chash) + if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())) + || __constrain_hash(__pn->__hash_, __bc) != __chash) { if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash) __bucket_list_[__chash] = nullptr; @@ -1783,6 +2244,21 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT __pn->__next_ = __cn->__next_; __cn->__next_ = nullptr; --size(); +#if _LIBCPP_DEBUG_LEVEL >= 2 + __c_node* __c = __get_db()->__find_c_and_lock(this); + for (__i_node** __p = __c->end_; __p != __c->beg_; ) + { + --__p; + iterator* __i = static_cast<iterator*>((*__p)->__i_); + if (__i->__node_ == __cn) + { + (*__p)->__c_ = nullptr; + if (--__c->end_ != __p) + memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); + } + } + __get_db()->unlock(); +#endif return __node_holder(__cn, _Dp(__node_alloc(), true)); } @@ -1907,16 +2383,21 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u) __p3_.swap(__u.__p3_); if (size() > 0) __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] = - static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())); if (__u.size() > 0) __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash_, __u.bucket_count())] = - static_cast<__node_pointer>(_VSTD::addressof(__u.__p1_.first())); + static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__u.__p1_.first())); +#if _LIBCPP_DEBUG_LEVEL >= 2 + __get_db()->swap(this, &__u); +#endif } template <class _Tp, class _Hash, class _Equal, class _Alloc> typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type __hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const { + _LIBCPP_ASSERT(__n < bucket_count(), + "unordered container::bucket_size(n) called with n >= bucket_count()"); __node_const_pointer __np = __bucket_list_[__n]; size_type __bc = bucket_count(); size_type __r = 0; @@ -1940,6 +2421,37 @@ swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x, __x.swap(__y); } +#if _LIBCPP_DEBUG_LEVEL >= 2 + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +bool +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const +{ + return __i->__node_ != nullptr; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +bool +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const +{ + return false; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +bool +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const +{ + return false; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +bool +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const +{ + return false; +} + +#endif // _LIBCPP_DEBUG_LEVEL >= 2 _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP__HASH_TABLE |