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