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