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