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