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