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 | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 956 | {return __constrain_hash(hash_function()(__k), bucket_count());} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 957 | |
| 958 | template <class _Key> |
| 959 | iterator find(const _Key& __x); |
| 960 | template <class _Key> |
| 961 | const_iterator find(const _Key& __x) const; |
| 962 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 963 | typedef __hash_node_destructor<__node_allocator> _Dp; |
| 964 | typedef unique_ptr<__node, _Dp> __node_holder; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 965 | |
| 966 | iterator erase(const_iterator __p); |
| 967 | iterator erase(const_iterator __first, const_iterator __last); |
| 968 | template <class _Key> |
| 969 | size_type __erase_unique(const _Key& __k); |
| 970 | template <class _Key> |
| 971 | size_type __erase_multi(const _Key& __k); |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 972 | __node_holder remove(const_iterator __p) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 973 | |
| 974 | template <class _Key> |
| 975 | size_type __count_unique(const _Key& __k) const; |
| 976 | template <class _Key> |
| 977 | size_type __count_multi(const _Key& __k) const; |
| 978 | |
| 979 | template <class _Key> |
| 980 | pair<iterator, iterator> |
| 981 | __equal_range_unique(const _Key& __k); |
| 982 | template <class _Key> |
| 983 | pair<const_iterator, const_iterator> |
| 984 | __equal_range_unique(const _Key& __k) const; |
| 985 | |
| 986 | template <class _Key> |
| 987 | pair<iterator, iterator> |
| 988 | __equal_range_multi(const _Key& __k); |
| 989 | template <class _Key> |
| 990 | pair<const_iterator, const_iterator> |
| 991 | __equal_range_multi(const _Key& __k) const; |
| 992 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 993 | void swap(__hash_table& __u) |
| 994 | _NOEXCEPT_( |
| 995 | (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value || |
| 996 | __is_nothrow_swappable<__pointer_allocator>::value) && |
| 997 | (!__node_traits::propagate_on_container_swap::value || |
| 998 | __is_nothrow_swappable<__node_allocator>::value) && |
| 999 | __is_nothrow_swappable<hasher>::value && |
| 1000 | __is_nothrow_swappable<key_equal>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1001 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1002 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1003 | size_type max_bucket_count() const _NOEXCEPT |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1004 | {return __pointer_alloc_traits::max_size(__bucket_list_.get_deleter().__alloc());} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1005 | size_type bucket_size(size_type __n) const; |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1006 | _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1007 | { |
| 1008 | size_type __bc = bucket_count(); |
| 1009 | return __bc != 0 ? (float)size() / __bc : 0.f; |
| 1010 | } |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1011 | _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1012 | {max_load_factor() = _VSTD::max(__mlf, load_factor());} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1013 | |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1014 | _LIBCPP_INLINE_VISIBILITY |
| 1015 | local_iterator |
| 1016 | begin(size_type __n) |
| 1017 | { |
| 1018 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1019 | return local_iterator(__bucket_list_[__n], __n, bucket_count(), this); |
| 1020 | #else |
| 1021 | return local_iterator(__bucket_list_[__n], __n, bucket_count()); |
| 1022 | #endif |
| 1023 | } |
| 1024 | |
| 1025 | _LIBCPP_INLINE_VISIBILITY |
| 1026 | local_iterator |
| 1027 | end(size_type __n) |
| 1028 | { |
| 1029 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1030 | return local_iterator(nullptr, __n, bucket_count(), this); |
| 1031 | #else |
| 1032 | return local_iterator(nullptr, __n, bucket_count()); |
| 1033 | #endif |
| 1034 | } |
| 1035 | |
| 1036 | _LIBCPP_INLINE_VISIBILITY |
| 1037 | const_local_iterator |
| 1038 | cbegin(size_type __n) const |
| 1039 | { |
| 1040 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1041 | return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this); |
| 1042 | #else |
| 1043 | return const_local_iterator(__bucket_list_[__n], __n, bucket_count()); |
| 1044 | #endif |
| 1045 | } |
| 1046 | |
| 1047 | _LIBCPP_INLINE_VISIBILITY |
| 1048 | const_local_iterator |
| 1049 | cend(size_type __n) const |
| 1050 | { |
| 1051 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1052 | return const_local_iterator(nullptr, __n, bucket_count(), this); |
| 1053 | #else |
| 1054 | return const_local_iterator(nullptr, __n, bucket_count()); |
| 1055 | #endif |
| 1056 | } |
| 1057 | |
| 1058 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1059 | |
| 1060 | bool __dereferenceable(const const_iterator* __i) const; |
| 1061 | bool __decrementable(const const_iterator* __i) const; |
| 1062 | bool __addable(const const_iterator* __i, ptrdiff_t __n) const; |
| 1063 | bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const; |
| 1064 | |
| 1065 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 |
| 1066 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1067 | private: |
| 1068 | void __rehash(size_type __n); |
| 1069 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1070 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1071 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1072 | template <class ..._Args> |
| 1073 | __node_holder __construct_node(_Args&& ...__args); |
Howard Hinnant | bfd5530 | 2010-09-04 23:46:48 +0000 | [diff] [blame] | 1074 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1075 | __node_holder __construct_node(value_type&& __v, size_t __hash); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1076 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1077 | __node_holder __construct_node(const value_type& __v); |
| 1078 | #endif |
| 1079 | __node_holder __construct_node(const value_type& __v, size_t __hash); |
| 1080 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1081 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1082 | void __copy_assign_alloc(const __hash_table& __u) |
| 1083 | {__copy_assign_alloc(__u, integral_constant<bool, |
| 1084 | __node_traits::propagate_on_container_copy_assignment::value>());} |
| 1085 | void __copy_assign_alloc(const __hash_table& __u, true_type); |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1086 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1087 | void __copy_assign_alloc(const __hash_table&, false_type) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1088 | |
| 1089 | void __move_assign(__hash_table& __u, false_type); |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1090 | void __move_assign(__hash_table& __u, true_type) |
| 1091 | _NOEXCEPT_( |
| 1092 | is_nothrow_move_assignable<__node_allocator>::value && |
| 1093 | is_nothrow_move_assignable<hasher>::value && |
| 1094 | is_nothrow_move_assignable<key_equal>::value); |
| 1095 | _LIBCPP_INLINE_VISIBILITY |
| 1096 | void __move_assign_alloc(__hash_table& __u) |
| 1097 | _NOEXCEPT_( |
| 1098 | !__node_traits::propagate_on_container_move_assignment::value || |
| 1099 | (is_nothrow_move_assignable<__pointer_allocator>::value && |
| 1100 | is_nothrow_move_assignable<__node_allocator>::value)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1101 | {__move_assign_alloc(__u, integral_constant<bool, |
| 1102 | __node_traits::propagate_on_container_move_assignment::value>());} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1103 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1104 | void __move_assign_alloc(__hash_table& __u, true_type) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1105 | _NOEXCEPT_( |
| 1106 | is_nothrow_move_assignable<__pointer_allocator>::value && |
| 1107 | is_nothrow_move_assignable<__node_allocator>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1108 | { |
| 1109 | __bucket_list_.get_deleter().__alloc() = |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1110 | _VSTD::move(__u.__bucket_list_.get_deleter().__alloc()); |
| 1111 | __node_alloc() = _VSTD::move(__u.__node_alloc()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1112 | } |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1113 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1114 | void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1115 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1116 | template <class _Ap> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1117 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1118 | static |
| 1119 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1120 | __swap_alloc(_Ap& __x, _Ap& __y) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1121 | _NOEXCEPT_( |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1122 | !allocator_traits<_Ap>::propagate_on_container_swap::value || |
| 1123 | __is_nothrow_swappable<_Ap>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1124 | { |
| 1125 | __swap_alloc(__x, __y, |
| 1126 | integral_constant<bool, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1127 | allocator_traits<_Ap>::propagate_on_container_swap::value |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1128 | >()); |
| 1129 | } |
| 1130 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1131 | template <class _Ap> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1132 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1133 | static |
| 1134 | void |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1135 | __swap_alloc(_Ap& __x, _Ap& __y, true_type) |
| 1136 | _NOEXCEPT_(__is_nothrow_swappable<_Ap>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1137 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1138 | using _VSTD::swap; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1139 | swap(__x, __y); |
| 1140 | } |
| 1141 | |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1142 | template <class _Ap> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1143 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1144 | static |
| 1145 | void |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 1146 | __swap_alloc(_Ap&, _Ap&, false_type) _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1147 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1148 | void __deallocate(__node_pointer __np) _NOEXCEPT; |
| 1149 | __node_pointer __detach() _NOEXCEPT; |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1150 | |
| 1151 | template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS unordered_map; |
| 1152 | 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] | 1153 | }; |
| 1154 | |
| 1155 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1156 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1157 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table() |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1158 | _NOEXCEPT_( |
| 1159 | is_nothrow_default_constructible<__bucket_list>::value && |
| 1160 | is_nothrow_default_constructible<__first_node>::value && |
| 1161 | is_nothrow_default_constructible<hasher>::value && |
| 1162 | is_nothrow_default_constructible<key_equal>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1163 | : __p2_(0), |
| 1164 | __p3_(1.0f) |
| 1165 | { |
| 1166 | } |
| 1167 | |
| 1168 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1169 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1170 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf, |
| 1171 | const key_equal& __eql) |
| 1172 | : __bucket_list_(nullptr, __bucket_list_deleter()), |
| 1173 | __p1_(), |
| 1174 | __p2_(0, __hf), |
| 1175 | __p3_(1.0f, __eql) |
| 1176 | { |
| 1177 | } |
| 1178 | |
| 1179 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1180 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf, |
| 1181 | const key_equal& __eql, |
| 1182 | const allocator_type& __a) |
| 1183 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
| 1184 | __p1_(__node_allocator(__a)), |
| 1185 | __p2_(0, __hf), |
| 1186 | __p3_(1.0f, __eql) |
| 1187 | { |
| 1188 | } |
| 1189 | |
| 1190 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1191 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a) |
| 1192 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
| 1193 | __p1_(__node_allocator(__a)), |
| 1194 | __p2_(0), |
| 1195 | __p3_(1.0f) |
| 1196 | { |
| 1197 | } |
| 1198 | |
| 1199 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1200 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u) |
| 1201 | : __bucket_list_(nullptr, |
| 1202 | __bucket_list_deleter(allocator_traits<__pointer_allocator>:: |
| 1203 | select_on_container_copy_construction( |
| 1204 | __u.__bucket_list_.get_deleter().__alloc()), 0)), |
| 1205 | __p1_(allocator_traits<__node_allocator>:: |
| 1206 | select_on_container_copy_construction(__u.__node_alloc())), |
| 1207 | __p2_(0, __u.hash_function()), |
| 1208 | __p3_(__u.__p3_) |
| 1209 | { |
| 1210 | } |
| 1211 | |
| 1212 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1213 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u, |
| 1214 | const allocator_type& __a) |
| 1215 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
| 1216 | __p1_(__node_allocator(__a)), |
| 1217 | __p2_(0, __u.hash_function()), |
| 1218 | __p3_(__u.__p3_) |
| 1219 | { |
| 1220 | } |
| 1221 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1222 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1223 | |
| 1224 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1225 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1226 | _NOEXCEPT_( |
| 1227 | is_nothrow_move_constructible<__bucket_list>::value && |
| 1228 | is_nothrow_move_constructible<__first_node>::value && |
| 1229 | is_nothrow_move_constructible<hasher>::value && |
| 1230 | is_nothrow_move_constructible<key_equal>::value) |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1231 | : __bucket_list_(_VSTD::move(__u.__bucket_list_)), |
| 1232 | __p1_(_VSTD::move(__u.__p1_)), |
| 1233 | __p2_(_VSTD::move(__u.__p2_)), |
| 1234 | __p3_(_VSTD::move(__u.__p3_)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1235 | { |
| 1236 | if (size() > 0) |
| 1237 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1238 | __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] = |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1239 | 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] | 1240 | __u.__p1_.first().__next_ = nullptr; |
| 1241 | __u.size() = 0; |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1246 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u, |
| 1247 | const allocator_type& __a) |
| 1248 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
| 1249 | __p1_(__node_allocator(__a)), |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1250 | __p2_(0, _VSTD::move(__u.hash_function())), |
| 1251 | __p3_(_VSTD::move(__u.__p3_)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1252 | { |
| 1253 | if (__a == allocator_type(__u.__node_alloc())) |
| 1254 | { |
| 1255 | __bucket_list_.reset(__u.__bucket_list_.release()); |
| 1256 | __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size(); |
| 1257 | __u.__bucket_list_.get_deleter().size() = 0; |
| 1258 | if (__u.size() > 0) |
| 1259 | { |
| 1260 | __p1_.first().__next_ = __u.__p1_.first().__next_; |
| 1261 | __u.__p1_.first().__next_ = nullptr; |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1262 | __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] = |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1263 | 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] | 1264 | size() = __u.size(); |
| 1265 | __u.size() = 0; |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1270 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1271 | |
| 1272 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1273 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table() |
| 1274 | { |
| 1275 | __deallocate(__p1_.first().__next_); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1276 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1277 | __get_db()->__erase_c(this); |
| 1278 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1282 | void |
| 1283 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc( |
| 1284 | const __hash_table& __u, true_type) |
| 1285 | { |
| 1286 | if (__node_alloc() != __u.__node_alloc()) |
| 1287 | { |
| 1288 | clear(); |
| 1289 | __bucket_list_.reset(); |
| 1290 | __bucket_list_.get_deleter().size() = 0; |
| 1291 | } |
| 1292 | __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc(); |
| 1293 | __node_alloc() = __u.__node_alloc(); |
| 1294 | } |
| 1295 | |
| 1296 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1297 | __hash_table<_Tp, _Hash, _Equal, _Alloc>& |
| 1298 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u) |
| 1299 | { |
| 1300 | if (this != &__u) |
| 1301 | { |
| 1302 | __copy_assign_alloc(__u); |
| 1303 | hash_function() = __u.hash_function(); |
| 1304 | key_eq() = __u.key_eq(); |
| 1305 | max_load_factor() = __u.max_load_factor(); |
| 1306 | __assign_multi(__u.begin(), __u.end()); |
| 1307 | } |
| 1308 | return *this; |
| 1309 | } |
| 1310 | |
| 1311 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1312 | void |
| 1313 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1314 | _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1315 | { |
| 1316 | __node_allocator& __na = __node_alloc(); |
| 1317 | while (__np != nullptr) |
| 1318 | { |
| 1319 | __node_pointer __next = __np->__next_; |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1320 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1321 | __c_node* __c = __get_db()->__find_c_and_lock(this); |
| 1322 | for (__i_node** __p = __c->end_; __p != __c->beg_; ) |
| 1323 | { |
| 1324 | --__p; |
| 1325 | iterator* __i = static_cast<iterator*>((*__p)->__i_); |
| 1326 | if (__i->__node_ == __np) |
| 1327 | { |
| 1328 | (*__p)->__c_ = nullptr; |
| 1329 | if (--__c->end_ != __p) |
| 1330 | memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); |
| 1331 | } |
| 1332 | } |
| 1333 | __get_db()->unlock(); |
| 1334 | #endif |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1335 | __node_traits::destroy(__na, _VSTD::addressof(__np->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1336 | __node_traits::deallocate(__na, __np, 1); |
| 1337 | __np = __next; |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1342 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1343 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1344 | { |
| 1345 | size_type __bc = bucket_count(); |
| 1346 | for (size_type __i = 0; __i < __bc; ++__i) |
| 1347 | __bucket_list_[__i] = nullptr; |
| 1348 | size() = 0; |
| 1349 | __node_pointer __cache = __p1_.first().__next_; |
| 1350 | __p1_.first().__next_ = nullptr; |
| 1351 | return __cache; |
| 1352 | } |
| 1353 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1354 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1355 | |
| 1356 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1357 | void |
| 1358 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( |
| 1359 | __hash_table& __u, true_type) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1360 | _NOEXCEPT_( |
| 1361 | is_nothrow_move_assignable<__node_allocator>::value && |
| 1362 | is_nothrow_move_assignable<hasher>::value && |
| 1363 | is_nothrow_move_assignable<key_equal>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1364 | { |
| 1365 | clear(); |
| 1366 | __bucket_list_.reset(__u.__bucket_list_.release()); |
| 1367 | __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size(); |
| 1368 | __u.__bucket_list_.get_deleter().size() = 0; |
| 1369 | __move_assign_alloc(__u); |
| 1370 | size() = __u.size(); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1371 | hash_function() = _VSTD::move(__u.hash_function()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1372 | max_load_factor() = __u.max_load_factor(); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1373 | key_eq() = _VSTD::move(__u.key_eq()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1374 | __p1_.first().__next_ = __u.__p1_.first().__next_; |
| 1375 | if (size() > 0) |
| 1376 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1377 | __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] = |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1378 | 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] | 1379 | __u.__p1_.first().__next_ = nullptr; |
| 1380 | __u.size() = 0; |
| 1381 | } |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1382 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1383 | __get_db()->swap(this, &__u); |
| 1384 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1388 | void |
| 1389 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( |
| 1390 | __hash_table& __u, false_type) |
| 1391 | { |
| 1392 | if (__node_alloc() == __u.__node_alloc()) |
| 1393 | __move_assign(__u, true_type()); |
| 1394 | else |
| 1395 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1396 | hash_function() = _VSTD::move(__u.hash_function()); |
| 1397 | key_eq() = _VSTD::move(__u.key_eq()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1398 | max_load_factor() = __u.max_load_factor(); |
| 1399 | if (bucket_count() != 0) |
| 1400 | { |
| 1401 | __node_pointer __cache = __detach(); |
| 1402 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1403 | try |
| 1404 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1405 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1406 | const_iterator __i = __u.begin(); |
| 1407 | while (__cache != nullptr && __u.size() != 0) |
| 1408 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1409 | __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1410 | __node_pointer __next = __cache->__next_; |
| 1411 | __node_insert_multi(__cache); |
| 1412 | __cache = __next; |
| 1413 | } |
| 1414 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1415 | } |
| 1416 | catch (...) |
| 1417 | { |
| 1418 | __deallocate(__cache); |
| 1419 | throw; |
| 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 | __deallocate(__cache); |
| 1423 | } |
| 1424 | const_iterator __i = __u.begin(); |
| 1425 | while (__u.size() != 0) |
| 1426 | { |
| 1427 | __node_holder __h = |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1428 | __construct_node(_VSTD::move(__u.remove(__i++)->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1429 | __node_insert_multi(__h.get()); |
| 1430 | __h.release(); |
| 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1436 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1437 | __hash_table<_Tp, _Hash, _Equal, _Alloc>& |
| 1438 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1439 | _NOEXCEPT_( |
| 1440 | __node_traits::propagate_on_container_move_assignment::value && |
| 1441 | is_nothrow_move_assignable<__node_allocator>::value && |
| 1442 | is_nothrow_move_assignable<hasher>::value && |
| 1443 | is_nothrow_move_assignable<key_equal>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1444 | { |
| 1445 | __move_assign(__u, integral_constant<bool, |
| 1446 | __node_traits::propagate_on_container_move_assignment::value>()); |
| 1447 | return *this; |
| 1448 | } |
| 1449 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1450 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1451 | |
| 1452 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1453 | template <class _InputIterator> |
| 1454 | void |
| 1455 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first, |
| 1456 | _InputIterator __last) |
| 1457 | { |
| 1458 | if (bucket_count() != 0) |
| 1459 | { |
| 1460 | __node_pointer __cache = __detach(); |
| 1461 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1462 | try |
| 1463 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1464 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1465 | for (; __cache != nullptr && __first != __last; ++__first) |
| 1466 | { |
| 1467 | __cache->__value_ = *__first; |
| 1468 | __node_pointer __next = __cache->__next_; |
| 1469 | __node_insert_unique(__cache); |
| 1470 | __cache = __next; |
| 1471 | } |
| 1472 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1473 | } |
| 1474 | catch (...) |
| 1475 | { |
| 1476 | __deallocate(__cache); |
| 1477 | throw; |
| 1478 | } |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1479 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1480 | __deallocate(__cache); |
| 1481 | } |
| 1482 | for (; __first != __last; ++__first) |
| 1483 | __insert_unique(*__first); |
| 1484 | } |
| 1485 | |
| 1486 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1487 | template <class _InputIterator> |
| 1488 | void |
| 1489 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first, |
| 1490 | _InputIterator __last) |
| 1491 | { |
| 1492 | if (bucket_count() != 0) |
| 1493 | { |
| 1494 | __node_pointer __cache = __detach(); |
| 1495 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1496 | try |
| 1497 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1498 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1499 | for (; __cache != nullptr && __first != __last; ++__first) |
| 1500 | { |
| 1501 | __cache->__value_ = *__first; |
| 1502 | __node_pointer __next = __cache->__next_; |
| 1503 | __node_insert_multi(__cache); |
| 1504 | __cache = __next; |
| 1505 | } |
| 1506 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1507 | } |
| 1508 | catch (...) |
| 1509 | { |
| 1510 | __deallocate(__cache); |
| 1511 | throw; |
| 1512 | } |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1513 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1514 | __deallocate(__cache); |
| 1515 | } |
| 1516 | for (; __first != __last; ++__first) |
| 1517 | __insert_multi(*__first); |
| 1518 | } |
| 1519 | |
| 1520 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1521 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1522 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1523 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1524 | { |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1525 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1526 | return iterator(__p1_.first().__next_, this); |
| 1527 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1528 | return iterator(__p1_.first().__next_); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1529 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1533 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1534 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1535 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1536 | { |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1537 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1538 | return iterator(nullptr, this); |
| 1539 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1540 | return iterator(nullptr); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1541 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1545 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1546 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1547 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1548 | { |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1549 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1550 | return const_iterator(__p1_.first().__next_, this); |
| 1551 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1552 | return const_iterator(__p1_.first().__next_); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1553 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1557 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1558 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1559 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1560 | { |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1561 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1562 | return const_iterator(nullptr, this); |
| 1563 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1564 | return const_iterator(nullptr); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1565 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1569 | void |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 1570 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1571 | { |
| 1572 | if (size() > 0) |
| 1573 | { |
| 1574 | __deallocate(__p1_.first().__next_); |
| 1575 | __p1_.first().__next_ = nullptr; |
| 1576 | size_type __bc = bucket_count(); |
Howard Hinnant | 9f66bff | 2011-07-05 14:14:17 +0000 | [diff] [blame] | 1577 | for (size_type __i = 0; __i < __bc; ++__i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1578 | __bucket_list_[__i] = nullptr; |
| 1579 | size() = 0; |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1584 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
| 1585 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd) |
| 1586 | { |
| 1587 | __nd->__hash_ = hash_function()(__nd->__value_); |
| 1588 | size_type __bc = bucket_count(); |
| 1589 | bool __inserted = false; |
| 1590 | __node_pointer __ndptr; |
| 1591 | size_t __chash; |
| 1592 | if (__bc != 0) |
| 1593 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1594 | __chash = __constrain_hash(__nd->__hash_, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1595 | __ndptr = __bucket_list_[__chash]; |
| 1596 | if (__ndptr != nullptr) |
| 1597 | { |
| 1598 | for (__ndptr = __ndptr->__next_; __ndptr != nullptr && |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1599 | __constrain_hash(__ndptr->__hash_, __bc) == __chash; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1600 | __ndptr = __ndptr->__next_) |
| 1601 | { |
| 1602 | if (key_eq()(__ndptr->__value_, __nd->__value_)) |
| 1603 | goto __done; |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | { |
| 1608 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 1609 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1610 | rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1611 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 1612 | __bc = bucket_count(); |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1613 | __chash = __constrain_hash(__nd->__hash_, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1614 | } |
| 1615 | // insert_after __bucket_list_[__chash], or __first_node if bucket is null |
| 1616 | __node_pointer __pn = __bucket_list_[__chash]; |
| 1617 | if (__pn == nullptr) |
| 1618 | { |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1619 | __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] | 1620 | __nd->__next_ = __pn->__next_; |
| 1621 | __pn->__next_ = __nd; |
| 1622 | // fix up __bucket_list_ |
| 1623 | __bucket_list_[__chash] = __pn; |
| 1624 | if (__nd->__next_ != nullptr) |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1625 | __bucket_list_[__constrain_hash(__nd->__next_->__hash_, __bc)] = __nd; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1626 | } |
| 1627 | else |
| 1628 | { |
| 1629 | __nd->__next_ = __pn->__next_; |
| 1630 | __pn->__next_ = __nd; |
| 1631 | } |
| 1632 | __ndptr = __nd; |
| 1633 | // increment size |
| 1634 | ++size(); |
| 1635 | __inserted = true; |
| 1636 | } |
| 1637 | __done: |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1638 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1639 | return pair<iterator, bool>(iterator(__ndptr, this), __inserted); |
| 1640 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1641 | return pair<iterator, bool>(iterator(__ndptr), __inserted); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1642 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1646 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1647 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp) |
| 1648 | { |
| 1649 | __cp->__hash_ = hash_function()(__cp->__value_); |
| 1650 | size_type __bc = bucket_count(); |
| 1651 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 1652 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1653 | rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1654 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 1655 | __bc = bucket_count(); |
| 1656 | } |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1657 | size_t __chash = __constrain_hash(__cp->__hash_, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1658 | __node_pointer __pn = __bucket_list_[__chash]; |
| 1659 | if (__pn == nullptr) |
| 1660 | { |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1661 | __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] | 1662 | __cp->__next_ = __pn->__next_; |
| 1663 | __pn->__next_ = __cp; |
| 1664 | // fix up __bucket_list_ |
| 1665 | __bucket_list_[__chash] = __pn; |
| 1666 | if (__cp->__next_ != nullptr) |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1667 | __bucket_list_[__constrain_hash(__cp->__next_->__hash_, __bc)] = __cp; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1668 | } |
| 1669 | else |
| 1670 | { |
| 1671 | for (bool __found = false; __pn->__next_ != nullptr && |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1672 | __constrain_hash(__pn->__next_->__hash_, __bc) == __chash; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1673 | __pn = __pn->__next_) |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1674 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1675 | // __found key_eq() action |
| 1676 | // false false loop |
| 1677 | // true true loop |
| 1678 | // false true set __found to true |
| 1679 | // true false break |
| 1680 | if (__found != (__pn->__next_->__hash_ == __cp->__hash_ && |
| 1681 | key_eq()(__pn->__next_->__value_, __cp->__value_))) |
| 1682 | { |
| 1683 | if (!__found) |
| 1684 | __found = true; |
| 1685 | else |
| 1686 | break; |
| 1687 | } |
| 1688 | } |
| 1689 | __cp->__next_ = __pn->__next_; |
| 1690 | __pn->__next_ = __cp; |
| 1691 | if (__cp->__next_ != nullptr) |
| 1692 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1693 | size_t __nhash = __constrain_hash(__cp->__next_->__hash_, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1694 | if (__nhash != __chash) |
| 1695 | __bucket_list_[__nhash] = __cp; |
| 1696 | } |
| 1697 | } |
| 1698 | ++size(); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1699 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1700 | return iterator(__cp, this); |
| 1701 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1702 | return iterator(__cp); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1703 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1707 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1708 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi( |
| 1709 | const_iterator __p, __node_pointer __cp) |
| 1710 | { |
| 1711 | if (__p != end() && key_eq()(*__p, __cp->__value_)) |
| 1712 | { |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1713 | __node_pointer __np = __p.__node_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1714 | __cp->__hash_ = __np->__hash_; |
| 1715 | size_type __bc = bucket_count(); |
| 1716 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 1717 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1718 | rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1719 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 1720 | __bc = bucket_count(); |
| 1721 | } |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1722 | size_t __chash = __constrain_hash(__cp->__hash_, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1723 | __node_pointer __pp = __bucket_list_[__chash]; |
| 1724 | while (__pp->__next_ != __np) |
| 1725 | __pp = __pp->__next_; |
| 1726 | __cp->__next_ = __np; |
| 1727 | __pp->__next_ = __cp; |
| 1728 | ++size(); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1729 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1730 | return iterator(__cp, this); |
| 1731 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1732 | return iterator(__cp); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1733 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1734 | } |
| 1735 | return __node_insert_multi(__cp); |
| 1736 | } |
| 1737 | |
| 1738 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1739 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
| 1740 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(const value_type& __x) |
| 1741 | { |
| 1742 | size_t __hash = hash_function()(__x); |
| 1743 | size_type __bc = bucket_count(); |
| 1744 | bool __inserted = false; |
| 1745 | __node_pointer __nd; |
| 1746 | size_t __chash; |
| 1747 | if (__bc != 0) |
| 1748 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1749 | __chash = __constrain_hash(__hash, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1750 | __nd = __bucket_list_[__chash]; |
| 1751 | if (__nd != nullptr) |
| 1752 | { |
| 1753 | for (__nd = __nd->__next_; __nd != nullptr && |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1754 | __constrain_hash(__nd->__hash_, __bc) == __chash; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1755 | __nd = __nd->__next_) |
| 1756 | { |
| 1757 | if (key_eq()(__nd->__value_, __x)) |
| 1758 | goto __done; |
| 1759 | } |
| 1760 | } |
| 1761 | } |
| 1762 | { |
| 1763 | __node_holder __h = __construct_node(__x, __hash); |
| 1764 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 1765 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1766 | rehash(_VSTD::max<size_type>(2 * __bc + !__is_power2(__bc), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1767 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 1768 | __bc = bucket_count(); |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1769 | __chash = __constrain_hash(__hash, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1770 | } |
| 1771 | // insert_after __bucket_list_[__chash], or __first_node if bucket is null |
| 1772 | __node_pointer __pn = __bucket_list_[__chash]; |
| 1773 | if (__pn == nullptr) |
| 1774 | { |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1775 | __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] | 1776 | __h->__next_ = __pn->__next_; |
| 1777 | __pn->__next_ = __h.get(); |
| 1778 | // fix up __bucket_list_ |
| 1779 | __bucket_list_[__chash] = __pn; |
| 1780 | if (__h->__next_ != nullptr) |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1781 | __bucket_list_[__constrain_hash(__h->__next_->__hash_, __bc)] = __h.get(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1782 | } |
| 1783 | else |
| 1784 | { |
| 1785 | __h->__next_ = __pn->__next_; |
| 1786 | __pn->__next_ = __h.get(); |
| 1787 | } |
| 1788 | __nd = __h.release(); |
| 1789 | // increment size |
| 1790 | ++size(); |
| 1791 | __inserted = true; |
| 1792 | } |
| 1793 | __done: |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1794 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1795 | return pair<iterator, bool>(iterator(__nd, this), __inserted); |
| 1796 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1797 | return pair<iterator, bool>(iterator(__nd), __inserted); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1798 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1801 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1802 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1803 | |
| 1804 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1805 | template <class... _Args> |
| 1806 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
| 1807 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique(_Args&&... __args) |
| 1808 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1809 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1810 | pair<iterator, bool> __r = __node_insert_unique(__h.get()); |
| 1811 | if (__r.second) |
| 1812 | __h.release(); |
| 1813 | return __r; |
| 1814 | } |
| 1815 | |
| 1816 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1817 | template <class... _Args> |
| 1818 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1819 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args) |
| 1820 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1821 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1822 | iterator __r = __node_insert_multi(__h.get()); |
| 1823 | __h.release(); |
| 1824 | return __r; |
| 1825 | } |
| 1826 | |
| 1827 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1828 | template <class... _Args> |
| 1829 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1830 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi( |
| 1831 | const_iterator __p, _Args&&... __args) |
| 1832 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1833 | __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1834 | iterator __r = __node_insert_multi(__p, __h.get()); |
| 1835 | __h.release(); |
| 1836 | return __r; |
| 1837 | } |
| 1838 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1839 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 1840 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1841 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1842 | template <class _Pp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1843 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1844 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(_Pp&& __x) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1845 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1846 | __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1847 | pair<iterator, bool> __r = __node_insert_unique(__h.get()); |
| 1848 | if (__r.second) |
| 1849 | __h.release(); |
| 1850 | return __r; |
| 1851 | } |
| 1852 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1853 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1854 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1855 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1856 | |
| 1857 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1858 | template <class _Pp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1859 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1860 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(_Pp&& __x) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1861 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1862 | __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1863 | iterator __r = __node_insert_multi(__h.get()); |
| 1864 | __h.release(); |
| 1865 | return __r; |
| 1866 | } |
| 1867 | |
| 1868 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1869 | template <class _Pp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1870 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1871 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p, |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1872 | _Pp&& __x) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1873 | { |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 1874 | __node_holder __h = __construct_node(_VSTD::forward<_Pp>(__x)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1875 | iterator __r = __node_insert_multi(__p, __h.get()); |
| 1876 | __h.release(); |
| 1877 | return __r; |
| 1878 | } |
| 1879 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1880 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1881 | |
| 1882 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1883 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1884 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const value_type& __x) |
| 1885 | { |
| 1886 | __node_holder __h = __construct_node(__x); |
| 1887 | iterator __r = __node_insert_multi(__h.get()); |
| 1888 | __h.release(); |
| 1889 | return __r; |
| 1890 | } |
| 1891 | |
| 1892 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1893 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1894 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p, |
| 1895 | const value_type& __x) |
| 1896 | { |
| 1897 | __node_holder __h = __construct_node(__x); |
| 1898 | iterator __r = __node_insert_multi(__p, __h.get()); |
| 1899 | __h.release(); |
| 1900 | return __r; |
| 1901 | } |
| 1902 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1903 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1904 | |
| 1905 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1906 | void |
| 1907 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n) |
| 1908 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1909 | if (__n == 1) |
| 1910 | __n = 2; |
| 1911 | else if (__n & (__n - 1)) |
| 1912 | __n = __next_prime(__n); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1913 | size_type __bc = bucket_count(); |
| 1914 | if (__n > __bc) |
| 1915 | __rehash(__n); |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1916 | else if (__n < __bc) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1917 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1918 | __n = _VSTD::max<size_type> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1919 | ( |
| 1920 | __n, |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1921 | __is_power2(__bc) ? __next_pow2(size_t(ceil(float(size()) / max_load_factor()))) : |
| 1922 | __next_prime(size_t(ceil(float(size()) / max_load_factor()))) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1923 | ); |
| 1924 | if (__n < __bc) |
| 1925 | __rehash(__n); |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1930 | void |
| 1931 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc) |
| 1932 | { |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 1933 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 1934 | __get_db()->__invalidate_all(this); |
| 1935 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1936 | __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc(); |
| 1937 | __bucket_list_.reset(__nbc > 0 ? |
| 1938 | __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr); |
| 1939 | __bucket_list_.get_deleter().size() = __nbc; |
| 1940 | if (__nbc > 0) |
| 1941 | { |
| 1942 | for (size_type __i = 0; __i < __nbc; ++__i) |
| 1943 | __bucket_list_[__i] = nullptr; |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 1944 | __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] | 1945 | __node_pointer __cp = __pp->__next_; |
| 1946 | if (__cp != nullptr) |
| 1947 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1948 | size_type __chash = __constrain_hash(__cp->__hash_, __nbc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1949 | __bucket_list_[__chash] = __pp; |
| 1950 | size_type __phash = __chash; |
| 1951 | for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr; |
| 1952 | __cp = __pp->__next_) |
| 1953 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1954 | __chash = __constrain_hash(__cp->__hash_, __nbc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1955 | if (__chash == __phash) |
| 1956 | __pp = __cp; |
| 1957 | else |
| 1958 | { |
| 1959 | if (__bucket_list_[__chash] == nullptr) |
| 1960 | { |
| 1961 | __bucket_list_[__chash] = __pp; |
| 1962 | __pp = __cp; |
| 1963 | __phash = __chash; |
| 1964 | } |
| 1965 | else |
| 1966 | { |
| 1967 | __node_pointer __np = __cp; |
| 1968 | for (; __np->__next_ != nullptr && |
| 1969 | key_eq()(__cp->__value_, __np->__next_->__value_); |
| 1970 | __np = __np->__next_) |
| 1971 | ; |
| 1972 | __pp->__next_ = __np->__next_; |
| 1973 | __np->__next_ = __bucket_list_[__chash]->__next_; |
| 1974 | __bucket_list_[__chash]->__next_ = __cp; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1975 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1976 | } |
| 1977 | } |
| 1978 | } |
| 1979 | } |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1984 | template <class _Key> |
| 1985 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1986 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) |
| 1987 | { |
| 1988 | size_t __hash = hash_function()(__k); |
| 1989 | size_type __bc = bucket_count(); |
| 1990 | if (__bc != 0) |
| 1991 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1992 | size_t __chash = __constrain_hash(__hash, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1993 | __node_pointer __nd = __bucket_list_[__chash]; |
| 1994 | if (__nd != nullptr) |
| 1995 | { |
| 1996 | for (__nd = __nd->__next_; __nd != nullptr && |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 1997 | __constrain_hash(__nd->__hash_, __bc) == __chash; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1998 | __nd = __nd->__next_) |
| 1999 | { |
| 2000 | if (key_eq()(__nd->__value_, __k)) |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2001 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2002 | return iterator(__nd, this); |
| 2003 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2004 | return iterator(__nd); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2005 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | return end(); |
| 2010 | } |
| 2011 | |
| 2012 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2013 | template <class _Key> |
| 2014 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator |
| 2015 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const |
| 2016 | { |
| 2017 | size_t __hash = hash_function()(__k); |
| 2018 | size_type __bc = bucket_count(); |
| 2019 | if (__bc != 0) |
| 2020 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2021 | size_t __chash = __constrain_hash(__hash, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2022 | __node_const_pointer __nd = __bucket_list_[__chash]; |
| 2023 | if (__nd != nullptr) |
| 2024 | { |
| 2025 | for (__nd = __nd->__next_; __nd != nullptr && |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2026 | __constrain_hash(__nd->__hash_, __bc) == __chash; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2027 | __nd = __nd->__next_) |
| 2028 | { |
| 2029 | if (key_eq()(__nd->__value_, __k)) |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2030 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2031 | return const_iterator(__nd, this); |
| 2032 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2033 | return const_iterator(__nd); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2034 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2035 | } |
| 2036 | } |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2037 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2038 | } |
| 2039 | return end(); |
| 2040 | } |
| 2041 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2042 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 2043 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2044 | |
| 2045 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2046 | template <class ..._Args> |
| 2047 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
| 2048 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args) |
| 2049 | { |
| 2050 | __node_allocator& __na = __node_alloc(); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2051 | __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2052 | __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2053 | __h.get_deleter().__value_constructed = true; |
| 2054 | __h->__hash_ = hash_function()(__h->__value_); |
| 2055 | __h->__next_ = nullptr; |
| 2056 | return __h; |
| 2057 | } |
| 2058 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2059 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 2060 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2061 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2062 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
| 2063 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(value_type&& __v, |
| 2064 | size_t __hash) |
| 2065 | { |
| 2066 | __node_allocator& __na = __node_alloc(); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2067 | __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2068 | __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::move(__v)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2069 | __h.get_deleter().__value_constructed = true; |
| 2070 | __h->__hash_ = __hash; |
| 2071 | __h->__next_ = nullptr; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2072 | return _VSTD::move(__h); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2075 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2076 | |
| 2077 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2078 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
| 2079 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v) |
| 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_), __v); |
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; |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2087 | return _VSTD::move(__h); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 2090 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2091 | |
| 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(const 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_), __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 | |
| 2106 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2107 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2108 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p) |
| 2109 | { |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 2110 | __node_pointer __np = __p.__node_; |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2111 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2112 | _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, |
| 2113 | "unordered container erase(iterator) called with an iterator not" |
| 2114 | " referring to this container"); |
| 2115 | _LIBCPP_ASSERT(__p != end(), |
| 2116 | "unordered container erase(iterator) called with a non-dereferenceable iterator"); |
| 2117 | iterator __r(__np, this); |
| 2118 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2119 | iterator __r(__np); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2120 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2121 | ++__r; |
| 2122 | remove(__p); |
| 2123 | return __r; |
| 2124 | } |
| 2125 | |
| 2126 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2127 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 2128 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first, |
| 2129 | const_iterator __last) |
| 2130 | { |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2131 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2132 | _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this, |
| 2133 | "unodered container::erase(iterator, iterator) called with an iterator not" |
| 2134 | " referring to this unodered container"); |
| 2135 | _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this, |
| 2136 | "unodered container::erase(iterator, iterator) called with an iterator not" |
| 2137 | " referring to this unodered container"); |
| 2138 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2139 | for (const_iterator __p = __first; __first != __last; __p = __first) |
| 2140 | { |
| 2141 | ++__first; |
| 2142 | erase(__p); |
| 2143 | } |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 2144 | __node_pointer __np = __last.__node_; |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2145 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2146 | return iterator (__np, this); |
| 2147 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2148 | return iterator (__np); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2149 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2153 | template <class _Key> |
| 2154 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 2155 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k) |
| 2156 | { |
| 2157 | iterator __i = find(__k); |
| 2158 | if (__i == end()) |
| 2159 | return 0; |
| 2160 | erase(__i); |
| 2161 | return 1; |
| 2162 | } |
| 2163 | |
| 2164 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2165 | template <class _Key> |
| 2166 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 2167 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k) |
| 2168 | { |
| 2169 | size_type __r = 0; |
| 2170 | iterator __i = find(__k); |
| 2171 | if (__i != end()) |
| 2172 | { |
| 2173 | iterator __e = end(); |
| 2174 | do |
| 2175 | { |
| 2176 | erase(__i++); |
| 2177 | ++__r; |
| 2178 | } while (__i != __e && key_eq()(*__i, __k)); |
| 2179 | } |
| 2180 | return __r; |
| 2181 | } |
| 2182 | |
| 2183 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2184 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 2185 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2186 | { |
| 2187 | // current node |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 2188 | __node_pointer __cn = __p.__node_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2189 | size_type __bc = bucket_count(); |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2190 | size_t __chash = __constrain_hash(__cn->__hash_, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2191 | // find previous node |
| 2192 | __node_pointer __pn = __bucket_list_[__chash]; |
| 2193 | for (; __pn->__next_ != __cn; __pn = __pn->__next_) |
| 2194 | ; |
| 2195 | // Fix up __bucket_list_ |
| 2196 | // if __pn is not in same bucket (before begin is not in same bucket) && |
| 2197 | // 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] | 2198 | if (__pn == static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::pointer_to(__p1_.first())) |
| 2199 | || __constrain_hash(__pn->__hash_, __bc) != __chash) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2200 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2201 | if (__cn->__next_ == nullptr || __constrain_hash(__cn->__next_->__hash_, __bc) != __chash) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2202 | __bucket_list_[__chash] = nullptr; |
| 2203 | } |
| 2204 | // if __cn->__next_ is not in same bucket (nullptr is in same bucket) |
| 2205 | if (__cn->__next_ != nullptr) |
| 2206 | { |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2207 | size_t __nhash = __constrain_hash(__cn->__next_->__hash_, __bc); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2208 | if (__nhash != __chash) |
| 2209 | __bucket_list_[__nhash] = __pn; |
| 2210 | } |
| 2211 | // remove __cn |
| 2212 | __pn->__next_ = __cn->__next_; |
| 2213 | __cn->__next_ = nullptr; |
| 2214 | --size(); |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2215 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2216 | __c_node* __c = __get_db()->__find_c_and_lock(this); |
| 2217 | for (__i_node** __p = __c->end_; __p != __c->beg_; ) |
| 2218 | { |
| 2219 | --__p; |
| 2220 | iterator* __i = static_cast<iterator*>((*__p)->__i_); |
| 2221 | if (__i->__node_ == __cn) |
| 2222 | { |
| 2223 | (*__p)->__c_ = nullptr; |
| 2224 | if (--__c->end_ != __p) |
| 2225 | memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); |
| 2226 | } |
| 2227 | } |
| 2228 | __get_db()->unlock(); |
| 2229 | #endif |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2230 | return __node_holder(__cn, _Dp(__node_alloc(), true)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2234 | template <class _Key> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 2235 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2236 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 2237 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const |
| 2238 | { |
| 2239 | return static_cast<size_type>(find(__k) != end()); |
| 2240 | } |
| 2241 | |
| 2242 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2243 | template <class _Key> |
| 2244 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 2245 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const |
| 2246 | { |
| 2247 | size_type __r = 0; |
| 2248 | const_iterator __i = find(__k); |
| 2249 | if (__i != end()) |
| 2250 | { |
| 2251 | const_iterator __e = end(); |
| 2252 | do |
| 2253 | { |
| 2254 | ++__i; |
| 2255 | ++__r; |
| 2256 | } while (__i != __e && key_eq()(*__i, __k)); |
| 2257 | } |
| 2258 | return __r; |
| 2259 | } |
| 2260 | |
| 2261 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2262 | template <class _Key> |
| 2263 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, |
| 2264 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator> |
| 2265 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique( |
| 2266 | const _Key& __k) |
| 2267 | { |
| 2268 | iterator __i = find(__k); |
| 2269 | iterator __j = __i; |
| 2270 | if (__i != end()) |
| 2271 | ++__j; |
| 2272 | return pair<iterator, iterator>(__i, __j); |
| 2273 | } |
| 2274 | |
| 2275 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2276 | template <class _Key> |
| 2277 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator, |
| 2278 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator> |
| 2279 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique( |
| 2280 | const _Key& __k) const |
| 2281 | { |
| 2282 | const_iterator __i = find(__k); |
| 2283 | const_iterator __j = __i; |
| 2284 | if (__i != end()) |
| 2285 | ++__j; |
| 2286 | return pair<const_iterator, const_iterator>(__i, __j); |
| 2287 | } |
| 2288 | |
| 2289 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2290 | template <class _Key> |
| 2291 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, |
| 2292 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator> |
| 2293 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi( |
| 2294 | const _Key& __k) |
| 2295 | { |
| 2296 | iterator __i = find(__k); |
| 2297 | iterator __j = __i; |
| 2298 | if (__i != end()) |
| 2299 | { |
| 2300 | iterator __e = end(); |
| 2301 | do |
| 2302 | { |
| 2303 | ++__j; |
| 2304 | } while (__j != __e && key_eq()(*__j, __k)); |
| 2305 | } |
| 2306 | return pair<iterator, iterator>(__i, __j); |
| 2307 | } |
| 2308 | |
| 2309 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2310 | template <class _Key> |
| 2311 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator, |
| 2312 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator> |
| 2313 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi( |
| 2314 | const _Key& __k) const |
| 2315 | { |
| 2316 | const_iterator __i = find(__k); |
| 2317 | const_iterator __j = __i; |
| 2318 | if (__i != end()) |
| 2319 | { |
| 2320 | const_iterator __e = end(); |
| 2321 | do |
| 2322 | { |
| 2323 | ++__j; |
| 2324 | } while (__j != __e && key_eq()(*__j, __k)); |
| 2325 | } |
| 2326 | return pair<const_iterator, const_iterator>(__i, __j); |
| 2327 | } |
| 2328 | |
| 2329 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2330 | void |
| 2331 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 2332 | _NOEXCEPT_( |
| 2333 | (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value || |
| 2334 | __is_nothrow_swappable<__pointer_allocator>::value) && |
| 2335 | (!__node_traits::propagate_on_container_swap::value || |
| 2336 | __is_nothrow_swappable<__node_allocator>::value) && |
| 2337 | __is_nothrow_swappable<hasher>::value && |
| 2338 | __is_nothrow_swappable<key_equal>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2339 | { |
| 2340 | { |
| 2341 | __node_pointer_pointer __npp = __bucket_list_.release(); |
| 2342 | __bucket_list_.reset(__u.__bucket_list_.release()); |
| 2343 | __u.__bucket_list_.reset(__npp); |
| 2344 | } |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2345 | _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] | 2346 | __swap_alloc(__bucket_list_.get_deleter().__alloc(), |
| 2347 | __u.__bucket_list_.get_deleter().__alloc()); |
| 2348 | __swap_alloc(__node_alloc(), __u.__node_alloc()); |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2349 | _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2350 | __p2_.swap(__u.__p2_); |
| 2351 | __p3_.swap(__u.__p3_); |
| 2352 | if (size() > 0) |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2353 | __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash_, bucket_count())] = |
Howard Hinnant | 7a6b7ce | 2013-06-22 15:21:29 +0000 | [diff] [blame] | 2354 | 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] | 2355 | if (__u.size() > 0) |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2356 | __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] | 2357 | 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^] | 2358 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2359 | __get_db()->swap(this, &__u); |
| 2360 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2364 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 2365 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const |
| 2366 | { |
| 2367 | __node_const_pointer __np = __bucket_list_[__n]; |
| 2368 | size_type __bc = bucket_count(); |
| 2369 | size_type __r = 0; |
| 2370 | if (__np != nullptr) |
| 2371 | { |
| 2372 | for (__np = __np->__next_; __np != nullptr && |
Howard Hinnant | 7a44515 | 2012-07-06 17:31:14 +0000 | [diff] [blame] | 2373 | __constrain_hash(__np->__hash_, __bc) == __n; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2374 | __np = __np->__next_, ++__r) |
| 2375 | ; |
| 2376 | } |
| 2377 | return __r; |
| 2378 | } |
| 2379 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame] | 2380 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2381 | inline _LIBCPP_INLINE_VISIBILITY |
| 2382 | void |
| 2383 | swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x, |
| 2384 | __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y) |
| 2385 | _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) |
| 2386 | { |
| 2387 | __x.swap(__y); |
| 2388 | } |
| 2389 | |
Howard Hinnant | 3921364 | 2013-07-23 22:01:58 +0000 | [diff] [blame^] | 2390 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 2391 | |
| 2392 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2393 | bool |
| 2394 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const |
| 2395 | { |
| 2396 | return __i->__node_ != nullptr; |
| 2397 | } |
| 2398 | |
| 2399 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2400 | bool |
| 2401 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const |
| 2402 | { |
| 2403 | return false; |
| 2404 | } |
| 2405 | |
| 2406 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2407 | bool |
| 2408 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const |
| 2409 | { |
| 2410 | return false; |
| 2411 | } |
| 2412 | |
| 2413 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 2414 | bool |
| 2415 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const |
| 2416 | { |
| 2417 | return false; |
| 2418 | } |
| 2419 | |
| 2420 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2421 | _LIBCPP_END_NAMESPACE_STD |
| 2422 | |
| 2423 | #endif // _LIBCPP__HASH_TABLE |