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