Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP__HASH_TABLE |
| 12 | #define _LIBCPP__HASH_TABLE |
| 13 | |
| 14 | #include <__config> |
| 15 | #include <initializer_list> |
| 16 | #include <memory> |
| 17 | #include <iterator> |
| 18 | #include <algorithm> |
| 19 | #include <cmath> |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 20 | #include <utility> |
Eric Fiselier | 04333f9 | 2017-01-13 22:42:53 +0000 | [diff] [blame] | 21 | #include <type_traits> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | |
Eric Fiselier | c1bd919 | 2014-08-10 23:53:08 +0000 | [diff] [blame] | 23 | #include <__debug> |
Howard Hinnant | 42a3046 | 2013-08-02 00:26:35 +0000 | [diff] [blame] | 24 | |
Howard Hinnant | 073458b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 25 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | #pragma GCC system_header |
Howard Hinnant | 073458b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 27 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | |
Eric Fiselier | a016efb | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 29 | _LIBCPP_PUSH_MACROS |
| 30 | #include <__undef_macros> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 32 | |
Eric Fiselier | a016efb | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 33 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 34 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 35 | template <class _Key, class _Tp> |
| 36 | struct __hash_value_type; |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 37 | |
Eric Fiselier | 04333f9 | 2017-01-13 22:42:53 +0000 | [diff] [blame] | 38 | template <class _Key, class _Cp, class _Hash, |
| 39 | bool = is_empty<_Hash>::value && !__libcpp_is_final<_Hash>::value> |
| 40 | class __unordered_map_hasher; |
| 41 | |
| 42 | template <class _Key, class _Cp, class _Pred, |
| 43 | bool = is_empty<_Pred>::value && !__libcpp_is_final<_Pred>::value |
| 44 | > |
| 45 | class __unordered_map_equal; |
| 46 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 47 | #ifndef _LIBCPP_CXX03_LANG |
| 48 | template <class _Tp> |
| 49 | struct __is_hash_value_type_imp : false_type {}; |
| 50 | |
| 51 | template <class _Key, class _Value> |
| 52 | struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {}; |
| 53 | |
| 54 | template <class ..._Args> |
| 55 | struct __is_hash_value_type : false_type {}; |
| 56 | |
| 57 | template <class _One> |
| 58 | struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {}; |
| 59 | #endif |
| 60 | |
Howard Hinnant | 6e41256 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 61 | _LIBCPP_FUNC_VIS |
Howard Hinnant | ce53420 | 2011-06-14 19:58:17 +0000 | [diff] [blame] | 62 | size_t __next_prime(size_t __n); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 63 | |
| 64 | template <class _NodePtr> |
| 65 | struct __hash_node_base |
| 66 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 67 | typedef typename pointer_traits<_NodePtr>::element_type __node_type; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 68 | typedef __hash_node_base __first_node; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 69 | typedef typename __rebind_pointer<_NodePtr, __first_node>::type __node_base_pointer; |
| 70 | typedef _NodePtr __node_pointer; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 71 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 72 | #if defined(_LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB) |
| 73 | typedef __node_base_pointer __next_pointer; |
| 74 | #else |
| 75 | typedef typename conditional< |
| 76 | is_pointer<__node_pointer>::value, |
| 77 | __node_base_pointer, |
| 78 | __node_pointer>::type __next_pointer; |
| 79 | #endif |
| 80 | |
| 81 | __next_pointer __next_; |
| 82 | |
| 83 | _LIBCPP_INLINE_VISIBILITY |
| 84 | __next_pointer __ptr() _NOEXCEPT { |
| 85 | return static_cast<__next_pointer>( |
| 86 | pointer_traits<__node_base_pointer>::pointer_to(*this)); |
| 87 | } |
| 88 | |
| 89 | _LIBCPP_INLINE_VISIBILITY |
| 90 | __node_pointer __upcast() _NOEXCEPT { |
| 91 | return static_cast<__node_pointer>( |
| 92 | pointer_traits<__node_base_pointer>::pointer_to(*this)); |
| 93 | } |
| 94 | |
| 95 | _LIBCPP_INLINE_VISIBILITY |
| 96 | size_t __hash() const _NOEXCEPT { |
| 97 | return static_cast<__node_type const&>(*this).__hash_; |
| 98 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 99 | |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 100 | _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | template <class _Tp, class _VoidPtr> |
| 104 | struct __hash_node |
| 105 | : public __hash_node_base |
| 106 | < |
Eric Fiselier | 934b092 | 2015-12-30 21:52:00 +0000 | [diff] [blame] | 107 | typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 108 | > |
| 109 | { |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 110 | typedef _Tp __node_value_type; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 111 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 112 | size_t __hash_; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 113 | __node_value_type __value_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 116 | inline _LIBCPP_INLINE_VISIBILITY |
| 117 | bool |
Eric Fiselier | 9ba5c11 | 2015-02-02 21:31:48 +0000 | [diff] [blame] | 118 | __is_hash_power2(size_t __bc) |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 119 | { |
| 120 | return __bc > 2 && !(__bc & (__bc - 1)); |
| 121 | } |
| 122 | |
| 123 | inline _LIBCPP_INLINE_VISIBILITY |
| 124 | size_t |
| 125 | __constrain_hash(size_t __h, size_t __bc) |
| 126 | { |
Eric Fiselier | 118cb41 | 2016-07-11 22:02:02 +0000 | [diff] [blame] | 127 | return !(__bc & (__bc - 1)) ? __h & (__bc - 1) : |
| 128 | (__h < __bc ? __h : __h % __bc); |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | inline _LIBCPP_INLINE_VISIBILITY |
| 132 | size_t |
Eric Fiselier | 9ba5c11 | 2015-02-02 21:31:48 +0000 | [diff] [blame] | 133 | __next_hash_pow2(size_t __n) |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 134 | { |
Marshall Clow | 87af646 | 2017-06-03 00:08:32 +0000 | [diff] [blame] | 135 | return __n < 2 ? __n : (size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1))); |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Duncan P. N. Exon Smith | fde79b4 | 2016-03-17 20:45:20 +0000 | [diff] [blame] | 138 | |
Howard Hinnant | ce53420 | 2011-06-14 19:58:17 +0000 | [diff] [blame] | 139 | template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 140 | |
Eric Fiselier | e2f2d1e | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 141 | template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_iterator; |
| 142 | template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_iterator; |
| 143 | template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_local_iterator; |
| 144 | template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator; |
| 145 | template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_iterator; |
| 146 | template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 147 | |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 148 | template <class _Tp> |
Eric Fiselier | 43b121d | 2016-02-20 07:59:16 +0000 | [diff] [blame] | 149 | struct __hash_key_value_types { |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 150 | static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, ""); |
| 151 | typedef _Tp key_type; |
| 152 | typedef _Tp __node_value_type; |
| 153 | typedef _Tp __container_value_type; |
| 154 | static const bool __is_map = false; |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 155 | |
| 156 | _LIBCPP_INLINE_VISIBILITY |
| 157 | static key_type const& __get_key(_Tp const& __v) { |
| 158 | return __v; |
| 159 | } |
| 160 | _LIBCPP_INLINE_VISIBILITY |
| 161 | static __container_value_type const& __get_value(__node_value_type const& __v) { |
| 162 | return __v; |
| 163 | } |
| 164 | _LIBCPP_INLINE_VISIBILITY |
| 165 | static __container_value_type* __get_ptr(__node_value_type& __n) { |
| 166 | return _VSTD::addressof(__n); |
| 167 | } |
| 168 | #ifndef _LIBCPP_CXX03_LANG |
| 169 | _LIBCPP_INLINE_VISIBILITY |
Erik Pilkington | f52318b | 2018-06-04 20:38:23 +0000 | [diff] [blame] | 170 | static __container_value_type&& __move(__node_value_type& __v) { |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 171 | return _VSTD::move(__v); |
| 172 | } |
| 173 | #endif |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | template <class _Key, class _Tp> |
Eric Fiselier | 43b121d | 2016-02-20 07:59:16 +0000 | [diff] [blame] | 177 | struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > { |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 178 | typedef _Key key_type; |
| 179 | typedef _Tp mapped_type; |
| 180 | typedef __hash_value_type<_Key, _Tp> __node_value_type; |
| 181 | typedef pair<const _Key, _Tp> __container_value_type; |
| 182 | typedef __container_value_type __map_value_type; |
| 183 | static const bool __is_map = true; |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 184 | |
| 185 | _LIBCPP_INLINE_VISIBILITY |
| 186 | static key_type const& __get_key(__container_value_type const& __v) { |
| 187 | return __v.first; |
| 188 | } |
| 189 | |
| 190 | template <class _Up> |
| 191 | _LIBCPP_INLINE_VISIBILITY |
| 192 | static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value, |
| 193 | __container_value_type const&>::type |
| 194 | __get_value(_Up& __t) { |
Erik Pilkington | f52318b | 2018-06-04 20:38:23 +0000 | [diff] [blame] | 195 | return __t.__get_value(); |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | template <class _Up> |
| 199 | _LIBCPP_INLINE_VISIBILITY |
| 200 | static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value, |
| 201 | __container_value_type const&>::type |
| 202 | __get_value(_Up& __t) { |
| 203 | return __t; |
| 204 | } |
| 205 | |
| 206 | _LIBCPP_INLINE_VISIBILITY |
| 207 | static __container_value_type* __get_ptr(__node_value_type& __n) { |
Erik Pilkington | f52318b | 2018-06-04 20:38:23 +0000 | [diff] [blame] | 208 | return _VSTD::addressof(__n.__get_value()); |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 209 | } |
| 210 | #ifndef _LIBCPP_CXX03_LANG |
| 211 | _LIBCPP_INLINE_VISIBILITY |
Erik Pilkington | f52318b | 2018-06-04 20:38:23 +0000 | [diff] [blame] | 212 | static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) { |
| 213 | return __v.__move(); |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 214 | } |
| 215 | #endif |
| 216 | |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Eric Fiselier | 43b121d | 2016-02-20 07:59:16 +0000 | [diff] [blame] | 219 | template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>, |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 220 | bool = _KVTypes::__is_map> |
Eric Fiselier | 43b121d | 2016-02-20 07:59:16 +0000 | [diff] [blame] | 221 | struct __hash_map_pointer_types {}; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 222 | |
| 223 | template <class _Tp, class _AllocPtr, class _KVTypes> |
Eric Fiselier | 43b121d | 2016-02-20 07:59:16 +0000 | [diff] [blame] | 224 | struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> { |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 225 | typedef typename _KVTypes::__map_value_type _Mv; |
| 226 | typedef typename __rebind_pointer<_AllocPtr, _Mv>::type |
| 227 | __map_value_type_pointer; |
| 228 | typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type |
| 229 | __const_map_value_type_pointer; |
| 230 | }; |
| 231 | |
| 232 | template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type> |
| 233 | struct __hash_node_types; |
| 234 | |
| 235 | template <class _NodePtr, class _Tp, class _VoidPtr> |
| 236 | struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> > |
Eric Fiselier | 43b121d | 2016-02-20 07:59:16 +0000 | [diff] [blame] | 237 | : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr> |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 238 | |
| 239 | { |
Eric Fiselier | 43b121d | 2016-02-20 07:59:16 +0000 | [diff] [blame] | 240 | typedef __hash_key_value_types<_Tp> __base; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 241 | |
| 242 | public: |
| 243 | typedef ptrdiff_t difference_type; |
| 244 | typedef size_t size_type; |
| 245 | |
| 246 | typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer; |
| 247 | |
| 248 | typedef typename pointer_traits<_NodePtr>::element_type __node_type; |
| 249 | typedef _NodePtr __node_pointer; |
| 250 | |
| 251 | typedef __hash_node_base<__node_pointer> __node_base_type; |
| 252 | typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type |
| 253 | __node_base_pointer; |
| 254 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 255 | typedef typename __node_base_type::__next_pointer __next_pointer; |
| 256 | |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 257 | typedef _Tp __node_value_type; |
| 258 | typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type |
| 259 | __node_value_type_pointer; |
| 260 | typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type |
| 261 | __const_node_value_type_pointer; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 262 | |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 263 | private: |
| 264 | static_assert(!is_const<__node_type>::value, |
| 265 | "_NodePtr should never be a pointer to const"); |
| 266 | static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value), |
| 267 | "_VoidPtr does not point to unqualified void type"); |
| 268 | static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type, |
| 269 | _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr."); |
| 270 | }; |
| 271 | |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 272 | template <class _HashIterator> |
| 273 | struct __hash_node_types_from_iterator; |
| 274 | template <class _NodePtr> |
| 275 | struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {}; |
| 276 | template <class _NodePtr> |
| 277 | struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {}; |
| 278 | template <class _NodePtr> |
| 279 | struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {}; |
| 280 | template <class _NodePtr> |
| 281 | struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {}; |
| 282 | |
| 283 | |
| 284 | template <class _NodeValueTp, class _VoidPtr> |
| 285 | struct __make_hash_node_types { |
| 286 | typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp; |
| 287 | typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr; |
| 288 | typedef __hash_node_types<_NodePtr> type; |
| 289 | }; |
| 290 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 291 | template <class _NodePtr> |
Eric Fiselier | e2f2d1e | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 292 | class _LIBCPP_TEMPLATE_VIS __hash_iterator |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 293 | { |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 294 | typedef __hash_node_types<_NodePtr> _NodeTypes; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 295 | typedef _NodePtr __node_pointer; |
| 296 | typedef typename _NodeTypes::__next_pointer __next_pointer; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 297 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 298 | __next_pointer __node_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 299 | |
| 300 | public: |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 301 | typedef forward_iterator_tag iterator_category; |
| 302 | typedef typename _NodeTypes::__node_value_type value_type; |
| 303 | typedef typename _NodeTypes::difference_type difference_type; |
| 304 | typedef value_type& reference; |
| 305 | typedef typename _NodeTypes::__node_value_type_pointer pointer; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 306 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 307 | _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT : __node_(nullptr) { |
| 308 | _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this)); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 312 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 313 | __hash_iterator(const __hash_iterator& __i) |
| 314 | : __node_(__i.__node_) |
| 315 | { |
| 316 | __get_db()->__iterator_copy(this, &__i); |
| 317 | } |
| 318 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 319 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 320 | ~__hash_iterator() |
| 321 | { |
| 322 | __get_db()->__erase_i(this); |
| 323 | } |
| 324 | |
| 325 | _LIBCPP_INLINE_VISIBILITY |
| 326 | __hash_iterator& operator=(const __hash_iterator& __i) |
| 327 | { |
| 328 | if (this != &__i) |
| 329 | { |
| 330 | __get_db()->__iterator_copy(this, &__i); |
| 331 | __node_ = __i.__node_; |
| 332 | } |
| 333 | return *this; |
| 334 | } |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 335 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 |
| 336 | |
| 337 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 338 | reference operator*() const { |
| 339 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
| 340 | "Attempted to dereference a non-dereferenceable unordered container iterator"); |
| 341 | return __node_->__upcast()->__value_; |
| 342 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 343 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 344 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 345 | pointer operator->() const { |
| 346 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
| 347 | "Attempted to dereference a non-dereferenceable unordered container iterator"); |
| 348 | return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_); |
| 349 | } |
| 350 | |
| 351 | _LIBCPP_INLINE_VISIBILITY |
| 352 | __hash_iterator& operator++() { |
| 353 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 354 | "Attempted to increment non-incrementable unordered container iterator"); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 355 | __node_ = __node_->__next_; |
| 356 | return *this; |
| 357 | } |
| 358 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 359 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 360 | __hash_iterator operator++(int) |
| 361 | { |
| 362 | __hash_iterator __t(*this); |
| 363 | ++(*this); |
| 364 | return __t; |
| 365 | } |
| 366 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 367 | friend _LIBCPP_INLINE_VISIBILITY |
| 368 | bool operator==(const __hash_iterator& __x, const __hash_iterator& __y) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 369 | { |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 370 | return __x.__node_ == __y.__node_; |
| 371 | } |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 372 | friend _LIBCPP_INLINE_VISIBILITY |
| 373 | bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 374 | {return !(__x == __y);} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 375 | |
| 376 | private: |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 377 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 378 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 379 | __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 380 | : __node_(__node) |
| 381 | { |
| 382 | __get_db()->__insert_ic(this, __c); |
| 383 | } |
| 384 | #else |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 385 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 386 | __hash_iterator(__next_pointer __node) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 387 | : __node_(__node) |
| 388 | {} |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 389 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 390 | template <class, class, class, class> friend class __hash_table; |
Eric Fiselier | e2f2d1e | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 391 | template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator; |
| 392 | template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator; |
| 393 | template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map; |
| 394 | template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 397 | template <class _NodePtr> |
Eric Fiselier | e2f2d1e | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 398 | class _LIBCPP_TEMPLATE_VIS __hash_const_iterator |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 399 | { |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 400 | static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, ""); |
| 401 | typedef __hash_node_types<_NodePtr> _NodeTypes; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 402 | typedef _NodePtr __node_pointer; |
| 403 | typedef typename _NodeTypes::__next_pointer __next_pointer; |
Eric Fiselier | 757373e | 2016-02-18 00:20:34 +0000 | [diff] [blame] | 404 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 405 | __next_pointer __node_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 406 | |
| 407 | public: |
Eric Fiselier | 757373e | 2016-02-18 00:20:34 +0000 | [diff] [blame] | 408 | typedef __hash_iterator<_NodePtr> __non_const_iterator; |
| 409 | |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 410 | typedef forward_iterator_tag iterator_category; |
| 411 | typedef typename _NodeTypes::__node_value_type value_type; |
| 412 | typedef typename _NodeTypes::difference_type difference_type; |
| 413 | typedef const value_type& reference; |
| 414 | typedef typename _NodeTypes::__const_node_value_type_pointer pointer; |
| 415 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 416 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 417 | _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT : __node_(nullptr) { |
| 418 | _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this)); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 419 | } |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 420 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 421 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 422 | __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 423 | : __node_(__x.__node_) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 424 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 425 | _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x)); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 429 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 430 | __hash_const_iterator(const __hash_const_iterator& __i) |
| 431 | : __node_(__i.__node_) |
| 432 | { |
| 433 | __get_db()->__iterator_copy(this, &__i); |
| 434 | } |
| 435 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 436 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 437 | ~__hash_const_iterator() |
| 438 | { |
| 439 | __get_db()->__erase_i(this); |
| 440 | } |
| 441 | |
| 442 | _LIBCPP_INLINE_VISIBILITY |
| 443 | __hash_const_iterator& operator=(const __hash_const_iterator& __i) |
| 444 | { |
| 445 | if (this != &__i) |
| 446 | { |
| 447 | __get_db()->__iterator_copy(this, &__i); |
| 448 | __node_ = __i.__node_; |
| 449 | } |
| 450 | return *this; |
| 451 | } |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 452 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 |
| 453 | |
| 454 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 455 | reference operator*() const { |
| 456 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 457 | "Attempted to dereference a non-dereferenceable unordered container const_iterator"); |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 458 | return __node_->__upcast()->__value_; |
| 459 | } |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 460 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 461 | pointer operator->() const { |
| 462 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 463 | "Attempted to dereference a non-dereferenceable unordered container const_iterator"); |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 464 | return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_); |
| 465 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 466 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 467 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 468 | __hash_const_iterator& operator++() { |
| 469 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
| 470 | "Attempted to increment non-incrementable unordered container const_iterator"); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 471 | __node_ = __node_->__next_; |
| 472 | return *this; |
| 473 | } |
| 474 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 475 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 476 | __hash_const_iterator operator++(int) |
| 477 | { |
| 478 | __hash_const_iterator __t(*this); |
| 479 | ++(*this); |
| 480 | return __t; |
| 481 | } |
| 482 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 483 | friend _LIBCPP_INLINE_VISIBILITY |
| 484 | bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 485 | { |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 486 | return __x.__node_ == __y.__node_; |
| 487 | } |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 488 | friend _LIBCPP_INLINE_VISIBILITY |
| 489 | bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 490 | {return !(__x == __y);} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 491 | |
| 492 | private: |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 493 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 494 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 495 | __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 496 | : __node_(__node) |
| 497 | { |
| 498 | __get_db()->__insert_ic(this, __c); |
| 499 | } |
| 500 | #else |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 501 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 502 | __hash_const_iterator(__next_pointer __node) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 503 | : __node_(__node) |
| 504 | {} |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 505 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 506 | template <class, class, class, class> friend class __hash_table; |
Eric Fiselier | e2f2d1e | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 507 | template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator; |
| 508 | template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map; |
| 509 | template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 510 | }; |
| 511 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 512 | template <class _NodePtr> |
Eric Fiselier | e2f2d1e | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 513 | class _LIBCPP_TEMPLATE_VIS __hash_local_iterator |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 514 | { |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 515 | typedef __hash_node_types<_NodePtr> _NodeTypes; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 516 | typedef _NodePtr __node_pointer; |
| 517 | typedef typename _NodeTypes::__next_pointer __next_pointer; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 518 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 519 | __next_pointer __node_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 520 | size_t __bucket_; |
| 521 | size_t __bucket_count_; |
| 522 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 523 | public: |
| 524 | typedef forward_iterator_tag iterator_category; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 525 | typedef typename _NodeTypes::__node_value_type value_type; |
| 526 | typedef typename _NodeTypes::difference_type difference_type; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 527 | typedef value_type& reference; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 528 | typedef typename _NodeTypes::__node_value_type_pointer pointer; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 529 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 530 | _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT : __node_(nullptr) { |
| 531 | _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this)); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 535 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 536 | __hash_local_iterator(const __hash_local_iterator& __i) |
| 537 | : __node_(__i.__node_), |
| 538 | __bucket_(__i.__bucket_), |
| 539 | __bucket_count_(__i.__bucket_count_) |
| 540 | { |
| 541 | __get_db()->__iterator_copy(this, &__i); |
| 542 | } |
| 543 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 544 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 545 | ~__hash_local_iterator() |
| 546 | { |
| 547 | __get_db()->__erase_i(this); |
| 548 | } |
| 549 | |
| 550 | _LIBCPP_INLINE_VISIBILITY |
| 551 | __hash_local_iterator& operator=(const __hash_local_iterator& __i) |
| 552 | { |
| 553 | if (this != &__i) |
| 554 | { |
| 555 | __get_db()->__iterator_copy(this, &__i); |
| 556 | __node_ = __i.__node_; |
| 557 | __bucket_ = __i.__bucket_; |
| 558 | __bucket_count_ = __i.__bucket_count_; |
| 559 | } |
| 560 | return *this; |
| 561 | } |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 562 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 |
| 563 | |
| 564 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 565 | reference operator*() const { |
| 566 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 567 | "Attempted to dereference a non-dereferenceable unordered container local_iterator"); |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 568 | return __node_->__upcast()->__value_; |
| 569 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 570 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 571 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 572 | pointer operator->() const { |
| 573 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
| 574 | "Attempted to dereference a non-dereferenceable unordered container local_iterator"); |
| 575 | return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_); |
| 576 | } |
| 577 | |
| 578 | _LIBCPP_INLINE_VISIBILITY |
| 579 | __hash_local_iterator& operator++() { |
| 580 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 581 | "Attempted to increment non-incrementable unordered container local_iterator"); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 582 | __node_ = __node_->__next_; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 583 | if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 584 | __node_ = nullptr; |
| 585 | return *this; |
| 586 | } |
| 587 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 588 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 589 | __hash_local_iterator operator++(int) |
| 590 | { |
| 591 | __hash_local_iterator __t(*this); |
| 592 | ++(*this); |
| 593 | return __t; |
| 594 | } |
| 595 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 596 | friend _LIBCPP_INLINE_VISIBILITY |
| 597 | bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 598 | { |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 599 | return __x.__node_ == __y.__node_; |
| 600 | } |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 601 | friend _LIBCPP_INLINE_VISIBILITY |
| 602 | bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 603 | {return !(__x == __y);} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 604 | |
| 605 | private: |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 606 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 607 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 608 | __hash_local_iterator(__next_pointer __node, size_t __bucket, |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 609 | size_t __bucket_count, const void* __c) _NOEXCEPT |
| 610 | : __node_(__node), |
| 611 | __bucket_(__bucket), |
| 612 | __bucket_count_(__bucket_count) |
| 613 | { |
| 614 | __get_db()->__insert_ic(this, __c); |
| 615 | if (__node_ != nullptr) |
| 616 | __node_ = __node_->__next_; |
| 617 | } |
| 618 | #else |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 619 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 620 | __hash_local_iterator(__next_pointer __node, size_t __bucket, |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 621 | size_t __bucket_count) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 622 | : __node_(__node), |
| 623 | __bucket_(__bucket), |
| 624 | __bucket_count_(__bucket_count) |
| 625 | { |
| 626 | if (__node_ != nullptr) |
| 627 | __node_ = __node_->__next_; |
| 628 | } |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 629 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 630 | template <class, class, class, class> friend class __hash_table; |
Eric Fiselier | e2f2d1e | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 631 | template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator; |
| 632 | template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 633 | }; |
| 634 | |
| 635 | template <class _ConstNodePtr> |
Eric Fiselier | e2f2d1e | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 636 | class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 637 | { |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 638 | typedef __hash_node_types<_ConstNodePtr> _NodeTypes; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 639 | typedef _ConstNodePtr __node_pointer; |
| 640 | typedef typename _NodeTypes::__next_pointer __next_pointer; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 641 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 642 | __next_pointer __node_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 643 | size_t __bucket_; |
| 644 | size_t __bucket_count_; |
| 645 | |
| 646 | typedef pointer_traits<__node_pointer> __pointer_traits; |
| 647 | typedef typename __pointer_traits::element_type __node; |
| 648 | typedef typename remove_const<__node>::type __non_const_node; |
Eric Fiselier | 934b092 | 2015-12-30 21:52:00 +0000 | [diff] [blame] | 649 | typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type |
| 650 | __non_const_node_pointer; |
Eric Fiselier | 757373e | 2016-02-18 00:20:34 +0000 | [diff] [blame] | 651 | public: |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 652 | typedef __hash_local_iterator<__non_const_node_pointer> |
| 653 | __non_const_iterator; |
Eric Fiselier | 757373e | 2016-02-18 00:20:34 +0000 | [diff] [blame] | 654 | |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 655 | typedef forward_iterator_tag iterator_category; |
| 656 | typedef typename _NodeTypes::__node_value_type value_type; |
| 657 | typedef typename _NodeTypes::difference_type difference_type; |
| 658 | typedef const value_type& reference; |
| 659 | typedef typename _NodeTypes::__const_node_value_type_pointer pointer; |
Eric Fiselier | 934b092 | 2015-12-30 21:52:00 +0000 | [diff] [blame] | 660 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 661 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 662 | _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT : __node_(nullptr) { |
| 663 | _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this)); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 666 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 667 | __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 668 | : __node_(__x.__node_), |
| 669 | __bucket_(__x.__bucket_), |
| 670 | __bucket_count_(__x.__bucket_count_) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 671 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 672 | _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x)); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 676 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 677 | __hash_const_local_iterator(const __hash_const_local_iterator& __i) |
| 678 | : __node_(__i.__node_), |
| 679 | __bucket_(__i.__bucket_), |
| 680 | __bucket_count_(__i.__bucket_count_) |
| 681 | { |
| 682 | __get_db()->__iterator_copy(this, &__i); |
| 683 | } |
| 684 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 685 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 686 | ~__hash_const_local_iterator() |
| 687 | { |
| 688 | __get_db()->__erase_i(this); |
| 689 | } |
| 690 | |
| 691 | _LIBCPP_INLINE_VISIBILITY |
| 692 | __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i) |
| 693 | { |
| 694 | if (this != &__i) |
| 695 | { |
| 696 | __get_db()->__iterator_copy(this, &__i); |
| 697 | __node_ = __i.__node_; |
| 698 | __bucket_ = __i.__bucket_; |
| 699 | __bucket_count_ = __i.__bucket_count_; |
| 700 | } |
| 701 | return *this; |
| 702 | } |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 703 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 |
| 704 | |
| 705 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 706 | reference operator*() const { |
| 707 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 708 | "Attempted to dereference a non-dereferenceable unordered container const_local_iterator"); |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 709 | return __node_->__upcast()->__value_; |
| 710 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 711 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 712 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 713 | pointer operator->() const { |
| 714 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
| 715 | "Attempted to dereference a non-dereferenceable unordered container const_local_iterator"); |
| 716 | return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_); |
| 717 | } |
| 718 | |
| 719 | _LIBCPP_INLINE_VISIBILITY |
| 720 | __hash_const_local_iterator& operator++() { |
| 721 | _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 722 | "Attempted to increment non-incrementable unordered container const_local_iterator"); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 723 | __node_ = __node_->__next_; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 724 | if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 725 | __node_ = nullptr; |
| 726 | return *this; |
| 727 | } |
| 728 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 729 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 730 | __hash_const_local_iterator operator++(int) |
| 731 | { |
| 732 | __hash_const_local_iterator __t(*this); |
| 733 | ++(*this); |
| 734 | return __t; |
| 735 | } |
| 736 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 737 | friend _LIBCPP_INLINE_VISIBILITY |
| 738 | bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 739 | { |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 740 | return __x.__node_ == __y.__node_; |
| 741 | } |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 742 | friend _LIBCPP_INLINE_VISIBILITY |
| 743 | bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 744 | {return !(__x == __y);} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 745 | |
| 746 | private: |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 747 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 748 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 749 | __hash_const_local_iterator(__next_pointer __node, size_t __bucket, |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 750 | size_t __bucket_count, const void* __c) _NOEXCEPT |
| 751 | : __node_(__node), |
| 752 | __bucket_(__bucket), |
| 753 | __bucket_count_(__bucket_count) |
| 754 | { |
| 755 | __get_db()->__insert_ic(this, __c); |
| 756 | if (__node_ != nullptr) |
| 757 | __node_ = __node_->__next_; |
| 758 | } |
| 759 | #else |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 760 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 761 | __hash_const_local_iterator(__next_pointer __node, size_t __bucket, |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 762 | size_t __bucket_count) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 763 | : __node_(__node), |
| 764 | __bucket_(__bucket), |
| 765 | __bucket_count_(__bucket_count) |
| 766 | { |
| 767 | if (__node_ != nullptr) |
| 768 | __node_ = __node_->__next_; |
| 769 | } |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 770 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 771 | template <class, class, class, class> friend class __hash_table; |
Eric Fiselier | e2f2d1e | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 772 | template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 773 | }; |
| 774 | |
| 775 | template <class _Alloc> |
| 776 | class __bucket_list_deallocator |
| 777 | { |
| 778 | typedef _Alloc allocator_type; |
| 779 | typedef allocator_traits<allocator_type> __alloc_traits; |
| 780 | typedef typename __alloc_traits::size_type size_type; |
| 781 | |
| 782 | __compressed_pair<size_type, allocator_type> __data_; |
| 783 | public: |
| 784 | typedef typename __alloc_traits::pointer pointer; |
| 785 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 786 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 787 | __bucket_list_deallocator() |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 788 | _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 789 | : __data_(0) {} |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 790 | |
| 791 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 792 | __bucket_list_deallocator(const allocator_type& __a, size_type __size) |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 793 | _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 794 | : __data_(__size, __a) {} |
| 795 | |
Eric Fiselier | afa7a95 | 2017-04-19 01:23:04 +0000 | [diff] [blame] | 796 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 797 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 798 | __bucket_list_deallocator(__bucket_list_deallocator&& __x) |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 799 | _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 800 | : __data_(_VSTD::move(__x.__data_)) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 801 | { |
| 802 | __x.size() = 0; |
| 803 | } |
Eric Fiselier | afa7a95 | 2017-04-19 01:23:04 +0000 | [diff] [blame] | 804 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 805 | |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 806 | _LIBCPP_INLINE_VISIBILITY |
| 807 | size_type& size() _NOEXCEPT {return __data_.first();} |
| 808 | _LIBCPP_INLINE_VISIBILITY |
| 809 | size_type size() const _NOEXCEPT {return __data_.first();} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 810 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 811 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 812 | allocator_type& __alloc() _NOEXCEPT {return __data_.second();} |
| 813 | _LIBCPP_INLINE_VISIBILITY |
| 814 | const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();} |
| 815 | |
| 816 | _LIBCPP_INLINE_VISIBILITY |
| 817 | void operator()(pointer __p) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 818 | { |
| 819 | __alloc_traits::deallocate(__alloc(), __p, size()); |
| 820 | } |
| 821 | }; |
| 822 | |
Howard Hinnant | ce53420 | 2011-06-14 19:58:17 +0000 | [diff] [blame] | 823 | template <class _Alloc> class __hash_map_node_destructor; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 824 | |
| 825 | template <class _Alloc> |
| 826 | class __hash_node_destructor |
| 827 | { |
| 828 | typedef _Alloc allocator_type; |
| 829 | typedef allocator_traits<allocator_type> __alloc_traits; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 830 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 831 | public: |
| 832 | typedef typename __alloc_traits::pointer pointer; |
| 833 | private: |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 834 | typedef __hash_node_types<pointer> _NodeTypes; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 835 | |
| 836 | allocator_type& __na_; |
| 837 | |
| 838 | __hash_node_destructor& operator=(const __hash_node_destructor&); |
| 839 | |
| 840 | public: |
| 841 | bool __value_constructed; |
| 842 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 843 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | f622b58 | 2011-07-31 17:04:30 +0000 | [diff] [blame] | 844 | explicit __hash_node_destructor(allocator_type& __na, |
| 845 | bool __constructed = false) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 846 | : __na_(__na), |
Howard Hinnant | f622b58 | 2011-07-31 17:04:30 +0000 | [diff] [blame] | 847 | __value_constructed(__constructed) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 848 | {} |
| 849 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 850 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 851 | void operator()(pointer __p) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 852 | { |
| 853 | if (__value_constructed) |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 854 | __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 855 | if (__p) |
| 856 | __alloc_traits::deallocate(__na_, __p, 1); |
| 857 | } |
| 858 | |
| 859 | template <class> friend class __hash_map_node_destructor; |
| 860 | }; |
| 861 | |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 862 | #if _LIBCPP_STD_VER > 14 |
| 863 | template <class _NodeType, class _Alloc> |
| 864 | struct __generic_container_node_destructor; |
| 865 | |
| 866 | template <class _Tp, class _VoidPtr, class _Alloc> |
| 867 | struct __generic_container_node_destructor<__hash_node<_Tp, _VoidPtr>, _Alloc> |
| 868 | : __hash_node_destructor<_Alloc> |
| 869 | { |
| 870 | using __hash_node_destructor<_Alloc>::__hash_node_destructor; |
| 871 | }; |
| 872 | #endif |
Eric Fiselier | 04333f9 | 2017-01-13 22:42:53 +0000 | [diff] [blame] | 873 | |
| 874 | #ifndef _LIBCPP_CXX03_LANG |
| 875 | template <class _Key, class _Hash, class _Equal, class _Alloc> |
| 876 | struct __diagnose_hash_table_helper { |
| 877 | static constexpr bool __trigger_diagnostics() |
Eric Fiselier | bd6a2d8 | 2017-03-03 22:35:58 +0000 | [diff] [blame] | 878 | _LIBCPP_DIAGNOSE_WARNING(__check_hash_requirements<_Key, _Hash>::value |
Eric Fiselier | acb2158 | 2017-03-01 02:02:28 +0000 | [diff] [blame] | 879 | && !__invokable<_Hash const&, _Key const&>::value, |
| 880 | "the specified hash functor does not provide a const call operator") |
| 881 | _LIBCPP_DIAGNOSE_WARNING(is_copy_constructible<_Equal>::value |
| 882 | && !__invokable<_Equal const&, _Key const&, _Key const&>::value, |
| 883 | "the specified comparator type does not provide a const call operator") |
| 884 | { |
Eric Fiselier | bd6a2d8 | 2017-03-03 22:35:58 +0000 | [diff] [blame] | 885 | static_assert(__check_hash_requirements<_Key, _Hash>::value, |
| 886 | "the specified hash does not meet the Hash requirements"); |
Eric Fiselier | acb2158 | 2017-03-01 02:02:28 +0000 | [diff] [blame] | 887 | static_assert(is_copy_constructible<_Equal>::value, |
| 888 | "the specified comparator is required to be copy constructible"); |
| 889 | return true; |
| 890 | } |
Eric Fiselier | 04333f9 | 2017-01-13 22:42:53 +0000 | [diff] [blame] | 891 | }; |
| 892 | |
| 893 | template <class _Key, class _Value, class _Hash, class _Equal, class _Alloc> |
| 894 | struct __diagnose_hash_table_helper< |
| 895 | __hash_value_type<_Key, _Value>, |
| 896 | __unordered_map_hasher<_Key, __hash_value_type<_Key, _Value>, _Hash>, |
| 897 | __unordered_map_equal<_Key, __hash_value_type<_Key, _Value>, _Equal>, |
| 898 | _Alloc> |
| 899 | : __diagnose_hash_table_helper<_Key, _Hash, _Equal, _Alloc> |
| 900 | { |
| 901 | }; |
| 902 | #endif // _LIBCPP_CXX03_LANG |
| 903 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 904 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 905 | class __hash_table |
| 906 | { |
| 907 | public: |
| 908 | typedef _Tp value_type; |
| 909 | typedef _Hash hasher; |
| 910 | typedef _Equal key_equal; |
| 911 | typedef _Alloc allocator_type; |
| 912 | |
| 913 | private: |
| 914 | typedef allocator_traits<allocator_type> __alloc_traits; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 915 | typedef typename |
| 916 | __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type |
| 917 | _NodeTypes; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 918 | public: |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 919 | |
| 920 | typedef typename _NodeTypes::__node_value_type __node_value_type; |
| 921 | typedef typename _NodeTypes::__container_value_type __container_value_type; |
Duncan P. N. Exon Smith | fde79b4 | 2016-03-17 20:45:20 +0000 | [diff] [blame] | 922 | typedef typename _NodeTypes::key_type key_type; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 923 | typedef value_type& reference; |
| 924 | typedef const value_type& const_reference; |
| 925 | typedef typename __alloc_traits::pointer pointer; |
| 926 | typedef typename __alloc_traits::const_pointer const_pointer; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 927 | #ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 928 | typedef typename __alloc_traits::size_type size_type; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 929 | #else |
| 930 | typedef typename _NodeTypes::size_type size_type; |
| 931 | #endif |
| 932 | typedef typename _NodeTypes::difference_type difference_type; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 933 | public: |
| 934 | // Create __node |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 935 | |
| 936 | typedef typename _NodeTypes::__node_type __node; |
Marshall Clow | 1f50801 | 2015-04-07 05:21:38 +0000 | [diff] [blame] | 937 | typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 938 | typedef allocator_traits<__node_allocator> __node_traits; |
Eric Fiselier | 8e39768 | 2016-02-11 15:22:37 +0000 | [diff] [blame] | 939 | typedef typename _NodeTypes::__void_pointer __void_pointer; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 940 | typedef typename _NodeTypes::__node_pointer __node_pointer; |
| 941 | typedef typename _NodeTypes::__node_pointer __node_const_pointer; |
| 942 | typedef typename _NodeTypes::__node_base_type __first_node; |
| 943 | typedef typename _NodeTypes::__node_base_pointer __node_base_pointer; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 944 | typedef typename _NodeTypes::__next_pointer __next_pointer; |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 945 | |
| 946 | private: |
| 947 | // check for sane allocator pointer rebinding semantics. Rebinding the |
| 948 | // allocator for a new pointer type should be exactly the same as rebinding |
| 949 | // the pointer using 'pointer_traits'. |
| 950 | static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value), |
| 951 | "Allocator does not rebind pointers in a sane manner."); |
| 952 | typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type |
| 953 | __node_base_allocator; |
| 954 | typedef allocator_traits<__node_base_allocator> __node_base_traits; |
| 955 | static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value), |
| 956 | "Allocator does not rebind pointers in a sane manner."); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 957 | |
| 958 | private: |
| 959 | |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 960 | typedef typename __rebind_alloc_helper<__node_traits, __next_pointer>::type __pointer_allocator; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 961 | typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 962 | typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 963 | typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 964 | typedef typename __bucket_list_deleter::pointer __node_pointer_pointer; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 965 | |
Eric Fiselier | acb2158 | 2017-03-01 02:02:28 +0000 | [diff] [blame] | 966 | #ifndef _LIBCPP_CXX03_LANG |
| 967 | static_assert(__diagnose_hash_table_helper<_Tp, _Hash, _Equal, _Alloc>::__trigger_diagnostics(), ""); |
| 968 | #endif |
| 969 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 970 | // --- Member data begin --- |
Eric Fiselier | 75d0dcf | 2016-02-10 20:46:23 +0000 | [diff] [blame] | 971 | __bucket_list __bucket_list_; |
| 972 | __compressed_pair<__first_node, __node_allocator> __p1_; |
| 973 | __compressed_pair<size_type, hasher> __p2_; |
| 974 | __compressed_pair<float, key_equal> __p3_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 975 | // --- Member data end --- |
| 976 | |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 977 | _LIBCPP_INLINE_VISIBILITY |
| 978 | size_type& size() _NOEXCEPT {return __p2_.first();} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 979 | public: |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 980 | _LIBCPP_INLINE_VISIBILITY |
| 981 | size_type size() const _NOEXCEPT {return __p2_.first();} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 982 | |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 983 | _LIBCPP_INLINE_VISIBILITY |
| 984 | hasher& hash_function() _NOEXCEPT {return __p2_.second();} |
| 985 | _LIBCPP_INLINE_VISIBILITY |
| 986 | const hasher& hash_function() const _NOEXCEPT {return __p2_.second();} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 987 | |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 988 | _LIBCPP_INLINE_VISIBILITY |
| 989 | float& max_load_factor() _NOEXCEPT {return __p3_.first();} |
| 990 | _LIBCPP_INLINE_VISIBILITY |
| 991 | float max_load_factor() const _NOEXCEPT {return __p3_.first();} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 992 | |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 993 | _LIBCPP_INLINE_VISIBILITY |
| 994 | key_equal& key_eq() _NOEXCEPT {return __p3_.second();} |
| 995 | _LIBCPP_INLINE_VISIBILITY |
| 996 | const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 997 | |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 998 | _LIBCPP_INLINE_VISIBILITY |
| 999 | __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();} |
| 1000 | _LIBCPP_INLINE_VISIBILITY |
| 1001 | const __node_allocator& __node_alloc() const _NOEXCEPT |
| 1002 | {return __p1_.second();} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1003 | |
| 1004 | public: |
| 1005 | typedef __hash_iterator<__node_pointer> iterator; |
Howard Hinnant | 307f814 | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1006 | typedef __hash_const_iterator<__node_pointer> const_iterator; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1007 | typedef __hash_local_iterator<__node_pointer> local_iterator; |
Howard Hinnant | 307f814 | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1008 | typedef __hash_const_local_iterator<__node_pointer> const_local_iterator; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1009 | |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1010 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1011 | __hash_table() |
| 1012 | _NOEXCEPT_( |
| 1013 | is_nothrow_default_constructible<__bucket_list>::value && |
| 1014 | is_nothrow_default_constructible<__first_node>::value && |
| 1015 | is_nothrow_default_constructible<__node_allocator>::value && |
| 1016 | is_nothrow_default_constructible<hasher>::value && |
| 1017 | is_nothrow_default_constructible<key_equal>::value); |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1018 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1019 | __hash_table(const hasher& __hf, const key_equal& __eql); |
| 1020 | __hash_table(const hasher& __hf, const key_equal& __eql, |
| 1021 | const allocator_type& __a); |
| 1022 | explicit __hash_table(const allocator_type& __a); |
| 1023 | __hash_table(const __hash_table& __u); |
| 1024 | __hash_table(const __hash_table& __u, const allocator_type& __a); |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1025 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1026 | __hash_table(__hash_table&& __u) |
| 1027 | _NOEXCEPT_( |
| 1028 | is_nothrow_move_constructible<__bucket_list>::value && |
| 1029 | is_nothrow_move_constructible<__first_node>::value && |
| 1030 | is_nothrow_move_constructible<__node_allocator>::value && |
| 1031 | is_nothrow_move_constructible<hasher>::value && |
| 1032 | is_nothrow_move_constructible<key_equal>::value); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1033 | __hash_table(__hash_table&& __u, const allocator_type& __a); |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1034 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1035 | ~__hash_table(); |
| 1036 | |
| 1037 | __hash_table& operator=(const __hash_table& __u); |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1038 | #ifndef _LIBCPP_CXX03_LANG |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1039 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1040 | __hash_table& operator=(__hash_table&& __u) |
| 1041 | _NOEXCEPT_( |
| 1042 | __node_traits::propagate_on_container_move_assignment::value && |
| 1043 | is_nothrow_move_assignable<__node_allocator>::value && |
| 1044 | is_nothrow_move_assignable<hasher>::value && |
| 1045 | is_nothrow_move_assignable<key_equal>::value); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1046 | #endif |
| 1047 | template <class _InputIterator> |
| 1048 | void __assign_unique(_InputIterator __first, _InputIterator __last); |
| 1049 | template <class _InputIterator> |
| 1050 | void __assign_multi(_InputIterator __first, _InputIterator __last); |
| 1051 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1052 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1053 | size_type max_size() const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1054 | { |
Eric Fiselier | 55b31b4e | 2016-11-23 01:18:56 +0000 | [diff] [blame] | 1055 | return std::min<size_type>( |
Eric Fiselier | 341c9dd | 2016-11-23 09:16:12 +0000 | [diff] [blame] | 1056 | __node_traits::max_size(__node_alloc()), |
Eric Fiselier | 55b31b4e | 2016-11-23 01:18:56 +0000 | [diff] [blame] | 1057 | numeric_limits<difference_type >::max() |
| 1058 | ); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1061 | private: |
| 1062 | _LIBCPP_INLINE_VISIBILITY |
| 1063 | __next_pointer __node_insert_multi_prepare(size_t __cp_hash, |
| 1064 | value_type& __cp_val); |
| 1065 | _LIBCPP_INLINE_VISIBILITY |
| 1066 | void __node_insert_multi_perform(__node_pointer __cp, |
| 1067 | __next_pointer __pn) _NOEXCEPT; |
| 1068 | |
| 1069 | _LIBCPP_INLINE_VISIBILITY |
| 1070 | __next_pointer __node_insert_unique_prepare(size_t __nd_hash, |
| 1071 | value_type& __nd_val); |
| 1072 | _LIBCPP_INLINE_VISIBILITY |
| 1073 | void __node_insert_unique_perform(__node_pointer __ptr) _NOEXCEPT; |
| 1074 | |
| 1075 | public: |
| 1076 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1077 | pair<iterator, bool> __node_insert_unique(__node_pointer __nd); |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1078 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1079 | iterator __node_insert_multi(__node_pointer __nd); |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1080 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1081 | iterator __node_insert_multi(const_iterator __p, |
| 1082 | __node_pointer __nd); |
| 1083 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1084 | #ifndef _LIBCPP_CXX03_LANG |
| 1085 | template <class _Key, class ..._Args> |
Duncan P. N. Exon Smith | fde79b4 | 2016-03-17 20:45:20 +0000 | [diff] [blame] | 1086 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1087 | pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1088 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1089 | template <class... _Args> |
Duncan P. N. Exon Smith | fde79b4 | 2016-03-17 20:45:20 +0000 | [diff] [blame] | 1090 | _LIBCPP_INLINE_VISIBILITY |
| 1091 | pair<iterator, bool> __emplace_unique_impl(_Args&&... __args); |
| 1092 | |
| 1093 | template <class _Pp> |
| 1094 | _LIBCPP_INLINE_VISIBILITY |
| 1095 | pair<iterator, bool> __emplace_unique(_Pp&& __x) { |
| 1096 | return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x), |
| 1097 | __can_extract_key<_Pp, key_type>()); |
| 1098 | } |
Eric Fiselier | 5008868 | 2016-04-16 00:23:12 +0000 | [diff] [blame] | 1099 | |
| 1100 | template <class _First, class _Second> |
| 1101 | _LIBCPP_INLINE_VISIBILITY |
| 1102 | typename enable_if< |
| 1103 | __can_extract_map_key<_First, key_type, __container_value_type>::value, |
| 1104 | pair<iterator, bool> |
| 1105 | >::type __emplace_unique(_First&& __f, _Second&& __s) { |
| 1106 | return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f), |
| 1107 | _VSTD::forward<_Second>(__s)); |
| 1108 | } |
| 1109 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1110 | template <class... _Args> |
Duncan P. N. Exon Smith | fde79b4 | 2016-03-17 20:45:20 +0000 | [diff] [blame] | 1111 | _LIBCPP_INLINE_VISIBILITY |
| 1112 | pair<iterator, bool> __emplace_unique(_Args&&... __args) { |
| 1113 | return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...); |
| 1114 | } |
| 1115 | |
| 1116 | template <class _Pp> |
| 1117 | _LIBCPP_INLINE_VISIBILITY |
| 1118 | pair<iterator, bool> |
| 1119 | __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) { |
| 1120 | return __emplace_unique_impl(_VSTD::forward<_Pp>(__x)); |
| 1121 | } |
| 1122 | template <class _Pp> |
| 1123 | _LIBCPP_INLINE_VISIBILITY |
| 1124 | pair<iterator, bool> |
| 1125 | __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) { |
| 1126 | return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x)); |
| 1127 | } |
| 1128 | template <class _Pp> |
| 1129 | _LIBCPP_INLINE_VISIBILITY |
| 1130 | pair<iterator, bool> |
| 1131 | __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) { |
| 1132 | return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x)); |
| 1133 | } |
| 1134 | |
| 1135 | template <class... _Args> |
| 1136 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1137 | iterator __emplace_multi(_Args&&... __args); |
| 1138 | template <class... _Args> |
Duncan P. N. Exon Smith | fde79b4 | 2016-03-17 20:45:20 +0000 | [diff] [blame] | 1139 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1140 | iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args); |
| 1141 | |
| 1142 | |
Eric Fiselier | de3f2b3 | 2015-06-13 07:18:32 +0000 | [diff] [blame] | 1143 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1144 | pair<iterator, bool> |
| 1145 | __insert_unique(__container_value_type&& __x) { |
| 1146 | return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x)); |
| 1147 | } |
| 1148 | |
| 1149 | template <class _Pp, class = typename enable_if< |
| 1150 | !__is_same_uncvref<_Pp, __container_value_type>::value |
| 1151 | >::type> |
Eric Fiselier | de3f2b3 | 2015-06-13 07:18:32 +0000 | [diff] [blame] | 1152 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1153 | pair<iterator, bool> __insert_unique(_Pp&& __x) { |
| 1154 | return __emplace_unique(_VSTD::forward<_Pp>(__x)); |
| 1155 | } |
| 1156 | |
| 1157 | template <class _Pp> |
| 1158 | _LIBCPP_INLINE_VISIBILITY |
| 1159 | iterator __insert_multi(_Pp&& __x) { |
| 1160 | return __emplace_multi(_VSTD::forward<_Pp>(__x)); |
| 1161 | } |
| 1162 | |
| 1163 | template <class _Pp> |
| 1164 | _LIBCPP_INLINE_VISIBILITY |
| 1165 | iterator __insert_multi(const_iterator __p, _Pp&& __x) { |
| 1166 | return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x)); |
| 1167 | } |
| 1168 | |
| 1169 | #else // !defined(_LIBCPP_CXX03_LANG) |
| 1170 | template <class _Key, class _Args> |
Eric Fiselier | 4271d01 | 2016-09-25 04:05:46 +0000 | [diff] [blame] | 1171 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1172 | pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args); |
| 1173 | |
| 1174 | iterator __insert_multi(const __container_value_type& __x); |
| 1175 | iterator __insert_multi(const_iterator __p, const __container_value_type& __x); |
Eric Fiselier | de3f2b3 | 2015-06-13 07:18:32 +0000 | [diff] [blame] | 1176 | #endif |
| 1177 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1178 | _LIBCPP_INLINE_VISIBILITY |
| 1179 | pair<iterator, bool> __insert_unique(const __container_value_type& __x) { |
| 1180 | return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x); |
| 1181 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1182 | |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 1183 | #if _LIBCPP_STD_VER > 14 |
| 1184 | template <class _NodeHandle, class _InsertReturnType> |
| 1185 | _LIBCPP_INLINE_VISIBILITY |
| 1186 | _InsertReturnType __node_handle_insert_unique(_NodeHandle&& __nh); |
| 1187 | template <class _NodeHandle> |
| 1188 | _LIBCPP_INLINE_VISIBILITY |
| 1189 | iterator __node_handle_insert_unique(const_iterator __hint, |
| 1190 | _NodeHandle&& __nh); |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1191 | template <class _Table> |
| 1192 | _LIBCPP_INLINE_VISIBILITY |
| 1193 | void __node_handle_merge_unique(_Table& __source); |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 1194 | |
| 1195 | template <class _NodeHandle> |
| 1196 | _LIBCPP_INLINE_VISIBILITY |
| 1197 | iterator __node_handle_insert_multi(_NodeHandle&& __nh); |
| 1198 | template <class _NodeHandle> |
| 1199 | _LIBCPP_INLINE_VISIBILITY |
| 1200 | iterator __node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh); |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1201 | template <class _Table> |
| 1202 | _LIBCPP_INLINE_VISIBILITY |
| 1203 | void __node_handle_merge_multi(_Table& __source); |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 1204 | |
| 1205 | template <class _NodeHandle> |
| 1206 | _LIBCPP_INLINE_VISIBILITY |
| 1207 | _NodeHandle __node_handle_extract(key_type const& __key); |
| 1208 | template <class _NodeHandle> |
| 1209 | _LIBCPP_INLINE_VISIBILITY |
| 1210 | _NodeHandle __node_handle_extract(const_iterator __it); |
| 1211 | #endif |
| 1212 | |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1213 | void clear() _NOEXCEPT; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1214 | void rehash(size_type __n); |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1215 | _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1216 | {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));} |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1217 | |
| 1218 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1219 | size_type bucket_count() const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1220 | { |
| 1221 | return __bucket_list_.get_deleter().size(); |
| 1222 | } |
| 1223 | |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1224 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1225 | iterator begin() _NOEXCEPT; |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1226 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1227 | iterator end() _NOEXCEPT; |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1228 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1229 | const_iterator begin() const _NOEXCEPT; |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1230 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1231 | const_iterator end() const _NOEXCEPT; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1232 | |
| 1233 | template <class _Key> |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1234 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1235 | size_type bucket(const _Key& __k) const |
Howard Hinnant | e5c13de | 2013-07-29 19:05:47 +0000 | [diff] [blame] | 1236 | { |
| 1237 | _LIBCPP_ASSERT(bucket_count() > 0, |
| 1238 | "unordered container::bucket(key) called when bucket_count() == 0"); |
| 1239 | return __constrain_hash(hash_function()(__k), bucket_count()); |
| 1240 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1241 | |
| 1242 | template <class _Key> |
| 1243 | iterator find(const _Key& __x); |
| 1244 | template <class _Key> |
| 1245 | const_iterator find(const _Key& __x) const; |
| 1246 | |
Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1247 | typedef __hash_node_destructor<__node_allocator> _Dp; |
| 1248 | typedef unique_ptr<__node, _Dp> __node_holder; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1249 | |
| 1250 | iterator erase(const_iterator __p); |
| 1251 | iterator erase(const_iterator __first, const_iterator __last); |
| 1252 | template <class _Key> |
| 1253 | size_type __erase_unique(const _Key& __k); |
| 1254 | template <class _Key> |
| 1255 | size_type __erase_multi(const _Key& __k); |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1256 | __node_holder remove(const_iterator __p) _NOEXCEPT; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1257 | |
| 1258 | template <class _Key> |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1259 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1260 | size_type __count_unique(const _Key& __k) const; |
| 1261 | template <class _Key> |
| 1262 | size_type __count_multi(const _Key& __k) const; |
| 1263 | |
| 1264 | template <class _Key> |
| 1265 | pair<iterator, iterator> |
| 1266 | __equal_range_unique(const _Key& __k); |
| 1267 | template <class _Key> |
| 1268 | pair<const_iterator, const_iterator> |
| 1269 | __equal_range_unique(const _Key& __k) const; |
| 1270 | |
| 1271 | template <class _Key> |
| 1272 | pair<iterator, iterator> |
| 1273 | __equal_range_multi(const _Key& __k); |
| 1274 | template <class _Key> |
| 1275 | pair<const_iterator, const_iterator> |
| 1276 | __equal_range_multi(const _Key& __k) const; |
| 1277 | |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1278 | void swap(__hash_table& __u) |
Eric Fiselier | 87a8249 | 2015-07-18 20:40:46 +0000 | [diff] [blame] | 1279 | #if _LIBCPP_STD_VER <= 11 |
Eric Fiselier | 780b51d | 2016-12-28 05:53:01 +0000 | [diff] [blame] | 1280 | _NOEXCEPT_DEBUG_( |
Marshall Clow | e3fbe14 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 1281 | __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value |
Marshall Clow | e3fbe14 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 1282 | && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value |
| 1283 | || __is_nothrow_swappable<__pointer_allocator>::value) |
| 1284 | && (!__node_traits::propagate_on_container_swap::value |
| 1285 | || __is_nothrow_swappable<__node_allocator>::value) |
Marshall Clow | e3fbe14 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 1286 | ); |
Eric Fiselier | 87a8249 | 2015-07-18 20:40:46 +0000 | [diff] [blame] | 1287 | #else |
Eric Fiselier | 780b51d | 2016-12-28 05:53:01 +0000 | [diff] [blame] | 1288 | _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value); |
Eric Fiselier | 87a8249 | 2015-07-18 20:40:46 +0000 | [diff] [blame] | 1289 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1290 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1291 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1292 | size_type max_bucket_count() const _NOEXCEPT |
Eric Fiselier | 55b31b4e | 2016-11-23 01:18:56 +0000 | [diff] [blame] | 1293 | {return max_size(); } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1294 | size_type bucket_size(size_type __n) const; |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1295 | _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1296 | { |
| 1297 | size_type __bc = bucket_count(); |
| 1298 | return __bc != 0 ? (float)size() / __bc : 0.f; |
| 1299 | } |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1300 | _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT |
Howard Hinnant | e5c13de | 2013-07-29 19:05:47 +0000 | [diff] [blame] | 1301 | { |
| 1302 | _LIBCPP_ASSERT(__mlf > 0, |
| 1303 | "unordered container::max_load_factor(lf) called with lf <= 0"); |
| 1304 | max_load_factor() = _VSTD::max(__mlf, load_factor()); |
| 1305 | } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1306 | |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1307 | _LIBCPP_INLINE_VISIBILITY |
| 1308 | local_iterator |
| 1309 | begin(size_type __n) |
| 1310 | { |
Howard Hinnant | e5c13de | 2013-07-29 19:05:47 +0000 | [diff] [blame] | 1311 | _LIBCPP_ASSERT(__n < bucket_count(), |
| 1312 | "unordered container::begin(n) called with n >= bucket_count()"); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1313 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1314 | return local_iterator(__bucket_list_[__n], __n, bucket_count(), this); |
| 1315 | #else |
| 1316 | return local_iterator(__bucket_list_[__n], __n, bucket_count()); |
| 1317 | #endif |
| 1318 | } |
| 1319 | |
| 1320 | _LIBCPP_INLINE_VISIBILITY |
| 1321 | local_iterator |
| 1322 | end(size_type __n) |
| 1323 | { |
Howard Hinnant | e5c13de | 2013-07-29 19:05:47 +0000 | [diff] [blame] | 1324 | _LIBCPP_ASSERT(__n < bucket_count(), |
| 1325 | "unordered container::end(n) called with n >= bucket_count()"); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1326 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1327 | return local_iterator(nullptr, __n, bucket_count(), this); |
| 1328 | #else |
| 1329 | return local_iterator(nullptr, __n, bucket_count()); |
| 1330 | #endif |
| 1331 | } |
| 1332 | |
| 1333 | _LIBCPP_INLINE_VISIBILITY |
| 1334 | const_local_iterator |
| 1335 | cbegin(size_type __n) const |
| 1336 | { |
Howard Hinnant | e5c13de | 2013-07-29 19:05:47 +0000 | [diff] [blame] | 1337 | _LIBCPP_ASSERT(__n < bucket_count(), |
| 1338 | "unordered container::cbegin(n) called with n >= bucket_count()"); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1339 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1340 | return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this); |
| 1341 | #else |
| 1342 | return const_local_iterator(__bucket_list_[__n], __n, bucket_count()); |
| 1343 | #endif |
| 1344 | } |
| 1345 | |
| 1346 | _LIBCPP_INLINE_VISIBILITY |
| 1347 | const_local_iterator |
| 1348 | cend(size_type __n) const |
| 1349 | { |
Howard Hinnant | e5c13de | 2013-07-29 19:05:47 +0000 | [diff] [blame] | 1350 | _LIBCPP_ASSERT(__n < bucket_count(), |
| 1351 | "unordered container::cend(n) called with n >= bucket_count()"); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1352 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1353 | return const_local_iterator(nullptr, __n, bucket_count(), this); |
| 1354 | #else |
| 1355 | return const_local_iterator(nullptr, __n, bucket_count()); |
| 1356 | #endif |
| 1357 | } |
| 1358 | |
| 1359 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1360 | |
| 1361 | bool __dereferenceable(const const_iterator* __i) const; |
| 1362 | bool __decrementable(const const_iterator* __i) const; |
| 1363 | bool __addable(const const_iterator* __i, ptrdiff_t __n) const; |
| 1364 | bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const; |
| 1365 | |
| 1366 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 |
| 1367 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1368 | private: |
| 1369 | void __rehash(size_type __n); |
| 1370 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1371 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1372 | template <class ..._Args> |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1373 | __node_holder __construct_node(_Args&& ...__args); |
| 1374 | |
| 1375 | template <class _First, class ..._Rest> |
| 1376 | __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest); |
| 1377 | #else // _LIBCPP_CXX03_LANG |
| 1378 | __node_holder __construct_node(const __container_value_type& __v); |
| 1379 | __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1380 | #endif |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1381 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1382 | |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1383 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1384 | void __copy_assign_alloc(const __hash_table& __u) |
| 1385 | {__copy_assign_alloc(__u, integral_constant<bool, |
| 1386 | __node_traits::propagate_on_container_copy_assignment::value>());} |
| 1387 | void __copy_assign_alloc(const __hash_table& __u, true_type); |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1388 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c206366 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1389 | void __copy_assign_alloc(const __hash_table&, false_type) {} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1390 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1391 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1392 | void __move_assign(__hash_table& __u, false_type); |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1393 | void __move_assign(__hash_table& __u, true_type) |
| 1394 | _NOEXCEPT_( |
| 1395 | is_nothrow_move_assignable<__node_allocator>::value && |
| 1396 | is_nothrow_move_assignable<hasher>::value && |
| 1397 | is_nothrow_move_assignable<key_equal>::value); |
| 1398 | _LIBCPP_INLINE_VISIBILITY |
| 1399 | void __move_assign_alloc(__hash_table& __u) |
| 1400 | _NOEXCEPT_( |
| 1401 | !__node_traits::propagate_on_container_move_assignment::value || |
| 1402 | (is_nothrow_move_assignable<__pointer_allocator>::value && |
| 1403 | is_nothrow_move_assignable<__node_allocator>::value)) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1404 | {__move_assign_alloc(__u, integral_constant<bool, |
| 1405 | __node_traits::propagate_on_container_move_assignment::value>());} |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1406 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1407 | void __move_assign_alloc(__hash_table& __u, true_type) |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1408 | _NOEXCEPT_( |
| 1409 | is_nothrow_move_assignable<__pointer_allocator>::value && |
| 1410 | is_nothrow_move_assignable<__node_allocator>::value) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1411 | { |
| 1412 | __bucket_list_.get_deleter().__alloc() = |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1413 | _VSTD::move(__u.__bucket_list_.get_deleter().__alloc()); |
| 1414 | __node_alloc() = _VSTD::move(__u.__node_alloc()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1415 | } |
Howard Hinnant | 43d9923 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1416 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1417 | void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {} |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1418 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1419 | |
Eric Fiselier | cd71f44 | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 1420 | void __deallocate_node(__next_pointer __np) _NOEXCEPT; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1421 | __next_pointer __detach() _NOEXCEPT; |
Howard Hinnant | 307f814 | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1422 | |
Eric Fiselier | e2f2d1e | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1423 | template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map; |
| 1424 | template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1425 | }; |
| 1426 | |
| 1427 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1428 | inline |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1429 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table() |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1430 | _NOEXCEPT_( |
| 1431 | is_nothrow_default_constructible<__bucket_list>::value && |
| 1432 | is_nothrow_default_constructible<__first_node>::value && |
Eric Fiselier | e2e332a | 2015-12-16 00:53:04 +0000 | [diff] [blame] | 1433 | is_nothrow_default_constructible<__node_allocator>::value && |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1434 | is_nothrow_default_constructible<hasher>::value && |
| 1435 | is_nothrow_default_constructible<key_equal>::value) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1436 | : __p2_(0), |
| 1437 | __p3_(1.0f) |
| 1438 | { |
| 1439 | } |
| 1440 | |
| 1441 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1442 | inline |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1443 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf, |
| 1444 | const key_equal& __eql) |
| 1445 | : __bucket_list_(nullptr, __bucket_list_deleter()), |
| 1446 | __p1_(), |
| 1447 | __p2_(0, __hf), |
| 1448 | __p3_(1.0f, __eql) |
| 1449 | { |
| 1450 | } |
| 1451 | |
| 1452 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1453 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf, |
| 1454 | const key_equal& __eql, |
| 1455 | const allocator_type& __a) |
| 1456 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
Eric Fiselier | c88580c | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1457 | __p1_(__second_tag(), __node_allocator(__a)), |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1458 | __p2_(0, __hf), |
| 1459 | __p3_(1.0f, __eql) |
| 1460 | { |
| 1461 | } |
| 1462 | |
| 1463 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1464 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a) |
| 1465 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
Eric Fiselier | c88580c | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1466 | __p1_(__second_tag(), __node_allocator(__a)), |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1467 | __p2_(0), |
| 1468 | __p3_(1.0f) |
| 1469 | { |
| 1470 | } |
| 1471 | |
| 1472 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1473 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u) |
| 1474 | : __bucket_list_(nullptr, |
| 1475 | __bucket_list_deleter(allocator_traits<__pointer_allocator>:: |
| 1476 | select_on_container_copy_construction( |
| 1477 | __u.__bucket_list_.get_deleter().__alloc()), 0)), |
Eric Fiselier | c88580c | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1478 | __p1_(__second_tag(), allocator_traits<__node_allocator>:: |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1479 | select_on_container_copy_construction(__u.__node_alloc())), |
| 1480 | __p2_(0, __u.hash_function()), |
| 1481 | __p3_(__u.__p3_) |
| 1482 | { |
| 1483 | } |
| 1484 | |
| 1485 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1486 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u, |
| 1487 | const allocator_type& __a) |
| 1488 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
Eric Fiselier | c88580c | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1489 | __p1_(__second_tag(), __node_allocator(__a)), |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1490 | __p2_(0, __u.hash_function()), |
| 1491 | __p3_(__u.__p3_) |
| 1492 | { |
| 1493 | } |
| 1494 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1495 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1496 | |
| 1497 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1498 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u) |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1499 | _NOEXCEPT_( |
| 1500 | is_nothrow_move_constructible<__bucket_list>::value && |
| 1501 | is_nothrow_move_constructible<__first_node>::value && |
Eric Fiselier | e2e332a | 2015-12-16 00:53:04 +0000 | [diff] [blame] | 1502 | is_nothrow_move_constructible<__node_allocator>::value && |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1503 | is_nothrow_move_constructible<hasher>::value && |
| 1504 | is_nothrow_move_constructible<key_equal>::value) |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1505 | : __bucket_list_(_VSTD::move(__u.__bucket_list_)), |
| 1506 | __p1_(_VSTD::move(__u.__p1_)), |
| 1507 | __p2_(_VSTD::move(__u.__p2_)), |
| 1508 | __p3_(_VSTD::move(__u.__p3_)) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1509 | { |
| 1510 | if (size() > 0) |
| 1511 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1512 | __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = |
| 1513 | __p1_.first().__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1514 | __u.__p1_.first().__next_ = nullptr; |
| 1515 | __u.size() = 0; |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1520 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u, |
| 1521 | const allocator_type& __a) |
| 1522 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
Eric Fiselier | c88580c | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1523 | __p1_(__second_tag(), __node_allocator(__a)), |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1524 | __p2_(0, _VSTD::move(__u.hash_function())), |
| 1525 | __p3_(_VSTD::move(__u.__p3_)) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1526 | { |
| 1527 | if (__a == allocator_type(__u.__node_alloc())) |
| 1528 | { |
| 1529 | __bucket_list_.reset(__u.__bucket_list_.release()); |
| 1530 | __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size(); |
| 1531 | __u.__bucket_list_.get_deleter().size() = 0; |
| 1532 | if (__u.size() > 0) |
| 1533 | { |
| 1534 | __p1_.first().__next_ = __u.__p1_.first().__next_; |
| 1535 | __u.__p1_.first().__next_ = nullptr; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1536 | __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = |
| 1537 | __p1_.first().__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1538 | size() = __u.size(); |
| 1539 | __u.size() = 0; |
| 1540 | } |
| 1541 | } |
| 1542 | } |
| 1543 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1544 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1545 | |
| 1546 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1547 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table() |
| 1548 | { |
Eric Fiselier | acb2158 | 2017-03-01 02:02:28 +0000 | [diff] [blame] | 1549 | #if defined(_LIBCPP_CXX03_LANG) |
Marshall Clow | 3b8669e | 2016-06-30 22:05:45 +0000 | [diff] [blame] | 1550 | static_assert((is_copy_constructible<key_equal>::value), |
| 1551 | "Predicate must be copy-constructible."); |
| 1552 | static_assert((is_copy_constructible<hasher>::value), |
| 1553 | "Hasher must be copy-constructible."); |
Eric Fiselier | 04333f9 | 2017-01-13 22:42:53 +0000 | [diff] [blame] | 1554 | #endif |
Eric Fiselier | acb2158 | 2017-03-01 02:02:28 +0000 | [diff] [blame] | 1555 | |
Eric Fiselier | cd71f44 | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 1556 | __deallocate_node(__p1_.first().__next_); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1557 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1558 | __get_db()->__erase_c(this); |
| 1559 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1563 | void |
| 1564 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc( |
| 1565 | const __hash_table& __u, true_type) |
| 1566 | { |
| 1567 | if (__node_alloc() != __u.__node_alloc()) |
| 1568 | { |
| 1569 | clear(); |
| 1570 | __bucket_list_.reset(); |
| 1571 | __bucket_list_.get_deleter().size() = 0; |
| 1572 | } |
| 1573 | __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc(); |
| 1574 | __node_alloc() = __u.__node_alloc(); |
| 1575 | } |
| 1576 | |
| 1577 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1578 | __hash_table<_Tp, _Hash, _Equal, _Alloc>& |
| 1579 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u) |
| 1580 | { |
| 1581 | if (this != &__u) |
| 1582 | { |
| 1583 | __copy_assign_alloc(__u); |
| 1584 | hash_function() = __u.hash_function(); |
| 1585 | key_eq() = __u.key_eq(); |
| 1586 | max_load_factor() = __u.max_load_factor(); |
| 1587 | __assign_multi(__u.begin(), __u.end()); |
| 1588 | } |
| 1589 | return *this; |
| 1590 | } |
| 1591 | |
| 1592 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1593 | void |
Eric Fiselier | cd71f44 | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 1594 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate_node(__next_pointer __np) |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1595 | _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1596 | { |
| 1597 | __node_allocator& __na = __node_alloc(); |
| 1598 | while (__np != nullptr) |
| 1599 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1600 | __next_pointer __next = __np->__next_; |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1601 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1602 | __c_node* __c = __get_db()->__find_c_and_lock(this); |
| 1603 | for (__i_node** __p = __c->end_; __p != __c->beg_; ) |
| 1604 | { |
| 1605 | --__p; |
| 1606 | iterator* __i = static_cast<iterator*>((*__p)->__i_); |
| 1607 | if (__i->__node_ == __np) |
| 1608 | { |
| 1609 | (*__p)->__c_ = nullptr; |
| 1610 | if (--__c->end_ != __p) |
| 1611 | memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); |
| 1612 | } |
| 1613 | } |
| 1614 | __get_db()->unlock(); |
| 1615 | #endif |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1616 | __node_pointer __real_np = __np->__upcast(); |
| 1617 | __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_)); |
| 1618 | __node_traits::deallocate(__na, __real_np, 1); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1619 | __np = __next; |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1624 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1625 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1626 | { |
| 1627 | size_type __bc = bucket_count(); |
| 1628 | for (size_type __i = 0; __i < __bc; ++__i) |
| 1629 | __bucket_list_[__i] = nullptr; |
| 1630 | size() = 0; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1631 | __next_pointer __cache = __p1_.first().__next_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1632 | __p1_.first().__next_ = nullptr; |
| 1633 | return __cache; |
| 1634 | } |
| 1635 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1636 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1637 | |
| 1638 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1639 | void |
| 1640 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( |
| 1641 | __hash_table& __u, true_type) |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1642 | _NOEXCEPT_( |
| 1643 | is_nothrow_move_assignable<__node_allocator>::value && |
| 1644 | is_nothrow_move_assignable<hasher>::value && |
| 1645 | is_nothrow_move_assignable<key_equal>::value) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1646 | { |
| 1647 | clear(); |
| 1648 | __bucket_list_.reset(__u.__bucket_list_.release()); |
| 1649 | __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size(); |
| 1650 | __u.__bucket_list_.get_deleter().size() = 0; |
| 1651 | __move_assign_alloc(__u); |
| 1652 | size() = __u.size(); |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1653 | hash_function() = _VSTD::move(__u.hash_function()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1654 | max_load_factor() = __u.max_load_factor(); |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1655 | key_eq() = _VSTD::move(__u.key_eq()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1656 | __p1_.first().__next_ = __u.__p1_.first().__next_; |
| 1657 | if (size() > 0) |
| 1658 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1659 | __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = |
| 1660 | __p1_.first().__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1661 | __u.__p1_.first().__next_ = nullptr; |
| 1662 | __u.size() = 0; |
| 1663 | } |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1664 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1665 | __get_db()->swap(this, &__u); |
| 1666 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1670 | void |
| 1671 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( |
| 1672 | __hash_table& __u, false_type) |
| 1673 | { |
| 1674 | if (__node_alloc() == __u.__node_alloc()) |
| 1675 | __move_assign(__u, true_type()); |
| 1676 | else |
| 1677 | { |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1678 | hash_function() = _VSTD::move(__u.hash_function()); |
| 1679 | key_eq() = _VSTD::move(__u.key_eq()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1680 | max_load_factor() = __u.max_load_factor(); |
| 1681 | if (bucket_count() != 0) |
| 1682 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1683 | __next_pointer __cache = __detach(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1684 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1685 | try |
| 1686 | { |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1687 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1688 | const_iterator __i = __u.begin(); |
| 1689 | while (__cache != nullptr && __u.size() != 0) |
| 1690 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1691 | __cache->__upcast()->__value_ = |
| 1692 | _VSTD::move(__u.remove(__i++)->__value_); |
| 1693 | __next_pointer __next = __cache->__next_; |
| 1694 | __node_insert_multi(__cache->__upcast()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1695 | __cache = __next; |
| 1696 | } |
| 1697 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1698 | } |
| 1699 | catch (...) |
| 1700 | { |
Eric Fiselier | cd71f44 | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 1701 | __deallocate_node(__cache); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1702 | throw; |
| 1703 | } |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1704 | #endif // _LIBCPP_NO_EXCEPTIONS |
Eric Fiselier | cd71f44 | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 1705 | __deallocate_node(__cache); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1706 | } |
| 1707 | const_iterator __i = __u.begin(); |
| 1708 | while (__u.size() != 0) |
| 1709 | { |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1710 | __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1711 | __node_insert_multi(__h.get()); |
| 1712 | __h.release(); |
| 1713 | } |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1718 | inline |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1719 | __hash_table<_Tp, _Hash, _Equal, _Alloc>& |
| 1720 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u) |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1721 | _NOEXCEPT_( |
| 1722 | __node_traits::propagate_on_container_move_assignment::value && |
| 1723 | is_nothrow_move_assignable<__node_allocator>::value && |
| 1724 | is_nothrow_move_assignable<hasher>::value && |
| 1725 | is_nothrow_move_assignable<key_equal>::value) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1726 | { |
| 1727 | __move_assign(__u, integral_constant<bool, |
| 1728 | __node_traits::propagate_on_container_move_assignment::value>()); |
| 1729 | return *this; |
| 1730 | } |
| 1731 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1732 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1733 | |
| 1734 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1735 | template <class _InputIterator> |
| 1736 | void |
| 1737 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first, |
| 1738 | _InputIterator __last) |
| 1739 | { |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1740 | typedef iterator_traits<_InputIterator> _ITraits; |
| 1741 | typedef typename _ITraits::value_type _ItValueType; |
| 1742 | static_assert((is_same<_ItValueType, __container_value_type>::value), |
| 1743 | "__assign_unique may only be called with the containers value type"); |
| 1744 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1745 | if (bucket_count() != 0) |
| 1746 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1747 | __next_pointer __cache = __detach(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1748 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1749 | try |
| 1750 | { |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1751 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1752 | for (; __cache != nullptr && __first != __last; ++__first) |
| 1753 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1754 | __cache->__upcast()->__value_ = *__first; |
| 1755 | __next_pointer __next = __cache->__next_; |
| 1756 | __node_insert_unique(__cache->__upcast()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1757 | __cache = __next; |
| 1758 | } |
| 1759 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1760 | } |
| 1761 | catch (...) |
| 1762 | { |
Eric Fiselier | cd71f44 | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 1763 | __deallocate_node(__cache); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1764 | throw; |
| 1765 | } |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1766 | #endif // _LIBCPP_NO_EXCEPTIONS |
Eric Fiselier | cd71f44 | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 1767 | __deallocate_node(__cache); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1768 | } |
| 1769 | for (; __first != __last; ++__first) |
| 1770 | __insert_unique(*__first); |
| 1771 | } |
| 1772 | |
| 1773 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1774 | template <class _InputIterator> |
| 1775 | void |
| 1776 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first, |
| 1777 | _InputIterator __last) |
| 1778 | { |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1779 | typedef iterator_traits<_InputIterator> _ITraits; |
| 1780 | typedef typename _ITraits::value_type _ItValueType; |
| 1781 | static_assert((is_same<_ItValueType, __container_value_type>::value || |
| 1782 | is_same<_ItValueType, __node_value_type>::value), |
| 1783 | "__assign_multi may only be called with the containers value type" |
| 1784 | " or the nodes value type"); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1785 | if (bucket_count() != 0) |
| 1786 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1787 | __next_pointer __cache = __detach(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1788 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1789 | try |
| 1790 | { |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1791 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1792 | for (; __cache != nullptr && __first != __last; ++__first) |
| 1793 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1794 | __cache->__upcast()->__value_ = *__first; |
| 1795 | __next_pointer __next = __cache->__next_; |
| 1796 | __node_insert_multi(__cache->__upcast()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1797 | __cache = __next; |
| 1798 | } |
| 1799 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1800 | } |
| 1801 | catch (...) |
| 1802 | { |
Eric Fiselier | cd71f44 | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 1803 | __deallocate_node(__cache); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1804 | throw; |
| 1805 | } |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1806 | #endif // _LIBCPP_NO_EXCEPTIONS |
Eric Fiselier | cd71f44 | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 1807 | __deallocate_node(__cache); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1808 | } |
| 1809 | for (; __first != __last; ++__first) |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 1810 | __insert_multi(_NodeTypes::__get_value(*__first)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1814 | inline |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1815 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1816 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1817 | { |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1818 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1819 | return iterator(__p1_.first().__next_, this); |
| 1820 | #else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1821 | return iterator(__p1_.first().__next_); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1822 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1826 | inline |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1827 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1828 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1829 | { |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1830 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1831 | return iterator(nullptr, this); |
| 1832 | #else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1833 | return iterator(nullptr); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1834 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1838 | inline |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1839 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1840 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1841 | { |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1842 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1843 | return const_iterator(__p1_.first().__next_, this); |
| 1844 | #else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1845 | return const_iterator(__p1_.first().__next_); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1846 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 1850 | inline |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1851 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1852 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1853 | { |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1854 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1855 | return const_iterator(nullptr, this); |
| 1856 | #else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1857 | return const_iterator(nullptr); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 1858 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1862 | void |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1863 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1864 | { |
| 1865 | if (size() > 0) |
| 1866 | { |
Eric Fiselier | cd71f44 | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 1867 | __deallocate_node(__p1_.first().__next_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1868 | __p1_.first().__next_ = nullptr; |
| 1869 | size_type __bc = bucket_count(); |
Howard Hinnant | 1f8da84 | 2011-07-05 14:14:17 +0000 | [diff] [blame] | 1870 | for (size_type __i = 0; __i < __bc; ++__i) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1871 | __bucket_list_[__i] = nullptr; |
| 1872 | size() = 0; |
| 1873 | } |
| 1874 | } |
| 1875 | |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Prepare the container for an insertion of the value __value with the hash |
| 1878 | // __hash. This does a lookup into the container to see if __value is already |
| 1879 | // present, and performs a rehash if necessary. Returns a pointer to the |
| 1880 | // existing element if it exists, otherwise nullptr. |
| 1881 | // |
| 1882 | // Note that this function does forward exceptions if key_eq() throws, and never |
| 1883 | // mutates __value or actually inserts into the map. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1884 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1885 | _LIBCPP_INLINE_VISIBILITY |
| 1886 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer |
| 1887 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_prepare( |
| 1888 | size_t __hash, value_type& __value) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1889 | { |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1890 | size_type __bc = bucket_count(); |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1891 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1892 | if (__bc != 0) |
| 1893 | { |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1894 | size_t __chash = __constrain_hash(__hash, __bc); |
| 1895 | __next_pointer __ndptr = __bucket_list_[__chash]; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1896 | if (__ndptr != nullptr) |
| 1897 | { |
| 1898 | for (__ndptr = __ndptr->__next_; __ndptr != nullptr && |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1899 | __constrain_hash(__ndptr->__hash(), __bc) == __chash; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1900 | __ndptr = __ndptr->__next_) |
| 1901 | { |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1902 | if (key_eq()(__ndptr->__upcast()->__value_, __value)) |
| 1903 | return __ndptr; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1904 | } |
| 1905 | } |
| 1906 | } |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1907 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1908 | { |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1909 | rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc), |
| 1910 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1911 | } |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1912 | return nullptr; |
| 1913 | } |
| 1914 | |
| 1915 | // Insert the node __nd into the container by pushing it into the right bucket, |
| 1916 | // and updating size(). Assumes that __nd->__hash is up-to-date, and that |
| 1917 | // rehashing has already occurred and that no element with the same key exists |
| 1918 | // in the map. |
| 1919 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1920 | _LIBCPP_INLINE_VISIBILITY |
| 1921 | void |
| 1922 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique_perform( |
| 1923 | __node_pointer __nd) _NOEXCEPT |
| 1924 | { |
| 1925 | size_type __bc = bucket_count(); |
| 1926 | size_t __chash = __constrain_hash(__nd->__hash(), __bc); |
| 1927 | // insert_after __bucket_list_[__chash], or __first_node if bucket is null |
| 1928 | __next_pointer __pn = __bucket_list_[__chash]; |
| 1929 | if (__pn == nullptr) |
| 1930 | { |
| 1931 | __pn =__p1_.first().__ptr(); |
| 1932 | __nd->__next_ = __pn->__next_; |
| 1933 | __pn->__next_ = __nd->__ptr(); |
| 1934 | // fix up __bucket_list_ |
| 1935 | __bucket_list_[__chash] = __pn; |
| 1936 | if (__nd->__next_ != nullptr) |
| 1937 | __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr(); |
| 1938 | } |
| 1939 | else |
| 1940 | { |
| 1941 | __nd->__next_ = __pn->__next_; |
| 1942 | __pn->__next_ = __nd->__ptr(); |
| 1943 | } |
| 1944 | ++size(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1948 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
| 1949 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1950 | { |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1951 | __nd->__hash_ = hash_function()(__nd->__value_); |
| 1952 | __next_pointer __existing_node = |
| 1953 | __node_insert_unique_prepare(__nd->__hash(), __nd->__value_); |
| 1954 | |
| 1955 | // Insert the node, unless it already exists in the container. |
| 1956 | bool __inserted = false; |
| 1957 | if (__existing_node == nullptr) |
| 1958 | { |
| 1959 | __node_insert_unique_perform(__nd); |
| 1960 | __existing_node = __nd->__ptr(); |
| 1961 | __inserted = true; |
| 1962 | } |
| 1963 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1964 | return pair<iterator, bool>(iterator(__existing_node, this), __inserted); |
| 1965 | #else |
| 1966 | return pair<iterator, bool>(iterator(__existing_node), __inserted); |
| 1967 | #endif |
| 1968 | } |
| 1969 | |
| 1970 | // Prepare the container for an insertion of the value __cp_val with the hash |
| 1971 | // __cp_hash. This does a lookup into the container to see if __cp_value is |
| 1972 | // already present, and performs a rehash if necessary. Returns a pointer to the |
| 1973 | // last occurance of __cp_val in the map. |
| 1974 | // |
| 1975 | // Note that this function does forward exceptions if key_eq() throws, and never |
| 1976 | // mutates __value or actually inserts into the map. |
| 1977 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1978 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer |
| 1979 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_prepare( |
| 1980 | size_t __cp_hash, value_type& __cp_val) |
| 1981 | { |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1982 | size_type __bc = bucket_count(); |
| 1983 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 1984 | { |
Eric Fiselier | 9ba5c11 | 2015-02-02 21:31:48 +0000 | [diff] [blame] | 1985 | rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc), |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1986 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 1987 | __bc = bucket_count(); |
| 1988 | } |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1989 | size_t __chash = __constrain_hash(__cp_hash, __bc); |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 1990 | __next_pointer __pn = __bucket_list_[__chash]; |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1991 | if (__pn != nullptr) |
| 1992 | { |
| 1993 | for (bool __found = false; __pn->__next_ != nullptr && |
| 1994 | __constrain_hash(__pn->__next_->__hash(), __bc) == __chash; |
| 1995 | __pn = __pn->__next_) |
| 1996 | { |
| 1997 | // __found key_eq() action |
| 1998 | // false false loop |
| 1999 | // true true loop |
| 2000 | // false true set __found to true |
| 2001 | // true false break |
| 2002 | if (__found != (__pn->__next_->__hash() == __cp_hash && |
| 2003 | key_eq()(__pn->__next_->__upcast()->__value_, __cp_val))) |
| 2004 | { |
| 2005 | if (!__found) |
| 2006 | __found = true; |
| 2007 | else |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
| 2011 | } |
| 2012 | return __pn; |
| 2013 | } |
| 2014 | |
| 2015 | // Insert the node __cp into the container after __pn (which is the last node in |
| 2016 | // the bucket that compares equal to __cp). Rehashing, and checking for |
| 2017 | // uniqueness has already been performed (in __node_insert_multi_prepare), so |
| 2018 | // all we need to do is update the bucket and size(). Assumes that __cp->__hash |
| 2019 | // is up-to-date. |
| 2020 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2021 | void |
| 2022 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi_perform( |
| 2023 | __node_pointer __cp, __next_pointer __pn) _NOEXCEPT |
| 2024 | { |
| 2025 | size_type __bc = bucket_count(); |
| 2026 | size_t __chash = __constrain_hash(__cp->__hash_, __bc); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2027 | if (__pn == nullptr) |
| 2028 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2029 | __pn =__p1_.first().__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2030 | __cp->__next_ = __pn->__next_; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2031 | __pn->__next_ = __cp->__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2032 | // fix up __bucket_list_ |
| 2033 | __bucket_list_[__chash] = __pn; |
| 2034 | if (__cp->__next_ != nullptr) |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2035 | __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)] |
| 2036 | = __cp->__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2037 | } |
| 2038 | else |
| 2039 | { |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2040 | __cp->__next_ = __pn->__next_; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2041 | __pn->__next_ = __cp->__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2042 | if (__cp->__next_ != nullptr) |
| 2043 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2044 | size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2045 | if (__nhash != __chash) |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2046 | __bucket_list_[__nhash] = __cp->__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2047 | } |
| 2048 | } |
| 2049 | ++size(); |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | |
| 2053 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2054 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2055 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp) |
| 2056 | { |
| 2057 | __cp->__hash_ = hash_function()(__cp->__value_); |
| 2058 | __next_pointer __pn = __node_insert_multi_prepare(__cp->__hash(), __cp->__value_); |
| 2059 | __node_insert_multi_perform(__cp, __pn); |
| 2060 | |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2061 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2062 | return iterator(__cp->__ptr(), this); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2063 | #else |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2064 | return iterator(__cp->__ptr()); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2065 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2069 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2070 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi( |
| 2071 | const_iterator __p, __node_pointer __cp) |
| 2072 | { |
Howard Hinnant | 2f51de5 | 2013-08-02 17:50:49 +0000 | [diff] [blame] | 2073 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2074 | _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, |
| 2075 | "unordered container::emplace_hint(const_iterator, args...) called with an iterator not" |
| 2076 | " referring to this unordered container"); |
| 2077 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2078 | if (__p != end() && key_eq()(*__p, __cp->__value_)) |
| 2079 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2080 | __next_pointer __np = __p.__node_; |
| 2081 | __cp->__hash_ = __np->__hash(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2082 | size_type __bc = bucket_count(); |
| 2083 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 2084 | { |
Eric Fiselier | 9ba5c11 | 2015-02-02 21:31:48 +0000 | [diff] [blame] | 2085 | rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc), |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2086 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 2087 | __bc = bucket_count(); |
| 2088 | } |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2089 | size_t __chash = __constrain_hash(__cp->__hash_, __bc); |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2090 | __next_pointer __pp = __bucket_list_[__chash]; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2091 | while (__pp->__next_ != __np) |
| 2092 | __pp = __pp->__next_; |
| 2093 | __cp->__next_ = __np; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2094 | __pp->__next_ = static_cast<__next_pointer>(__cp); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2095 | ++size(); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2096 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2097 | return iterator(static_cast<__next_pointer>(__cp), this); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2098 | #else |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2099 | return iterator(static_cast<__next_pointer>(__cp)); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2100 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2101 | } |
| 2102 | return __node_insert_multi(__cp); |
| 2103 | } |
| 2104 | |
Eric Fiselier | de3f2b3 | 2015-06-13 07:18:32 +0000 | [diff] [blame] | 2105 | |
| 2106 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2107 | #ifndef _LIBCPP_CXX03_LANG |
Eric Fiselier | de3f2b3 | 2015-06-13 07:18:32 +0000 | [diff] [blame] | 2108 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2109 | template <class _Key, class ..._Args> |
Eric Fiselier | de3f2b3 | 2015-06-13 07:18:32 +0000 | [diff] [blame] | 2110 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2111 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args) |
Eric Fiselier | de3f2b3 | 2015-06-13 07:18:32 +0000 | [diff] [blame] | 2112 | #else |
| 2113 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2114 | template <class _Key, class _Args> |
Eric Fiselier | de3f2b3 | 2015-06-13 07:18:32 +0000 | [diff] [blame] | 2115 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2116 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args) |
Eric Fiselier | de3f2b3 | 2015-06-13 07:18:32 +0000 | [diff] [blame] | 2117 | #endif |
| 2118 | { |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2119 | |
| 2120 | size_t __hash = hash_function()(__k); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2121 | size_type __bc = bucket_count(); |
| 2122 | bool __inserted = false; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2123 | __next_pointer __nd; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2124 | size_t __chash; |
| 2125 | if (__bc != 0) |
| 2126 | { |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2127 | __chash = __constrain_hash(__hash, __bc); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2128 | __nd = __bucket_list_[__chash]; |
| 2129 | if (__nd != nullptr) |
| 2130 | { |
| 2131 | for (__nd = __nd->__next_; __nd != nullptr && |
Eric Fiselier | 1a06fe5 | 2016-07-24 06:22:25 +0000 | [diff] [blame] | 2132 | (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2133 | __nd = __nd->__next_) |
| 2134 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2135 | if (key_eq()(__nd->__upcast()->__value_, __k)) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2136 | goto __done; |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | { |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2141 | #ifndef _LIBCPP_CXX03_LANG |
| 2142 | __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...); |
| 2143 | #else |
| 2144 | __node_holder __h = __construct_node_hash(__hash, __args); |
| 2145 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2146 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 2147 | { |
Eric Fiselier | 9ba5c11 | 2015-02-02 21:31:48 +0000 | [diff] [blame] | 2148 | rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc), |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2149 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 2150 | __bc = bucket_count(); |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2151 | __chash = __constrain_hash(__hash, __bc); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2152 | } |
| 2153 | // insert_after __bucket_list_[__chash], or __first_node if bucket is null |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2154 | __next_pointer __pn = __bucket_list_[__chash]; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2155 | if (__pn == nullptr) |
| 2156 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2157 | __pn = __p1_.first().__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2158 | __h->__next_ = __pn->__next_; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2159 | __pn->__next_ = __h.get()->__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2160 | // fix up __bucket_list_ |
| 2161 | __bucket_list_[__chash] = __pn; |
| 2162 | if (__h->__next_ != nullptr) |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2163 | __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)] |
| 2164 | = __h.get()->__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2165 | } |
| 2166 | else |
| 2167 | { |
| 2168 | __h->__next_ = __pn->__next_; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2169 | __pn->__next_ = static_cast<__next_pointer>(__h.get()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2170 | } |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2171 | __nd = static_cast<__next_pointer>(__h.release()); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2172 | // increment size |
| 2173 | ++size(); |
| 2174 | __inserted = true; |
| 2175 | } |
| 2176 | __done: |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2177 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2178 | return pair<iterator, bool>(iterator(__nd, this), __inserted); |
| 2179 | #else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2180 | return pair<iterator, bool>(iterator(__nd), __inserted); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2181 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2184 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2185 | |
| 2186 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2187 | template <class... _Args> |
| 2188 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
Duncan P. N. Exon Smith | fde79b4 | 2016-03-17 20:45:20 +0000 | [diff] [blame] | 2189 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2190 | { |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2191 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2192 | pair<iterator, bool> __r = __node_insert_unique(__h.get()); |
| 2193 | if (__r.second) |
| 2194 | __h.release(); |
| 2195 | return __r; |
| 2196 | } |
| 2197 | |
| 2198 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2199 | template <class... _Args> |
| 2200 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2201 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args) |
| 2202 | { |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2203 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2204 | iterator __r = __node_insert_multi(__h.get()); |
| 2205 | __h.release(); |
| 2206 | return __r; |
| 2207 | } |
| 2208 | |
| 2209 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2210 | template <class... _Args> |
| 2211 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2212 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi( |
| 2213 | const_iterator __p, _Args&&... __args) |
| 2214 | { |
Howard Hinnant | e5c13de | 2013-07-29 19:05:47 +0000 | [diff] [blame] | 2215 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2216 | _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, |
| 2217 | "unordered container::emplace_hint(const_iterator, args...) called with an iterator not" |
| 2218 | " referring to this unordered container"); |
| 2219 | #endif |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2220 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2221 | iterator __r = __node_insert_multi(__p, __h.get()); |
| 2222 | __h.release(); |
| 2223 | return __r; |
| 2224 | } |
| 2225 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2226 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2227 | |
| 2228 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2229 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2230 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2231 | { |
| 2232 | __node_holder __h = __construct_node(__x); |
| 2233 | iterator __r = __node_insert_multi(__h.get()); |
| 2234 | __h.release(); |
| 2235 | return __r; |
| 2236 | } |
| 2237 | |
| 2238 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2239 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2240 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p, |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2241 | const __container_value_type& __x) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2242 | { |
Howard Hinnant | e5c13de | 2013-07-29 19:05:47 +0000 | [diff] [blame] | 2243 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2244 | _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, |
| 2245 | "unordered container::insert(const_iterator, lvalue) called with an iterator not" |
| 2246 | " referring to this unordered container"); |
| 2247 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2248 | __node_holder __h = __construct_node(__x); |
| 2249 | iterator __r = __node_insert_multi(__p, __h.get()); |
| 2250 | __h.release(); |
| 2251 | return __r; |
| 2252 | } |
| 2253 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2254 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2255 | |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 2256 | #if _LIBCPP_STD_VER > 14 |
| 2257 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2258 | template <class _NodeHandle, class _InsertReturnType> |
| 2259 | _LIBCPP_INLINE_VISIBILITY |
| 2260 | _InsertReturnType |
| 2261 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique( |
| 2262 | _NodeHandle&& __nh) |
| 2263 | { |
| 2264 | if (__nh.empty()) |
| 2265 | return _InsertReturnType{end(), false, _NodeHandle()}; |
| 2266 | pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_); |
| 2267 | if (__result.second) |
| 2268 | __nh.__release(); |
| 2269 | return _InsertReturnType{__result.first, __result.second, _VSTD::move(__nh)}; |
| 2270 | } |
| 2271 | |
| 2272 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2273 | template <class _NodeHandle> |
| 2274 | _LIBCPP_INLINE_VISIBILITY |
| 2275 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2276 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique( |
| 2277 | const_iterator, _NodeHandle&& __nh) |
| 2278 | { |
| 2279 | if (__nh.empty()) |
| 2280 | return end(); |
| 2281 | pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_); |
| 2282 | if (__result.second) |
| 2283 | __nh.__release(); |
| 2284 | return __result.first; |
| 2285 | } |
| 2286 | |
| 2287 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2288 | template <class _NodeHandle> |
| 2289 | _LIBCPP_INLINE_VISIBILITY |
| 2290 | _NodeHandle |
| 2291 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract( |
| 2292 | key_type const& __key) |
| 2293 | { |
| 2294 | iterator __i = find(__key); |
| 2295 | if (__i == end()) |
| 2296 | return _NodeHandle(); |
| 2297 | return __node_handle_extract<_NodeHandle>(__i); |
| 2298 | } |
| 2299 | |
| 2300 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2301 | template <class _NodeHandle> |
| 2302 | _LIBCPP_INLINE_VISIBILITY |
| 2303 | _NodeHandle |
| 2304 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract( |
| 2305 | const_iterator __p) |
| 2306 | { |
| 2307 | allocator_type __alloc(__node_alloc()); |
| 2308 | return _NodeHandle(remove(__p).release(), __alloc); |
| 2309 | } |
| 2310 | |
| 2311 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 2312 | template <class _Table> |
| 2313 | _LIBCPP_INLINE_VISIBILITY |
| 2314 | void |
| 2315 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_unique( |
| 2316 | _Table& __source) |
| 2317 | { |
| 2318 | static_assert(is_same<__node, typename _Table::__node>::value, ""); |
| 2319 | |
| 2320 | for (typename _Table::iterator __it = __source.begin(); |
| 2321 | __it != __source.end();) |
| 2322 | { |
| 2323 | __node_pointer __src_ptr = __it.__node_->__upcast(); |
| 2324 | size_t __hash = hash_function()(__src_ptr->__value_); |
| 2325 | __next_pointer __existing_node = |
| 2326 | __node_insert_unique_prepare(__hash, __src_ptr->__value_); |
| 2327 | auto __prev_iter = __it++; |
| 2328 | if (__existing_node == nullptr) |
| 2329 | { |
| 2330 | (void)__source.remove(__prev_iter).release(); |
| 2331 | __src_ptr->__hash_ = __hash; |
| 2332 | __node_insert_unique_perform(__src_ptr); |
| 2333 | } |
| 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 2338 | template <class _NodeHandle> |
| 2339 | _LIBCPP_INLINE_VISIBILITY |
| 2340 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2341 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi( |
| 2342 | _NodeHandle&& __nh) |
| 2343 | { |
| 2344 | if (__nh.empty()) |
| 2345 | return end(); |
| 2346 | iterator __result = __node_insert_multi(__nh.__ptr_); |
| 2347 | __nh.__release(); |
| 2348 | return __result; |
| 2349 | } |
| 2350 | |
| 2351 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2352 | template <class _NodeHandle> |
| 2353 | _LIBCPP_INLINE_VISIBILITY |
| 2354 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2355 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi( |
| 2356 | const_iterator __hint, _NodeHandle&& __nh) |
| 2357 | { |
| 2358 | if (__nh.empty()) |
| 2359 | return end(); |
| 2360 | iterator __result = __node_insert_multi(__hint, __nh.__ptr_); |
| 2361 | __nh.__release(); |
| 2362 | return __result; |
| 2363 | } |
| 2364 | |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 2365 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2366 | template <class _Table> |
| 2367 | _LIBCPP_INLINE_VISIBILITY |
| 2368 | void |
| 2369 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_merge_multi( |
| 2370 | _Table& __source) |
| 2371 | { |
| 2372 | static_assert(is_same<typename _Table::__node, __node>::value, ""); |
| 2373 | |
| 2374 | for (typename _Table::iterator __it = __source.begin(); |
| 2375 | __it != __source.end();) |
| 2376 | { |
| 2377 | __node_pointer __src_ptr = __it.__node_->__upcast(); |
| 2378 | size_t __src_hash = hash_function()(__src_ptr->__value_); |
| 2379 | __next_pointer __pn = |
| 2380 | __node_insert_multi_prepare(__src_hash, __src_ptr->__value_); |
| 2381 | (void)__source.remove(__it++).release(); |
| 2382 | __src_ptr->__hash_ = __src_hash; |
| 2383 | __node_insert_multi_perform(__src_ptr, __pn); |
| 2384 | } |
| 2385 | } |
Erik Pilkington | b0386a5 | 2018-08-01 01:33:38 +0000 | [diff] [blame] | 2386 | #endif // _LIBCPP_STD_VER > 14 |
| 2387 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2388 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2389 | void |
| 2390 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n) |
| 2391 | { |
Dan Albert | 553b09b | 2018-01-08 22:57:12 +0000 | [diff] [blame] | 2392 | if (__n == 1) |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2393 | __n = 2; |
| 2394 | else if (__n & (__n - 1)) |
| 2395 | __n = __next_prime(__n); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2396 | size_type __bc = bucket_count(); |
| 2397 | if (__n > __bc) |
| 2398 | __rehash(__n); |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2399 | else if (__n < __bc) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2400 | { |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2401 | __n = _VSTD::max<size_type> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2402 | ( |
| 2403 | __n, |
Eric Fiselier | 9ba5c11 | 2015-02-02 21:31:48 +0000 | [diff] [blame] | 2404 | __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) : |
| 2405 | __next_prime(size_t(ceil(float(size()) / max_load_factor()))) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2406 | ); |
| 2407 | if (__n < __bc) |
| 2408 | __rehash(__n); |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2413 | void |
| 2414 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc) |
| 2415 | { |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2416 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2417 | __get_db()->__invalidate_all(this); |
| 2418 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2419 | __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc(); |
| 2420 | __bucket_list_.reset(__nbc > 0 ? |
| 2421 | __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr); |
| 2422 | __bucket_list_.get_deleter().size() = __nbc; |
| 2423 | if (__nbc > 0) |
| 2424 | { |
| 2425 | for (size_type __i = 0; __i < __nbc; ++__i) |
| 2426 | __bucket_list_[__i] = nullptr; |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2427 | __next_pointer __pp = __p1_.first().__ptr(); |
| 2428 | __next_pointer __cp = __pp->__next_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2429 | if (__cp != nullptr) |
| 2430 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2431 | size_type __chash = __constrain_hash(__cp->__hash(), __nbc); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2432 | __bucket_list_[__chash] = __pp; |
| 2433 | size_type __phash = __chash; |
| 2434 | for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr; |
| 2435 | __cp = __pp->__next_) |
| 2436 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2437 | __chash = __constrain_hash(__cp->__hash(), __nbc); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2438 | if (__chash == __phash) |
| 2439 | __pp = __cp; |
| 2440 | else |
| 2441 | { |
| 2442 | if (__bucket_list_[__chash] == nullptr) |
| 2443 | { |
| 2444 | __bucket_list_[__chash] = __pp; |
| 2445 | __pp = __cp; |
| 2446 | __phash = __chash; |
| 2447 | } |
| 2448 | else |
| 2449 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2450 | __next_pointer __np = __cp; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2451 | for (; __np->__next_ != nullptr && |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2452 | key_eq()(__cp->__upcast()->__value_, |
| 2453 | __np->__next_->__upcast()->__value_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2454 | __np = __np->__next_) |
| 2455 | ; |
| 2456 | __pp->__next_ = __np->__next_; |
| 2457 | __np->__next_ = __bucket_list_[__chash]->__next_; |
| 2458 | __bucket_list_[__chash]->__next_ = __cp; |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2459 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | } |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2468 | template <class _Key> |
| 2469 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2470 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) |
| 2471 | { |
| 2472 | size_t __hash = hash_function()(__k); |
| 2473 | size_type __bc = bucket_count(); |
| 2474 | if (__bc != 0) |
| 2475 | { |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2476 | size_t __chash = __constrain_hash(__hash, __bc); |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2477 | __next_pointer __nd = __bucket_list_[__chash]; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2478 | if (__nd != nullptr) |
| 2479 | { |
| 2480 | for (__nd = __nd->__next_; __nd != nullptr && |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2481 | (__nd->__hash() == __hash |
| 2482 | || __constrain_hash(__nd->__hash(), __bc) == __chash); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2483 | __nd = __nd->__next_) |
| 2484 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2485 | if ((__nd->__hash() == __hash) |
| 2486 | && key_eq()(__nd->__upcast()->__value_, __k)) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2487 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2488 | return iterator(__nd, this); |
| 2489 | #else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2490 | return iterator(__nd); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2491 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2492 | } |
| 2493 | } |
| 2494 | } |
| 2495 | return end(); |
| 2496 | } |
| 2497 | |
| 2498 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2499 | template <class _Key> |
| 2500 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator |
| 2501 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const |
| 2502 | { |
| 2503 | size_t __hash = hash_function()(__k); |
| 2504 | size_type __bc = bucket_count(); |
| 2505 | if (__bc != 0) |
| 2506 | { |
Howard Hinnant | 4cb38a8 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2507 | size_t __chash = __constrain_hash(__hash, __bc); |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2508 | __next_pointer __nd = __bucket_list_[__chash]; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2509 | if (__nd != nullptr) |
| 2510 | { |
| 2511 | for (__nd = __nd->__next_; __nd != nullptr && |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2512 | (__hash == __nd->__hash() |
| 2513 | || __constrain_hash(__nd->__hash(), __bc) == __chash); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2514 | __nd = __nd->__next_) |
| 2515 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2516 | if ((__nd->__hash() == __hash) |
| 2517 | && key_eq()(__nd->__upcast()->__value_, __k)) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2518 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2519 | return const_iterator(__nd, this); |
| 2520 | #else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2521 | return const_iterator(__nd); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2522 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2523 | } |
| 2524 | } |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2525 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2526 | } |
| 2527 | return end(); |
| 2528 | } |
| 2529 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2530 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2531 | |
| 2532 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2533 | template <class ..._Args> |
| 2534 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
| 2535 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args) |
| 2536 | { |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2537 | static_assert(!__is_hash_value_type<_Args...>::value, |
| 2538 | "Construct cannot be called with a hash value type"); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2539 | __node_allocator& __na = __node_alloc(); |
Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2540 | __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2541 | __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2542 | __h.get_deleter().__value_constructed = true; |
| 2543 | __h->__hash_ = hash_function()(__h->__value_); |
| 2544 | __h->__next_ = nullptr; |
| 2545 | return __h; |
| 2546 | } |
| 2547 | |
| 2548 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2549 | template <class _First, class ..._Rest> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2550 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2551 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash( |
| 2552 | size_t __hash, _First&& __f, _Rest&& ...__rest) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2553 | { |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2554 | static_assert(!__is_hash_value_type<_First, _Rest...>::value, |
| 2555 | "Construct cannot be called with a hash value type"); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2556 | __node_allocator& __na = __node_alloc(); |
Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2557 | __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2558 | __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), |
| 2559 | _VSTD::forward<_First>(__f), |
| 2560 | _VSTD::forward<_Rest>(__rest)...); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2561 | __h.get_deleter().__value_constructed = true; |
| 2562 | __h->__hash_ = __hash; |
| 2563 | __h->__next_ = nullptr; |
Howard Hinnant | 179b1f8 | 2013-08-22 18:29:50 +0000 | [diff] [blame] | 2564 | return __h; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2567 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2568 | |
| 2569 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2570 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2571 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2572 | { |
| 2573 | __node_allocator& __na = __node_alloc(); |
Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2574 | __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2575 | __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2576 | __h.get_deleter().__value_constructed = true; |
| 2577 | __h->__hash_ = hash_function()(__h->__value_); |
| 2578 | __h->__next_ = nullptr; |
Dimitry Andric | 251c629 | 2015-08-19 06:43:33 +0000 | [diff] [blame] | 2579 | return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2582 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2583 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2584 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash, |
| 2585 | const __container_value_type& __v) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2586 | { |
| 2587 | __node_allocator& __na = __node_alloc(); |
Howard Hinnant | c003db1 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2588 | __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2589 | __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2590 | __h.get_deleter().__value_constructed = true; |
| 2591 | __h->__hash_ = __hash; |
| 2592 | __h->__next_ = nullptr; |
Dimitry Andric | 251c629 | 2015-08-19 06:43:33 +0000 | [diff] [blame] | 2593 | return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Eric Fiselier | fcd0221 | 2016-02-11 11:59:44 +0000 | [diff] [blame] | 2596 | #endif // _LIBCPP_CXX03_LANG |
| 2597 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2598 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2599 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2600 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p) |
| 2601 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2602 | __next_pointer __np = __p.__node_; |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2603 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2604 | _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, |
| 2605 | "unordered container erase(iterator) called with an iterator not" |
| 2606 | " referring to this container"); |
| 2607 | _LIBCPP_ASSERT(__p != end(), |
| 2608 | "unordered container erase(iterator) called with a non-dereferenceable iterator"); |
| 2609 | iterator __r(__np, this); |
| 2610 | #else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2611 | iterator __r(__np); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2612 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2613 | ++__r; |
| 2614 | remove(__p); |
| 2615 | return __r; |
| 2616 | } |
| 2617 | |
| 2618 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2619 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2620 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first, |
| 2621 | const_iterator __last) |
| 2622 | { |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2623 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2624 | _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this, |
| 2625 | "unodered container::erase(iterator, iterator) called with an iterator not" |
| 2626 | " referring to this unodered container"); |
| 2627 | _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this, |
| 2628 | "unodered container::erase(iterator, iterator) called with an iterator not" |
| 2629 | " referring to this unodered container"); |
| 2630 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2631 | for (const_iterator __p = __first; __first != __last; __p = __first) |
| 2632 | { |
| 2633 | ++__first; |
| 2634 | erase(__p); |
| 2635 | } |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2636 | __next_pointer __np = __last.__node_; |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2637 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2638 | return iterator (__np, this); |
| 2639 | #else |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2640 | return iterator (__np); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2641 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2645 | template <class _Key> |
| 2646 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 2647 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k) |
| 2648 | { |
| 2649 | iterator __i = find(__k); |
| 2650 | if (__i == end()) |
| 2651 | return 0; |
| 2652 | erase(__i); |
| 2653 | return 1; |
| 2654 | } |
| 2655 | |
| 2656 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2657 | template <class _Key> |
| 2658 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 2659 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k) |
| 2660 | { |
| 2661 | size_type __r = 0; |
| 2662 | iterator __i = find(__k); |
| 2663 | if (__i != end()) |
| 2664 | { |
| 2665 | iterator __e = end(); |
| 2666 | do |
| 2667 | { |
| 2668 | erase(__i++); |
| 2669 | ++__r; |
| 2670 | } while (__i != __e && key_eq()(*__i, __k)); |
| 2671 | } |
| 2672 | return __r; |
| 2673 | } |
| 2674 | |
| 2675 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2676 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 2677 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2678 | { |
| 2679 | // current node |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2680 | __next_pointer __cn = __p.__node_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2681 | size_type __bc = bucket_count(); |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2682 | size_t __chash = __constrain_hash(__cn->__hash(), __bc); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2683 | // find previous node |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2684 | __next_pointer __pn = __bucket_list_[__chash]; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2685 | for (; __pn->__next_ != __cn; __pn = __pn->__next_) |
| 2686 | ; |
| 2687 | // Fix up __bucket_list_ |
| 2688 | // if __pn is not in same bucket (before begin is not in same bucket) && |
| 2689 | // if __cn->__next_ is not in same bucket (nullptr is not in same bucket) |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2690 | if (__pn == __p1_.first().__ptr() |
| 2691 | || __constrain_hash(__pn->__hash(), __bc) != __chash) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2692 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2693 | if (__cn->__next_ == nullptr |
| 2694 | || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2695 | __bucket_list_[__chash] = nullptr; |
| 2696 | } |
| 2697 | // if __cn->__next_ is not in same bucket (nullptr is in same bucket) |
| 2698 | if (__cn->__next_ != nullptr) |
| 2699 | { |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2700 | size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2701 | if (__nhash != __chash) |
| 2702 | __bucket_list_[__nhash] = __pn; |
| 2703 | } |
| 2704 | // remove __cn |
| 2705 | __pn->__next_ = __cn->__next_; |
| 2706 | __cn->__next_ = nullptr; |
| 2707 | --size(); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2708 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2709 | __c_node* __c = __get_db()->__find_c_and_lock(this); |
Eric Fiselier | 780b51d | 2016-12-28 05:53:01 +0000 | [diff] [blame] | 2710 | for (__i_node** __dp = __c->end_; __dp != __c->beg_; ) |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2711 | { |
Eric Fiselier | 780b51d | 2016-12-28 05:53:01 +0000 | [diff] [blame] | 2712 | --__dp; |
| 2713 | iterator* __i = static_cast<iterator*>((*__dp)->__i_); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2714 | if (__i->__node_ == __cn) |
| 2715 | { |
Eric Fiselier | 780b51d | 2016-12-28 05:53:01 +0000 | [diff] [blame] | 2716 | (*__dp)->__c_ = nullptr; |
| 2717 | if (--__c->end_ != __dp) |
| 2718 | memmove(__dp, __dp+1, (__c->end_ - __dp)*sizeof(__i_node*)); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | __get_db()->unlock(); |
| 2722 | #endif |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2723 | return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2727 | template <class _Key> |
Evgeniy Stepanov | 906c872 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2728 | inline |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2729 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 2730 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const |
| 2731 | { |
| 2732 | return static_cast<size_type>(find(__k) != end()); |
| 2733 | } |
| 2734 | |
| 2735 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2736 | template <class _Key> |
| 2737 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 2738 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const |
| 2739 | { |
| 2740 | size_type __r = 0; |
| 2741 | const_iterator __i = find(__k); |
| 2742 | if (__i != end()) |
| 2743 | { |
| 2744 | const_iterator __e = end(); |
| 2745 | do |
| 2746 | { |
| 2747 | ++__i; |
| 2748 | ++__r; |
| 2749 | } while (__i != __e && key_eq()(*__i, __k)); |
| 2750 | } |
| 2751 | return __r; |
| 2752 | } |
| 2753 | |
| 2754 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2755 | template <class _Key> |
| 2756 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, |
| 2757 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator> |
| 2758 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique( |
| 2759 | const _Key& __k) |
| 2760 | { |
| 2761 | iterator __i = find(__k); |
| 2762 | iterator __j = __i; |
| 2763 | if (__i != end()) |
| 2764 | ++__j; |
| 2765 | return pair<iterator, iterator>(__i, __j); |
| 2766 | } |
| 2767 | |
| 2768 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2769 | template <class _Key> |
| 2770 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator, |
| 2771 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator> |
| 2772 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique( |
| 2773 | const _Key& __k) const |
| 2774 | { |
| 2775 | const_iterator __i = find(__k); |
| 2776 | const_iterator __j = __i; |
| 2777 | if (__i != end()) |
| 2778 | ++__j; |
| 2779 | return pair<const_iterator, const_iterator>(__i, __j); |
| 2780 | } |
| 2781 | |
| 2782 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2783 | template <class _Key> |
| 2784 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, |
| 2785 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator> |
| 2786 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi( |
| 2787 | const _Key& __k) |
| 2788 | { |
| 2789 | iterator __i = find(__k); |
| 2790 | iterator __j = __i; |
| 2791 | if (__i != end()) |
| 2792 | { |
| 2793 | iterator __e = end(); |
| 2794 | do |
| 2795 | { |
| 2796 | ++__j; |
| 2797 | } while (__j != __e && key_eq()(*__j, __k)); |
| 2798 | } |
| 2799 | return pair<iterator, iterator>(__i, __j); |
| 2800 | } |
| 2801 | |
| 2802 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2803 | template <class _Key> |
| 2804 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator, |
| 2805 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator> |
| 2806 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi( |
| 2807 | const _Key& __k) const |
| 2808 | { |
| 2809 | const_iterator __i = find(__k); |
| 2810 | const_iterator __j = __i; |
| 2811 | if (__i != end()) |
| 2812 | { |
| 2813 | const_iterator __e = end(); |
| 2814 | do |
| 2815 | { |
| 2816 | ++__j; |
| 2817 | } while (__j != __e && key_eq()(*__j, __k)); |
| 2818 | } |
| 2819 | return pair<const_iterator, const_iterator>(__i, __j); |
| 2820 | } |
| 2821 | |
| 2822 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2823 | void |
| 2824 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u) |
Eric Fiselier | 87a8249 | 2015-07-18 20:40:46 +0000 | [diff] [blame] | 2825 | #if _LIBCPP_STD_VER <= 11 |
Eric Fiselier | 780b51d | 2016-12-28 05:53:01 +0000 | [diff] [blame] | 2826 | _NOEXCEPT_DEBUG_( |
Marshall Clow | e3fbe14 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 2827 | __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value |
Marshall Clow | e3fbe14 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 2828 | && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value |
| 2829 | || __is_nothrow_swappable<__pointer_allocator>::value) |
| 2830 | && (!__node_traits::propagate_on_container_swap::value |
| 2831 | || __is_nothrow_swappable<__node_allocator>::value) |
Marshall Clow | e3fbe14 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 2832 | ) |
Eric Fiselier | 87a8249 | 2015-07-18 20:40:46 +0000 | [diff] [blame] | 2833 | #else |
Eric Fiselier | 780b51d | 2016-12-28 05:53:01 +0000 | [diff] [blame] | 2834 | _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value) |
Eric Fiselier | 87a8249 | 2015-07-18 20:40:46 +0000 | [diff] [blame] | 2835 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2836 | { |
Eric Fiselier | 780b51d | 2016-12-28 05:53:01 +0000 | [diff] [blame] | 2837 | _LIBCPP_ASSERT(__node_traits::propagate_on_container_swap::value || |
| 2838 | this->__node_alloc() == __u.__node_alloc(), |
| 2839 | "list::swap: Either propagate_on_container_swap must be true" |
| 2840 | " or the allocators must compare equal"); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2841 | { |
| 2842 | __node_pointer_pointer __npp = __bucket_list_.release(); |
| 2843 | __bucket_list_.reset(__u.__bucket_list_.release()); |
| 2844 | __u.__bucket_list_.reset(__npp); |
| 2845 | } |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2846 | _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size()); |
Marshall Clow | e3fbe14 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 2847 | __swap_allocator(__bucket_list_.get_deleter().__alloc(), |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2848 | __u.__bucket_list_.get_deleter().__alloc()); |
Marshall Clow | e3fbe14 | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 2849 | __swap_allocator(__node_alloc(), __u.__node_alloc()); |
Howard Hinnant | ce48a11 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2850 | _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2851 | __p2_.swap(__u.__p2_); |
| 2852 | __p3_.swap(__u.__p3_); |
| 2853 | if (size() > 0) |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2854 | __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] = |
| 2855 | __p1_.first().__ptr(); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2856 | if (__u.size() > 0) |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2857 | __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] = |
| 2858 | __u.__p1_.first().__ptr(); |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2859 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2860 | __get_db()->swap(this, &__u); |
| 2861 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2865 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 2866 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const |
| 2867 | { |
Howard Hinnant | e5c13de | 2013-07-29 19:05:47 +0000 | [diff] [blame] | 2868 | _LIBCPP_ASSERT(__n < bucket_count(), |
| 2869 | "unordered container::bucket_size(n) called with n >= bucket_count()"); |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2870 | __next_pointer __np = __bucket_list_[__n]; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2871 | size_type __bc = bucket_count(); |
| 2872 | size_type __r = 0; |
| 2873 | if (__np != nullptr) |
| 2874 | { |
| 2875 | for (__np = __np->__next_; __np != nullptr && |
Eric Fiselier | 40492ba | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 2876 | __constrain_hash(__np->__hash(), __bc) == __n; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2877 | __np = __np->__next_, ++__r) |
| 2878 | ; |
| 2879 | } |
| 2880 | return __r; |
| 2881 | } |
| 2882 | |
Howard Hinnant | 3714107 | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 2883 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2884 | inline _LIBCPP_INLINE_VISIBILITY |
| 2885 | void |
| 2886 | swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x, |
| 2887 | __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y) |
| 2888 | _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) |
| 2889 | { |
| 2890 | __x.swap(__y); |
| 2891 | } |
| 2892 | |
Howard Hinnant | b24c802 | 2013-07-23 22:01:58 +0000 | [diff] [blame] | 2893 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2894 | |
| 2895 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2896 | bool |
| 2897 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const |
| 2898 | { |
| 2899 | return __i->__node_ != nullptr; |
| 2900 | } |
| 2901 | |
| 2902 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2903 | bool |
| 2904 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const |
| 2905 | { |
| 2906 | return false; |
| 2907 | } |
| 2908 | |
| 2909 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2910 | bool |
| 2911 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const |
| 2912 | { |
| 2913 | return false; |
| 2914 | } |
| 2915 | |
| 2916 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2917 | bool |
| 2918 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const |
| 2919 | { |
| 2920 | return false; |
| 2921 | } |
| 2922 | |
| 2923 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 |
Eric Fiselier | a016efb | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 2924 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2925 | _LIBCPP_END_NAMESPACE_STD |
| 2926 | |
Eric Fiselier | a016efb | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 2927 | _LIBCPP_POP_MACROS |
| 2928 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2929 | #endif // _LIBCPP__HASH_TABLE |