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 | |
| 21 | #pragma GCC system_header |
| 22 | |
| 23 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 24 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 25 | _LIBCPP_VISIBLE |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | size_t __next_prime(size_t); |
| 27 | |
| 28 | template <class _NodePtr> |
| 29 | struct __hash_node_base |
| 30 | { |
| 31 | typedef __hash_node_base __first_node; |
Howard Hinnant | df85e57 | 2011-02-27 18:02:02 +0000 | [diff] [blame] | 32 | // typedef _NodePtr pointer; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | |
Howard Hinnant | df85e57 | 2011-02-27 18:02:02 +0000 | [diff] [blame] | 34 | _NodePtr __next_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 35 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 36 | _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | template <class _Tp, class _VoidPtr> |
| 40 | struct __hash_node |
| 41 | : public __hash_node_base |
| 42 | < |
| 43 | typename pointer_traits<_VoidPtr>::template |
| 44 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 45 | rebind<__hash_node<_Tp, _VoidPtr> > |
| 46 | #else |
| 47 | rebind<__hash_node<_Tp, _VoidPtr> >::other |
| 48 | #endif |
| 49 | > |
| 50 | { |
| 51 | typedef _Tp value_type; |
| 52 | |
| 53 | size_t __hash_; |
| 54 | value_type __value_; |
| 55 | }; |
| 56 | |
| 57 | template <class, class, class, class> class __hash_table; |
| 58 | template <class> class __hash_const_iterator; |
| 59 | template <class> class __hash_map_iterator; |
| 60 | template <class> class __hash_map_const_iterator; |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 61 | template <class, class, class, class, class> class _LIBCPP_VISIBLE unordered_map; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 62 | |
| 63 | template <class _NodePtr> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 64 | class _LIBCPP_VISIBLE __hash_iterator |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 65 | { |
| 66 | typedef _NodePtr __node_pointer; |
| 67 | |
| 68 | __node_pointer __node_; |
| 69 | |
| 70 | public: |
| 71 | typedef forward_iterator_tag iterator_category; |
| 72 | typedef typename pointer_traits<__node_pointer>::element_type::value_type value_type; |
| 73 | typedef typename pointer_traits<__node_pointer>::difference_type difference_type; |
| 74 | typedef value_type& reference; |
| 75 | typedef typename pointer_traits<__node_pointer>::template |
| 76 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 77 | rebind<value_type> |
| 78 | #else |
| 79 | rebind<value_type>::other |
| 80 | #endif |
| 81 | pointer; |
| 82 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 83 | _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 84 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 85 | _LIBCPP_INLINE_VISIBILITY |
| 86 | reference operator*() const {return __node_->__value_;} |
| 87 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 88 | pointer operator->() const {return _STD::addressof(__node_->__value_);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 89 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 90 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | __hash_iterator& operator++() |
| 92 | { |
| 93 | __node_ = __node_->__next_; |
| 94 | return *this; |
| 95 | } |
| 96 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 97 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 98 | __hash_iterator operator++(int) |
| 99 | { |
| 100 | __hash_iterator __t(*this); |
| 101 | ++(*this); |
| 102 | return __t; |
| 103 | } |
| 104 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 105 | friend _LIBCPP_INLINE_VISIBILITY |
| 106 | bool operator==(const __hash_iterator& __x, const __hash_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 107 | {return __x.__node_ == __y.__node_;} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 108 | friend _LIBCPP_INLINE_VISIBILITY |
| 109 | bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 110 | {return __x.__node_ != __y.__node_;} |
| 111 | |
| 112 | private: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 113 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 114 | __hash_iterator(__node_pointer __node) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 115 | : __node_(__node) |
| 116 | {} |
| 117 | |
| 118 | template <class, class, class, class> friend class __hash_table; |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 119 | template <class> friend class _LIBCPP_VISIBLE __hash_const_iterator; |
| 120 | template <class> friend class _LIBCPP_VISIBLE __hash_map_iterator; |
| 121 | template <class, class, class, class, class> friend class _LIBCPP_VISIBLE unordered_map; |
| 122 | template <class, class, class, class, class> friend class _LIBCPP_VISIBLE unordered_multimap; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | template <class _ConstNodePtr> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 126 | class _LIBCPP_VISIBLE __hash_const_iterator |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 127 | { |
| 128 | typedef _ConstNodePtr __node_pointer; |
| 129 | |
| 130 | __node_pointer __node_; |
| 131 | |
| 132 | typedef typename remove_const< |
| 133 | typename pointer_traits<__node_pointer>::element_type |
| 134 | >::type __node; |
| 135 | |
| 136 | public: |
| 137 | typedef forward_iterator_tag iterator_category; |
| 138 | typedef typename __node::value_type value_type; |
| 139 | typedef typename pointer_traits<__node_pointer>::difference_type difference_type; |
| 140 | typedef const value_type& reference; |
| 141 | typedef typename pointer_traits<__node_pointer>::template |
| 142 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 143 | rebind<const value_type> |
| 144 | #else |
| 145 | rebind<const value_type>::other |
| 146 | #endif |
| 147 | pointer; |
| 148 | typedef typename pointer_traits<__node_pointer>::template |
| 149 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 150 | rebind<__node> |
| 151 | #else |
| 152 | rebind<__node>::other |
| 153 | #endif |
| 154 | __non_const_node_pointer; |
| 155 | typedef __hash_iterator<__non_const_node_pointer> __non_const_iterator; |
| 156 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 157 | _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT {} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 158 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 159 | __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 160 | : __node_(__x.__node_) |
| 161 | {} |
| 162 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 163 | _LIBCPP_INLINE_VISIBILITY |
| 164 | reference operator*() const {return __node_->__value_;} |
| 165 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 166 | pointer operator->() const {return _STD::addressof(__node_->__value_);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 167 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 168 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 169 | __hash_const_iterator& operator++() |
| 170 | { |
| 171 | __node_ = __node_->__next_; |
| 172 | return *this; |
| 173 | } |
| 174 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 175 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 176 | __hash_const_iterator operator++(int) |
| 177 | { |
| 178 | __hash_const_iterator __t(*this); |
| 179 | ++(*this); |
| 180 | return __t; |
| 181 | } |
| 182 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 183 | friend _LIBCPP_INLINE_VISIBILITY |
| 184 | bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 185 | {return __x.__node_ == __y.__node_;} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 186 | friend _LIBCPP_INLINE_VISIBILITY |
| 187 | bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 188 | {return __x.__node_ != __y.__node_;} |
| 189 | |
| 190 | private: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 191 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 192 | __hash_const_iterator(__node_pointer __node) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 193 | : __node_(__node) |
| 194 | {} |
| 195 | |
| 196 | template <class, class, class, class> friend class __hash_table; |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 197 | template <class> friend class _LIBCPP_VISIBLE __hash_map_const_iterator; |
| 198 | template <class, class, class, class, class> friend class _LIBCPP_VISIBLE unordered_map; |
| 199 | template <class, class, class, class, class> friend class _LIBCPP_VISIBLE unordered_multimap; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 202 | template <class> class _LIBCPP_VISIBLE __hash_const_local_iterator; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 203 | |
| 204 | template <class _NodePtr> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 205 | class _LIBCPP_VISIBLE __hash_local_iterator |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 206 | { |
| 207 | typedef _NodePtr __node_pointer; |
| 208 | |
| 209 | __node_pointer __node_; |
| 210 | size_t __bucket_; |
| 211 | size_t __bucket_count_; |
| 212 | |
| 213 | typedef pointer_traits<__node_pointer> __pointer_traits; |
| 214 | public: |
| 215 | typedef forward_iterator_tag iterator_category; |
| 216 | typedef typename __pointer_traits::element_type::value_type value_type; |
| 217 | typedef typename __pointer_traits::difference_type difference_type; |
| 218 | typedef value_type& reference; |
| 219 | typedef typename __pointer_traits::template |
| 220 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 221 | rebind<value_type> |
| 222 | #else |
| 223 | rebind<value_type>::other |
| 224 | #endif |
| 225 | pointer; |
| 226 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 227 | _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 228 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 229 | _LIBCPP_INLINE_VISIBILITY |
| 230 | reference operator*() const {return __node_->__value_;} |
| 231 | _LIBCPP_INLINE_VISIBILITY |
| 232 | pointer operator->() const {return &__node_->__value_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 233 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 234 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 235 | __hash_local_iterator& operator++() |
| 236 | { |
| 237 | __node_ = __node_->__next_; |
| 238 | if (__node_ != nullptr && __node_->__hash_ % __bucket_count_ != __bucket_) |
| 239 | __node_ = nullptr; |
| 240 | return *this; |
| 241 | } |
| 242 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 243 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 244 | __hash_local_iterator operator++(int) |
| 245 | { |
| 246 | __hash_local_iterator __t(*this); |
| 247 | ++(*this); |
| 248 | return __t; |
| 249 | } |
| 250 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 251 | friend _LIBCPP_INLINE_VISIBILITY |
| 252 | bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 253 | {return __x.__node_ == __y.__node_;} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 254 | friend _LIBCPP_INLINE_VISIBILITY |
| 255 | bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 256 | {return __x.__node_ != __y.__node_;} |
| 257 | |
| 258 | private: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 259 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 260 | __hash_local_iterator(__node_pointer __node, size_t __bucket, |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 261 | size_t __bucket_count) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 262 | : __node_(__node), |
| 263 | __bucket_(__bucket), |
| 264 | __bucket_count_(__bucket_count) |
| 265 | { |
| 266 | if (__node_ != nullptr) |
| 267 | __node_ = __node_->__next_; |
| 268 | } |
| 269 | |
| 270 | template <class, class, class, class> friend class __hash_table; |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 271 | template <class> friend class _LIBCPP_VISIBLE __hash_const_local_iterator; |
| 272 | template <class> friend class _LIBCPP_VISIBLE __hash_map_iterator; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | template <class _ConstNodePtr> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 276 | class _LIBCPP_VISIBLE __hash_const_local_iterator |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 277 | { |
| 278 | typedef _ConstNodePtr __node_pointer; |
| 279 | |
| 280 | __node_pointer __node_; |
| 281 | size_t __bucket_; |
| 282 | size_t __bucket_count_; |
| 283 | |
| 284 | typedef pointer_traits<__node_pointer> __pointer_traits; |
| 285 | typedef typename __pointer_traits::element_type __node; |
| 286 | typedef typename remove_const<__node>::type __non_const_node; |
| 287 | typedef typename pointer_traits<__node_pointer>::template |
| 288 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 289 | rebind<__non_const_node> |
| 290 | #else |
| 291 | rebind<__non_const_node>::other |
| 292 | #endif |
| 293 | __non_const_node_pointer; |
| 294 | typedef __hash_local_iterator<__non_const_node_pointer> |
| 295 | __non_const_iterator; |
| 296 | public: |
| 297 | typedef forward_iterator_tag iterator_category; |
| 298 | typedef typename remove_const< |
| 299 | typename __pointer_traits::element_type::value_type |
| 300 | >::type value_type; |
| 301 | typedef typename __pointer_traits::difference_type difference_type; |
| 302 | typedef const value_type& reference; |
| 303 | typedef typename __pointer_traits::template |
| 304 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 305 | rebind<const value_type> |
| 306 | #else |
| 307 | rebind<const value_type>::other |
| 308 | #endif |
| 309 | pointer; |
| 310 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 311 | _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT {} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 312 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 313 | __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 314 | : __node_(__x.__node_), |
| 315 | __bucket_(__x.__bucket_), |
| 316 | __bucket_count_(__x.__bucket_count_) |
| 317 | {} |
| 318 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 319 | _LIBCPP_INLINE_VISIBILITY |
| 320 | reference operator*() const {return __node_->__value_;} |
| 321 | _LIBCPP_INLINE_VISIBILITY |
| 322 | pointer operator->() const {return &__node_->__value_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 323 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 324 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 325 | __hash_const_local_iterator& operator++() |
| 326 | { |
| 327 | __node_ = __node_->__next_; |
| 328 | if (__node_ != nullptr && __node_->__hash_ % __bucket_count_ != __bucket_) |
| 329 | __node_ = nullptr; |
| 330 | return *this; |
| 331 | } |
| 332 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 333 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 334 | __hash_const_local_iterator operator++(int) |
| 335 | { |
| 336 | __hash_const_local_iterator __t(*this); |
| 337 | ++(*this); |
| 338 | return __t; |
| 339 | } |
| 340 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 341 | friend _LIBCPP_INLINE_VISIBILITY |
| 342 | bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 343 | {return __x.__node_ == __y.__node_;} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 344 | friend _LIBCPP_INLINE_VISIBILITY |
| 345 | bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 346 | {return __x.__node_ != __y.__node_;} |
| 347 | |
| 348 | private: |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 349 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 350 | __hash_const_local_iterator(__node_pointer __node, size_t __bucket, |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 351 | size_t __bucket_count) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 352 | : __node_(__node), |
| 353 | __bucket_(__bucket), |
| 354 | __bucket_count_(__bucket_count) |
| 355 | { |
| 356 | if (__node_ != nullptr) |
| 357 | __node_ = __node_->__next_; |
| 358 | } |
| 359 | |
| 360 | template <class, class, class, class> friend class __hash_table; |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 361 | template <class> friend class _LIBCPP_VISIBLE __hash_map_const_iterator; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | template <class _Alloc> |
| 365 | class __bucket_list_deallocator |
| 366 | { |
| 367 | typedef _Alloc allocator_type; |
| 368 | typedef allocator_traits<allocator_type> __alloc_traits; |
| 369 | typedef typename __alloc_traits::size_type size_type; |
| 370 | |
| 371 | __compressed_pair<size_type, allocator_type> __data_; |
| 372 | public: |
| 373 | typedef typename __alloc_traits::pointer pointer; |
| 374 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 375 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 376 | __bucket_list_deallocator() |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 377 | _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 378 | : __data_(0) {} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 379 | |
| 380 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 381 | __bucket_list_deallocator(const allocator_type& __a, size_type __size) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 382 | _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 383 | : __data_(__size, __a) {} |
| 384 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 385 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 386 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 387 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 388 | __bucket_list_deallocator(__bucket_list_deallocator&& __x) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 389 | _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 390 | : __data_(_STD::move(__x.__data_)) |
| 391 | { |
| 392 | __x.size() = 0; |
| 393 | } |
| 394 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 395 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 396 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 397 | _LIBCPP_INLINE_VISIBILITY |
| 398 | size_type& size() _NOEXCEPT {return __data_.first();} |
| 399 | _LIBCPP_INLINE_VISIBILITY |
| 400 | size_type size() const _NOEXCEPT {return __data_.first();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 401 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 402 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 403 | allocator_type& __alloc() _NOEXCEPT {return __data_.second();} |
| 404 | _LIBCPP_INLINE_VISIBILITY |
| 405 | const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();} |
| 406 | |
| 407 | _LIBCPP_INLINE_VISIBILITY |
| 408 | void operator()(pointer __p) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 409 | { |
| 410 | __alloc_traits::deallocate(__alloc(), __p, size()); |
| 411 | } |
| 412 | }; |
| 413 | |
| 414 | template <class> class __hash_map_node_destructor; |
| 415 | |
| 416 | template <class _Alloc> |
| 417 | class __hash_node_destructor |
| 418 | { |
| 419 | typedef _Alloc allocator_type; |
| 420 | typedef allocator_traits<allocator_type> __alloc_traits; |
| 421 | typedef typename __alloc_traits::value_type::value_type value_type; |
| 422 | public: |
| 423 | typedef typename __alloc_traits::pointer pointer; |
| 424 | private: |
| 425 | |
| 426 | allocator_type& __na_; |
| 427 | |
| 428 | __hash_node_destructor& operator=(const __hash_node_destructor&); |
| 429 | |
| 430 | public: |
| 431 | bool __value_constructed; |
| 432 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 433 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 434 | explicit __hash_node_destructor(allocator_type& __na) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 435 | : __na_(__na), |
| 436 | __value_constructed(false) |
| 437 | {} |
| 438 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 439 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 440 | void operator()(pointer __p) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 441 | { |
| 442 | if (__value_constructed) |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 443 | __alloc_traits::destroy(__na_, _STD::addressof(__p->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 444 | if (__p) |
| 445 | __alloc_traits::deallocate(__na_, __p, 1); |
| 446 | } |
| 447 | |
| 448 | template <class> friend class __hash_map_node_destructor; |
| 449 | }; |
| 450 | |
| 451 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 452 | class __hash_table |
| 453 | { |
| 454 | public: |
| 455 | typedef _Tp value_type; |
| 456 | typedef _Hash hasher; |
| 457 | typedef _Equal key_equal; |
| 458 | typedef _Alloc allocator_type; |
| 459 | |
| 460 | private: |
| 461 | typedef allocator_traits<allocator_type> __alloc_traits; |
| 462 | public: |
| 463 | typedef value_type& reference; |
| 464 | typedef const value_type& const_reference; |
| 465 | typedef typename __alloc_traits::pointer pointer; |
| 466 | typedef typename __alloc_traits::const_pointer const_pointer; |
| 467 | typedef typename __alloc_traits::size_type size_type; |
| 468 | typedef typename __alloc_traits::difference_type difference_type; |
| 469 | public: |
| 470 | // Create __node |
| 471 | typedef __hash_node<value_type, typename __alloc_traits::void_pointer> __node; |
| 472 | typedef typename __node::__first_node __first_node; |
| 473 | typedef typename __alloc_traits::template |
| 474 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 475 | rebind_alloc<__node> |
| 476 | #else |
| 477 | rebind_alloc<__node>::other |
| 478 | #endif |
| 479 | __node_allocator; |
| 480 | typedef allocator_traits<__node_allocator> __node_traits; |
| 481 | typedef typename __node_traits::pointer __node_pointer; |
| 482 | typedef typename __node_traits::const_pointer __node_const_pointer; |
| 483 | |
| 484 | private: |
| 485 | |
| 486 | typedef typename __node_traits::template |
| 487 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 488 | rebind_alloc<__node_pointer> |
| 489 | #else |
| 490 | rebind_alloc<__node_pointer>::other |
| 491 | #endif |
| 492 | __pointer_allocator; |
| 493 | typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter; |
| 494 | typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list; |
| 495 | typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits; |
| 496 | typedef typename __bucket_list_deleter::pointer __node_pointer_pointer; |
| 497 | |
| 498 | // --- Member data begin --- |
| 499 | __bucket_list __bucket_list_; |
| 500 | __compressed_pair<__first_node, __node_allocator> __p1_; |
| 501 | __compressed_pair<size_type, hasher> __p2_; |
| 502 | __compressed_pair<float, key_equal> __p3_; |
| 503 | // --- Member data end --- |
| 504 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 505 | _LIBCPP_INLINE_VISIBILITY |
| 506 | size_type& size() _NOEXCEPT {return __p2_.first();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 507 | public: |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 508 | _LIBCPP_INLINE_VISIBILITY |
| 509 | size_type size() const _NOEXCEPT {return __p2_.first();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 510 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 511 | _LIBCPP_INLINE_VISIBILITY |
| 512 | hasher& hash_function() _NOEXCEPT {return __p2_.second();} |
| 513 | _LIBCPP_INLINE_VISIBILITY |
| 514 | const hasher& hash_function() const _NOEXCEPT {return __p2_.second();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 515 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 516 | _LIBCPP_INLINE_VISIBILITY |
| 517 | float& max_load_factor() _NOEXCEPT {return __p3_.first();} |
| 518 | _LIBCPP_INLINE_VISIBILITY |
| 519 | float max_load_factor() const _NOEXCEPT {return __p3_.first();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 520 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 521 | _LIBCPP_INLINE_VISIBILITY |
| 522 | key_equal& key_eq() _NOEXCEPT {return __p3_.second();} |
| 523 | _LIBCPP_INLINE_VISIBILITY |
| 524 | const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 525 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 526 | _LIBCPP_INLINE_VISIBILITY |
| 527 | __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();} |
| 528 | _LIBCPP_INLINE_VISIBILITY |
| 529 | const __node_allocator& __node_alloc() const _NOEXCEPT |
| 530 | {return __p1_.second();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 531 | |
| 532 | public: |
| 533 | typedef __hash_iterator<__node_pointer> iterator; |
| 534 | typedef __hash_const_iterator<__node_const_pointer> const_iterator; |
| 535 | typedef __hash_local_iterator<__node_pointer> local_iterator; |
| 536 | typedef __hash_const_local_iterator<__node_const_pointer> const_local_iterator; |
| 537 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 538 | __hash_table() |
| 539 | _NOEXCEPT_( |
| 540 | is_nothrow_default_constructible<__bucket_list>::value && |
| 541 | is_nothrow_default_constructible<__first_node>::value && |
| 542 | is_nothrow_default_constructible<__node_allocator>::value && |
| 543 | is_nothrow_default_constructible<hasher>::value && |
| 544 | is_nothrow_default_constructible<key_equal>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 545 | __hash_table(const hasher& __hf, const key_equal& __eql); |
| 546 | __hash_table(const hasher& __hf, const key_equal& __eql, |
| 547 | const allocator_type& __a); |
| 548 | explicit __hash_table(const allocator_type& __a); |
| 549 | __hash_table(const __hash_table& __u); |
| 550 | __hash_table(const __hash_table& __u, const allocator_type& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 551 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 552 | __hash_table(__hash_table&& __u) |
| 553 | _NOEXCEPT_( |
| 554 | is_nothrow_move_constructible<__bucket_list>::value && |
| 555 | is_nothrow_move_constructible<__first_node>::value && |
| 556 | is_nothrow_move_constructible<__node_allocator>::value && |
| 557 | is_nothrow_move_constructible<hasher>::value && |
| 558 | is_nothrow_move_constructible<key_equal>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 559 | __hash_table(__hash_table&& __u, const allocator_type& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 560 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 561 | ~__hash_table(); |
| 562 | |
| 563 | __hash_table& operator=(const __hash_table& __u); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 564 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 565 | __hash_table& operator=(__hash_table&& __u) |
| 566 | _NOEXCEPT_( |
| 567 | __node_traits::propagate_on_container_move_assignment::value && |
| 568 | is_nothrow_move_assignable<__node_allocator>::value && |
| 569 | is_nothrow_move_assignable<hasher>::value && |
| 570 | is_nothrow_move_assignable<key_equal>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 571 | #endif |
| 572 | template <class _InputIterator> |
| 573 | void __assign_unique(_InputIterator __first, _InputIterator __last); |
| 574 | template <class _InputIterator> |
| 575 | void __assign_multi(_InputIterator __first, _InputIterator __last); |
| 576 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 577 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 578 | size_type max_size() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 579 | { |
| 580 | return allocator_traits<__pointer_allocator>::max_size( |
| 581 | __bucket_list_.get_deleter().__alloc()); |
| 582 | } |
| 583 | |
| 584 | pair<iterator, bool> __node_insert_unique(__node_pointer __nd); |
| 585 | iterator __node_insert_multi(__node_pointer __nd); |
| 586 | iterator __node_insert_multi(const_iterator __p, |
| 587 | __node_pointer __nd); |
| 588 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 589 | #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 590 | template <class... _Args> |
| 591 | pair<iterator, bool> __emplace_unique(_Args&&... __args); |
| 592 | template <class... _Args> |
| 593 | iterator __emplace_multi(_Args&&... __args); |
| 594 | template <class... _Args> |
| 595 | iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 596 | #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 597 | |
| 598 | pair<iterator, bool> __insert_unique(const value_type& __x); |
| 599 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 600 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 601 | template <class _P> |
| 602 | pair<iterator, bool> __insert_unique(_P&& __x); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 603 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 604 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 605 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 606 | template <class _P> |
| 607 | iterator __insert_multi(_P&& __x); |
| 608 | template <class _P> |
| 609 | iterator __insert_multi(const_iterator __p, _P&& __x); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 610 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 611 | iterator __insert_multi(const value_type& __x); |
| 612 | iterator __insert_multi(const_iterator __p, const value_type& __x); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 613 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 614 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 615 | void clear() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 616 | void rehash(size_type __n); |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 617 | _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 618 | {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 619 | |
| 620 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 621 | size_type bucket_count() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 622 | { |
| 623 | return __bucket_list_.get_deleter().size(); |
| 624 | } |
| 625 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 626 | iterator begin() _NOEXCEPT; |
| 627 | iterator end() _NOEXCEPT; |
| 628 | const_iterator begin() const _NOEXCEPT; |
| 629 | const_iterator end() const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 630 | |
| 631 | template <class _Key> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 632 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 633 | size_type bucket(const _Key& __k) const |
| 634 | {return hash_function()(__k) % bucket_count();} |
| 635 | |
| 636 | template <class _Key> |
| 637 | iterator find(const _Key& __x); |
| 638 | template <class _Key> |
| 639 | const_iterator find(const _Key& __x) const; |
| 640 | |
| 641 | typedef __hash_node_destructor<__node_allocator> _D; |
| 642 | typedef unique_ptr<__node, _D> __node_holder; |
| 643 | |
| 644 | iterator erase(const_iterator __p); |
| 645 | iterator erase(const_iterator __first, const_iterator __last); |
| 646 | template <class _Key> |
| 647 | size_type __erase_unique(const _Key& __k); |
| 648 | template <class _Key> |
| 649 | size_type __erase_multi(const _Key& __k); |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 650 | __node_holder remove(const_iterator __p) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 651 | |
| 652 | template <class _Key> |
| 653 | size_type __count_unique(const _Key& __k) const; |
| 654 | template <class _Key> |
| 655 | size_type __count_multi(const _Key& __k) const; |
| 656 | |
| 657 | template <class _Key> |
| 658 | pair<iterator, iterator> |
| 659 | __equal_range_unique(const _Key& __k); |
| 660 | template <class _Key> |
| 661 | pair<const_iterator, const_iterator> |
| 662 | __equal_range_unique(const _Key& __k) const; |
| 663 | |
| 664 | template <class _Key> |
| 665 | pair<iterator, iterator> |
| 666 | __equal_range_multi(const _Key& __k); |
| 667 | template <class _Key> |
| 668 | pair<const_iterator, const_iterator> |
| 669 | __equal_range_multi(const _Key& __k) const; |
| 670 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 671 | void swap(__hash_table& __u) |
| 672 | _NOEXCEPT_( |
| 673 | (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value || |
| 674 | __is_nothrow_swappable<__pointer_allocator>::value) && |
| 675 | (!__node_traits::propagate_on_container_swap::value || |
| 676 | __is_nothrow_swappable<__node_allocator>::value) && |
| 677 | __is_nothrow_swappable<hasher>::value && |
| 678 | __is_nothrow_swappable<key_equal>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 679 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 680 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 681 | size_type max_bucket_count() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 682 | {return __bucket_list_.get_deleter().__alloc().max_size();} |
| 683 | size_type bucket_size(size_type __n) const; |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 684 | _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 685 | { |
| 686 | size_type __bc = bucket_count(); |
| 687 | return __bc != 0 ? (float)size() / __bc : 0.f; |
| 688 | } |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 689 | _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 690 | {max_load_factor() = _STD::max(__mlf, load_factor());} |
| 691 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 692 | _LIBCPP_INLINE_VISIBILITY local_iterator begin(size_type __n) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 693 | {return local_iterator(__bucket_list_[__n], __n, bucket_count());} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 694 | _LIBCPP_INLINE_VISIBILITY local_iterator end(size_type __n) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 695 | {return local_iterator(nullptr, __n, bucket_count());} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 696 | _LIBCPP_INLINE_VISIBILITY const_local_iterator cbegin(size_type __n) const |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 697 | {return const_local_iterator(__bucket_list_[__n], __n, bucket_count());} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 698 | _LIBCPP_INLINE_VISIBILITY const_local_iterator cend(size_type __n) const |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 699 | {return const_local_iterator(nullptr, __n, bucket_count());} |
| 700 | private: |
| 701 | void __rehash(size_type __n); |
| 702 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 703 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 704 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 705 | template <class ..._Args> |
| 706 | __node_holder __construct_node(_Args&& ...__args); |
Howard Hinnant | bfd5530 | 2010-09-04 23:46:48 +0000 | [diff] [blame] | 707 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 708 | __node_holder __construct_node(value_type&& __v, size_t __hash); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 709 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 710 | __node_holder __construct_node(const value_type& __v); |
| 711 | #endif |
| 712 | __node_holder __construct_node(const value_type& __v, size_t __hash); |
| 713 | |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 714 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 715 | void __copy_assign_alloc(const __hash_table& __u) |
| 716 | {__copy_assign_alloc(__u, integral_constant<bool, |
| 717 | __node_traits::propagate_on_container_copy_assignment::value>());} |
| 718 | void __copy_assign_alloc(const __hash_table& __u, true_type); |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 719 | _LIBCPP_INLINE_VISIBILITY |
| 720 | void __copy_assign_alloc(const __hash_table& __u, false_type) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 721 | |
| 722 | void __move_assign(__hash_table& __u, false_type); |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 723 | void __move_assign(__hash_table& __u, true_type) |
| 724 | _NOEXCEPT_( |
| 725 | is_nothrow_move_assignable<__node_allocator>::value && |
| 726 | is_nothrow_move_assignable<hasher>::value && |
| 727 | is_nothrow_move_assignable<key_equal>::value); |
| 728 | _LIBCPP_INLINE_VISIBILITY |
| 729 | void __move_assign_alloc(__hash_table& __u) |
| 730 | _NOEXCEPT_( |
| 731 | !__node_traits::propagate_on_container_move_assignment::value || |
| 732 | (is_nothrow_move_assignable<__pointer_allocator>::value && |
| 733 | is_nothrow_move_assignable<__node_allocator>::value)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 734 | {__move_assign_alloc(__u, integral_constant<bool, |
| 735 | __node_traits::propagate_on_container_move_assignment::value>());} |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 736 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 737 | void __move_assign_alloc(__hash_table& __u, true_type) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 738 | _NOEXCEPT_( |
| 739 | is_nothrow_move_assignable<__pointer_allocator>::value && |
| 740 | is_nothrow_move_assignable<__node_allocator>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 741 | { |
| 742 | __bucket_list_.get_deleter().__alloc() = |
| 743 | _STD::move(__u.__bucket_list_.get_deleter().__alloc()); |
| 744 | __node_alloc() = _STD::move(__u.__node_alloc()); |
| 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 __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 748 | |
| 749 | template <class _A> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 750 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 751 | static |
| 752 | void |
| 753 | __swap_alloc(_A& __x, _A& __y) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 754 | _NOEXCEPT_( |
| 755 | !allocator_traits<_A>::propagate_on_container_swap::value || |
| 756 | __is_nothrow_swappable<_A>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 757 | { |
| 758 | __swap_alloc(__x, __y, |
| 759 | integral_constant<bool, |
| 760 | allocator_traits<_A>::propagate_on_container_swap::value |
| 761 | >()); |
| 762 | } |
| 763 | |
| 764 | template <class _A> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 765 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 766 | static |
| 767 | void |
| 768 | __swap_alloc(_A& __x, _A& __y, true_type) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 769 | _NOEXCEPT_(__is_nothrow_swappable<_A>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 770 | { |
| 771 | using _STD::swap; |
| 772 | swap(__x, __y); |
| 773 | } |
| 774 | |
| 775 | template <class _A> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 776 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 777 | static |
| 778 | void |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 779 | __swap_alloc(_A& __x, _A& __y, false_type) _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 780 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 781 | void __deallocate(__node_pointer __np) _NOEXCEPT; |
| 782 | __node_pointer __detach() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 783 | }; |
| 784 | |
| 785 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 786 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 787 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table() |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 788 | _NOEXCEPT_( |
| 789 | is_nothrow_default_constructible<__bucket_list>::value && |
| 790 | is_nothrow_default_constructible<__first_node>::value && |
| 791 | is_nothrow_default_constructible<hasher>::value && |
| 792 | is_nothrow_default_constructible<key_equal>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 793 | : __p2_(0), |
| 794 | __p3_(1.0f) |
| 795 | { |
| 796 | } |
| 797 | |
| 798 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 799 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 800 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf, |
| 801 | const key_equal& __eql) |
| 802 | : __bucket_list_(nullptr, __bucket_list_deleter()), |
| 803 | __p1_(), |
| 804 | __p2_(0, __hf), |
| 805 | __p3_(1.0f, __eql) |
| 806 | { |
| 807 | } |
| 808 | |
| 809 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 810 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf, |
| 811 | const key_equal& __eql, |
| 812 | const allocator_type& __a) |
| 813 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
| 814 | __p1_(__node_allocator(__a)), |
| 815 | __p2_(0, __hf), |
| 816 | __p3_(1.0f, __eql) |
| 817 | { |
| 818 | } |
| 819 | |
| 820 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 821 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a) |
| 822 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
| 823 | __p1_(__node_allocator(__a)), |
| 824 | __p2_(0), |
| 825 | __p3_(1.0f) |
| 826 | { |
| 827 | } |
| 828 | |
| 829 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 830 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u) |
| 831 | : __bucket_list_(nullptr, |
| 832 | __bucket_list_deleter(allocator_traits<__pointer_allocator>:: |
| 833 | select_on_container_copy_construction( |
| 834 | __u.__bucket_list_.get_deleter().__alloc()), 0)), |
| 835 | __p1_(allocator_traits<__node_allocator>:: |
| 836 | select_on_container_copy_construction(__u.__node_alloc())), |
| 837 | __p2_(0, __u.hash_function()), |
| 838 | __p3_(__u.__p3_) |
| 839 | { |
| 840 | } |
| 841 | |
| 842 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 843 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u, |
| 844 | const allocator_type& __a) |
| 845 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
| 846 | __p1_(__node_allocator(__a)), |
| 847 | __p2_(0, __u.hash_function()), |
| 848 | __p3_(__u.__p3_) |
| 849 | { |
| 850 | } |
| 851 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 852 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 853 | |
| 854 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 855 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 856 | _NOEXCEPT_( |
| 857 | is_nothrow_move_constructible<__bucket_list>::value && |
| 858 | is_nothrow_move_constructible<__first_node>::value && |
| 859 | is_nothrow_move_constructible<hasher>::value && |
| 860 | is_nothrow_move_constructible<key_equal>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 861 | : __bucket_list_(_STD::move(__u.__bucket_list_)), |
| 862 | __p1_(_STD::move(__u.__p1_)), |
| 863 | __p2_(_STD::move(__u.__p2_)), |
| 864 | __p3_(_STD::move(__u.__p3_)) |
| 865 | { |
| 866 | if (size() > 0) |
| 867 | { |
| 868 | __bucket_list_[__p1_.first().__next_->__hash_ % bucket_count()] = |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 869 | static_cast<__node_pointer>(_STD::addressof(__p1_.first())); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 870 | __u.__p1_.first().__next_ = nullptr; |
| 871 | __u.size() = 0; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 876 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u, |
| 877 | const allocator_type& __a) |
| 878 | : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), |
| 879 | __p1_(__node_allocator(__a)), |
| 880 | __p2_(0, _STD::move(__u.hash_function())), |
| 881 | __p3_(_STD::move(__u.__p3_)) |
| 882 | { |
| 883 | if (__a == allocator_type(__u.__node_alloc())) |
| 884 | { |
| 885 | __bucket_list_.reset(__u.__bucket_list_.release()); |
| 886 | __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size(); |
| 887 | __u.__bucket_list_.get_deleter().size() = 0; |
| 888 | if (__u.size() > 0) |
| 889 | { |
| 890 | __p1_.first().__next_ = __u.__p1_.first().__next_; |
| 891 | __u.__p1_.first().__next_ = nullptr; |
| 892 | __bucket_list_[__p1_.first().__next_->__hash_ % bucket_count()] = |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 893 | static_cast<__node_pointer>(_STD::addressof(__p1_.first())); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 894 | size() = __u.size(); |
| 895 | __u.size() = 0; |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 900 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 901 | |
| 902 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 903 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table() |
| 904 | { |
| 905 | __deallocate(__p1_.first().__next_); |
| 906 | } |
| 907 | |
| 908 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 909 | void |
| 910 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc( |
| 911 | const __hash_table& __u, true_type) |
| 912 | { |
| 913 | if (__node_alloc() != __u.__node_alloc()) |
| 914 | { |
| 915 | clear(); |
| 916 | __bucket_list_.reset(); |
| 917 | __bucket_list_.get_deleter().size() = 0; |
| 918 | } |
| 919 | __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc(); |
| 920 | __node_alloc() = __u.__node_alloc(); |
| 921 | } |
| 922 | |
| 923 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 924 | __hash_table<_Tp, _Hash, _Equal, _Alloc>& |
| 925 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u) |
| 926 | { |
| 927 | if (this != &__u) |
| 928 | { |
| 929 | __copy_assign_alloc(__u); |
| 930 | hash_function() = __u.hash_function(); |
| 931 | key_eq() = __u.key_eq(); |
| 932 | max_load_factor() = __u.max_load_factor(); |
| 933 | __assign_multi(__u.begin(), __u.end()); |
| 934 | } |
| 935 | return *this; |
| 936 | } |
| 937 | |
| 938 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 939 | void |
| 940 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 941 | _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 942 | { |
| 943 | __node_allocator& __na = __node_alloc(); |
| 944 | while (__np != nullptr) |
| 945 | { |
| 946 | __node_pointer __next = __np->__next_; |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 947 | __node_traits::destroy(__na, _STD::addressof(__np->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 948 | __node_traits::deallocate(__na, __np, 1); |
| 949 | __np = __next; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 954 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 955 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 956 | { |
| 957 | size_type __bc = bucket_count(); |
| 958 | for (size_type __i = 0; __i < __bc; ++__i) |
| 959 | __bucket_list_[__i] = nullptr; |
| 960 | size() = 0; |
| 961 | __node_pointer __cache = __p1_.first().__next_; |
| 962 | __p1_.first().__next_ = nullptr; |
| 963 | return __cache; |
| 964 | } |
| 965 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 966 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 967 | |
| 968 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 969 | void |
| 970 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( |
| 971 | __hash_table& __u, true_type) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 972 | _NOEXCEPT_( |
| 973 | is_nothrow_move_assignable<__node_allocator>::value && |
| 974 | is_nothrow_move_assignable<hasher>::value && |
| 975 | is_nothrow_move_assignable<key_equal>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 976 | { |
| 977 | clear(); |
| 978 | __bucket_list_.reset(__u.__bucket_list_.release()); |
| 979 | __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size(); |
| 980 | __u.__bucket_list_.get_deleter().size() = 0; |
| 981 | __move_assign_alloc(__u); |
| 982 | size() = __u.size(); |
| 983 | hash_function() = _STD::move(__u.hash_function()); |
| 984 | max_load_factor() = __u.max_load_factor(); |
| 985 | key_eq() = _STD::move(__u.key_eq()); |
| 986 | __p1_.first().__next_ = __u.__p1_.first().__next_; |
| 987 | if (size() > 0) |
| 988 | { |
| 989 | __bucket_list_[__p1_.first().__next_->__hash_ % bucket_count()] = |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 990 | static_cast<__node_pointer>(_STD::addressof(__p1_.first())); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 991 | __u.__p1_.first().__next_ = nullptr; |
| 992 | __u.size() = 0; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 997 | void |
| 998 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( |
| 999 | __hash_table& __u, false_type) |
| 1000 | { |
| 1001 | if (__node_alloc() == __u.__node_alloc()) |
| 1002 | __move_assign(__u, true_type()); |
| 1003 | else |
| 1004 | { |
| 1005 | hash_function() = _STD::move(__u.hash_function()); |
| 1006 | key_eq() = _STD::move(__u.key_eq()); |
| 1007 | max_load_factor() = __u.max_load_factor(); |
| 1008 | if (bucket_count() != 0) |
| 1009 | { |
| 1010 | __node_pointer __cache = __detach(); |
| 1011 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1012 | try |
| 1013 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1014 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1015 | const_iterator __i = __u.begin(); |
| 1016 | while (__cache != nullptr && __u.size() != 0) |
| 1017 | { |
| 1018 | __cache->__value_ = _STD::move(__u.remove(__i++)->__value_); |
| 1019 | __node_pointer __next = __cache->__next_; |
| 1020 | __node_insert_multi(__cache); |
| 1021 | __cache = __next; |
| 1022 | } |
| 1023 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1024 | } |
| 1025 | catch (...) |
| 1026 | { |
| 1027 | __deallocate(__cache); |
| 1028 | throw; |
| 1029 | } |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1030 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1031 | __deallocate(__cache); |
| 1032 | } |
| 1033 | const_iterator __i = __u.begin(); |
| 1034 | while (__u.size() != 0) |
| 1035 | { |
| 1036 | __node_holder __h = |
| 1037 | __construct_node(_STD::move(__u.remove(__i++)->__value_)); |
| 1038 | __node_insert_multi(__h.get()); |
| 1039 | __h.release(); |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1045 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1046 | __hash_table<_Tp, _Hash, _Equal, _Alloc>& |
| 1047 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 1048 | _NOEXCEPT_( |
| 1049 | __node_traits::propagate_on_container_move_assignment::value && |
| 1050 | is_nothrow_move_assignable<__node_allocator>::value && |
| 1051 | is_nothrow_move_assignable<hasher>::value && |
| 1052 | is_nothrow_move_assignable<key_equal>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1053 | { |
| 1054 | __move_assign(__u, integral_constant<bool, |
| 1055 | __node_traits::propagate_on_container_move_assignment::value>()); |
| 1056 | return *this; |
| 1057 | } |
| 1058 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1059 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1060 | |
| 1061 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1062 | template <class _InputIterator> |
| 1063 | void |
| 1064 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first, |
| 1065 | _InputIterator __last) |
| 1066 | { |
| 1067 | if (bucket_count() != 0) |
| 1068 | { |
| 1069 | __node_pointer __cache = __detach(); |
| 1070 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1071 | try |
| 1072 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1073 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1074 | for (; __cache != nullptr && __first != __last; ++__first) |
| 1075 | { |
| 1076 | __cache->__value_ = *__first; |
| 1077 | __node_pointer __next = __cache->__next_; |
| 1078 | __node_insert_unique(__cache); |
| 1079 | __cache = __next; |
| 1080 | } |
| 1081 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1082 | } |
| 1083 | catch (...) |
| 1084 | { |
| 1085 | __deallocate(__cache); |
| 1086 | throw; |
| 1087 | } |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1088 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1089 | __deallocate(__cache); |
| 1090 | } |
| 1091 | for (; __first != __last; ++__first) |
| 1092 | __insert_unique(*__first); |
| 1093 | } |
| 1094 | |
| 1095 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1096 | template <class _InputIterator> |
| 1097 | void |
| 1098 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first, |
| 1099 | _InputIterator __last) |
| 1100 | { |
| 1101 | if (bucket_count() != 0) |
| 1102 | { |
| 1103 | __node_pointer __cache = __detach(); |
| 1104 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1105 | try |
| 1106 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1107 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1108 | for (; __cache != nullptr && __first != __last; ++__first) |
| 1109 | { |
| 1110 | __cache->__value_ = *__first; |
| 1111 | __node_pointer __next = __cache->__next_; |
| 1112 | __node_insert_multi(__cache); |
| 1113 | __cache = __next; |
| 1114 | } |
| 1115 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1116 | } |
| 1117 | catch (...) |
| 1118 | { |
| 1119 | __deallocate(__cache); |
| 1120 | throw; |
| 1121 | } |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1122 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1123 | __deallocate(__cache); |
| 1124 | } |
| 1125 | for (; __first != __last; ++__first) |
| 1126 | __insert_multi(*__first); |
| 1127 | } |
| 1128 | |
| 1129 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1130 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1131 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 1132 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1133 | { |
| 1134 | return iterator(__p1_.first().__next_); |
| 1135 | } |
| 1136 | |
| 1137 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1138 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1139 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 1140 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1141 | { |
| 1142 | return iterator(nullptr); |
| 1143 | } |
| 1144 | |
| 1145 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1146 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1147 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 1148 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1149 | { |
| 1150 | return const_iterator(__p1_.first().__next_); |
| 1151 | } |
| 1152 | |
| 1153 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1154 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1155 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 1156 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1157 | { |
| 1158 | return const_iterator(nullptr); |
| 1159 | } |
| 1160 | |
| 1161 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1162 | void |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 1163 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1164 | { |
| 1165 | if (size() > 0) |
| 1166 | { |
| 1167 | __deallocate(__p1_.first().__next_); |
| 1168 | __p1_.first().__next_ = nullptr; |
| 1169 | size_type __bc = bucket_count(); |
| 1170 | for (size_type __i; __i < __bc; ++__i) |
| 1171 | __bucket_list_[__i] = nullptr; |
| 1172 | size() = 0; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1177 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
| 1178 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd) |
| 1179 | { |
| 1180 | __nd->__hash_ = hash_function()(__nd->__value_); |
| 1181 | size_type __bc = bucket_count(); |
| 1182 | bool __inserted = false; |
| 1183 | __node_pointer __ndptr; |
| 1184 | size_t __chash; |
| 1185 | if (__bc != 0) |
| 1186 | { |
| 1187 | __chash = __nd->__hash_ % __bc; |
| 1188 | __ndptr = __bucket_list_[__chash]; |
| 1189 | if (__ndptr != nullptr) |
| 1190 | { |
| 1191 | for (__ndptr = __ndptr->__next_; __ndptr != nullptr && |
| 1192 | __ndptr->__hash_ % __bc == __chash; |
| 1193 | __ndptr = __ndptr->__next_) |
| 1194 | { |
| 1195 | if (key_eq()(__ndptr->__value_, __nd->__value_)) |
| 1196 | goto __done; |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | { |
| 1201 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 1202 | { |
| 1203 | rehash(_STD::max<size_type>(2 * __bc + 1, |
| 1204 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 1205 | __bc = bucket_count(); |
| 1206 | __chash = __nd->__hash_ % __bc; |
| 1207 | } |
| 1208 | // insert_after __bucket_list_[__chash], or __first_node if bucket is null |
| 1209 | __node_pointer __pn = __bucket_list_[__chash]; |
| 1210 | if (__pn == nullptr) |
| 1211 | { |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1212 | __pn = static_cast<__node_pointer>(_STD::addressof(__p1_.first())); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1213 | __nd->__next_ = __pn->__next_; |
| 1214 | __pn->__next_ = __nd; |
| 1215 | // fix up __bucket_list_ |
| 1216 | __bucket_list_[__chash] = __pn; |
| 1217 | if (__nd->__next_ != nullptr) |
| 1218 | __bucket_list_[__nd->__next_->__hash_ % __bc] = __nd; |
| 1219 | } |
| 1220 | else |
| 1221 | { |
| 1222 | __nd->__next_ = __pn->__next_; |
| 1223 | __pn->__next_ = __nd; |
| 1224 | } |
| 1225 | __ndptr = __nd; |
| 1226 | // increment size |
| 1227 | ++size(); |
| 1228 | __inserted = true; |
| 1229 | } |
| 1230 | __done: |
| 1231 | return pair<iterator, bool>(iterator(__ndptr), __inserted); |
| 1232 | } |
| 1233 | |
| 1234 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1235 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1236 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp) |
| 1237 | { |
| 1238 | __cp->__hash_ = hash_function()(__cp->__value_); |
| 1239 | size_type __bc = bucket_count(); |
| 1240 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 1241 | { |
| 1242 | rehash(_STD::max<size_type>(2 * __bc + 1, |
| 1243 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 1244 | __bc = bucket_count(); |
| 1245 | } |
| 1246 | size_t __chash = __cp->__hash_ % __bc; |
| 1247 | __node_pointer __pn = __bucket_list_[__chash]; |
| 1248 | if (__pn == nullptr) |
| 1249 | { |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1250 | __pn = static_cast<__node_pointer>(_STD::addressof(__p1_.first())); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1251 | __cp->__next_ = __pn->__next_; |
| 1252 | __pn->__next_ = __cp; |
| 1253 | // fix up __bucket_list_ |
| 1254 | __bucket_list_[__chash] = __pn; |
| 1255 | if (__cp->__next_ != nullptr) |
| 1256 | __bucket_list_[__cp->__next_->__hash_ % __bc] = __cp; |
| 1257 | } |
| 1258 | else |
| 1259 | { |
| 1260 | for (bool __found = false; __pn->__next_ != nullptr && |
| 1261 | __pn->__next_->__hash_ % __bc == __chash; |
| 1262 | __pn = __pn->__next_) |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1263 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1264 | // __found key_eq() action |
| 1265 | // false false loop |
| 1266 | // true true loop |
| 1267 | // false true set __found to true |
| 1268 | // true false break |
| 1269 | if (__found != (__pn->__next_->__hash_ == __cp->__hash_ && |
| 1270 | key_eq()(__pn->__next_->__value_, __cp->__value_))) |
| 1271 | { |
| 1272 | if (!__found) |
| 1273 | __found = true; |
| 1274 | else |
| 1275 | break; |
| 1276 | } |
| 1277 | } |
| 1278 | __cp->__next_ = __pn->__next_; |
| 1279 | __pn->__next_ = __cp; |
| 1280 | if (__cp->__next_ != nullptr) |
| 1281 | { |
| 1282 | size_t __nhash = __cp->__next_->__hash_ % __bc; |
| 1283 | if (__nhash != __chash) |
| 1284 | __bucket_list_[__nhash] = __cp; |
| 1285 | } |
| 1286 | } |
| 1287 | ++size(); |
| 1288 | return iterator(__cp); |
| 1289 | } |
| 1290 | |
| 1291 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1292 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1293 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi( |
| 1294 | const_iterator __p, __node_pointer __cp) |
| 1295 | { |
| 1296 | if (__p != end() && key_eq()(*__p, __cp->__value_)) |
| 1297 | { |
| 1298 | __node_pointer __np = const_cast<__node_pointer>(__p.__node_); |
| 1299 | __cp->__hash_ = __np->__hash_; |
| 1300 | size_type __bc = bucket_count(); |
| 1301 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 1302 | { |
| 1303 | rehash(_STD::max<size_type>(2 * __bc + 1, |
| 1304 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 1305 | __bc = bucket_count(); |
| 1306 | } |
| 1307 | size_t __chash = __cp->__hash_ % __bc; |
| 1308 | __node_pointer __pp = __bucket_list_[__chash]; |
| 1309 | while (__pp->__next_ != __np) |
| 1310 | __pp = __pp->__next_; |
| 1311 | __cp->__next_ = __np; |
| 1312 | __pp->__next_ = __cp; |
| 1313 | ++size(); |
| 1314 | return iterator(__cp); |
| 1315 | } |
| 1316 | return __node_insert_multi(__cp); |
| 1317 | } |
| 1318 | |
| 1319 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1320 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
| 1321 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(const value_type& __x) |
| 1322 | { |
| 1323 | size_t __hash = hash_function()(__x); |
| 1324 | size_type __bc = bucket_count(); |
| 1325 | bool __inserted = false; |
| 1326 | __node_pointer __nd; |
| 1327 | size_t __chash; |
| 1328 | if (__bc != 0) |
| 1329 | { |
| 1330 | __chash = __hash % __bc; |
| 1331 | __nd = __bucket_list_[__chash]; |
| 1332 | if (__nd != nullptr) |
| 1333 | { |
| 1334 | for (__nd = __nd->__next_; __nd != nullptr && |
| 1335 | __nd->__hash_ % __bc == __chash; |
| 1336 | __nd = __nd->__next_) |
| 1337 | { |
| 1338 | if (key_eq()(__nd->__value_, __x)) |
| 1339 | goto __done; |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | { |
| 1344 | __node_holder __h = __construct_node(__x, __hash); |
| 1345 | if (size()+1 > __bc * max_load_factor() || __bc == 0) |
| 1346 | { |
| 1347 | rehash(_STD::max<size_type>(2 * __bc + 1, |
| 1348 | size_type(ceil(float(size() + 1) / max_load_factor())))); |
| 1349 | __bc = bucket_count(); |
| 1350 | __chash = __hash % __bc; |
| 1351 | } |
| 1352 | // insert_after __bucket_list_[__chash], or __first_node if bucket is null |
| 1353 | __node_pointer __pn = __bucket_list_[__chash]; |
| 1354 | if (__pn == nullptr) |
| 1355 | { |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1356 | __pn = static_cast<__node_pointer>(_STD::addressof(__p1_.first())); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1357 | __h->__next_ = __pn->__next_; |
| 1358 | __pn->__next_ = __h.get(); |
| 1359 | // fix up __bucket_list_ |
| 1360 | __bucket_list_[__chash] = __pn; |
| 1361 | if (__h->__next_ != nullptr) |
| 1362 | __bucket_list_[__h->__next_->__hash_ % __bc] = __h.get(); |
| 1363 | } |
| 1364 | else |
| 1365 | { |
| 1366 | __h->__next_ = __pn->__next_; |
| 1367 | __pn->__next_ = __h.get(); |
| 1368 | } |
| 1369 | __nd = __h.release(); |
| 1370 | // increment size |
| 1371 | ++size(); |
| 1372 | __inserted = true; |
| 1373 | } |
| 1374 | __done: |
| 1375 | return pair<iterator, bool>(iterator(__nd), __inserted); |
| 1376 | } |
| 1377 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1378 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1379 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1380 | |
| 1381 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1382 | template <class... _Args> |
| 1383 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
| 1384 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique(_Args&&... __args) |
| 1385 | { |
| 1386 | __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...); |
| 1387 | pair<iterator, bool> __r = __node_insert_unique(__h.get()); |
| 1388 | if (__r.second) |
| 1389 | __h.release(); |
| 1390 | return __r; |
| 1391 | } |
| 1392 | |
| 1393 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1394 | template <class... _Args> |
| 1395 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1396 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args) |
| 1397 | { |
| 1398 | __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...); |
| 1399 | iterator __r = __node_insert_multi(__h.get()); |
| 1400 | __h.release(); |
| 1401 | return __r; |
| 1402 | } |
| 1403 | |
| 1404 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1405 | template <class... _Args> |
| 1406 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1407 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi( |
| 1408 | const_iterator __p, _Args&&... __args) |
| 1409 | { |
| 1410 | __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...); |
| 1411 | iterator __r = __node_insert_multi(__p, __h.get()); |
| 1412 | __h.release(); |
| 1413 | return __r; |
| 1414 | } |
| 1415 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1416 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 1417 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1418 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1419 | template <class _P> |
| 1420 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> |
| 1421 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(_P&& __x) |
| 1422 | { |
| 1423 | __node_holder __h = __construct_node(_STD::forward<_P>(__x)); |
| 1424 | pair<iterator, bool> __r = __node_insert_unique(__h.get()); |
| 1425 | if (__r.second) |
| 1426 | __h.release(); |
| 1427 | return __r; |
| 1428 | } |
| 1429 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1430 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1431 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1432 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1433 | |
| 1434 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1435 | template <class _P> |
| 1436 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1437 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(_P&& __x) |
| 1438 | { |
| 1439 | __node_holder __h = __construct_node(_STD::forward<_P>(__x)); |
| 1440 | iterator __r = __node_insert_multi(__h.get()); |
| 1441 | __h.release(); |
| 1442 | return __r; |
| 1443 | } |
| 1444 | |
| 1445 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1446 | template <class _P> |
| 1447 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1448 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p, |
| 1449 | _P&& __x) |
| 1450 | { |
| 1451 | __node_holder __h = __construct_node(_STD::forward<_P>(__x)); |
| 1452 | iterator __r = __node_insert_multi(__p, __h.get()); |
| 1453 | __h.release(); |
| 1454 | return __r; |
| 1455 | } |
| 1456 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1457 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1458 | |
| 1459 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1460 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1461 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const value_type& __x) |
| 1462 | { |
| 1463 | __node_holder __h = __construct_node(__x); |
| 1464 | iterator __r = __node_insert_multi(__h.get()); |
| 1465 | __h.release(); |
| 1466 | return __r; |
| 1467 | } |
| 1468 | |
| 1469 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1470 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1471 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p, |
| 1472 | const value_type& __x) |
| 1473 | { |
| 1474 | __node_holder __h = __construct_node(__x); |
| 1475 | iterator __r = __node_insert_multi(__p, __h.get()); |
| 1476 | __h.release(); |
| 1477 | return __r; |
| 1478 | } |
| 1479 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1480 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1481 | |
| 1482 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1483 | void |
| 1484 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n) |
| 1485 | { |
| 1486 | __n = __next_prime(_STD::max<size_type>(__n, size() > 0)); |
| 1487 | size_type __bc = bucket_count(); |
| 1488 | if (__n > __bc) |
| 1489 | __rehash(__n); |
| 1490 | else |
| 1491 | { |
| 1492 | __n = _STD::max<size_type> |
| 1493 | ( |
| 1494 | __n, |
| 1495 | __next_prime(size_t(ceil(float(size()) / max_load_factor()))) |
| 1496 | ); |
| 1497 | if (__n < __bc) |
| 1498 | __rehash(__n); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1503 | void |
| 1504 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc) |
| 1505 | { |
| 1506 | __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc(); |
| 1507 | __bucket_list_.reset(__nbc > 0 ? |
| 1508 | __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr); |
| 1509 | __bucket_list_.get_deleter().size() = __nbc; |
| 1510 | if (__nbc > 0) |
| 1511 | { |
| 1512 | for (size_type __i = 0; __i < __nbc; ++__i) |
| 1513 | __bucket_list_[__i] = nullptr; |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1514 | __node_pointer __pp(static_cast<__node_pointer>(_STD::addressof(__p1_.first()))); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1515 | __node_pointer __cp = __pp->__next_; |
| 1516 | if (__cp != nullptr) |
| 1517 | { |
| 1518 | size_type __chash = __cp->__hash_ % __nbc; |
| 1519 | __bucket_list_[__chash] = __pp; |
| 1520 | size_type __phash = __chash; |
| 1521 | for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr; |
| 1522 | __cp = __pp->__next_) |
| 1523 | { |
| 1524 | __chash = __cp->__hash_ % __nbc; |
| 1525 | if (__chash == __phash) |
| 1526 | __pp = __cp; |
| 1527 | else |
| 1528 | { |
| 1529 | if (__bucket_list_[__chash] == nullptr) |
| 1530 | { |
| 1531 | __bucket_list_[__chash] = __pp; |
| 1532 | __pp = __cp; |
| 1533 | __phash = __chash; |
| 1534 | } |
| 1535 | else |
| 1536 | { |
| 1537 | __node_pointer __np = __cp; |
| 1538 | for (; __np->__next_ != nullptr && |
| 1539 | key_eq()(__cp->__value_, __np->__next_->__value_); |
| 1540 | __np = __np->__next_) |
| 1541 | ; |
| 1542 | __pp->__next_ = __np->__next_; |
| 1543 | __np->__next_ = __bucket_list_[__chash]->__next_; |
| 1544 | __bucket_list_[__chash]->__next_ = __cp; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1545 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1554 | template <class _Key> |
| 1555 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1556 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) |
| 1557 | { |
| 1558 | size_t __hash = hash_function()(__k); |
| 1559 | size_type __bc = bucket_count(); |
| 1560 | if (__bc != 0) |
| 1561 | { |
| 1562 | size_t __chash = __hash % __bc; |
| 1563 | __node_pointer __nd = __bucket_list_[__chash]; |
| 1564 | if (__nd != nullptr) |
| 1565 | { |
| 1566 | for (__nd = __nd->__next_; __nd != nullptr && |
| 1567 | __nd->__hash_ % __bc == __chash; |
| 1568 | __nd = __nd->__next_) |
| 1569 | { |
| 1570 | if (key_eq()(__nd->__value_, __k)) |
| 1571 | return iterator(__nd); |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | return end(); |
| 1576 | } |
| 1577 | |
| 1578 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1579 | template <class _Key> |
| 1580 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator |
| 1581 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const |
| 1582 | { |
| 1583 | size_t __hash = hash_function()(__k); |
| 1584 | size_type __bc = bucket_count(); |
| 1585 | if (__bc != 0) |
| 1586 | { |
| 1587 | size_t __chash = __hash % __bc; |
| 1588 | __node_const_pointer __nd = __bucket_list_[__chash]; |
| 1589 | if (__nd != nullptr) |
| 1590 | { |
| 1591 | for (__nd = __nd->__next_; __nd != nullptr && |
| 1592 | __nd->__hash_ % __bc == __chash; |
| 1593 | __nd = __nd->__next_) |
| 1594 | { |
| 1595 | if (key_eq()(__nd->__value_, __k)) |
| 1596 | return const_iterator(__nd); |
| 1597 | } |
| 1598 | } |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1599 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1600 | } |
| 1601 | return end(); |
| 1602 | } |
| 1603 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1604 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1605 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1606 | |
| 1607 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1608 | template <class ..._Args> |
| 1609 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
| 1610 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args) |
| 1611 | { |
| 1612 | __node_allocator& __na = __node_alloc(); |
| 1613 | __node_holder __h(__node_traits::allocate(__na, 1), _D(__na)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1614 | __node_traits::construct(__na, _STD::addressof(__h->__value_), _STD::forward<_Args>(__args)...); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1615 | __h.get_deleter().__value_constructed = true; |
| 1616 | __h->__hash_ = hash_function()(__h->__value_); |
| 1617 | __h->__next_ = nullptr; |
| 1618 | return __h; |
| 1619 | } |
| 1620 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1621 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 1622 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1623 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1624 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
| 1625 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(value_type&& __v, |
| 1626 | size_t __hash) |
| 1627 | { |
| 1628 | __node_allocator& __na = __node_alloc(); |
| 1629 | __node_holder __h(__node_traits::allocate(__na, 1), _D(__na)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1630 | __node_traits::construct(__na, _STD::addressof(__h->__value_), _STD::move(__v)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1631 | __h.get_deleter().__value_constructed = true; |
| 1632 | __h->__hash_ = __hash; |
| 1633 | __h->__next_ = nullptr; |
| 1634 | return _STD::move(__h); |
| 1635 | } |
| 1636 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1637 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1638 | |
| 1639 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1640 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
| 1641 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v) |
| 1642 | { |
| 1643 | __node_allocator& __na = __node_alloc(); |
| 1644 | __node_holder __h(__node_traits::allocate(__na, 1), _D(__na)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1645 | __node_traits::construct(__na, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1646 | __h.get_deleter().__value_constructed = true; |
| 1647 | __h->__hash_ = hash_function()(__h->__value_); |
| 1648 | __h->__next_ = nullptr; |
| 1649 | return _STD::move(__h); |
| 1650 | } |
| 1651 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1652 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1653 | |
| 1654 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1655 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
| 1656 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v, |
| 1657 | size_t __hash) |
| 1658 | { |
| 1659 | __node_allocator& __na = __node_alloc(); |
| 1660 | __node_holder __h(__node_traits::allocate(__na, 1), _D(__na)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1661 | __node_traits::construct(__na, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1662 | __h.get_deleter().__value_constructed = true; |
| 1663 | __h->__hash_ = __hash; |
| 1664 | __h->__next_ = nullptr; |
| 1665 | return _STD::move(__h); |
| 1666 | } |
| 1667 | |
| 1668 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1669 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1670 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p) |
| 1671 | { |
| 1672 | __node_pointer __np = const_cast<__node_pointer>(__p.__node_); |
| 1673 | iterator __r(__np); |
| 1674 | ++__r; |
| 1675 | remove(__p); |
| 1676 | return __r; |
| 1677 | } |
| 1678 | |
| 1679 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1680 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator |
| 1681 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first, |
| 1682 | const_iterator __last) |
| 1683 | { |
| 1684 | for (const_iterator __p = __first; __first != __last; __p = __first) |
| 1685 | { |
| 1686 | ++__first; |
| 1687 | erase(__p); |
| 1688 | } |
| 1689 | __node_pointer __np = const_cast<__node_pointer>(__last.__node_); |
| 1690 | return iterator (__np); |
| 1691 | } |
| 1692 | |
| 1693 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1694 | template <class _Key> |
| 1695 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 1696 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k) |
| 1697 | { |
| 1698 | iterator __i = find(__k); |
| 1699 | if (__i == end()) |
| 1700 | return 0; |
| 1701 | erase(__i); |
| 1702 | return 1; |
| 1703 | } |
| 1704 | |
| 1705 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1706 | template <class _Key> |
| 1707 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 1708 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k) |
| 1709 | { |
| 1710 | size_type __r = 0; |
| 1711 | iterator __i = find(__k); |
| 1712 | if (__i != end()) |
| 1713 | { |
| 1714 | iterator __e = end(); |
| 1715 | do |
| 1716 | { |
| 1717 | erase(__i++); |
| 1718 | ++__r; |
| 1719 | } while (__i != __e && key_eq()(*__i, __k)); |
| 1720 | } |
| 1721 | return __r; |
| 1722 | } |
| 1723 | |
| 1724 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1725 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 1726 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1727 | { |
| 1728 | // current node |
| 1729 | __node_pointer __cn = const_cast<__node_pointer>(__p.__node_); |
| 1730 | size_type __bc = bucket_count(); |
| 1731 | size_t __chash = __cn->__hash_ % __bc; |
| 1732 | // find previous node |
| 1733 | __node_pointer __pn = __bucket_list_[__chash]; |
| 1734 | for (; __pn->__next_ != __cn; __pn = __pn->__next_) |
| 1735 | ; |
| 1736 | // Fix up __bucket_list_ |
| 1737 | // if __pn is not in same bucket (before begin is not in same bucket) && |
| 1738 | // if __cn->__next_ is not in same bucket (nullptr is not in same bucket) |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1739 | if (__pn == _STD::addressof(__p1_.first()) || __pn->__hash_ % __bc != __chash) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1740 | { |
| 1741 | if (__cn->__next_ == nullptr || __cn->__next_->__hash_ % __bc != __chash) |
| 1742 | __bucket_list_[__chash] = nullptr; |
| 1743 | } |
| 1744 | // if __cn->__next_ is not in same bucket (nullptr is in same bucket) |
| 1745 | if (__cn->__next_ != nullptr) |
| 1746 | { |
| 1747 | size_t __nhash = __cn->__next_->__hash_ % __bc; |
| 1748 | if (__nhash != __chash) |
| 1749 | __bucket_list_[__nhash] = __pn; |
| 1750 | } |
| 1751 | // remove __cn |
| 1752 | __pn->__next_ = __cn->__next_; |
| 1753 | __cn->__next_ = nullptr; |
| 1754 | --size(); |
| 1755 | return __node_holder(__cn, _D(__node_alloc())); |
| 1756 | } |
| 1757 | |
| 1758 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1759 | template <class _Key> |
Howard Hinnant | 99acc50 | 2010-09-21 17:32:39 +0000 | [diff] [blame] | 1760 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1761 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 1762 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const |
| 1763 | { |
| 1764 | return static_cast<size_type>(find(__k) != end()); |
| 1765 | } |
| 1766 | |
| 1767 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1768 | template <class _Key> |
| 1769 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 1770 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const |
| 1771 | { |
| 1772 | size_type __r = 0; |
| 1773 | const_iterator __i = find(__k); |
| 1774 | if (__i != end()) |
| 1775 | { |
| 1776 | const_iterator __e = end(); |
| 1777 | do |
| 1778 | { |
| 1779 | ++__i; |
| 1780 | ++__r; |
| 1781 | } while (__i != __e && key_eq()(*__i, __k)); |
| 1782 | } |
| 1783 | return __r; |
| 1784 | } |
| 1785 | |
| 1786 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1787 | template <class _Key> |
| 1788 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, |
| 1789 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator> |
| 1790 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique( |
| 1791 | const _Key& __k) |
| 1792 | { |
| 1793 | iterator __i = find(__k); |
| 1794 | iterator __j = __i; |
| 1795 | if (__i != end()) |
| 1796 | ++__j; |
| 1797 | return pair<iterator, iterator>(__i, __j); |
| 1798 | } |
| 1799 | |
| 1800 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1801 | template <class _Key> |
| 1802 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator, |
| 1803 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator> |
| 1804 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique( |
| 1805 | const _Key& __k) const |
| 1806 | { |
| 1807 | const_iterator __i = find(__k); |
| 1808 | const_iterator __j = __i; |
| 1809 | if (__i != end()) |
| 1810 | ++__j; |
| 1811 | return pair<const_iterator, const_iterator>(__i, __j); |
| 1812 | } |
| 1813 | |
| 1814 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1815 | template <class _Key> |
| 1816 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, |
| 1817 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator> |
| 1818 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi( |
| 1819 | const _Key& __k) |
| 1820 | { |
| 1821 | iterator __i = find(__k); |
| 1822 | iterator __j = __i; |
| 1823 | if (__i != end()) |
| 1824 | { |
| 1825 | iterator __e = end(); |
| 1826 | do |
| 1827 | { |
| 1828 | ++__j; |
| 1829 | } while (__j != __e && key_eq()(*__j, __k)); |
| 1830 | } |
| 1831 | return pair<iterator, iterator>(__i, __j); |
| 1832 | } |
| 1833 | |
| 1834 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1835 | template <class _Key> |
| 1836 | pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator, |
| 1837 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator> |
| 1838 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi( |
| 1839 | const _Key& __k) const |
| 1840 | { |
| 1841 | const_iterator __i = find(__k); |
| 1842 | const_iterator __j = __i; |
| 1843 | if (__i != end()) |
| 1844 | { |
| 1845 | const_iterator __e = end(); |
| 1846 | do |
| 1847 | { |
| 1848 | ++__j; |
| 1849 | } while (__j != __e && key_eq()(*__j, __k)); |
| 1850 | } |
| 1851 | return pair<const_iterator, const_iterator>(__i, __j); |
| 1852 | } |
| 1853 | |
| 1854 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1855 | void |
| 1856 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u) |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 1857 | _NOEXCEPT_( |
| 1858 | (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value || |
| 1859 | __is_nothrow_swappable<__pointer_allocator>::value) && |
| 1860 | (!__node_traits::propagate_on_container_swap::value || |
| 1861 | __is_nothrow_swappable<__node_allocator>::value) && |
| 1862 | __is_nothrow_swappable<hasher>::value && |
| 1863 | __is_nothrow_swappable<key_equal>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1864 | { |
| 1865 | { |
| 1866 | __node_pointer_pointer __npp = __bucket_list_.release(); |
| 1867 | __bucket_list_.reset(__u.__bucket_list_.release()); |
| 1868 | __u.__bucket_list_.reset(__npp); |
| 1869 | } |
| 1870 | _STD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size()); |
| 1871 | __swap_alloc(__bucket_list_.get_deleter().__alloc(), |
| 1872 | __u.__bucket_list_.get_deleter().__alloc()); |
| 1873 | __swap_alloc(__node_alloc(), __u.__node_alloc()); |
| 1874 | _STD::swap(__p1_.first().__next_, __u.__p1_.first().__next_); |
| 1875 | __p2_.swap(__u.__p2_); |
| 1876 | __p3_.swap(__u.__p3_); |
| 1877 | if (size() > 0) |
| 1878 | __bucket_list_[__p1_.first().__next_->__hash_ % bucket_count()] = |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1879 | static_cast<__node_pointer>(_STD::addressof(__p1_.first())); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1880 | if (__u.size() > 0) |
| 1881 | __u.__bucket_list_[__u.__p1_.first().__next_->__hash_ % __u.bucket_count()] = |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1882 | static_cast<__node_pointer>(_STD::addressof(__u.__p1_.first())); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1886 | typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type |
| 1887 | __hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const |
| 1888 | { |
| 1889 | __node_const_pointer __np = __bucket_list_[__n]; |
| 1890 | size_type __bc = bucket_count(); |
| 1891 | size_type __r = 0; |
| 1892 | if (__np != nullptr) |
| 1893 | { |
| 1894 | for (__np = __np->__next_; __np != nullptr && |
| 1895 | __np->__hash_ % __bc == __n; |
| 1896 | __np = __np->__next_, ++__r) |
| 1897 | ; |
| 1898 | } |
| 1899 | return __r; |
| 1900 | } |
| 1901 | |
Howard Hinnant | 5f2f14c | 2011-06-04 18:54:24 +0000 | [diff] [blame^] | 1902 | template <class _Tp, class _Hash, class _Equal, class _Alloc> |
| 1903 | inline _LIBCPP_INLINE_VISIBILITY |
| 1904 | void |
| 1905 | swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x, |
| 1906 | __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y) |
| 1907 | _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) |
| 1908 | { |
| 1909 | __x.swap(__y); |
| 1910 | } |
| 1911 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1912 | _LIBCPP_END_NAMESPACE_STD |
| 1913 | |
| 1914 | #endif // _LIBCPP__HASH_TABLE |