summaryrefslogtreecommitdiff
path: root/include/__hash_table
diff options
context:
space:
mode:
Diffstat (limited to 'include/__hash_table')
-rw-r--r--include/__hash_table611
1 files changed, 416 insertions, 195 deletions
diff --git a/include/__hash_table b/include/__hash_table
index c7d1ef3d082f..08bc519ae17f 100644
--- a/include/__hash_table
+++ b/include/__hash_table
@@ -17,6 +17,7 @@
#include <iterator>
#include <algorithm>
#include <cmath>
+#include <utility>
#include <__undef_min_max>
#include <__undef___deallocate>
@@ -29,6 +30,29 @@
_LIBCPP_BEGIN_NAMESPACE_STD
+
+#ifndef _LIBCPP_CXX03_LANG
+template <class _Key, class _Tp>
+union __hash_value_type;
+#else
+template <class _Key, class _Tp>
+struct __hash_value_type;
+#endif
+
+#ifndef _LIBCPP_CXX03_LANG
+template <class _Tp>
+struct __is_hash_value_type_imp : false_type {};
+
+template <class _Key, class _Value>
+struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {};
+
+template <class ..._Args>
+struct __is_hash_value_type : false_type {};
+
+template <class _One>
+struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {};
+#endif
+
_LIBCPP_FUNC_VIS
size_t __next_prime(size_t __n);
@@ -49,10 +73,10 @@ struct __hash_node
typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
>
{
- typedef _Tp value_type;
+ typedef _Tp __node_value_type;
size_t __hash_;
- value_type __value_;
+ __node_value_type __value_;
};
inline _LIBCPP_INLINE_VISIBILITY
@@ -66,7 +90,8 @@ inline _LIBCPP_INLINE_VISIBILITY
size_t
__constrain_hash(size_t __h, size_t __bc)
{
- return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : __h % __bc;
+ return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
+ (__h < __bc ? __h : __h % __bc);
}
inline _LIBCPP_INLINE_VISIBILITY
@@ -76,24 +101,173 @@ __next_hash_pow2(size_t __n)
return size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1));
}
+
template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
+
+template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_iterator;
template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator;
+template <class _NodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator;
+template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_iterator;
template <class _HashIterator> class _LIBCPP_TYPE_VIS_ONLY __hash_map_const_iterator;
+template <class _Tp>
+struct __hash_key_value_types {
+ static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
+ typedef _Tp key_type;
+ typedef _Tp __node_value_type;
+ typedef _Tp __container_value_type;
+ static const bool __is_map = false;
+
+ _LIBCPP_INLINE_VISIBILITY
+ static key_type const& __get_key(_Tp const& __v) {
+ return __v;
+ }
+ _LIBCPP_INLINE_VISIBILITY
+ static __container_value_type const& __get_value(__node_value_type const& __v) {
+ return __v;
+ }
+ _LIBCPP_INLINE_VISIBILITY
+ static __container_value_type* __get_ptr(__node_value_type& __n) {
+ return _VSTD::addressof(__n);
+ }
+#ifndef _LIBCPP_CXX03_LANG
+ _LIBCPP_INLINE_VISIBILITY
+ static __container_value_type&& __move(__node_value_type& __v) {
+ return _VSTD::move(__v);
+ }
+#endif
+};
+
+template <class _Key, class _Tp>
+struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
+ typedef _Key key_type;
+ typedef _Tp mapped_type;
+ typedef __hash_value_type<_Key, _Tp> __node_value_type;
+ typedef pair<const _Key, _Tp> __container_value_type;
+ typedef pair<_Key, _Tp> __nc_value_type;
+ typedef __container_value_type __map_value_type;
+ static const bool __is_map = true;
+
+ _LIBCPP_INLINE_VISIBILITY
+ static key_type const& __get_key(__container_value_type const& __v) {
+ return __v.first;
+ }
+
+ template <class _Up>
+ _LIBCPP_INLINE_VISIBILITY
+ static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
+ __container_value_type const&>::type
+ __get_value(_Up& __t) {
+ return __t.__cc;
+ }
+
+ template <class _Up>
+ _LIBCPP_INLINE_VISIBILITY
+ static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
+ __container_value_type const&>::type
+ __get_value(_Up& __t) {
+ return __t;
+ }
+
+ _LIBCPP_INLINE_VISIBILITY
+ static __container_value_type* __get_ptr(__node_value_type& __n) {
+ return _VSTD::addressof(__n.__cc);
+ }
+#ifndef _LIBCPP_CXX03_LANG
+ _LIBCPP_INLINE_VISIBILITY
+ static __nc_value_type&& __move(__node_value_type& __v) {
+ return _VSTD::move(__v.__nc);
+ }
+#endif
+
+};
+
+template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
+ bool = _KVTypes::__is_map>
+struct __hash_map_pointer_types {};
+
+template <class _Tp, class _AllocPtr, class _KVTypes>
+struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
+ typedef typename _KVTypes::__map_value_type _Mv;
+ typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
+ __map_value_type_pointer;
+ typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
+ __const_map_value_type_pointer;
+};
+
+template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
+struct __hash_node_types;
+
+template <class _NodePtr, class _Tp, class _VoidPtr>
+struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
+ : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
+
+{
+ typedef __hash_key_value_types<_Tp> __base;
+
+public:
+ typedef ptrdiff_t difference_type;
+ typedef size_t size_type;
+
+ typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
+
+ typedef typename pointer_traits<_NodePtr>::element_type __node_type;
+ typedef _NodePtr __node_pointer;
+
+ typedef __hash_node_base<__node_pointer> __node_base_type;
+ typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
+ __node_base_pointer;
+
+ typedef _Tp __node_value_type;
+ typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
+ __node_value_type_pointer;
+ typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
+ __const_node_value_type_pointer;
+private:
+ static_assert(!is_const<__node_type>::value,
+ "_NodePtr should never be a pointer to const");
+ static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
+ "_VoidPtr does not point to unqualified void type");
+ static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
+ _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
+};
+
+
+
+template <class _HashIterator>
+struct __hash_node_types_from_iterator;
+template <class _NodePtr>
+struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
+template <class _NodePtr>
+struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
+template <class _NodePtr>
+struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
+template <class _NodePtr>
+struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
+
+
+template <class _NodeValueTp, class _VoidPtr>
+struct __make_hash_node_types {
+ typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
+ typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
+ typedef __hash_node_types<_NodePtr> type;
+};
+
template <class _NodePtr>
class _LIBCPP_TYPE_VIS_ONLY __hash_iterator
{
- typedef _NodePtr __node_pointer;
+ typedef __hash_node_types<_NodePtr> _NodeTypes;
+ typedef _NodePtr __node_pointer;
__node_pointer __node_;
public:
- typedef forward_iterator_tag iterator_category;
- typedef typename pointer_traits<__node_pointer>::element_type::value_type value_type;
- typedef typename pointer_traits<__node_pointer>::difference_type difference_type;
- typedef value_type& reference;
- typedef typename __rebind_pointer<__node_pointer, value_type>::type pointer;
+ typedef forward_iterator_tag iterator_category;
+ typedef typename _NodeTypes::__node_value_type value_type;
+ typedef typename _NodeTypes::difference_type difference_type;
+ typedef value_type& reference;
+ typedef typename _NodeTypes::__node_value_type_pointer pointer;
_LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT
#if _LIBCPP_STD_VER > 11
@@ -202,25 +376,24 @@ private:
template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
};
-template <class _ConstNodePtr>
+template <class _NodePtr>
class _LIBCPP_TYPE_VIS_ONLY __hash_const_iterator
{
- typedef _ConstNodePtr __node_pointer;
-
- __node_pointer __node_;
+ static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
+ typedef __hash_node_types<_NodePtr> _NodeTypes;
+ typedef _NodePtr __node_pointer;
- typedef typename remove_const<
- typename pointer_traits<__node_pointer>::element_type
- >::type __node;
+ __node_pointer __node_;
public:
- typedef forward_iterator_tag iterator_category;
- typedef typename __node::value_type value_type;
- typedef typename pointer_traits<__node_pointer>::difference_type difference_type;
- typedef const value_type& reference;
- typedef typename __rebind_pointer<__node_pointer, const value_type>::type pointer;
- typedef typename __rebind_pointer<__node_pointer, __node>::type __non_const_node_pointer;
- typedef __hash_iterator<__non_const_node_pointer> __non_const_iterator;
+ typedef __hash_iterator<_NodePtr> __non_const_iterator;
+
+ typedef forward_iterator_tag iterator_category;
+ typedef typename _NodeTypes::__node_value_type value_type;
+ typedef typename _NodeTypes::difference_type difference_type;
+ typedef const value_type& reference;
+ typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
+
_LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT
#if _LIBCPP_STD_VER > 11
@@ -336,24 +509,22 @@ private:
template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY unordered_multimap;
};
-template <class _ConstNodePtr> class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator;
-
template <class _NodePtr>
class _LIBCPP_TYPE_VIS_ONLY __hash_local_iterator
{
- typedef _NodePtr __node_pointer;
+ typedef __hash_node_types<_NodePtr> _NodeTypes;
+ typedef _NodePtr __node_pointer;
__node_pointer __node_;
size_t __bucket_;
size_t __bucket_count_;
- typedef pointer_traits<__node_pointer> __pointer_traits;
public:
typedef forward_iterator_tag iterator_category;
- typedef typename __pointer_traits::element_type::value_type value_type;
- typedef typename __pointer_traits::difference_type difference_type;
+ typedef typename _NodeTypes::__node_value_type value_type;
+ typedef typename _NodeTypes::difference_type difference_type;
typedef value_type& reference;
- typedef typename __rebind_pointer<__node_pointer, value_type>::type pointer;
+ typedef typename _NodeTypes::__node_value_type_pointer pointer;
_LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT
{
@@ -476,7 +647,8 @@ private:
template <class _ConstNodePtr>
class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
{
- typedef _ConstNodePtr __node_pointer;
+ typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
+ typedef _ConstNodePtr __node_pointer;
__node_pointer __node_;
size_t __bucket_;
@@ -487,18 +659,15 @@ class _LIBCPP_TYPE_VIS_ONLY __hash_const_local_iterator
typedef typename remove_const<__node>::type __non_const_node;
typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
__non_const_node_pointer;
-
+public:
typedef __hash_local_iterator<__non_const_node_pointer>
__non_const_iterator;
-public:
- typedef forward_iterator_tag iterator_category;
- typedef typename remove_const<
- typename __pointer_traits::element_type::value_type
- >::type value_type;
- typedef typename __pointer_traits::difference_type difference_type;
- typedef const value_type& reference;
- typedef typename __rebind_pointer<__node_pointer, const value_type>::type
- pointer;
+
+ typedef forward_iterator_tag iterator_category;
+ typedef typename _NodeTypes::__node_value_type value_type;
+ typedef typename _NodeTypes::difference_type difference_type;
+ typedef const value_type& reference;
+ typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
_LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT
@@ -686,10 +855,11 @@ class __hash_node_destructor
{
typedef _Alloc allocator_type;
typedef allocator_traits<allocator_type> __alloc_traits;
- typedef typename __alloc_traits::value_type::value_type value_type;
+
public:
typedef typename __alloc_traits::pointer pointer;
private:
+ typedef __hash_node_types<pointer> _NodeTypes;
allocator_type& __na_;
@@ -709,7 +879,7 @@ public:
void operator()(pointer __p) _NOEXCEPT
{
if (__value_constructed)
- __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_));
+ __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
if (__p)
__alloc_traits::deallocate(__na_, __p, 1);
}
@@ -728,23 +898,47 @@ public:
private:
typedef allocator_traits<allocator_type> __alloc_traits;
+ typedef typename
+ __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
+ _NodeTypes;
public:
+
+ typedef typename _NodeTypes::__node_value_type __node_value_type;
+ typedef typename _NodeTypes::__container_value_type __container_value_type;
+ typedef typename _NodeTypes::key_type key_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef typename __alloc_traits::pointer pointer;
typedef typename __alloc_traits::const_pointer const_pointer;
+#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
typedef typename __alloc_traits::size_type size_type;
- typedef typename __alloc_traits::difference_type difference_type;
+#else
+ typedef typename _NodeTypes::size_type size_type;
+#endif
+ typedef typename _NodeTypes::difference_type difference_type;
public:
// Create __node
- typedef __hash_node<value_type, typename __alloc_traits::void_pointer> __node;
+
+ typedef typename _NodeTypes::__node_type __node;
typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
typedef allocator_traits<__node_allocator> __node_traits;
- typedef typename __node_traits::pointer __node_pointer;
- typedef typename __node_traits::pointer __node_const_pointer;
- typedef __hash_node_base<__node_pointer> __first_node;
- typedef typename __rebind_pointer<__node_pointer, __first_node>::type
- __node_base_pointer;
+ typedef typename _NodeTypes::__void_pointer __void_pointer;
+ typedef typename _NodeTypes::__node_pointer __node_pointer;
+ typedef typename _NodeTypes::__node_pointer __node_const_pointer;
+ typedef typename _NodeTypes::__node_base_type __first_node;
+ typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
+
+private:
+ // check for sane allocator pointer rebinding semantics. Rebinding the
+ // allocator for a new pointer type should be exactly the same as rebinding
+ // the pointer using 'pointer_traits'.
+ static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
+ "Allocator does not rebind pointers in a sane manner.");
+ typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
+ __node_base_allocator;
+ typedef allocator_traits<__node_base_allocator> __node_base_traits;
+ static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
+ "Allocator does not rebind pointers in a sane manner.");
private:
@@ -755,10 +949,10 @@ private:
typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
// --- Member data begin ---
- __bucket_list __bucket_list_;
- __compressed_pair<__first_node, __node_allocator> __p1_;
- __compressed_pair<size_type, hasher> __p2_;
- __compressed_pair<float, key_equal> __p3_;
+ __bucket_list __bucket_list_;
+ __compressed_pair<__first_node, __node_allocator> __p1_;
+ __compressed_pair<size_type, hasher> __p2_;
+ __compressed_pair<float, key_equal> __p3_;
// --- Member data end ---
_LIBCPP_INLINE_VISIBILITY
@@ -809,7 +1003,7 @@ public:
explicit __hash_table(const allocator_type& __a);
__hash_table(const __hash_table& __u);
__hash_table(const __hash_table& __u, const allocator_type& __a);
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
+#ifndef _LIBCPP_CXX03_LANG
__hash_table(__hash_table&& __u)
_NOEXCEPT_(
is_nothrow_move_constructible<__bucket_list>::value &&
@@ -818,11 +1012,11 @@ public:
is_nothrow_move_constructible<hasher>::value &&
is_nothrow_move_constructible<key_equal>::value);
__hash_table(__hash_table&& __u, const allocator_type& __a);
-#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
+#endif // _LIBCPP_CXX03_LANG
~__hash_table();
__hash_table& operator=(const __hash_table& __u);
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
+#ifndef _LIBCPP_CXX03_LANG
_LIBCPP_INLINE_VISIBILITY
__hash_table& operator=(__hash_table&& __u)
_NOEXCEPT_(
@@ -848,41 +1042,103 @@ public:
iterator __node_insert_multi(const_iterator __p,
__node_pointer __nd);
-#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
- template <class... _Args>
- pair<iterator, bool> __emplace_unique(_Args&&... __args);
- template <class... _Args>
- iterator __emplace_multi(_Args&&... __args);
+#ifndef _LIBCPP_CXX03_LANG
+ template <class _Key, class ..._Args>
+ _LIBCPP_INLINE_VISIBILITY
+ pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
+
template <class... _Args>
- iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
-#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
+ _LIBCPP_INLINE_VISIBILITY
+ pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
- template <class _ValueTp>
+ template <class _Pp>
_LIBCPP_INLINE_VISIBILITY
- pair<iterator, bool> __insert_unique_value(_ValueTp&& __x);
-#else
+ pair<iterator, bool> __emplace_unique(_Pp&& __x) {
+ return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
+ __can_extract_key<_Pp, key_type>());
+ }
+
+ template <class _First, class _Second>
_LIBCPP_INLINE_VISIBILITY
- pair<iterator, bool> __insert_unique_value(const value_type& __x);
-#endif
+ typename enable_if<
+ __can_extract_map_key<_First, key_type, __container_value_type>::value,
+ pair<iterator, bool>
+ >::type __emplace_unique(_First&& __f, _Second&& __s) {
+ return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
+ _VSTD::forward<_Second>(__s));
+ }
- pair<iterator, bool> __insert_unique(const value_type& __x);
+ template <class... _Args>
+ _LIBCPP_INLINE_VISIBILITY
+ pair<iterator, bool> __emplace_unique(_Args&&... __args) {
+ return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
+ }
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
- pair<iterator, bool> __insert_unique(value_type&& __x);
template <class _Pp>
- pair<iterator, bool> __insert_unique(_Pp&& __x);
-#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
+ _LIBCPP_INLINE_VISIBILITY
+ pair<iterator, bool>
+ __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
+ return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
+ }
+ template <class _Pp>
+ _LIBCPP_INLINE_VISIBILITY
+ pair<iterator, bool>
+ __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
+ return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
+ }
+ template <class _Pp>
+ _LIBCPP_INLINE_VISIBILITY
+ pair<iterator, bool>
+ __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
+ return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
+ }
+
+ template <class... _Args>
+ _LIBCPP_INLINE_VISIBILITY
+ iterator __emplace_multi(_Args&&... __args);
+ template <class... _Args>
+ _LIBCPP_INLINE_VISIBILITY
+ iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
+
+
+ _LIBCPP_INLINE_VISIBILITY
+ pair<iterator, bool>
+ __insert_unique(__container_value_type&& __x) {
+ return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
+ }
+
+ template <class _Pp, class = typename enable_if<
+ !__is_same_uncvref<_Pp, __container_value_type>::value
+ >::type>
+ _LIBCPP_INLINE_VISIBILITY
+ pair<iterator, bool> __insert_unique(_Pp&& __x) {
+ return __emplace_unique(_VSTD::forward<_Pp>(__x));
+ }
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
template <class _Pp>
- iterator __insert_multi(_Pp&& __x);
+ _LIBCPP_INLINE_VISIBILITY
+ iterator __insert_multi(_Pp&& __x) {
+ return __emplace_multi(_VSTD::forward<_Pp>(__x));
+ }
+
template <class _Pp>
- iterator __insert_multi(const_iterator __p, _Pp&& __x);
-#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
- iterator __insert_multi(const value_type& __x);
- iterator __insert_multi(const_iterator __p, const value_type& __x);
-#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
+ _LIBCPP_INLINE_VISIBILITY
+ iterator __insert_multi(const_iterator __p, _Pp&& __x) {
+ return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
+ }
+
+#else // !defined(_LIBCPP_CXX03_LANG)
+ template <class _Key, class _Args>
+ pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
+
+ iterator __insert_multi(const __container_value_type& __x);
+ iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
+#endif
+
+ _LIBCPP_INLINE_VISIBILITY
+ pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
+ return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
+ }
void clear() _NOEXCEPT;
void rehash(size_type __n);
@@ -1042,16 +1298,17 @@ public:
private:
void __rehash(size_type __n);
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
-#ifndef _LIBCPP_HAS_NO_VARIADICS
+#ifndef _LIBCPP_CXX03_LANG
template <class ..._Args>
- __node_holder __construct_node(_Args&& ...__args);
-#endif // _LIBCPP_HAS_NO_VARIADICS
- __node_holder __construct_node(value_type&& __v, size_t __hash);
-#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
- __node_holder __construct_node(const value_type& __v);
+ __node_holder __construct_node(_Args&& ...__args);
+
+ template <class _First, class ..._Rest>
+ __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
+#else // _LIBCPP_CXX03_LANG
+ __node_holder __construct_node(const __container_value_type& __v);
+ __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
#endif
- __node_holder __construct_node(const value_type& __v, size_t __hash);
+
_LIBCPP_INLINE_VISIBILITY
void __copy_assign_alloc(const __hash_table& __u)
@@ -1061,6 +1318,7 @@ private:
_LIBCPP_INLINE_VISIBILITY
void __copy_assign_alloc(const __hash_table&, false_type) {}
+#ifndef _LIBCPP_CXX03_LANG
void __move_assign(__hash_table& __u, false_type);
void __move_assign(__hash_table& __u, true_type)
_NOEXCEPT_(
@@ -1087,6 +1345,7 @@ private:
}
_LIBCPP_INLINE_VISIBILITY
void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
+#endif // _LIBCPP_CXX03_LANG
void __deallocate(__node_pointer __np) _NOEXCEPT;
__node_pointer __detach() _NOEXCEPT;
@@ -1163,7 +1422,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
{
}
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
+#ifndef _LIBCPP_CXX03_LANG
template <class _Tp, class _Hash, class _Equal, class _Alloc>
__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
@@ -1212,11 +1471,15 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
}
}
-#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
+#endif // _LIBCPP_CXX03_LANG
template <class _Tp, class _Hash, class _Equal, class _Alloc>
__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
{
+ static_assert((is_copy_constructible<key_equal>::value),
+ "Predicate must be copy-constructible.");
+ static_assert((is_copy_constructible<hasher>::value),
+ "Hasher must be copy-constructible.");
__deallocate(__p1_.first().__next_);
#if _LIBCPP_DEBUG_LEVEL >= 2
__get_db()->__erase_c(this);
@@ -1277,7 +1540,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np)
}
__get_db()->unlock();
#endif
- __node_traits::destroy(__na, _VSTD::addressof(__np->__value_));
+ __node_traits::destroy(__na, _NodeTypes::__get_ptr(__np->__value_));
__node_traits::deallocate(__na, __np, 1);
__np = __next;
}
@@ -1296,7 +1559,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
return __cache;
}
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
+#ifndef _LIBCPP_CXX03_LANG
template <class _Tp, class _Hash, class _Equal, class _Alloc>
void
@@ -1369,8 +1632,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
const_iterator __i = __u.begin();
while (__u.size() != 0)
{
- __node_holder __h =
- __construct_node(_VSTD::move(__u.remove(__i++)->__value_));
+ __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
__node_insert_multi(__h.get());
__h.release();
}
@@ -1392,7 +1654,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
return *this;
}
-#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
+#endif // _LIBCPP_CXX03_LANG
template <class _Tp, class _Hash, class _Equal, class _Alloc>
template <class _InputIterator>
@@ -1400,6 +1662,11 @@ void
__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
_InputIterator __last)
{
+ typedef iterator_traits<_InputIterator> _ITraits;
+ typedef typename _ITraits::value_type _ItValueType;
+ static_assert((is_same<_ItValueType, __container_value_type>::value),
+ "__assign_unique may only be called with the containers value type");
+
if (bucket_count() != 0)
{
__node_pointer __cache = __detach();
@@ -1434,6 +1701,12 @@ void
__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
_InputIterator __last)
{
+ typedef iterator_traits<_InputIterator> _ITraits;
+ typedef typename _ITraits::value_type _ItValueType;
+ static_assert((is_same<_ItValueType, __container_value_type>::value ||
+ is_same<_ItValueType, __node_value_type>::value),
+ "__assign_multi may only be called with the containers value type"
+ " or the nodes value type");
if (bucket_count() != 0)
{
__node_pointer __cache = __detach();
@@ -1459,7 +1732,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
__deallocate(__cache);
}
for (; __first != __last; ++__first)
- __insert_multi(*__first);
+ __insert_multi(_NodeTypes::__get_value(*__first));
}
template <class _Tp, class _Hash, class _Equal, class _Alloc>
@@ -1685,31 +1958,24 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
return __node_insert_multi(__cp);
}
-template <class _Tp, class _Hash, class _Equal, class _Alloc>
-pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(const value_type& __x)
-{
- return __insert_unique_value(__x);
-}
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
+#ifndef _LIBCPP_CXX03_LANG
template <class _Tp, class _Hash, class _Equal, class _Alloc>
-template <class _ValueTp>
+template <class _Key, class ..._Args>
_LIBCPP_INLINE_VISIBILITY
pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique_value(_ValueTp&& __x)
+__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
#else
template <class _Tp, class _Hash, class _Equal, class _Alloc>
+template <class _Key, class _Args>
_LIBCPP_INLINE_VISIBILITY
pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique_value(const value_type& __x)
+__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
#endif
{
-#if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
- typedef const value_type& _ValueTp;
-#endif
- size_t __hash = hash_function()(__x);
+
+ size_t __hash = hash_function()(__k);
size_type __bc = bucket_count();
bool __inserted = false;
__node_pointer __nd;
@@ -1724,13 +1990,17 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique_value(const value_type
__constrain_hash(__nd->__hash_, __bc) == __chash;
__nd = __nd->__next_)
{
- if (key_eq()(__nd->__value_, __x))
+ if (key_eq()(__nd->__value_, __k))
goto __done;
}
}
}
{
- __node_holder __h = __construct_node(_VSTD::forward<_ValueTp>(__x), __hash);
+#ifndef _LIBCPP_CXX03_LANG
+ __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
+#else
+ __node_holder __h = __construct_node_hash(__hash, __args);
+#endif
if (size()+1 > __bc * max_load_factor() || __bc == 0)
{
rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
@@ -1742,7 +2012,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique_value(const value_type
__node_pointer __pn = __bucket_list_[__chash];
if (__pn == nullptr)
{
- __pn = static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first()));
+ __pn = static_cast<__node_pointer>(static_cast<__void_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())));
__h->__next_ = __pn->__next_;
__pn->__next_ = __h.get();
// fix up __bucket_list_
@@ -1768,13 +2038,12 @@ __done:
#endif
}
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
-#ifndef _LIBCPP_HAS_NO_VARIADICS
+#ifndef _LIBCPP_CXX03_LANG
template <class _Tp, class _Hash, class _Equal, class _Alloc>
template <class... _Args>
pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique(_Args&&... __args)
+__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
{
__node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
pair<iterator, bool> __r = __node_insert_unique(__h.get());
@@ -1811,64 +2080,11 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
return __r;
}
-#endif // _LIBCPP_HAS_NO_VARIADICS
+#else // _LIBCPP_CXX03_LANG
template <class _Tp, class _Hash, class _Equal, class _Alloc>
-pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(value_type&& __x)
-{
- return __insert_unique_value(_VSTD::move(__x));
-}
-
-template <class _Tp, class _Hash, class _Equal, class _Alloc>
-template <class _Pp>
-pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(_Pp&& __x)
-{
- __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
- pair<iterator, bool> __r = __node_insert_unique(__h.get());
- if (__r.second)
- __h.release();
- return __r;
-}
-
-#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
-
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
-
-template <class _Tp, class _Hash, class _Equal, class _Alloc>
-template <class _Pp>
typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(_Pp&& __x)
-{
- __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x));
- iterator __r = __node_insert_multi(__h.get());
- __h.release();
- return __r;
-}
-
-template <class _Tp, class _Hash, class _Equal, class _Alloc>
-template <class _Pp>
-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();
- return __r;
-}
-
-#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
-
-template <class _Tp, class _Hash, class _Equal, class _Alloc>
-typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const value_type& __x)
+__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
{
__node_holder __h = __construct_node(__x);
iterator __r = __node_insert_multi(__h.get());
@@ -1879,7 +2095,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const value_type& __x)
template <class _Tp, class _Hash, class _Equal, class _Alloc>
typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
- const value_type& __x)
+ const __container_value_type& __x)
{
#if _LIBCPP_DEBUG_LEVEL >= 2
_LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
@@ -1892,7 +2108,7 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
return __r;
}
-#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
+#endif // _LIBCPP_CXX03_LANG
template <class _Tp, class _Hash, class _Equal, class _Alloc>
void
@@ -1986,10 +2202,11 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
if (__nd != nullptr)
{
for (__nd = __nd->__next_; __nd != nullptr &&
- __constrain_hash(__nd->__hash_, __bc) == __chash;
+ (__nd->__hash_ == __hash
+ || __constrain_hash(__nd->__hash_, __bc) == __chash);
__nd = __nd->__next_)
{
- if (key_eq()(__nd->__value_, __k))
+ if ((__nd->__hash_ == __hash) && key_eq()(__nd->__value_, __k))
#if _LIBCPP_DEBUG_LEVEL >= 2
return iterator(__nd, this);
#else
@@ -2015,10 +2232,10 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
if (__nd != nullptr)
{
for (__nd = __nd->__next_; __nd != nullptr &&
- __constrain_hash(__nd->__hash_, __bc) == __chash;
+ (__hash == __nd->__hash_ || __constrain_hash(__nd->__hash_, __bc) == __chash);
__nd = __nd->__next_)
{
- if (key_eq()(__nd->__value_, __k))
+ if ((__nd->__hash_ == __hash) && key_eq()(__nd->__value_, __k))
#if _LIBCPP_DEBUG_LEVEL >= 2
return const_iterator(__nd, this);
#else
@@ -2031,70 +2248,74 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
return end();
}
-#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
-#ifndef _LIBCPP_HAS_NO_VARIADICS
+#ifndef _LIBCPP_CXX03_LANG
template <class _Tp, class _Hash, class _Equal, class _Alloc>
template <class ..._Args>
typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
{
+ static_assert(!__is_hash_value_type<_Args...>::value,
+ "Construct cannot be called with a hash value type");
__node_allocator& __na = __node_alloc();
__node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
- __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...);
+ __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
__h.get_deleter().__value_constructed = true;
__h->__hash_ = hash_function()(__h->__value_);
__h->__next_ = nullptr;
return __h;
}
-#endif // _LIBCPP_HAS_NO_VARIADICS
-
template <class _Tp, class _Hash, class _Equal, class _Alloc>
+template <class _First, class ..._Rest>
typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(value_type&& __v,
- size_t __hash)
+__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
+ size_t __hash, _First&& __f, _Rest&& ...__rest)
{
+ static_assert(!__is_hash_value_type<_First, _Rest...>::value,
+ "Construct cannot be called with a hash value type");
__node_allocator& __na = __node_alloc();
__node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
- __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::move(__v));
+ __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
+ _VSTD::forward<_First>(__f),
+ _VSTD::forward<_Rest>(__rest)...);
__h.get_deleter().__value_constructed = true;
__h->__hash_ = __hash;
__h->__next_ = nullptr;
return __h;
}
-#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
+#else // _LIBCPP_CXX03_LANG
template <class _Tp, class _Hash, class _Equal, class _Alloc>
typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v)
+__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
{
__node_allocator& __na = __node_alloc();
__node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
- __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
+ __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
__h.get_deleter().__value_constructed = true;
__h->__hash_ = hash_function()(__h->__value_);
__h->__next_ = nullptr;
return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
}
-#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
-
template <class _Tp, class _Hash, class _Equal, class _Alloc>
typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
-__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v,
- size_t __hash)
+__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
+ const __container_value_type& __v)
{
__node_allocator& __na = __node_alloc();
__node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
- __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
+ __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
__h.get_deleter().__value_constructed = true;
__h->__hash_ = __hash;
__h->__next_ = nullptr;
return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
}
+#endif // _LIBCPP_CXX03_LANG
+
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)