| 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP___TREE |
| 11 | #define _LIBCPP___TREE |
| 12 | |
| 13 | #include <__algorithm/min.h> |
| 14 | #include <__algorithm/specialized_algorithms.h> |
| 15 | #include <__assert> |
| 16 | #include <__config> |
| 17 | #include <__fwd/pair.h> |
| 18 | #include <__iterator/distance.h> |
| 19 | #include <__iterator/iterator_traits.h> |
| 20 | #include <__iterator/next.h> |
| 21 | #include <__memory/addressof.h> |
| 22 | #include <__memory/allocator_traits.h> |
| 23 | #include <__memory/compressed_pair.h> |
| 24 | #include <__memory/construct_at.h> |
| 25 | #include <__memory/pointer_traits.h> |
| 26 | #include <__memory/swap_allocator.h> |
| 27 | #include <__memory/unique_ptr.h> |
| 28 | #include <__new/launder.h> |
| 29 | #include <__type_traits/copy_cvref.h> |
| 30 | #include <__type_traits/enable_if.h> |
| 31 | #include <__type_traits/invoke.h> |
| 32 | #include <__type_traits/is_constructible.h> |
| 33 | #include <__type_traits/is_nothrow_assignable.h> |
| 34 | #include <__type_traits/is_nothrow_constructible.h> |
| 35 | #include <__type_traits/is_same.h> |
| 36 | #include <__type_traits/is_specialization.h> |
| 37 | #include <__type_traits/is_swappable.h> |
| 38 | #include <__type_traits/make_transparent.h> |
| 39 | #include <__type_traits/remove_const.h> |
| 40 | #include <__type_traits/remove_cvref.h> |
| 41 | #include <__utility/forward.h> |
| 42 | #include <__utility/lazy_synth_three_way_comparator.h> |
| 43 | #include <__utility/move.h> |
| 44 | #include <__utility/pair.h> |
| 45 | #include <__utility/swap.h> |
| 46 | #include <__utility/try_key_extraction.h> |
| 47 | #include <limits> |
| 48 | |
| 49 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 50 | # pragma GCC system_header |
| 51 | #endif |
| 52 | |
| 53 | _LIBCPP_PUSH_MACROS |
| 54 | #include <__undef_macros> |
| 55 | |
| 56 | _LIBCPP_DIAGNOSTIC_PUSH |
| 57 | // GCC complains about the backslashes at the end, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121528 |
| 58 | _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wcomment" ) |
| 59 | // __tree is a red-black-tree implementation used for the associative containers (i.e. (multi)map/set). It stores |
| 60 | // - (1) a pointer to the node with the smallest (i.e. leftmost) element, namely __begin_node_ |
| 61 | // - (2) the number of nodes in the tree, namely __size_ |
| 62 | // - (3) a pointer to the root of the tree, namely __end_node_ |
| 63 | // |
| 64 | // Storing (1) and (2) is required to allow for constant time lookups. A tree looks like this in memory: |
| 65 | // |
| 66 | // __end_node_ |
| 67 | // | |
| 68 | // root |
| 69 | // / \ |
| 70 | // l1 r1 |
| 71 | // / \ / \ |
| 72 | // ... ... ... ... |
| 73 | // |
| 74 | // All nodes except __end_node_ have a __left_ and __right_ pointer as well as a __parent_ pointer. |
| 75 | // __end_node_ only contains a __left_ pointer, which points to the root of the tree. |
| 76 | // This layout allows for iteration through the tree without a need for special handling of the end node. See |
| 77 | // __tree_next_iter and __tree_prev_iter for more details. |
| 78 | _LIBCPP_DIAGNOSTIC_POP |
| 79 | |
| 80 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 81 | |
| 82 | template <class _Pointer> |
| 83 | class __tree_end_node; |
| 84 | template <class _VoidPtr> |
| 85 | class __tree_node_base; |
| 86 | template <class _Tp, class _VoidPtr> |
| 87 | class __tree_node; |
| 88 | |
| 89 | template <class _Key, class _Value> |
| 90 | struct __value_type; |
| 91 | |
| 92 | /* |
| 93 | |
| 94 | _NodePtr algorithms |
| 95 | |
| 96 | The algorithms taking _NodePtr are red black tree algorithms. Those |
| 97 | algorithms taking a parameter named __root should assume that __root |
| 98 | points to a proper red black tree (unless otherwise specified). |
| 99 | |
| 100 | Each algorithm herein assumes that __root->__parent_ points to a non-null |
| 101 | structure which has a member __left_ which points back to __root. No other |
| 102 | member is read or written to at __root->__parent_. |
| 103 | |
| 104 | __root->__parent_ will be referred to below (in comments only) as end_node. |
| 105 | end_node->__left_ is an externably accessible lvalue for __root, and can be |
| 106 | changed by node insertion and removal (without explicit reference to end_node). |
| 107 | |
| 108 | All nodes (with the exception of end_node), even the node referred to as |
| 109 | __root, have a non-null __parent_ field. |
| 110 | |
| 111 | */ |
| 112 | |
| 113 | // Returns: true if __x is a left child of its parent, else false |
| 114 | // Precondition: __x != nullptr. |
| 115 | template <class _NodePtr> |
| 116 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool __tree_is_left_child(_NodePtr __x) _NOEXCEPT { |
| 117 | return __x == __x->__parent_->__left_; |
| 118 | } |
| 119 | |
| 120 | // Determines if the subtree rooted at __x is a proper red black subtree. If |
| 121 | // __x is a proper subtree, returns the black height (null counts as 1). If |
| 122 | // __x is an improper subtree, returns 0. |
| 123 | template <class _NodePtr> |
| 124 | _LIBCPP_CONSTEXPR_SINCE_CXX26 unsigned __tree_sub_invariant(_NodePtr __x) { |
| 125 | if (__x == nullptr) |
| 126 | return 1; |
| 127 | // parent consistency checked by caller |
| 128 | // check __x->__left_ consistency |
| 129 | if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x) |
| 130 | return 0; |
| 131 | // check __x->__right_ consistency |
| 132 | if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x) |
| 133 | return 0; |
| 134 | // check __x->__left_ != __x->__right_ unless both are nullptr |
| 135 | if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr) |
| 136 | return 0; |
| 137 | // If this is red, neither child can be red |
| 138 | if (!__x->__is_black_) { |
| 139 | if (__x->__left_ && !__x->__left_->__is_black_) |
| 140 | return 0; |
| 141 | if (__x->__right_ && !__x->__right_->__is_black_) |
| 142 | return 0; |
| 143 | } |
| 144 | unsigned __h = std::__tree_sub_invariant(__x->__left_); |
| 145 | if (__h == 0) |
| 146 | return 0; // invalid left subtree |
| 147 | if (__h != std::__tree_sub_invariant(__x->__right_)) |
| 148 | return 0; // invalid or different height right subtree |
| 149 | return __h + __x->__is_black_; // return black height of this node |
| 150 | } |
| 151 | |
| 152 | // Determines if the red black tree rooted at __root is a proper red black tree. |
| 153 | // __root == nullptr is a proper tree. Returns true if __root is a proper |
| 154 | // red black tree, else returns false. |
| 155 | template <class _NodePtr> |
| 156 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool __tree_invariant(_NodePtr __root) { |
| 157 | if (__root == nullptr) |
| 158 | return true; |
| 159 | // check __x->__parent_ consistency |
| 160 | if (__root->__parent_ == nullptr) |
| 161 | return false; |
| 162 | if (!std::__tree_is_left_child(__root)) |
| 163 | return false; |
| 164 | // root must be black |
| 165 | if (!__root->__is_black_) |
| 166 | return false; |
| 167 | // do normal node checks |
| 168 | return std::__tree_sub_invariant(__root) != 0; |
| 169 | } |
| 170 | |
| 171 | // Returns: pointer to the left-most node under __x. |
| 172 | template <class _NodePtr> |
| 173 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodePtr __tree_min(_NodePtr __x) _NOEXCEPT { |
| 174 | _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Root node shouldn't be null" ); |
| 175 | while (__x->__left_ != nullptr) |
| 176 | __x = __x->__left_; |
| 177 | return __x; |
| 178 | } |
| 179 | |
| 180 | // Returns: pointer to the right-most node under __x. |
| 181 | template <class _NodePtr> |
| 182 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodePtr __tree_max(_NodePtr __x) _NOEXCEPT { |
| 183 | _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Root node shouldn't be null" ); |
| 184 | while (__x->__right_ != nullptr) |
| 185 | __x = __x->__right_; |
| 186 | return __x; |
| 187 | } |
| 188 | |
| 189 | // Returns: pointer to the next in-order node after __x. |
| 190 | template <class _NodePtr> |
| 191 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodePtr __tree_next(_NodePtr __x) _NOEXCEPT { |
| 192 | _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null" ); |
| 193 | if (__x->__right_ != nullptr) |
| 194 | return std::__tree_min(__x->__right_); |
| 195 | while (!std::__tree_is_left_child(__x)) |
| 196 | __x = __x->__parent_unsafe(); |
| 197 | return __x->__parent_unsafe(); |
| 198 | } |
| 199 | |
| 200 | // __tree_next_iter and __tree_prev_iter implement iteration through the tree. The order is as follows: |
| 201 | // left sub-tree -> node -> right sub-tree. When the right-most node of a sub-tree is reached, we walk up the tree until |
| 202 | // we find a node where we were in the left sub-tree. We are _always_ in a left sub-tree, since the __end_node_ points |
| 203 | // to the actual root of the tree through a __left_ pointer. Incrementing the end() pointer is UB, so we can assume that |
| 204 | // never happens. |
| 205 | template <class _EndNodePtr, class _NodePtr> |
| 206 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _EndNodePtr __tree_next_iter(_NodePtr __x) _NOEXCEPT { |
| 207 | _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null" ); |
| 208 | if (__x->__right_ != nullptr) |
| 209 | return std::__static_fancy_pointer_cast<_EndNodePtr>(std::__tree_min(__x->__right_)); |
| 210 | while (!std::__tree_is_left_child(__x)) |
| 211 | __x = __x->__parent_unsafe(); |
| 212 | return std::__static_fancy_pointer_cast<_EndNodePtr>(__x->__parent_); |
| 213 | } |
| 214 | |
| 215 | // Returns: pointer to the previous in-order node before __x. |
| 216 | // Note: __x may be the end node. |
| 217 | template <class _NodePtr, class _EndNodePtr> |
| 218 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodePtr __tree_prev_iter(_EndNodePtr __x) _NOEXCEPT { |
| 219 | _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null" ); |
| 220 | if (__x->__left_ != nullptr) |
| 221 | return std::__tree_max(__x->__left_); |
| 222 | _NodePtr __xx = std::__static_fancy_pointer_cast<_NodePtr>(__x); |
| 223 | while (std::__tree_is_left_child(__xx)) |
| 224 | __xx = __xx->__parent_unsafe(); |
| 225 | return __xx->__parent_unsafe(); |
| 226 | } |
| 227 | |
| 228 | // Returns: pointer to a node which has no children |
| 229 | template <class _NodePtr> |
| 230 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodePtr __tree_leaf(_NodePtr __x) _NOEXCEPT { |
| 231 | _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null" ); |
| 232 | while (true) { |
| 233 | if (__x->__left_ != nullptr) { |
| 234 | __x = __x->__left_; |
| 235 | continue; |
| 236 | } |
| 237 | if (__x->__right_ != nullptr) { |
| 238 | __x = __x->__right_; |
| 239 | continue; |
| 240 | } |
| 241 | break; |
| 242 | } |
| 243 | return __x; |
| 244 | } |
| 245 | |
| 246 | // Effects: Makes __x->__right_ the subtree root with __x as its left child |
| 247 | // while preserving in-order order. |
| 248 | template <class _NodePtr> |
| 249 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __tree_left_rotate(_NodePtr __x) _NOEXCEPT { |
| 250 | _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null" ); |
| 251 | _LIBCPP_ASSERT_INTERNAL(__x->__right_ != nullptr, "node should have a right child" ); |
| 252 | _NodePtr __y = __x->__right_; |
| 253 | __x->__right_ = __y->__left_; |
| 254 | if (__x->__right_ != nullptr) |
| 255 | __x->__right_->__set_parent(__x); |
| 256 | __y->__parent_ = __x->__parent_; |
| 257 | if (std::__tree_is_left_child(__x)) |
| 258 | __x->__parent_->__left_ = __y; |
| 259 | else |
| 260 | __x->__parent_unsafe()->__right_ = __y; |
| 261 | __y->__left_ = __x; |
| 262 | __x->__set_parent(__y); |
| 263 | } |
| 264 | |
| 265 | // Effects: Makes __x->__left_ the subtree root with __x as its right child |
| 266 | // while preserving in-order order. |
| 267 | template <class _NodePtr> |
| 268 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __tree_right_rotate(_NodePtr __x) _NOEXCEPT { |
| 269 | _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null" ); |
| 270 | _LIBCPP_ASSERT_INTERNAL(__x->__left_ != nullptr, "node should have a left child" ); |
| 271 | _NodePtr __y = __x->__left_; |
| 272 | __x->__left_ = __y->__right_; |
| 273 | if (__x->__left_ != nullptr) |
| 274 | __x->__left_->__set_parent(__x); |
| 275 | __y->__parent_ = __x->__parent_; |
| 276 | if (std::__tree_is_left_child(__x)) |
| 277 | __x->__parent_->__left_ = __y; |
| 278 | else |
| 279 | __x->__parent_unsafe()->__right_ = __y; |
| 280 | __y->__right_ = __x; |
| 281 | __x->__set_parent(__y); |
| 282 | } |
| 283 | |
| 284 | // Effects: Rebalances __root after attaching __x to a leaf. |
| 285 | // Precondition: __x has no children. |
| 286 | // __x == __root or == a direct or indirect child of __root. |
| 287 | // If __x were to be unlinked from __root (setting __root to |
| 288 | // nullptr if __root == __x), __tree_invariant(__root) == true. |
| 289 | // Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_ |
| 290 | // may be different than the value passed in as __root. |
| 291 | template <class _NodePtr> |
| 292 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 293 | __tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT { |
| 294 | _LIBCPP_ASSERT_INTERNAL(__root != nullptr, "Root of the tree shouldn't be null" ); |
| 295 | _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Can't attach null node to a leaf" ); |
| 296 | __x->__is_black_ = __x == __root; |
| 297 | while (__x != __root && !__x->__parent_unsafe()->__is_black_) { |
| 298 | // __x->__parent_ != __root because __x->__parent_->__is_black == false |
| 299 | if (std::__tree_is_left_child(__x->__parent_unsafe())) { |
| 300 | _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_; |
| 301 | if (__y != nullptr && !__y->__is_black_) { |
| 302 | __x = __x->__parent_unsafe(); |
| 303 | __x->__is_black_ = true; |
| 304 | __x = __x->__parent_unsafe(); |
| 305 | __x->__is_black_ = __x == __root; |
| 306 | __y->__is_black_ = true; |
| 307 | } else { |
| 308 | if (!std::__tree_is_left_child(__x)) { |
| 309 | __x = __x->__parent_unsafe(); |
| 310 | std::__tree_left_rotate(__x); |
| 311 | } |
| 312 | __x = __x->__parent_unsafe(); |
| 313 | __x->__is_black_ = true; |
| 314 | __x = __x->__parent_unsafe(); |
| 315 | __x->__is_black_ = false; |
| 316 | std::__tree_right_rotate(__x); |
| 317 | break; |
| 318 | } |
| 319 | } else { |
| 320 | _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_; |
| 321 | if (__y != nullptr && !__y->__is_black_) { |
| 322 | __x = __x->__parent_unsafe(); |
| 323 | __x->__is_black_ = true; |
| 324 | __x = __x->__parent_unsafe(); |
| 325 | __x->__is_black_ = __x == __root; |
| 326 | __y->__is_black_ = true; |
| 327 | } else { |
| 328 | if (std::__tree_is_left_child(__x)) { |
| 329 | __x = __x->__parent_unsafe(); |
| 330 | std::__tree_right_rotate(__x); |
| 331 | } |
| 332 | __x = __x->__parent_unsafe(); |
| 333 | __x->__is_black_ = true; |
| 334 | __x = __x->__parent_unsafe(); |
| 335 | __x->__is_black_ = false; |
| 336 | std::__tree_left_rotate(__x); |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // Precondition: __z == __root or == a direct or indirect child of __root. |
| 344 | // Effects: unlinks __z from the tree rooted at __root, rebalancing as needed. |
| 345 | // Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_ |
| 346 | // nor any of its children refer to __z. end_node->__left_ |
| 347 | // may be different than the value passed in as __root. |
| 348 | template <class _NodePtr> |
| 349 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT { |
| 350 | _LIBCPP_ASSERT_INTERNAL(__root != nullptr, "Root node should not be null" ); |
| 351 | _LIBCPP_ASSERT_INTERNAL(__z != nullptr, "The node to remove should not be null" ); |
| 352 | _LIBCPP_ASSERT_INTERNAL(std::__tree_invariant(__root), "The tree invariants should hold" ); |
| 353 | // __z will be removed from the tree. Client still needs to destruct/deallocate it |
| 354 | // __y is either __z, or if __z has two children, __tree_next(__z). |
| 355 | // __y will have at most one child. |
| 356 | // __y will be the initial hole in the tree (make the hole at a leaf) |
| 357 | _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ? __z : std::__tree_next(__z); |
| 358 | // __x is __y's possibly null single child |
| 359 | _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_; |
| 360 | // __w is __x's possibly null uncle (will become __x's sibling) |
| 361 | _NodePtr __w = nullptr; |
| 362 | // link __x to __y's parent, and find __w |
| 363 | if (__x != nullptr) |
| 364 | __x->__parent_ = __y->__parent_; |
| 365 | if (std::__tree_is_left_child(__y)) { |
| 366 | __y->__parent_->__left_ = __x; |
| 367 | if (__y != __root) |
| 368 | __w = __y->__parent_unsafe()->__right_; |
| 369 | else |
| 370 | __root = __x; // __w == nullptr |
| 371 | } else { |
| 372 | __y->__parent_unsafe()->__right_ = __x; |
| 373 | // __y can't be root if it is a right child |
| 374 | __w = __y->__parent_->__left_; |
| 375 | } |
| 376 | bool __removed_black = __y->__is_black_; |
| 377 | // If we didn't remove __z, do so now by splicing in __y for __z, |
| 378 | // but copy __z's color. This does not impact __x or __w. |
| 379 | if (__y != __z) { |
| 380 | // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr |
| 381 | __y->__parent_ = __z->__parent_; |
| 382 | if (std::__tree_is_left_child(__z)) |
| 383 | __y->__parent_->__left_ = __y; |
| 384 | else |
| 385 | __y->__parent_unsafe()->__right_ = __y; |
| 386 | __y->__left_ = __z->__left_; |
| 387 | __y->__left_->__set_parent(__y); |
| 388 | __y->__right_ = __z->__right_; |
| 389 | if (__y->__right_ != nullptr) |
| 390 | __y->__right_->__set_parent(__y); |
| 391 | __y->__is_black_ = __z->__is_black_; |
| 392 | if (__root == __z) |
| 393 | __root = __y; |
| 394 | } |
| 395 | // There is no need to rebalance if we removed a red, or if we removed |
| 396 | // the last node. |
| 397 | if (__removed_black && __root != nullptr) { |
| 398 | // Rebalance: |
| 399 | // __x has an implicit black color (transferred from the removed __y) |
| 400 | // associated with it, no matter what its color is. |
| 401 | // If __x is __root (in which case it can't be null), it is supposed |
| 402 | // to be black anyway, and if it is doubly black, then the double |
| 403 | // can just be ignored. |
| 404 | // If __x is red (in which case it can't be null), then it can absorb |
| 405 | // the implicit black just by setting its color to black. |
| 406 | // Since __y was black and only had one child (which __x points to), __x |
| 407 | // is either red with no children, else null, otherwise __y would have |
| 408 | // different black heights under left and right pointers. |
| 409 | // if (__x == __root || __x != nullptr && !__x->__is_black_) |
| 410 | if (__x != nullptr) |
| 411 | __x->__is_black_ = true; |
| 412 | else { |
| 413 | // Else __x isn't root, and is "doubly black", even though it may |
| 414 | // be null. __w can not be null here, else the parent would |
| 415 | // see a black height >= 2 on the __x side and a black height |
| 416 | // of 1 on the __w side (__w must be a non-null black or a red |
| 417 | // with a non-null black child). |
| 418 | while (true) { |
| 419 | if (!std::__tree_is_left_child(__w)) // if x is left child |
| 420 | { |
| 421 | if (!__w->__is_black_) { |
| 422 | __w->__is_black_ = true; |
| 423 | __w->__parent_unsafe()->__is_black_ = false; |
| 424 | std::__tree_left_rotate(__w->__parent_unsafe()); |
| 425 | // __x is still valid |
| 426 | // reset __root only if necessary |
| 427 | if (__root == __w->__left_) |
| 428 | __root = __w; |
| 429 | // reset sibling, and it still can't be null |
| 430 | __w = __w->__left_->__right_; |
| 431 | } |
| 432 | // __w->__is_black_ is now true, __w may have null children |
| 433 | if ((__w->__left_ == nullptr || __w->__left_->__is_black_) && |
| 434 | (__w->__right_ == nullptr || __w->__right_->__is_black_)) { |
| 435 | __w->__is_black_ = false; |
| 436 | __x = __w->__parent_unsafe(); |
| 437 | // __x can no longer be null |
| 438 | if (__x == __root || !__x->__is_black_) { |
| 439 | __x->__is_black_ = true; |
| 440 | break; |
| 441 | } |
| 442 | // reset sibling, and it still can't be null |
| 443 | __w = std::__tree_is_left_child(__x) ? __x->__parent_unsafe()->__right_ : __x->__parent_->__left_; |
| 444 | // continue; |
| 445 | } else // __w has a red child |
| 446 | { |
| 447 | if (__w->__right_ == nullptr || __w->__right_->__is_black_) { |
| 448 | // __w left child is non-null and red |
| 449 | __w->__left_->__is_black_ = true; |
| 450 | __w->__is_black_ = false; |
| 451 | std::__tree_right_rotate(__w); |
| 452 | // __w is known not to be root, so root hasn't changed |
| 453 | // reset sibling, and it still can't be null |
| 454 | __w = __w->__parent_unsafe(); |
| 455 | } |
| 456 | // __w has a right red child, left child may be null |
| 457 | __w->__is_black_ = __w->__parent_unsafe()->__is_black_; |
| 458 | __w->__parent_unsafe()->__is_black_ = true; |
| 459 | __w->__right_->__is_black_ = true; |
| 460 | std::__tree_left_rotate(__w->__parent_unsafe()); |
| 461 | break; |
| 462 | } |
| 463 | } else { |
| 464 | if (!__w->__is_black_) { |
| 465 | __w->__is_black_ = true; |
| 466 | __w->__parent_unsafe()->__is_black_ = false; |
| 467 | std::__tree_right_rotate(__w->__parent_unsafe()); |
| 468 | // __x is still valid |
| 469 | // reset __root only if necessary |
| 470 | if (__root == __w->__right_) |
| 471 | __root = __w; |
| 472 | // reset sibling, and it still can't be null |
| 473 | __w = __w->__right_->__left_; |
| 474 | } |
| 475 | // __w->__is_black_ is now true, __w may have null children |
| 476 | if ((__w->__left_ == nullptr || __w->__left_->__is_black_) && |
| 477 | (__w->__right_ == nullptr || __w->__right_->__is_black_)) { |
| 478 | __w->__is_black_ = false; |
| 479 | __x = __w->__parent_unsafe(); |
| 480 | // __x can no longer be null |
| 481 | if (!__x->__is_black_ || __x == __root) { |
| 482 | __x->__is_black_ = true; |
| 483 | break; |
| 484 | } |
| 485 | // reset sibling, and it still can't be null |
| 486 | __w = std::__tree_is_left_child(__x) ? __x->__parent_unsafe()->__right_ : __x->__parent_->__left_; |
| 487 | // continue; |
| 488 | } else // __w has a red child |
| 489 | { |
| 490 | if (__w->__left_ == nullptr || __w->__left_->__is_black_) { |
| 491 | // __w right child is non-null and red |
| 492 | __w->__right_->__is_black_ = true; |
| 493 | __w->__is_black_ = false; |
| 494 | std::__tree_left_rotate(__w); |
| 495 | // __w is known not to be root, so root hasn't changed |
| 496 | // reset sibling, and it still can't be null |
| 497 | __w = __w->__parent_unsafe(); |
| 498 | } |
| 499 | // __w has a left red child, right child may be null |
| 500 | __w->__is_black_ = __w->__parent_unsafe()->__is_black_; |
| 501 | __w->__parent_unsafe()->__is_black_ = true; |
| 502 | __w->__left_->__is_black_ = true; |
| 503 | std::__tree_right_rotate(__w->__parent_unsafe()); |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | // node traits |
| 513 | |
| 514 | template <class _Tp> |
| 515 | inline const bool __is_tree_value_type_v = __is_specialization_v<_Tp, __value_type>; |
| 516 | |
| 517 | template <class _Tp> |
| 518 | struct __get_tree_key_type { |
| 519 | using type _LIBCPP_NODEBUG = _Tp; |
| 520 | }; |
| 521 | |
| 522 | template <class _Key, class _ValueT> |
| 523 | struct __get_tree_key_type<__value_type<_Key, _ValueT> > { |
| 524 | using type _LIBCPP_NODEBUG = _Key; |
| 525 | }; |
| 526 | |
| 527 | template <class _Tp> |
| 528 | using __get_tree_key_type_t _LIBCPP_NODEBUG = typename __get_tree_key_type<_Tp>::type; |
| 529 | |
| 530 | template <class _Tp> |
| 531 | struct __get_node_value_type { |
| 532 | using type _LIBCPP_NODEBUG = _Tp; |
| 533 | }; |
| 534 | |
| 535 | template <class _Key, class _ValueT> |
| 536 | struct __get_node_value_type<__value_type<_Key, _ValueT> > { |
| 537 | using type _LIBCPP_NODEBUG = pair<const _Key, _ValueT>; |
| 538 | }; |
| 539 | |
| 540 | template <class _Tp> |
| 541 | using __get_node_value_type_t _LIBCPP_NODEBUG = typename __get_node_value_type<_Tp>::type; |
| 542 | |
| 543 | template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type> |
| 544 | struct __tree_node_types; |
| 545 | |
| 546 | template <class _NodePtr, class _Tp, class _VoidPtr> |
| 547 | struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> > { |
| 548 | using __node_base_pointer _LIBCPP_NODEBUG = __rebind_pointer_t<_VoidPtr, __tree_node_base<_VoidPtr> >; |
| 549 | using __end_node_pointer _LIBCPP_NODEBUG = __rebind_pointer_t<_VoidPtr, __tree_end_node<__node_base_pointer> >; |
| 550 | |
| 551 | private: |
| 552 | static_assert(is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value, |
| 553 | "_VoidPtr does not point to unqualified void type" ); |
| 554 | }; |
| 555 | |
| 556 | // node |
| 557 | |
| 558 | template <class _Pointer> |
| 559 | class __tree_end_node { |
| 560 | public: |
| 561 | using pointer = _Pointer; |
| 562 | pointer __left_; |
| 563 | |
| 564 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_end_node() _NOEXCEPT : __left_() {} |
| 565 | }; |
| 566 | |
| 567 | template <class _VoidPtr> |
| 568 | class __tree_node_base : public __tree_end_node<__rebind_pointer_t<_VoidPtr, __tree_node_base<_VoidPtr> > > { |
| 569 | public: |
| 570 | using pointer = __rebind_pointer_t<_VoidPtr, __tree_node_base>; |
| 571 | using __end_node_pointer _LIBCPP_NODEBUG = __rebind_pointer_t<_VoidPtr, __tree_end_node<pointer> >; |
| 572 | |
| 573 | pointer __right_; |
| 574 | __end_node_pointer __parent_; |
| 575 | bool __is_black_; |
| 576 | |
| 577 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pointer __parent_unsafe() const { |
| 578 | return std::__static_fancy_pointer_cast<pointer>(__parent_); |
| 579 | } |
| 580 | |
| 581 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __set_parent(pointer __p) { |
| 582 | __parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__p); |
| 583 | } |
| 584 | |
| 585 | _LIBCPP_HIDE_FROM_ABI __tree_node_base() = default; |
| 586 | __tree_node_base(__tree_node_base const&) = delete; |
| 587 | __tree_node_base& operator=(__tree_node_base const&) = delete; |
| 588 | }; |
| 589 | |
| 590 | template <class _Tp, class _VoidPtr> |
| 591 | class __tree_node : public __tree_node_base<_VoidPtr> { |
| 592 | public: |
| 593 | using __node_value_type _LIBCPP_NODEBUG = __get_node_value_type_t<_Tp>; |
| 594 | |
| 595 | // We use a union to avoid initialization during member initialization, which allows us |
| 596 | // to use the allocator from the container to construct the `__node_value_type` in the |
| 597 | // memory provided by the union member |
| 598 | #ifndef _LIBCPP_CXX03_LANG |
| 599 | |
| 600 | private: |
| 601 | union { |
| 602 | __node_value_type __value_; |
| 603 | }; |
| 604 | |
| 605 | public: |
| 606 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_value_type& __get_value() { return __value_; } |
| 607 | #else |
| 608 | |
| 609 | private: |
| 610 | _ALIGNAS_TYPE(__node_value_type) unsigned char __buffer_[sizeof(__node_value_type)]; |
| 611 | |
| 612 | public: |
| 613 | _LIBCPP_HIDE_FROM_ABI __node_value_type& __get_value() { return *reinterpret_cast<__node_value_type*>(__buffer_); } |
| 614 | #endif |
| 615 | |
| 616 | template <class _Alloc, class... _Args> |
| 617 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit __tree_node(_Alloc& __na, _Args&&... __args) { |
| 618 | allocator_traits<_Alloc>::construct(__na, std::addressof(__get_value()), std::forward<_Args>(__args)...); |
| 619 | } |
| 620 | |
| 621 | ~__tree_node() = delete; |
| 622 | __tree_node(__tree_node const&) = delete; |
| 623 | __tree_node& operator=(__tree_node const&) = delete; |
| 624 | }; |
| 625 | |
| 626 | template <class _Allocator> |
| 627 | class __tree_node_destructor { |
| 628 | using allocator_type = _Allocator; |
| 629 | using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>; |
| 630 | |
| 631 | public: |
| 632 | using pointer = typename __alloc_traits::pointer; |
| 633 | |
| 634 | private: |
| 635 | allocator_type& __na_; |
| 636 | |
| 637 | public: |
| 638 | bool __value_constructed; |
| 639 | |
| 640 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_node_destructor(const __tree_node_destructor&) = default; |
| 641 | __tree_node_destructor& operator=(const __tree_node_destructor&) = delete; |
| 642 | |
| 643 | _LIBCPP_HIDE_FROM_ABI |
| 644 | _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT |
| 645 | : __na_(__na), |
| 646 | __value_constructed(__val) {} |
| 647 | |
| 648 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void operator()(pointer __p) _NOEXCEPT { |
| 649 | if (__value_constructed) |
| 650 | __alloc_traits::destroy(__na_, std::addressof(__p->__get_value())); |
| 651 | if (__p) |
| 652 | __alloc_traits::deallocate(__na_, __p, 1); |
| 653 | } |
| 654 | |
| 655 | template <class> |
| 656 | friend class __map_node_destructor; |
| 657 | }; |
| 658 | |
| 659 | #if _LIBCPP_STD_VER >= 17 |
| 660 | template <class _NodeType, class _Alloc> |
| 661 | struct __generic_container_node_destructor; |
| 662 | template <class _Tp, class _VoidPtr, class _Alloc> |
| 663 | struct __generic_container_node_destructor<__tree_node<_Tp, _VoidPtr>, _Alloc> : __tree_node_destructor<_Alloc> { |
| 664 | using __tree_node_destructor<_Alloc>::__tree_node_destructor; |
| 665 | }; |
| 666 | #endif |
| 667 | |
| 668 | // Do an in-order traversal of the tree until `__break` returns true. Takes the root node of the tree. |
| 669 | template <class _Reference, class _Break, class _NodePtr, class _Func, class _Proj> |
| 670 | #ifndef _LIBCPP_COMPILER_GCC // This function is recursive, so GCC complains about always_inline. |
| 671 | _LIBCPP_HIDE_FROM_ABI |
| 672 | #endif |
| 673 | _LIBCPP_CONSTEXPR_SINCE_CXX26 bool |
| 674 | __tree_iterate_from_root(_Break __break, _NodePtr __root, _Func& __func, _Proj& __proj) { |
| 675 | if (__root->__left_) { |
| 676 | if (std::__tree_iterate_from_root<_Reference>(__break, static_cast<_NodePtr>(__root->__left_), __func, __proj)) |
| 677 | return true; |
| 678 | } |
| 679 | if (__break(__root)) |
| 680 | return true; |
| 681 | std::__invoke(__func, std::__invoke(__proj, static_cast<_Reference>(__root->__get_value()))); |
| 682 | if (__root->__right_) |
| 683 | return std::__tree_iterate_from_root<_Reference>(__break, static_cast<_NodePtr>(__root->__right_), __func, __proj); |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | // Do an in-order traversal of the tree from __first to __last. |
| 688 | template <class _NodeIter, class _Func, class _Proj> |
| 689 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 690 | __tree_iterate_subrange(_NodeIter __first_it, _NodeIter __last_it, _Func& __func, _Proj& __proj) { |
| 691 | using _NodePtr = typename _NodeIter::__node_pointer; |
| 692 | using _Reference = typename _NodeIter::reference; |
| 693 | |
| 694 | auto __first = __first_it.__ptr_; |
| 695 | auto __last = __last_it.__ptr_; |
| 696 | |
| 697 | while (true) { |
| 698 | if (__first == __last) |
| 699 | return; |
| 700 | const auto __nfirst = static_cast<_NodePtr>(__first); |
| 701 | std::__invoke(__func, std::__invoke(__proj, static_cast<_Reference>(__nfirst->__get_value()))); |
| 702 | if (__nfirst->__right_) { |
| 703 | if (std::__tree_iterate_from_root<_Reference>( |
| 704 | [&](_NodePtr __node) -> bool { return __node == __last; }, |
| 705 | static_cast<_NodePtr>(__nfirst->__right_), |
| 706 | __func, |
| 707 | __proj)) |
| 708 | return; |
| 709 | } |
| 710 | while (!std::__tree_is_left_child(static_cast<_NodePtr>(__first))) |
| 711 | __first = static_cast<_NodePtr>(__first)->__parent_; |
| 712 | __first = static_cast<_NodePtr>(__first)->__parent_; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | template <class _Tp, class _NodePtr, class _DiffType> |
| 717 | class __tree_iterator { |
| 718 | using _NodeTypes _LIBCPP_NODEBUG = __tree_node_types<_NodePtr>; |
| 719 | // NOLINTNEXTLINE(libcpp-nodebug-on-aliases) lldb relies on this alias for pretty printing |
| 720 | using __node_pointer = _NodePtr; |
| 721 | using __node_base_pointer _LIBCPP_NODEBUG = typename _NodeTypes::__node_base_pointer; |
| 722 | using __end_node_pointer _LIBCPP_NODEBUG = typename _NodeTypes::__end_node_pointer; |
| 723 | |
| 724 | __end_node_pointer __ptr_; |
| 725 | |
| 726 | public: |
| 727 | using iterator_category = bidirectional_iterator_tag; |
| 728 | using value_type = __get_node_value_type_t<_Tp>; |
| 729 | using difference_type = _DiffType; |
| 730 | using reference = value_type&; |
| 731 | using pointer = __rebind_pointer_t<_NodePtr, value_type>; |
| 732 | |
| 733 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_iterator() _NOEXCEPT : __ptr_(nullptr) {} |
| 734 | |
| 735 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 reference operator*() const { return __get_np()->__get_value(); } |
| 736 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pointer operator->() const { |
| 737 | return pointer_traits<pointer>::pointer_to(__get_np()->__get_value()); |
| 738 | } |
| 739 | |
| 740 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_iterator& operator++() { |
| 741 | __ptr_ = std::__tree_next_iter<__end_node_pointer>(std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr_)); |
| 742 | return *this; |
| 743 | } |
| 744 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_iterator operator++(int) { |
| 745 | __tree_iterator __t(*this); |
| 746 | ++(*this); |
| 747 | return __t; |
| 748 | } |
| 749 | |
| 750 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_iterator& operator--() { |
| 751 | __ptr_ = std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_prev_iter<__node_base_pointer>(__ptr_)); |
| 752 | return *this; |
| 753 | } |
| 754 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_iterator operator--(int) { |
| 755 | __tree_iterator __t(*this); |
| 756 | --(*this); |
| 757 | return __t; |
| 758 | } |
| 759 | |
| 760 | friend _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool |
| 761 | operator==(const __tree_iterator& __x, const __tree_iterator& __y) { |
| 762 | return __x.__ptr_ == __y.__ptr_; |
| 763 | } |
| 764 | friend _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool |
| 765 | operator!=(const __tree_iterator& __x, const __tree_iterator& __y) { |
| 766 | return !(__x == __y); |
| 767 | } |
| 768 | |
| 769 | private: |
| 770 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT |
| 771 | : __ptr_(std::__static_fancy_pointer_cast<__end_node_pointer>(__p)) {} |
| 772 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT |
| 773 | : __ptr_(__p) {} |
| 774 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_pointer __get_np() const { |
| 775 | return std::__static_fancy_pointer_cast<__node_pointer>(__ptr_); |
| 776 | } |
| 777 | template <class, class, class> |
| 778 | friend class __tree; |
| 779 | template <class, class, class> |
| 780 | friend class __tree_const_iterator; |
| 781 | |
| 782 | template <class _NodeIter, class _Func, class _Proj> |
| 783 | _LIBCPP_CONSTEXPR_SINCE_CXX26 friend void __tree_iterate_subrange(_NodeIter, _NodeIter, _Func&, _Proj&); |
| 784 | }; |
| 785 | |
| 786 | #ifndef _LIBCPP_CXX03_LANG |
| 787 | // This also handles {multi,}set::iterator, since they're just aliases to __tree::iterator |
| 788 | template <class _Tp, class _NodePtr, class _DiffType> |
| 789 | struct __specialized_algorithm< |
| 790 | _Algorithm::__for_each, |
| 791 | __iterator_pair<__tree_iterator<_Tp, _NodePtr, _DiffType>, __tree_iterator<_Tp, _NodePtr, _DiffType>>> { |
| 792 | static const bool __has_algorithm = true; |
| 793 | |
| 794 | using __iterator _LIBCPP_NODEBUG = __tree_iterator<_Tp, _NodePtr, _DiffType>; |
| 795 | |
| 796 | template <class _Func, class _Proj> |
| 797 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static void |
| 798 | operator()(__iterator __first, __iterator __last, _Func& __func, _Proj& __proj) { |
| 799 | std::__tree_iterate_subrange(__first, __last, __func, __proj); |
| 800 | } |
| 801 | }; |
| 802 | #endif |
| 803 | |
| 804 | template <class _Tp, class _NodePtr, class _DiffType> |
| 805 | class __tree_const_iterator { |
| 806 | using _NodeTypes _LIBCPP_NODEBUG = __tree_node_types<_NodePtr>; |
| 807 | // NOLINTNEXTLINE(libcpp-nodebug-on-aliases) lldb relies on this alias for pretty printing |
| 808 | using __node_pointer = _NodePtr; |
| 809 | using __node_base_pointer _LIBCPP_NODEBUG = typename _NodeTypes::__node_base_pointer; |
| 810 | using __end_node_pointer _LIBCPP_NODEBUG = typename _NodeTypes::__end_node_pointer; |
| 811 | |
| 812 | __end_node_pointer __ptr_; |
| 813 | |
| 814 | public: |
| 815 | using iterator_category = bidirectional_iterator_tag; |
| 816 | using value_type = __get_node_value_type_t<_Tp>; |
| 817 | using difference_type = _DiffType; |
| 818 | using reference = const value_type&; |
| 819 | using pointer = __rebind_pointer_t<_NodePtr, const value_type>; |
| 820 | using __non_const_iterator _LIBCPP_NODEBUG = __tree_iterator<_Tp, __node_pointer, difference_type>; |
| 821 | |
| 822 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_const_iterator() _NOEXCEPT : __ptr_(nullptr) {} |
| 823 | |
| 824 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT |
| 825 | : __ptr_(__p.__ptr_) {} |
| 826 | |
| 827 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 reference operator*() const { return __get_np()->__get_value(); } |
| 828 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pointer operator->() const { |
| 829 | return pointer_traits<pointer>::pointer_to(__get_np()->__get_value()); |
| 830 | } |
| 831 | |
| 832 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_const_iterator& operator++() { |
| 833 | __ptr_ = std::__tree_next_iter<__end_node_pointer>(std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr_)); |
| 834 | return *this; |
| 835 | } |
| 836 | |
| 837 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_const_iterator operator++(int) { |
| 838 | __tree_const_iterator __t(*this); |
| 839 | ++(*this); |
| 840 | return __t; |
| 841 | } |
| 842 | |
| 843 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_const_iterator& operator--() { |
| 844 | __ptr_ = std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_prev_iter<__node_base_pointer>(__ptr_)); |
| 845 | return *this; |
| 846 | } |
| 847 | |
| 848 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_const_iterator operator--(int) { |
| 849 | __tree_const_iterator __t(*this); |
| 850 | --(*this); |
| 851 | return __t; |
| 852 | } |
| 853 | |
| 854 | friend _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool |
| 855 | operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y) { |
| 856 | return __x.__ptr_ == __y.__ptr_; |
| 857 | } |
| 858 | friend _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool |
| 859 | operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y) { |
| 860 | return !(__x == __y); |
| 861 | } |
| 862 | |
| 863 | private: |
| 864 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT |
| 865 | : __ptr_(std::__static_fancy_pointer_cast<__end_node_pointer>(__p)) {} |
| 866 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT |
| 867 | : __ptr_(std::__static_fancy_pointer_cast<__end_node_pointer>(__p)) {} |
| 868 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_pointer __get_np() const { |
| 869 | return std::__static_fancy_pointer_cast<__node_pointer>(__ptr_); |
| 870 | } |
| 871 | |
| 872 | template <class, class, class> |
| 873 | friend class __tree; |
| 874 | |
| 875 | template <class _NodeIter, class _Func, class _Proj> |
| 876 | _LIBCPP_CONSTEXPR_SINCE_CXX26 friend void __tree_iterate_subrange(_NodeIter, _NodeIter, _Func&, _Proj&); |
| 877 | }; |
| 878 | |
| 879 | #ifndef _LIBCPP_CXX03_LANG |
| 880 | // This also handles {multi,}set::const_iterator, since they're just aliases to __tree::iterator |
| 881 | template <class _Tp, class _NodePtr, class _DiffType> |
| 882 | struct __specialized_algorithm< |
| 883 | _Algorithm::__for_each, |
| 884 | __iterator_pair<__tree_const_iterator<_Tp, _NodePtr, _DiffType>, __tree_const_iterator<_Tp, _NodePtr, _DiffType>>> { |
| 885 | static const bool __has_algorithm = true; |
| 886 | |
| 887 | using __iterator _LIBCPP_NODEBUG = __tree_const_iterator<_Tp, _NodePtr, _DiffType>; |
| 888 | |
| 889 | template <class _Func, class _Proj> |
| 890 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static void |
| 891 | operator()(__iterator __first, __iterator __last, _Func& __func, _Proj& __proj) { |
| 892 | std::__tree_iterate_subrange(__first, __last, __func, __proj); |
| 893 | } |
| 894 | }; |
| 895 | #endif |
| 896 | |
| 897 | template <class _Tp, class _Compare> |
| 898 | #ifndef _LIBCPP_CXX03_LANG |
| 899 | _LIBCPP_DIAGNOSE_WARNING(!__is_invocable_v<_Compare const&, _Tp const&, _Tp const&>, |
| 900 | "the specified comparator type does not provide a viable const call operator" ) |
| 901 | #endif |
| 902 | int __diagnose_non_const_comparator(); |
| 903 | |
| 904 | template <class _Tp, class _Compare, class _Allocator> |
| 905 | class __tree { |
| 906 | public: |
| 907 | using value_type = __get_node_value_type_t<_Tp>; |
| 908 | using value_compare = _Compare; |
| 909 | using allocator_type = _Allocator; |
| 910 | |
| 911 | private: |
| 912 | using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>; |
| 913 | using key_type = __get_tree_key_type_t<_Tp>; |
| 914 | |
| 915 | public: |
| 916 | using pointer = typename __alloc_traits::pointer; |
| 917 | using const_pointer = typename __alloc_traits::const_pointer; |
| 918 | using size_type = typename __alloc_traits::size_type; |
| 919 | using difference_type = typename __alloc_traits::difference_type; |
| 920 | |
| 921 | using __void_pointer _LIBCPP_NODEBUG = typename __alloc_traits::void_pointer; |
| 922 | |
| 923 | using __node _LIBCPP_NODEBUG = __tree_node<_Tp, __void_pointer>; |
| 924 | // NOLINTNEXTLINE(libcpp-nodebug-on-aliases) lldb relies on this alias for pretty printing |
| 925 | using __node_pointer = __rebind_pointer_t<__void_pointer, __node>; |
| 926 | |
| 927 | using __node_base _LIBCPP_NODEBUG = __tree_node_base<__void_pointer>; |
| 928 | using __node_base_pointer _LIBCPP_NODEBUG = __rebind_pointer_t<__void_pointer, __node_base>; |
| 929 | |
| 930 | using __end_node_t _LIBCPP_NODEBUG = __tree_end_node<__node_base_pointer>; |
| 931 | using __end_node_pointer _LIBCPP_NODEBUG = __rebind_pointer_t<__void_pointer, __end_node_t>; |
| 932 | |
| 933 | using __node_allocator _LIBCPP_NODEBUG = __rebind_alloc<__alloc_traits, __node>; |
| 934 | using __node_traits _LIBCPP_NODEBUG = allocator_traits<__node_allocator>; |
| 935 | |
| 936 | private: |
| 937 | // check for sane allocator pointer rebinding semantics. Rebinding the |
| 938 | // allocator for a new pointer type should be exactly the same as rebinding |
| 939 | // the pointer using 'pointer_traits'. |
| 940 | static_assert(is_same<__node_pointer, typename __node_traits::pointer>::value, |
| 941 | "Allocator does not rebind pointers in a sane manner." ); |
| 942 | using __node_base_allocator _LIBCPP_NODEBUG = __rebind_alloc<__node_traits, __node_base>; |
| 943 | using __node_base_traits _LIBCPP_NODEBUG = allocator_traits<__node_base_allocator>; |
| 944 | static_assert(is_same<__node_base_pointer, typename __node_base_traits::pointer>::value, |
| 945 | "Allocator does not rebind pointers in a sane manner." ); |
| 946 | |
| 947 | private: |
| 948 | __end_node_pointer __begin_node_; |
| 949 | _LIBCPP_COMPRESSED_PAIR(__end_node_t, __end_node_, __node_allocator, __node_alloc_); |
| 950 | _LIBCPP_COMPRESSED_PAIR(size_type, __size_, value_compare, __value_comp_); |
| 951 | |
| 952 | public: |
| 953 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __end_node_pointer __end_node() _NOEXCEPT { |
| 954 | return pointer_traits<__end_node_pointer>::pointer_to(__end_node_); |
| 955 | } |
| 956 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __end_node_pointer __end_node() const _NOEXCEPT { |
| 957 | return pointer_traits<__end_node_pointer>::pointer_to(const_cast<__end_node_t&>(__end_node_)); |
| 958 | } |
| 959 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_allocator& __node_alloc() _NOEXCEPT { |
| 960 | return __node_alloc_; |
| 961 | } |
| 962 | |
| 963 | private: |
| 964 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const __node_allocator& __node_alloc() const _NOEXCEPT { |
| 965 | return __node_alloc_; |
| 966 | } |
| 967 | |
| 968 | public: |
| 969 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 allocator_type __alloc() const _NOEXCEPT { |
| 970 | return allocator_type(__node_alloc()); |
| 971 | } |
| 972 | |
| 973 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type size() const _NOEXCEPT { return __size_; } |
| 974 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 value_compare& value_comp() _NOEXCEPT { return __value_comp_; } |
| 975 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const value_compare& value_comp() const _NOEXCEPT { |
| 976 | return __value_comp_; |
| 977 | } |
| 978 | |
| 979 | public: |
| 980 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_pointer __root() const _NOEXCEPT { |
| 981 | return std::__static_fancy_pointer_cast<__node_pointer>(__end_node()->__left_); |
| 982 | } |
| 983 | |
| 984 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_base_pointer* __root_ptr() const _NOEXCEPT { |
| 985 | return std::addressof(__end_node()->__left_); |
| 986 | } |
| 987 | |
| 988 | using iterator = __tree_iterator<_Tp, __node_pointer, difference_type>; |
| 989 | using const_iterator = __tree_const_iterator<_Tp, __node_pointer, difference_type>; |
| 990 | |
| 991 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit __tree(const value_compare& __comp) _NOEXCEPT_( |
| 992 | is_nothrow_default_constructible<__node_allocator>::value&& is_nothrow_copy_constructible<value_compare>::value) |
| 993 | : __size_(0), __value_comp_(__comp) { |
| 994 | __begin_node_ = __end_node(); |
| 995 | } |
| 996 | |
| 997 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit __tree(const allocator_type& __a) |
| 998 | : __begin_node_(), __node_alloc_(__node_allocator(__a)), __size_(0) { |
| 999 | __begin_node_ = __end_node(); |
| 1000 | } |
| 1001 | |
| 1002 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree(const value_compare& __comp, const allocator_type& __a) |
| 1003 | : __begin_node_(), __node_alloc_(__node_allocator(__a)), __size_(0), __value_comp_(__comp) { |
| 1004 | __begin_node_ = __end_node(); |
| 1005 | } |
| 1006 | |
| 1007 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree(const __tree& __t); |
| 1008 | |
| 1009 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree(const __tree& __other, const allocator_type& __alloc) |
| 1010 | : __begin_node_(__end_node()), __node_alloc_(__alloc), __size_(0), __value_comp_(__other.value_comp()) { |
| 1011 | if (__other.size() == 0) |
| 1012 | return; |
| 1013 | |
| 1014 | *__root_ptr() = std::__static_fancy_pointer_cast<__node_base_pointer>(__copy_construct_tree(src: __other.__root())); |
| 1015 | __root()->__parent_ = __end_node(); |
| 1016 | __begin_node_ = std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_)); |
| 1017 | __size_ = __other.size(); |
| 1018 | } |
| 1019 | |
| 1020 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree& operator=(const __tree& __t); |
| 1021 | template <class _ForwardIterator> |
| 1022 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 1023 | __assign_unique(_ForwardIterator __first, _ForwardIterator __last); |
| 1024 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree(__tree&& __t) _NOEXCEPT_( |
| 1025 | is_nothrow_move_constructible<__node_allocator>::value&& is_nothrow_move_constructible<value_compare>::value); |
| 1026 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree(__tree&& __t, const allocator_type& __a); |
| 1027 | |
| 1028 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree& operator=(__tree&& __t) |
| 1029 | _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value && |
| 1030 | ((__node_traits::propagate_on_container_move_assignment::value && |
| 1031 | is_nothrow_move_assignable<__node_allocator>::value) || |
| 1032 | allocator_traits<__node_allocator>::is_always_equal::value)) { |
| 1033 | __move_assign(__t, |
| 1034 | integral_constant<bool, |
| 1035 | __node_traits::propagate_on_container_move_assignment::value || |
| 1036 | __node_traits::is_always_equal::value>()); |
| 1037 | return *this; |
| 1038 | } |
| 1039 | |
| 1040 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 ~__tree() { |
| 1041 | static_assert(is_copy_constructible<value_compare>::value, "Comparator must be copy-constructible." ); |
| 1042 | destroy(nd: __root()); |
| 1043 | } |
| 1044 | |
| 1045 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator begin() _NOEXCEPT { return iterator(__begin_node_); } |
| 1046 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator begin() const _NOEXCEPT { |
| 1047 | return const_iterator(__begin_node_); |
| 1048 | } |
| 1049 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator end() _NOEXCEPT { return iterator(__end_node()); } |
| 1050 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator end() const _NOEXCEPT { |
| 1051 | return const_iterator(__end_node()); |
| 1052 | } |
| 1053 | |
| 1054 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type max_size() const _NOEXCEPT { |
| 1055 | return std::min<size_type>(__node_traits::max_size(__node_alloc()), numeric_limits<difference_type >::max()); |
| 1056 | } |
| 1057 | |
| 1058 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void clear() _NOEXCEPT; |
| 1059 | |
| 1060 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void swap(__tree& __t) |
| 1061 | #if _LIBCPP_STD_VER <= 11 |
| 1062 | _NOEXCEPT_(__is_nothrow_swappable_v<value_compare> && |
| 1063 | (!__node_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__node_allocator>)); |
| 1064 | #else |
| 1065 | _NOEXCEPT_(__is_nothrow_swappable_v<value_compare>); |
| 1066 | #endif |
| 1067 | |
| 1068 | template <class... _Args> |
| 1069 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator __emplace_multi(_Args&&... __args); |
| 1070 | |
| 1071 | template <class... _Args> |
| 1072 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator |
| 1073 | __emplace_hint_multi(const_iterator __p, _Args&&... __args); |
| 1074 | |
| 1075 | template <class... _Args> |
| 1076 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> __emplace_unique(_Args&&... __args) { |
| 1077 | return std::__try_key_extraction<key_type>( |
| 1078 | [this](const key_type& __key, _Args&&... __args2) { |
| 1079 | auto [__parent, __child] = __find_equal(__key); |
| 1080 | __node_pointer __r = std::__static_fancy_pointer_cast<__node_pointer>(__child); |
| 1081 | bool __inserted = false; |
| 1082 | if (__child == nullptr) { |
| 1083 | __node_holder __h = __construct_node(std::forward<_Args>(__args2)...); |
| 1084 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__h.get())); |
| 1085 | __r = __h.release(); |
| 1086 | __inserted = true; |
| 1087 | } |
| 1088 | return pair<iterator, bool>(iterator(__r), __inserted); |
| 1089 | }, |
| 1090 | [this](_Args&&... __args2) { |
| 1091 | __node_holder __h = __construct_node(std::forward<_Args>(__args2)...); |
| 1092 | auto [__parent, __child] = __find_equal(__h->__get_value()); |
| 1093 | __node_pointer __r = std::__static_fancy_pointer_cast<__node_pointer>(__child); |
| 1094 | bool __inserted = false; |
| 1095 | if (__child == nullptr) { |
| 1096 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__h.get())); |
| 1097 | __r = __h.release(); |
| 1098 | __inserted = true; |
| 1099 | } |
| 1100 | return pair<iterator, bool>(iterator(__r), __inserted); |
| 1101 | }, |
| 1102 | std::forward<_Args>(__args)...); |
| 1103 | } |
| 1104 | |
| 1105 | template <class... _Args> |
| 1106 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> |
| 1107 | __emplace_hint_unique(const_iterator __p, _Args&&... __args) { |
| 1108 | return std::__try_key_extraction<key_type>( |
| 1109 | [this, __p](const key_type& __key, _Args&&... __args2) { |
| 1110 | __node_base_pointer __dummy; |
| 1111 | auto [__parent, __child] = __find_equal(__p, __dummy, __key); |
| 1112 | __node_pointer __r = std::__static_fancy_pointer_cast<__node_pointer>(__child); |
| 1113 | bool __inserted = false; |
| 1114 | if (__child == nullptr) { |
| 1115 | __node_holder __h = __construct_node(std::forward<_Args>(__args2)...); |
| 1116 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__h.get())); |
| 1117 | __r = __h.release(); |
| 1118 | __inserted = true; |
| 1119 | } |
| 1120 | return pair<iterator, bool>(iterator(__r), __inserted); |
| 1121 | }, |
| 1122 | [this, __p](_Args&&... __args2) { |
| 1123 | __node_holder __h = __construct_node(std::forward<_Args>(__args2)...); |
| 1124 | __node_base_pointer __dummy; |
| 1125 | auto [__parent, __child] = __find_equal(__p, __dummy, __h->__get_value()); |
| 1126 | __node_pointer __r = std::__static_fancy_pointer_cast<__node_pointer>(__child); |
| 1127 | if (__child == nullptr) { |
| 1128 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__h.get())); |
| 1129 | __r = __h.release(); |
| 1130 | } |
| 1131 | return pair<iterator, bool>(iterator(__r), __child == nullptr); |
| 1132 | }, |
| 1133 | std::forward<_Args>(__args)...); |
| 1134 | } |
| 1135 | |
| 1136 | template <class _InIter, class _Sent> |
| 1137 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __insert_range_multi(_InIter __first, _Sent __last) { |
| 1138 | if (__first == __last) |
| 1139 | return; |
| 1140 | |
| 1141 | if (__root() == nullptr) { // Make sure we always have a root node |
| 1142 | __insert_node_at(parent: __end_node(), |
| 1143 | child&: __end_node()->__left_, |
| 1144 | new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__construct_node(*__first).release())); |
| 1145 | ++__first; |
| 1146 | } |
| 1147 | |
| 1148 | auto __max_node = std::__static_fancy_pointer_cast<__node_pointer>( |
| 1149 | std::__tree_max(std::__static_fancy_pointer_cast<__node_base_pointer>(__root()))); |
| 1150 | |
| 1151 | for (; __first != __last; ++__first) { |
| 1152 | __node_holder __nd = __construct_node(*__first); |
| 1153 | // Always check the max node first. This optimizes for sorted ranges inserted at the end. |
| 1154 | if (!value_comp()(__nd->__get_value(), __max_node->__get_value())) { // __node >= __max_val |
| 1155 | __insert_node_at(parent: std::__static_fancy_pointer_cast<__end_node_pointer>(__max_node), |
| 1156 | child&: __max_node->__right_, |
| 1157 | new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.get())); |
| 1158 | __max_node = __nd.release(); |
| 1159 | } else { |
| 1160 | __end_node_pointer __parent; |
| 1161 | __node_base_pointer& __child = __find_leaf_high(__parent, v: __nd->__get_value()); |
| 1162 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.release())); |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | template <class _InIter, class _Sent> |
| 1168 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __insert_range_unique(_InIter __first, _Sent __last) { |
| 1169 | if (__first == __last) |
| 1170 | return; |
| 1171 | |
| 1172 | if (__root() == nullptr) { |
| 1173 | __insert_node_at(parent: __end_node(), |
| 1174 | child&: __end_node()->__left_, |
| 1175 | new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__construct_node(*__first).release())); |
| 1176 | ++__first; |
| 1177 | } |
| 1178 | |
| 1179 | auto __max_node = std::__static_fancy_pointer_cast<__node_pointer>( |
| 1180 | std::__tree_max(std::__static_fancy_pointer_cast<__node_base_pointer>(__root()))); |
| 1181 | |
| 1182 | using __reference = decltype(*__first); |
| 1183 | |
| 1184 | for (; __first != __last; ++__first) { |
| 1185 | std::__try_key_extraction<key_type>( |
| 1186 | [this, &__max_node](const key_type& __key, __reference&& __val) { |
| 1187 | if (value_comp()(__max_node->__get_value(), __key)) { // __key > __max_node |
| 1188 | __node_holder __nd = __construct_node(std::forward<__reference>(__val)); |
| 1189 | __insert_node_at(parent: std::__static_fancy_pointer_cast<__end_node_pointer>(__max_node), |
| 1190 | child&: __max_node->__right_, |
| 1191 | new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.get())); |
| 1192 | __max_node = __nd.release(); |
| 1193 | } else { |
| 1194 | auto [__parent, __child] = __find_equal(__key); |
| 1195 | if (__child == nullptr) { |
| 1196 | __node_holder __nd = __construct_node(std::forward<__reference>(__val)); |
| 1197 | __insert_node_at( |
| 1198 | __parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.release())); |
| 1199 | } |
| 1200 | } |
| 1201 | }, |
| 1202 | [this, &__max_node](__reference&& __val) { |
| 1203 | __node_holder __nd = __construct_node(std::forward<__reference>(__val)); |
| 1204 | if (value_comp()(__max_node->__get_value(), __nd->__get_value())) { // __node > __max_node |
| 1205 | __insert_node_at(parent: std::__static_fancy_pointer_cast<__end_node_pointer>(__max_node), |
| 1206 | child&: __max_node->__right_, |
| 1207 | new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.get())); |
| 1208 | __max_node = __nd.release(); |
| 1209 | } else { |
| 1210 | auto [__parent, __child] = __find_equal(__nd->__get_value()); |
| 1211 | if (__child == nullptr) { |
| 1212 | __insert_node_at( |
| 1213 | __parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__nd.release())); |
| 1214 | } |
| 1215 | } |
| 1216 | }, |
| 1217 | *__first); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator __remove_node_pointer(__node_pointer) _NOEXCEPT; |
| 1222 | |
| 1223 | #if _LIBCPP_STD_VER >= 17 |
| 1224 | template <class _NodeHandle, class _InsertReturnType> |
| 1225 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _InsertReturnType __node_handle_insert_unique(_NodeHandle&&); |
| 1226 | template <class _NodeHandle> |
| 1227 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator |
| 1228 | __node_handle_insert_unique(const_iterator, _NodeHandle&&); |
| 1229 | template <class _Comp2> |
| 1230 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 1231 | __node_handle_merge_unique(__tree<_Tp, _Comp2, _Allocator>& __source); |
| 1232 | |
| 1233 | template <class _NodeHandle> |
| 1234 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator __node_handle_insert_multi(_NodeHandle&&); |
| 1235 | template <class _NodeHandle> |
| 1236 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator |
| 1237 | __node_handle_insert_multi(const_iterator, _NodeHandle&&); |
| 1238 | template <class _Comp2> |
| 1239 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 1240 | __node_handle_merge_multi(__tree<_Tp, _Comp2, _Allocator>& __source); |
| 1241 | |
| 1242 | template <class _NodeHandle> |
| 1243 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodeHandle __node_handle_extract(key_type const&); |
| 1244 | template <class _NodeHandle> |
| 1245 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodeHandle __node_handle_extract(const_iterator); |
| 1246 | #endif |
| 1247 | |
| 1248 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator erase(const_iterator __p); |
| 1249 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator erase(const_iterator __f, const_iterator __l); |
| 1250 | template <class _Key> |
| 1251 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type __erase_unique(const _Key& __k); |
| 1252 | template <class _Key> |
| 1253 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type __erase_multi(const _Key& __k); |
| 1254 | |
| 1255 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 1256 | __insert_node_at(__end_node_pointer __parent, __node_base_pointer& __child, __node_base_pointer __new_node) _NOEXCEPT; |
| 1257 | |
| 1258 | template <class _Key> |
| 1259 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator find(const _Key& __key) { |
| 1260 | auto [__dummy, __match] = __find_equal(__key); |
| 1261 | if (__match == nullptr) |
| 1262 | return end(); |
| 1263 | return iterator(std::__static_fancy_pointer_cast<__node_pointer>(__match)); |
| 1264 | } |
| 1265 | |
| 1266 | template <class _Key> |
| 1267 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator find(const _Key& __key) const { |
| 1268 | auto [__dummy, __match] = __find_equal(__key); |
| 1269 | if (__match == nullptr) |
| 1270 | return end(); |
| 1271 | return const_iterator(std::__static_fancy_pointer_cast<__node_pointer>(__match)); |
| 1272 | } |
| 1273 | |
| 1274 | template <class _Key> |
| 1275 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type __count_unique(const _Key& __k) const; |
| 1276 | template <class _Key> |
| 1277 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type __count_multi(const _Key& __k) const; |
| 1278 | |
| 1279 | template <bool _LowerBound, class _Key> |
| 1280 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __end_node_pointer |
| 1281 | __lower_upper_bound_unique_impl(const _Key& __v) const { |
| 1282 | auto __rt = __root(); |
| 1283 | auto __result = __end_node(); |
| 1284 | auto __comp = __lazy_synth_three_way_comparator<_Compare, _Key, value_type>(value_comp()); |
| 1285 | while (__rt != nullptr) { |
| 1286 | auto __comp_res = __comp(__v, __rt->__get_value()); |
| 1287 | |
| 1288 | if (__comp_res.__less()) { |
| 1289 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt); |
| 1290 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_); |
| 1291 | } else if (__comp_res.__greater()) { |
| 1292 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_); |
| 1293 | } else if _LIBCPP_CONSTEXPR (_LowerBound) { |
| 1294 | return std::__static_fancy_pointer_cast<__end_node_pointer>(__rt); |
| 1295 | } else { |
| 1296 | return __rt->__right_ ? std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_min(__rt->__right_)) |
| 1297 | : __result; |
| 1298 | } |
| 1299 | } |
| 1300 | return __result; |
| 1301 | } |
| 1302 | |
| 1303 | // Compatibility escape hatch for comparators that are not strict weak orderings. This |
| 1304 | // can be removed for the LLVM 23 release. |
| 1305 | #if defined(_LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND) |
| 1306 | template <class _Key> |
| 1307 | _LIBCPP_HIDE_FROM_ABI __end_node_pointer __lower_bound_unique_compat_impl(const _Key& __v) const { |
| 1308 | auto __rt = __root(); |
| 1309 | auto __result = __end_node(); |
| 1310 | while (__rt != nullptr) { |
| 1311 | if (!value_comp()(__rt->__get_value(), __v)) { |
| 1312 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt); |
| 1313 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_); |
| 1314 | } else { |
| 1315 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_); |
| 1316 | } |
| 1317 | } |
| 1318 | return __result; |
| 1319 | } |
| 1320 | |
| 1321 | template <class _Key> |
| 1322 | _LIBCPP_HIDE_FROM_ABI __end_node_pointer __upper_bound_unique_compat_impl(const _Key& __v) const { |
| 1323 | auto __rt = __root(); |
| 1324 | auto __result = __end_node(); |
| 1325 | while (__rt != nullptr) { |
| 1326 | if (value_comp()(__v, __rt->__get_value())) { |
| 1327 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt); |
| 1328 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_); |
| 1329 | } else { |
| 1330 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_); |
| 1331 | } |
| 1332 | } |
| 1333 | return __result; |
| 1334 | } |
| 1335 | #endif // _LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND |
| 1336 | |
| 1337 | template <class _Key> |
| 1338 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator __lower_bound_unique(const _Key& __v) { |
| 1339 | #if defined(_LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND) |
| 1340 | return iterator(__lower_bound_unique_compat_impl(__v)); |
| 1341 | #else |
| 1342 | return iterator(__lower_upper_bound_unique_impl<true>(__v)); |
| 1343 | #endif |
| 1344 | } |
| 1345 | |
| 1346 | template <class _Key> |
| 1347 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator __lower_bound_unique(const _Key& __v) const { |
| 1348 | #if defined(_LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND) |
| 1349 | return const_iterator(__lower_bound_unique_compat_impl(__v)); |
| 1350 | #else |
| 1351 | return const_iterator(__lower_upper_bound_unique_impl<true>(__v)); |
| 1352 | #endif |
| 1353 | } |
| 1354 | |
| 1355 | template <class _Key> |
| 1356 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator __upper_bound_unique(const _Key& __v) { |
| 1357 | #if defined(_LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND) |
| 1358 | return iterator(__upper_bound_unique_compat_impl(__v)); |
| 1359 | #else |
| 1360 | return iterator(__lower_upper_bound_unique_impl<false>(__v)); |
| 1361 | #endif |
| 1362 | } |
| 1363 | |
| 1364 | template <class _Key> |
| 1365 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator __upper_bound_unique(const _Key& __v) const { |
| 1366 | #if defined(_LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND) |
| 1367 | return iterator(__upper_bound_unique_compat_impl(__v)); |
| 1368 | #else |
| 1369 | return iterator(__lower_upper_bound_unique_impl<false>(__v)); |
| 1370 | #endif |
| 1371 | } |
| 1372 | |
| 1373 | private: |
| 1374 | template <class _Key> |
| 1375 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator |
| 1376 | __lower_bound_multi(const _Key& __v, __node_pointer __root, __end_node_pointer __result); |
| 1377 | |
| 1378 | template <class _Key> |
| 1379 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator |
| 1380 | __lower_bound_multi(const _Key& __v, __node_pointer __root, __end_node_pointer __result) const; |
| 1381 | |
| 1382 | public: |
| 1383 | template <class _Key> |
| 1384 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator __lower_bound_multi(const _Key& __v) { |
| 1385 | return __lower_bound_multi(__v, __root(), __end_node()); |
| 1386 | } |
| 1387 | template <class _Key> |
| 1388 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator __lower_bound_multi(const _Key& __v) const { |
| 1389 | return __lower_bound_multi(__v, __root(), __end_node()); |
| 1390 | } |
| 1391 | |
| 1392 | template <class _Key> |
| 1393 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator __upper_bound_multi(const _Key& __v) { |
| 1394 | return __upper_bound_multi(__v, __root(), __end_node()); |
| 1395 | } |
| 1396 | |
| 1397 | template <class _Key> |
| 1398 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator __upper_bound_multi(const _Key& __v) const { |
| 1399 | return __upper_bound_multi(__v, __root(), __end_node()); |
| 1400 | } |
| 1401 | |
| 1402 | private: |
| 1403 | template <class _Key> |
| 1404 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator |
| 1405 | __upper_bound_multi(const _Key& __v, __node_pointer __root, __end_node_pointer __result); |
| 1406 | |
| 1407 | template <class _Key> |
| 1408 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator |
| 1409 | __upper_bound_multi(const _Key& __v, __node_pointer __root, __end_node_pointer __result) const; |
| 1410 | |
| 1411 | public: |
| 1412 | template <class _Key> |
| 1413 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, iterator> __equal_range_unique(const _Key& __k); |
| 1414 | template <class _Key> |
| 1415 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<const_iterator, const_iterator> |
| 1416 | __equal_range_unique(const _Key& __k) const; |
| 1417 | |
| 1418 | template <class _Key> |
| 1419 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, iterator> __equal_range_multi(const _Key& __k); |
| 1420 | template <class _Key> |
| 1421 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<const_iterator, const_iterator> |
| 1422 | __equal_range_multi(const _Key& __k) const; |
| 1423 | |
| 1424 | using _Dp _LIBCPP_NODEBUG = __tree_node_destructor<__node_allocator>; |
| 1425 | using __node_holder _LIBCPP_NODEBUG = unique_ptr<__node, _Dp>; |
| 1426 | |
| 1427 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_holder remove(const_iterator __p) _NOEXCEPT; |
| 1428 | |
| 1429 | // FIXME: Make this function const qualified. Unfortunately doing so |
| 1430 | // breaks existing code which uses non-const callable comparators. |
| 1431 | template <class _Key> |
| 1432 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<__end_node_pointer, __node_base_pointer&> |
| 1433 | __find_equal(const _Key& __v); |
| 1434 | |
| 1435 | template <class _Key> |
| 1436 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<__end_node_pointer, __node_base_pointer&> |
| 1437 | __find_equal(const _Key& __v) const { |
| 1438 | return const_cast<__tree*>(this)->__find_equal(__v); |
| 1439 | } |
| 1440 | |
| 1441 | template <class _Key> |
| 1442 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<__end_node_pointer, __node_base_pointer&> |
| 1443 | __find_equal(const_iterator __hint, __node_base_pointer& __dummy, const _Key& __v); |
| 1444 | |
| 1445 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __copy_assign_alloc(const __tree& __t) { |
| 1446 | __copy_assign_alloc(__t, integral_constant<bool, __node_traits::propagate_on_container_copy_assignment::value>()); |
| 1447 | } |
| 1448 | |
| 1449 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __copy_assign_alloc(const __tree& __t, true_type) { |
| 1450 | if (__node_alloc() != __t.__node_alloc()) |
| 1451 | clear(); |
| 1452 | __node_alloc() = __t.__node_alloc(); |
| 1453 | } |
| 1454 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __copy_assign_alloc(const __tree&, false_type) {} |
| 1455 | |
| 1456 | private: |
| 1457 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_base_pointer& |
| 1458 | __find_leaf_low(__end_node_pointer& __parent, const value_type& __v); |
| 1459 | |
| 1460 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_base_pointer& |
| 1461 | __find_leaf_high(__end_node_pointer& __parent, const value_type& __v); |
| 1462 | |
| 1463 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_base_pointer& |
| 1464 | __find_leaf(const_iterator __hint, __end_node_pointer& __parent, const value_type& __v); |
| 1465 | |
| 1466 | template <class... _Args> |
| 1467 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_holder __construct_node(_Args&&... __args); |
| 1468 | |
| 1469 | // TODO: Make this _LIBCPP_HIDE_FROM_ABI |
| 1470 | _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX26 void destroy(__node_pointer __nd) _NOEXCEPT { |
| 1471 | (__tree_deleter(__node_alloc_))(__nd); |
| 1472 | } |
| 1473 | |
| 1474 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __move_assign(__tree& __t, false_type); |
| 1475 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __move_assign(__tree& __t, true_type) _NOEXCEPT_( |
| 1476 | is_nothrow_move_assignable<value_compare>::value&& is_nothrow_move_assignable<__node_allocator>::value); |
| 1477 | |
| 1478 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __move_assign_alloc(__tree& __t) |
| 1479 | _NOEXCEPT_(!__node_traits::propagate_on_container_move_assignment::value || |
| 1480 | is_nothrow_move_assignable<__node_allocator>::value) { |
| 1481 | __move_assign_alloc(__t, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>()); |
| 1482 | } |
| 1483 | |
| 1484 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __move_assign_alloc(__tree& __t, true_type) |
| 1485 | _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) { |
| 1486 | __node_alloc() = std::move(__t.__node_alloc()); |
| 1487 | } |
| 1488 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {} |
| 1489 | |
| 1490 | template <class _From, class _ValueT = _Tp, __enable_if_t<__is_tree_value_type_v<_ValueT>, int> = 0> |
| 1491 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static void |
| 1492 | __assign_value(__get_node_value_type_t<value_type>& __lhs, _From&& __rhs) { |
| 1493 | using __key_type = __remove_const_t<typename value_type::first_type>; |
| 1494 | |
| 1495 | #if _LIBCPP_STD_VER >= 26 |
| 1496 | if constexpr (std::is_copy_constructible_v<decltype(__rhs.first)>) { |
| 1497 | // We need to replace the `const key_type` subobject by reconstruction during constant evaluation to avoid |
| 1498 | // undefined behavior. Currently, there is no constexpr-friend way to replacing a move-only key subobject. |
| 1499 | if consteval { |
| 1500 | std::destroy_at(std::addressof(__lhs)); |
| 1501 | std::construct_at(std::addressof(__lhs), __rhs.first, std::forward<_From>(__rhs).second); |
| 1502 | return; |
| 1503 | } |
| 1504 | } |
| 1505 | #endif |
| 1506 | |
| 1507 | // This is technically UB, since the object was constructed as `const`. |
| 1508 | // Clang doesn't optimize on this currently though. |
| 1509 | const_cast<__key_type&>(__lhs.first) = const_cast<__copy_cvref_t<_From, __key_type>&&>(__rhs.first); |
| 1510 | __lhs.second = std::forward<_From>(__rhs).second; |
| 1511 | } |
| 1512 | |
| 1513 | template <class _To, class _From, class _ValueT = _Tp, __enable_if_t<!__is_tree_value_type_v<_ValueT>, int> = 0> |
| 1514 | _LIBCPP_HIDE_FROM_ABI static void __assign_value(_To& __lhs, _From&& __rhs) { |
| 1515 | __lhs = std::forward<_From>(__rhs); |
| 1516 | } |
| 1517 | |
| 1518 | class __tree_deleter { |
| 1519 | __node_allocator& __alloc_; |
| 1520 | |
| 1521 | public: |
| 1522 | using pointer = __node_pointer; |
| 1523 | |
| 1524 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree_deleter(__node_allocator& __alloc) : __alloc_(__alloc) {} |
| 1525 | |
| 1526 | #ifdef _LIBCPP_COMPILER_CLANG_BASED // FIXME: GCC complains about not being able to always_inline a recursive function |
| 1527 | _LIBCPP_HIDE_FROM_ABI |
| 1528 | #endif |
| 1529 | _LIBCPP_CONSTEXPR_SINCE_CXX26 void operator()(__node_pointer __ptr) { |
| 1530 | if (!__ptr) |
| 1531 | return; |
| 1532 | |
| 1533 | (*this)(std::__static_fancy_pointer_cast<__node_pointer>(__ptr->__left_)); |
| 1534 | |
| 1535 | auto __right = __ptr->__right_; |
| 1536 | |
| 1537 | __node_traits::destroy(__alloc_, std::addressof(__ptr->__get_value())); |
| 1538 | __node_traits::deallocate(__alloc_, __ptr, 1); |
| 1539 | |
| 1540 | (*this)(std::__static_fancy_pointer_cast<__node_pointer>(__right)); |
| 1541 | } |
| 1542 | }; |
| 1543 | |
| 1544 | // This copy construction will always produce a correct red-black-tree assuming the incoming tree is correct, since we |
| 1545 | // copy the exact structure 1:1. Since this is for copy construction _only_ we know that we get a correct tree. If we |
| 1546 | // didn't get a correct tree, the invariants of __tree are broken and we have a much bigger problem than an improperly |
| 1547 | // balanced tree. |
| 1548 | template <class _NodeConstructor> |
| 1549 | #ifdef _LIBCPP_COMPILER_CLANG_BASED // FIXME: GCC complains about not being able to always_inline a recursive function |
| 1550 | _LIBCPP_HIDE_FROM_ABI |
| 1551 | #endif |
| 1552 | _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_pointer |
| 1553 | __construct_from_tree(__node_pointer __src, _NodeConstructor __construct) { |
| 1554 | if (!__src) |
| 1555 | return nullptr; |
| 1556 | |
| 1557 | __node_holder __new_node = __construct(__src->__get_value()); |
| 1558 | |
| 1559 | unique_ptr<__node, __tree_deleter> __left( |
| 1560 | __construct_from_tree(std::__static_fancy_pointer_cast<__node_pointer>(__src->__left_), __construct), |
| 1561 | __node_alloc_); |
| 1562 | __node_pointer __right = |
| 1563 | __construct_from_tree(std::__static_fancy_pointer_cast<__node_pointer>(__src->__right_), __construct); |
| 1564 | |
| 1565 | __node_pointer __new_node_ptr = __new_node.release(); |
| 1566 | |
| 1567 | __new_node_ptr->__is_black_ = __src->__is_black_; |
| 1568 | __new_node_ptr->__left_ = std::__static_fancy_pointer_cast<__node_base_pointer>(__left.release()); |
| 1569 | __new_node_ptr->__right_ = std::__static_fancy_pointer_cast<__node_base_pointer>(__right); |
| 1570 | if (__new_node_ptr->__left_) |
| 1571 | __new_node_ptr->__left_->__parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__new_node_ptr); |
| 1572 | if (__new_node_ptr->__right_) |
| 1573 | __new_node_ptr->__right_->__parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__new_node_ptr); |
| 1574 | return __new_node_ptr; |
| 1575 | } |
| 1576 | |
| 1577 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_pointer __copy_construct_tree(__node_pointer __src) { |
| 1578 | return __construct_from_tree(__src, [this](const value_type& __val) { return __construct_node(__val); }); |
| 1579 | } |
| 1580 | |
| 1581 | template <class _ValueT = _Tp, __enable_if_t<__is_tree_value_type_v<_ValueT>, int> = 0> |
| 1582 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_pointer __move_construct_tree(__node_pointer __src) { |
| 1583 | return __construct_from_tree(__src, [this](value_type& __val) { |
| 1584 | |
| 1585 | #if _LIBCPP_STD_VER >= 26 |
| 1586 | // We need to replace the `const key_type` subobject by reconstruction during constant evaluation to avoid |
| 1587 | // undefined behavior. Currently, there is no constexpr-friend way to replacing a move-only key subobject. |
| 1588 | if constexpr (std::is_copy_constructible_v<decltype(__val.first)>) { |
| 1589 | if consteval { |
| 1590 | return __construct_node(__val.first, std::move(__val.second)); |
| 1591 | } |
| 1592 | } |
| 1593 | #endif |
| 1594 | return __construct_node(const_cast<key_type&&>(__val.first), std::move(__val.second)); |
| 1595 | }); |
| 1596 | } |
| 1597 | |
| 1598 | template <class _ValueT = _Tp, __enable_if_t<!__is_tree_value_type_v<_ValueT>, int> = 0> |
| 1599 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_pointer __move_construct_tree(__node_pointer __src) { |
| 1600 | return __construct_from_tree(__src, [this](value_type& __val) { return __construct_node(std::move(__val)); }); |
| 1601 | } |
| 1602 | |
| 1603 | template <class _Assignment, class _ConstructionAlg> |
| 1604 | // This copy assignment will always produce a correct red-black-tree assuming the incoming tree is correct, since our |
| 1605 | // own tree is a red-black-tree and the incoming tree is a red-black-tree. The invariants of a red-black-tree are |
| 1606 | // temporarily not met until all of the incoming red-black tree is copied. |
| 1607 | #ifdef _LIBCPP_COMPILER_CLANG_BASED // FIXME: GCC complains about not being able to always_inline a recursive function |
| 1608 | _LIBCPP_HIDE_FROM_ABI |
| 1609 | #endif |
| 1610 | _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_pointer __assign_from_tree( |
| 1611 | __node_pointer __dest, __node_pointer __src, _Assignment __assign, _ConstructionAlg __construct_subtree) { |
| 1612 | if (!__src) { |
| 1613 | destroy(nd: __dest); |
| 1614 | return nullptr; |
| 1615 | } |
| 1616 | |
| 1617 | __assign(__dest->__get_value(), __src->__get_value()); |
| 1618 | __dest->__is_black_ = __src->__is_black_; |
| 1619 | |
| 1620 | // If we already have a left node in the destination tree, reuse it and copy-assign recursively |
| 1621 | if (__dest->__left_) { |
| 1622 | __dest->__left_ = std::__static_fancy_pointer_cast<__node_base_pointer>(__assign_from_tree( |
| 1623 | std::__static_fancy_pointer_cast<__node_pointer>(__dest->__left_), |
| 1624 | std::__static_fancy_pointer_cast<__node_pointer>(__src->__left_), |
| 1625 | __assign, |
| 1626 | __construct_subtree)); |
| 1627 | |
| 1628 | // Otherwise, we must create new nodes; copy-construct from here on |
| 1629 | } else if (__src->__left_) { |
| 1630 | auto __new_left = __construct_subtree(std::__static_fancy_pointer_cast<__node_pointer>(__src->__left_)); |
| 1631 | __dest->__left_ = std::__static_fancy_pointer_cast<__node_base_pointer>(__new_left); |
| 1632 | __new_left->__parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__dest); |
| 1633 | } |
| 1634 | |
| 1635 | // Identical to the left case above, just for the right nodes |
| 1636 | if (__dest->__right_) { |
| 1637 | __dest->__right_ = std::__static_fancy_pointer_cast<__node_base_pointer>(__assign_from_tree( |
| 1638 | std::__static_fancy_pointer_cast<__node_pointer>(__dest->__right_), |
| 1639 | std::__static_fancy_pointer_cast<__node_pointer>(__src->__right_), |
| 1640 | __assign, |
| 1641 | __construct_subtree)); |
| 1642 | } else if (__src->__right_) { |
| 1643 | auto __new_right = __construct_subtree(std::__static_fancy_pointer_cast<__node_pointer>(__src->__right_)); |
| 1644 | __dest->__right_ = std::__static_fancy_pointer_cast<__node_base_pointer>(__new_right); |
| 1645 | __new_right->__parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__dest); |
| 1646 | } |
| 1647 | |
| 1648 | return __dest; |
| 1649 | } |
| 1650 | |
| 1651 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_pointer |
| 1652 | __copy_assign_tree(__node_pointer __dest, __node_pointer __src) { |
| 1653 | return __assign_from_tree( |
| 1654 | __dest, |
| 1655 | __src, |
| 1656 | [](value_type& __lhs, const value_type& __rhs) { __assign_value(__lhs, __rhs); }, |
| 1657 | [this](__node_pointer __nd) { return __copy_construct_tree(src: __nd); }); |
| 1658 | } |
| 1659 | |
| 1660 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __node_pointer |
| 1661 | __move_assign_tree(__node_pointer __dest, __node_pointer __src) { |
| 1662 | return __assign_from_tree( |
| 1663 | __dest, |
| 1664 | __src, |
| 1665 | [](value_type& __lhs, value_type& __rhs) { __assign_value(__lhs, std::move(__rhs)); }, |
| 1666 | [this](__node_pointer __nd) { return __move_construct_tree(__nd); }); |
| 1667 | } |
| 1668 | |
| 1669 | friend struct __specialized_algorithm<_Algorithm::__for_each, __single_range<__tree> >; |
| 1670 | }; |
| 1671 | |
| 1672 | #if _LIBCPP_STD_VER >= 14 |
| 1673 | template <class _Tp, class _Compare, class _Allocator> |
| 1674 | struct __specialized_algorithm<_Algorithm::__for_each, __single_range<__tree<_Tp, _Compare, _Allocator> > > { |
| 1675 | static const bool __has_algorithm = true; |
| 1676 | |
| 1677 | using __node_pointer _LIBCPP_NODEBUG = typename __tree<_Tp, _Compare, _Allocator>::__node_pointer; |
| 1678 | |
| 1679 | template <class _Tree, class _Func, class _Proj> |
| 1680 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static auto |
| 1681 | operator()(_Tree&& __range, _Func __func, _Proj __proj) { |
| 1682 | if (__range.size() != 0) |
| 1683 | std::__tree_iterate_from_root<__copy_cvref_t<_Tree, typename __remove_cvref_t<_Tree>::value_type>>( |
| 1684 | [](__node_pointer) { return false; }, __range.__root(), __func, __proj); |
| 1685 | return std::make_pair(__range.end(), std::move(__func)); |
| 1686 | } |
| 1687 | }; |
| 1688 | #endif |
| 1689 | |
| 1690 | template <class _Tp, class _Compare, class _Allocator> |
| 1691 | _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree<_Tp, _Compare, _Allocator>& |
| 1692 | __tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t) { |
| 1693 | if (this == std::addressof(__t)) |
| 1694 | return *this; |
| 1695 | |
| 1696 | value_comp() = __t.value_comp(); |
| 1697 | __copy_assign_alloc(__t); |
| 1698 | |
| 1699 | if (__size_ != 0) { |
| 1700 | *__root_ptr() = std::__static_fancy_pointer_cast<__node_base_pointer>(__copy_assign_tree(dest: __root(), src: __t.__root())); |
| 1701 | } else { |
| 1702 | *__root_ptr() = std::__static_fancy_pointer_cast<__node_base_pointer>(__copy_construct_tree(src: __t.__root())); |
| 1703 | if (__root()) |
| 1704 | __root()->__parent_ = __end_node(); |
| 1705 | } |
| 1706 | __begin_node_ = __end_node()->__left_ |
| 1707 | ? std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_)) |
| 1708 | : __end_node(); |
| 1709 | __size_ = __t.size(); |
| 1710 | |
| 1711 | return *this; |
| 1712 | } |
| 1713 | |
| 1714 | template <class _Tp, class _Compare, class _Allocator> |
| 1715 | _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t) |
| 1716 | : __begin_node_(__end_node()), |
| 1717 | __node_alloc_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())), |
| 1718 | __size_(0), |
| 1719 | __value_comp_(__t.value_comp()) { |
| 1720 | if (__t.size() == 0) |
| 1721 | return; |
| 1722 | |
| 1723 | *__root_ptr() = std::__static_fancy_pointer_cast<__node_base_pointer>(__copy_construct_tree(src: __t.__root())); |
| 1724 | __root()->__parent_ = __end_node(); |
| 1725 | __begin_node_ = std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_)); |
| 1726 | __size_ = __t.size(); |
| 1727 | } |
| 1728 | |
| 1729 | template <class _Tp, class _Compare, class _Allocator> |
| 1730 | _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t) _NOEXCEPT_( |
| 1731 | is_nothrow_move_constructible<__node_allocator>::value&& is_nothrow_move_constructible<value_compare>::value) |
| 1732 | : __begin_node_(std::move(__t.__begin_node_)), |
| 1733 | __end_node_(std::move(__t.__end_node_)), |
| 1734 | __node_alloc_(std::move(__t.__node_alloc_)), |
| 1735 | __size_(__t.__size_), |
| 1736 | __value_comp_(std::move(__t.__value_comp_)) { |
| 1737 | if (__size_ == 0) |
| 1738 | __begin_node_ = __end_node(); |
| 1739 | else { |
| 1740 | __end_node()->__left_->__parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__end_node()); |
| 1741 | __t.__begin_node_ = __t.__end_node(); |
| 1742 | __t.__end_node()->__left_ = nullptr; |
| 1743 | __t.__size_ = 0; |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | template <class _Tp, class _Compare, class _Allocator> |
| 1748 | _LIBCPP_CONSTEXPR_SINCE_CXX26 __tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a) |
| 1749 | : __begin_node_(__end_node()), |
| 1750 | __node_alloc_(__node_allocator(__a)), |
| 1751 | __size_(0), |
| 1752 | __value_comp_(std::move(__t.value_comp())) { |
| 1753 | if (__t.size() == 0) |
| 1754 | return; |
| 1755 | if (__a == __t.__alloc()) { |
| 1756 | __begin_node_ = __t.__begin_node_; |
| 1757 | __end_node()->__left_ = __t.__end_node()->__left_; |
| 1758 | __end_node()->__left_->__parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__end_node()); |
| 1759 | __size_ = __t.__size_; |
| 1760 | __t.__begin_node_ = __t.__end_node(); |
| 1761 | __t.__end_node()->__left_ = nullptr; |
| 1762 | __t.__size_ = 0; |
| 1763 | } else { |
| 1764 | *__root_ptr() = std::__static_fancy_pointer_cast<__node_base_pointer>(__move_construct_tree(__t.__root())); |
| 1765 | __root()->__parent_ = __end_node(); |
| 1766 | __begin_node_ = std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_)); |
| 1767 | __size_ = __t.size(); |
| 1768 | __t.clear(); // Ensure that __t is in a valid state after moving out the keys |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | template <class _Tp, class _Compare, class _Allocator> |
| 1773 | _LIBCPP_CONSTEXPR_SINCE_CXX26 void __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type) |
| 1774 | _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value&& is_nothrow_move_assignable<__node_allocator>::value) { |
| 1775 | destroy(nd: std::__static_fancy_pointer_cast<__node_pointer>(__end_node()->__left_)); |
| 1776 | __begin_node_ = __t.__begin_node_; |
| 1777 | __end_node_ = __t.__end_node_; |
| 1778 | __move_assign_alloc(__t); |
| 1779 | __size_ = __t.__size_; |
| 1780 | __value_comp_ = std::move(__t.__value_comp_); |
| 1781 | if (__size_ == 0) |
| 1782 | __begin_node_ = __end_node(); |
| 1783 | else { |
| 1784 | __end_node()->__left_->__parent_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__end_node()); |
| 1785 | __t.__begin_node_ = __t.__end_node(); |
| 1786 | __t.__end_node()->__left_ = nullptr; |
| 1787 | __t.__size_ = 0; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | template <class _Tp, class _Compare, class _Allocator> |
| 1792 | _LIBCPP_CONSTEXPR_SINCE_CXX26 void __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type) { |
| 1793 | if (__node_alloc() == __t.__node_alloc()) { |
| 1794 | __move_assign(__t, true_type()); |
| 1795 | } else { |
| 1796 | value_comp() = std::move(__t.value_comp()); |
| 1797 | if (__size_ != 0) { |
| 1798 | *__root_ptr() = std::__static_fancy_pointer_cast<__node_base_pointer>(__move_assign_tree(dest: __root(), src: __t.__root())); |
| 1799 | } else { |
| 1800 | *__root_ptr() = std::__static_fancy_pointer_cast<__node_base_pointer>(__move_construct_tree(__t.__root())); |
| 1801 | if (__root()) |
| 1802 | __root()->__parent_ = __end_node(); |
| 1803 | } |
| 1804 | __begin_node_ = __end_node()->__left_ |
| 1805 | ? std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_)) |
| 1806 | : __end_node(); |
| 1807 | __size_ = __t.size(); |
| 1808 | __t.clear(); // Ensure that __t is in a valid state after moving out the keys |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | template <class _Tp, class _Compare, class _Allocator> |
| 1813 | _LIBCPP_CONSTEXPR_SINCE_CXX26 void __tree<_Tp, _Compare, _Allocator>::swap(__tree& __t) |
| 1814 | #if _LIBCPP_STD_VER <= 11 |
| 1815 | _NOEXCEPT_(__is_nothrow_swappable_v<value_compare> && |
| 1816 | (!__node_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__node_allocator>)) |
| 1817 | #else |
| 1818 | _NOEXCEPT_(__is_nothrow_swappable_v<value_compare>) |
| 1819 | #endif |
| 1820 | { |
| 1821 | using std::swap; |
| 1822 | swap(__begin_node_, __t.__begin_node_); |
| 1823 | swap(__end_node_, __t.__end_node_); |
| 1824 | std::__swap_allocator(__node_alloc(), __t.__node_alloc()); |
| 1825 | swap(__size_, __t.__size_); |
| 1826 | swap(__value_comp_, __t.__value_comp_); |
| 1827 | if (__size_ == 0) |
| 1828 | __begin_node_ = __end_node(); |
| 1829 | else |
| 1830 | __end_node()->__left_->__parent_ = __end_node(); |
| 1831 | if (__t.__size_ == 0) |
| 1832 | __t.__begin_node_ = __t.__end_node(); |
| 1833 | else |
| 1834 | __t.__end_node()->__left_->__parent_ = __t.__end_node(); |
| 1835 | } |
| 1836 | |
| 1837 | template <class _Tp, class _Compare, class _Allocator> |
| 1838 | _LIBCPP_CONSTEXPR_SINCE_CXX26 void __tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT { |
| 1839 | destroy(nd: __root()); |
| 1840 | __size_ = 0; |
| 1841 | __begin_node_ = __end_node(); |
| 1842 | __end_node()->__left_ = nullptr; |
| 1843 | } |
| 1844 | |
| 1845 | // Find lower_bound place to insert |
| 1846 | // Set __parent to parent of null leaf |
| 1847 | // Return reference to null leaf |
| 1848 | template <class _Tp, class _Compare, class _Allocator> |
| 1849 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& |
| 1850 | __tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__end_node_pointer& __parent, const value_type& __v) { |
| 1851 | __node_pointer __nd = __root(); |
| 1852 | if (__nd != nullptr) { |
| 1853 | while (true) { |
| 1854 | if (value_comp()(__nd->__get_value(), __v)) { |
| 1855 | if (__nd->__right_ != nullptr) |
| 1856 | __nd = std::__static_fancy_pointer_cast<__node_pointer>(__nd->__right_); |
| 1857 | else { |
| 1858 | __parent = std::__static_fancy_pointer_cast<__end_node_pointer>(__nd); |
| 1859 | return __nd->__right_; |
| 1860 | } |
| 1861 | } else { |
| 1862 | if (__nd->__left_ != nullptr) |
| 1863 | __nd = std::__static_fancy_pointer_cast<__node_pointer>(__nd->__left_); |
| 1864 | else { |
| 1865 | __parent = std::__static_fancy_pointer_cast<__end_node_pointer>(__nd); |
| 1866 | return __parent->__left_; |
| 1867 | } |
| 1868 | } |
| 1869 | } |
| 1870 | } |
| 1871 | __parent = __end_node(); |
| 1872 | return __parent->__left_; |
| 1873 | } |
| 1874 | |
| 1875 | // Find upper_bound place to insert |
| 1876 | // Set __parent to parent of null leaf |
| 1877 | // Return reference to null leaf |
| 1878 | template <class _Tp, class _Compare, class _Allocator> |
| 1879 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& |
| 1880 | __tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__end_node_pointer& __parent, const value_type& __v) { |
| 1881 | __node_pointer __nd = __root(); |
| 1882 | if (__nd != nullptr) { |
| 1883 | while (true) { |
| 1884 | if (value_comp()(__v, __nd->__get_value())) { |
| 1885 | if (__nd->__left_ != nullptr) |
| 1886 | __nd = std::__static_fancy_pointer_cast<__node_pointer>(__nd->__left_); |
| 1887 | else { |
| 1888 | __parent = std::__static_fancy_pointer_cast<__end_node_pointer>(__nd); |
| 1889 | return __parent->__left_; |
| 1890 | } |
| 1891 | } else { |
| 1892 | if (__nd->__right_ != nullptr) |
| 1893 | __nd = std::__static_fancy_pointer_cast<__node_pointer>(__nd->__right_); |
| 1894 | else { |
| 1895 | __parent = std::__static_fancy_pointer_cast<__end_node_pointer>(__nd); |
| 1896 | return __nd->__right_; |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | } |
| 1901 | __parent = __end_node(); |
| 1902 | return __parent->__left_; |
| 1903 | } |
| 1904 | |
| 1905 | // Find leaf place to insert closest to __hint |
| 1906 | // First check prior to __hint. |
| 1907 | // Next check after __hint. |
| 1908 | // Next do O(log N) search. |
| 1909 | // Set __parent to parent of null leaf |
| 1910 | // Return reference to null leaf |
| 1911 | template <class _Tp, class _Compare, class _Allocator> |
| 1912 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& |
| 1913 | __tree<_Tp, _Compare, _Allocator>::__find_leaf( |
| 1914 | const_iterator __hint, __end_node_pointer& __parent, const value_type& __v) { |
| 1915 | if (__hint == end() || !value_comp()(*__hint, __v)) // check before |
| 1916 | { |
| 1917 | // __v <= *__hint |
| 1918 | const_iterator __prior = __hint; |
| 1919 | if (__prior == begin() || !value_comp()(__v, *--__prior)) { |
| 1920 | // *prev(__hint) <= __v <= *__hint |
| 1921 | if (__hint.__ptr_->__left_ == nullptr) { |
| 1922 | __parent = std::__static_fancy_pointer_cast<__end_node_pointer>(__hint.__ptr_); |
| 1923 | return __parent->__left_; |
| 1924 | } else { |
| 1925 | __parent = std::__static_fancy_pointer_cast<__end_node_pointer>(__prior.__ptr_); |
| 1926 | return std::__static_fancy_pointer_cast<__node_base_pointer>(__prior.__ptr_)->__right_; |
| 1927 | } |
| 1928 | } |
| 1929 | // __v < *prev(__hint) |
| 1930 | return __find_leaf_high(__parent, __v); |
| 1931 | } |
| 1932 | // else __v > *__hint |
| 1933 | return __find_leaf_low(__parent, __v); |
| 1934 | } |
| 1935 | |
| 1936 | // Find __v |
| 1937 | // If __v exists, return the parent of the node of __v and a reference to the pointer to the node of __v. |
| 1938 | // If __v doesn't exist, return the parent of the null leaf and a reference to the pointer to the null leaf. |
| 1939 | template <class _Tp, class _Compare, class _Allocator> |
| 1940 | template <class _Key> |
| 1941 | _LIBCPP_HIDE_FROM_ABI |
| 1942 | _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<typename __tree<_Tp, _Compare, _Allocator>::__end_node_pointer, |
| 1943 | typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&> |
| 1944 | __tree<_Tp, _Compare, _Allocator>::__find_equal(const _Key& __v) { |
| 1945 | using _Pair = pair<__end_node_pointer, __node_base_pointer&>; |
| 1946 | |
| 1947 | __node_pointer __nd = __root(); |
| 1948 | |
| 1949 | if (__nd == nullptr) { |
| 1950 | auto __end = __end_node(); |
| 1951 | return _Pair(__end, __end->__left_); |
| 1952 | } |
| 1953 | |
| 1954 | __node_base_pointer* __node_ptr = __root_ptr(); |
| 1955 | auto&& __transparent = std::__as_transparent<key_type>(value_comp()); |
| 1956 | auto __comp = |
| 1957 | __lazy_synth_three_way_comparator<__make_transparent_t<key_type, _Compare>, _Key, value_type>(__transparent); |
| 1958 | |
| 1959 | while (true) { |
| 1960 | auto __comp_res = __comp(__v, __nd->__get_value()); |
| 1961 | |
| 1962 | if (__comp_res.__less()) { |
| 1963 | if (__nd->__left_ == nullptr) |
| 1964 | return _Pair(std::__static_fancy_pointer_cast<__end_node_pointer>(__nd), __nd->__left_); |
| 1965 | |
| 1966 | __node_ptr = std::addressof(__nd->__left_); |
| 1967 | __nd = std::__static_fancy_pointer_cast<__node_pointer>(__nd->__left_); |
| 1968 | } else if (__comp_res.__greater()) { |
| 1969 | if (__nd->__right_ == nullptr) |
| 1970 | return _Pair(std::__static_fancy_pointer_cast<__end_node_pointer>(__nd), __nd->__right_); |
| 1971 | |
| 1972 | __node_ptr = std::addressof(__nd->__right_); |
| 1973 | __nd = std::__static_fancy_pointer_cast<__node_pointer>(__nd->__right_); |
| 1974 | } else { |
| 1975 | return _Pair(std::__static_fancy_pointer_cast<__end_node_pointer>(__nd), *__node_ptr); |
| 1976 | } |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | // Find __v |
| 1981 | // First check prior to __hint. |
| 1982 | // Next check after __hint. |
| 1983 | // Next do O(log N) search. |
| 1984 | // If __v exists, return the parent of the node of __v and a reference to the pointer to the node of __v. |
| 1985 | // If __v doesn't exist, return the parent of the null leaf and a reference to the pointer to the null leaf. |
| 1986 | template <class _Tp, class _Compare, class _Allocator> |
| 1987 | template <class _Key> |
| 1988 | _LIBCPP_HIDE_FROM_ABI |
| 1989 | _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<typename __tree<_Tp, _Compare, _Allocator>::__end_node_pointer, |
| 1990 | typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&> |
| 1991 | __tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint, __node_base_pointer& __dummy, const _Key& __v) { |
| 1992 | using _Pair = pair<__end_node_pointer, __node_base_pointer&>; |
| 1993 | |
| 1994 | if (__hint == end() || value_comp()(__v, *__hint)) { // check before |
| 1995 | // __v < *__hint |
| 1996 | const_iterator __prior = __hint; |
| 1997 | if (__prior == begin() || value_comp()(*--__prior, __v)) { |
| 1998 | // *prev(__hint) < __v < *__hint |
| 1999 | if (__hint.__ptr_->__left_ == nullptr) |
| 2000 | return _Pair(__hint.__ptr_, __hint.__ptr_->__left_); |
| 2001 | return _Pair(__prior.__ptr_, std::__static_fancy_pointer_cast<__node_pointer>(__prior.__ptr_)->__right_); |
| 2002 | } |
| 2003 | // __v <= *prev(__hint) |
| 2004 | return __find_equal(__v); |
| 2005 | } |
| 2006 | |
| 2007 | if (value_comp()(*__hint, __v)) { // check after |
| 2008 | // *__hint < __v |
| 2009 | const_iterator __next = std::next(__hint); |
| 2010 | if (__next == end() || value_comp()(__v, *__next)) { |
| 2011 | // *__hint < __v < *std::next(__hint) |
| 2012 | if (__hint.__get_np()->__right_ == nullptr) |
| 2013 | return _Pair(__hint.__ptr_, std::__static_fancy_pointer_cast<__node_pointer>(__hint.__ptr_)->__right_); |
| 2014 | return _Pair(__next.__ptr_, __next.__ptr_->__left_); |
| 2015 | } |
| 2016 | // *next(__hint) <= __v |
| 2017 | return __find_equal(__v); |
| 2018 | } |
| 2019 | |
| 2020 | // else __v == *__hint |
| 2021 | __dummy = std::__static_fancy_pointer_cast<__node_base_pointer>(__hint.__ptr_); |
| 2022 | return _Pair(__hint.__ptr_, __dummy); |
| 2023 | } |
| 2024 | |
| 2025 | template <class _Tp, class _Compare, class _Allocator> |
| 2026 | _LIBCPP_CONSTEXPR_SINCE_CXX26 void __tree<_Tp, _Compare, _Allocator>::__insert_node_at( |
| 2027 | __end_node_pointer __parent, __node_base_pointer& __child, __node_base_pointer __new_node) _NOEXCEPT { |
| 2028 | __new_node->__left_ = nullptr; |
| 2029 | __new_node->__right_ = nullptr; |
| 2030 | __new_node->__parent_ = __parent; |
| 2031 | // __new_node->__is_black_ is initialized in __tree_balance_after_insert |
| 2032 | __child = __new_node; |
| 2033 | if (__begin_node_->__left_ != nullptr) |
| 2034 | __begin_node_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__begin_node_->__left_); |
| 2035 | std::__tree_balance_after_insert(__end_node()->__left_, __child); |
| 2036 | ++__size_; |
| 2037 | } |
| 2038 | |
| 2039 | template <class _Tp, class _Compare, class _Allocator> |
| 2040 | template <class... _Args> |
| 2041 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::__node_holder |
| 2042 | __tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&&... __args) { |
| 2043 | __node_allocator& __na = __node_alloc(); |
| 2044 | __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); |
| 2045 | std::__construct_at(std::addressof(*__h), __na, std::forward<_Args>(__args)...); |
| 2046 | __h.get_deleter().__value_constructed = true; |
| 2047 | return __h; |
| 2048 | } |
| 2049 | |
| 2050 | template <class _Tp, class _Compare, class _Allocator> |
| 2051 | template <class... _Args> |
| 2052 | typename __tree<_Tp, _Compare, _Allocator>::iterator _LIBCPP_CONSTEXPR_SINCE_CXX26 |
| 2053 | __tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args) { |
| 2054 | __node_holder __h = __construct_node(std::forward<_Args>(__args)...); |
| 2055 | __end_node_pointer __parent; |
| 2056 | __node_base_pointer& __child = __find_leaf_high(__parent, v: __h->__get_value()); |
| 2057 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__h.get())); |
| 2058 | return iterator(std::__static_fancy_pointer_cast<__node_pointer>(__h.release())); |
| 2059 | } |
| 2060 | |
| 2061 | template <class _Tp, class _Compare, class _Allocator> |
| 2062 | template <class... _Args> |
| 2063 | typename __tree<_Tp, _Compare, _Allocator>::iterator _LIBCPP_CONSTEXPR_SINCE_CXX26 |
| 2064 | __tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p, _Args&&... __args) { |
| 2065 | __node_holder __h = __construct_node(std::forward<_Args>(__args)...); |
| 2066 | __end_node_pointer __parent; |
| 2067 | __node_base_pointer& __child = __find_leaf(hint: __p, __parent, v: __h->__get_value()); |
| 2068 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__h.get())); |
| 2069 | return iterator(std::__static_fancy_pointer_cast<__node_pointer>(__h.release())); |
| 2070 | } |
| 2071 | |
| 2072 | template <class _Tp, class _Compare, class _Allocator> |
| 2073 | typename __tree<_Tp, _Compare, _Allocator>::iterator _LIBCPP_CONSTEXPR_SINCE_CXX26 |
| 2074 | __tree<_Tp, _Compare, _Allocator>::__remove_node_pointer(__node_pointer __ptr) _NOEXCEPT { |
| 2075 | iterator __r(__ptr); |
| 2076 | ++__r; |
| 2077 | if (__begin_node_ == __ptr) |
| 2078 | __begin_node_ = __r.__ptr_; |
| 2079 | --__size_; |
| 2080 | std::__tree_remove(__end_node()->__left_, std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr)); |
| 2081 | return __r; |
| 2082 | } |
| 2083 | |
| 2084 | #if _LIBCPP_STD_VER >= 17 |
| 2085 | template <class _Tp, class _Compare, class _Allocator> |
| 2086 | template <class _NodeHandle, class _InsertReturnType> |
| 2087 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _InsertReturnType |
| 2088 | __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(_NodeHandle&& __nh) { |
| 2089 | if (__nh.empty()) |
| 2090 | return _InsertReturnType{end(), false, _NodeHandle()}; |
| 2091 | |
| 2092 | __node_pointer __ptr = __nh.__ptr_; |
| 2093 | auto [__parent, __child] = __find_equal(__ptr->__get_value()); |
| 2094 | if (__child != nullptr) |
| 2095 | return _InsertReturnType{ |
| 2096 | iterator(std::__static_fancy_pointer_cast<__node_pointer>(__child)), false, std::move(__nh)}; |
| 2097 | |
| 2098 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr)); |
| 2099 | __nh.__release_ptr(); |
| 2100 | return _InsertReturnType{iterator(__ptr), true, _NodeHandle()}; |
| 2101 | } |
| 2102 | |
| 2103 | template <class _Tp, class _Compare, class _Allocator> |
| 2104 | template <class _NodeHandle> |
| 2105 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::iterator |
| 2106 | __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(const_iterator __hint, _NodeHandle&& __nh) { |
| 2107 | if (__nh.empty()) |
| 2108 | return end(); |
| 2109 | |
| 2110 | __node_pointer __ptr = __nh.__ptr_; |
| 2111 | __node_base_pointer __dummy; |
| 2112 | auto [__parent, __child] = __find_equal(__hint, __dummy, __ptr->__get_value()); |
| 2113 | __node_pointer __r = std::__static_fancy_pointer_cast<__node_pointer>(__child); |
| 2114 | if (__child == nullptr) { |
| 2115 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr)); |
| 2116 | __r = __ptr; |
| 2117 | __nh.__release_ptr(); |
| 2118 | } |
| 2119 | return iterator(__r); |
| 2120 | } |
| 2121 | |
| 2122 | template <class _Tp, class _Compare, class _Allocator> |
| 2123 | template <class _NodeHandle> |
| 2124 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodeHandle |
| 2125 | __tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key) { |
| 2126 | iterator __it = __lower_bound_multi(__key); |
| 2127 | if (__it == end() || __value_comp_(__key, *__it)) |
| 2128 | return _NodeHandle(); |
| 2129 | return __node_handle_extract<_NodeHandle>(__it); |
| 2130 | } |
| 2131 | |
| 2132 | template <class _Tp, class _Compare, class _Allocator> |
| 2133 | template <class _NodeHandle> |
| 2134 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodeHandle |
| 2135 | __tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p) { |
| 2136 | __node_pointer __np = __p.__get_np(); |
| 2137 | __remove_node_pointer(ptr: __np); |
| 2138 | return _NodeHandle(__np, __alloc()); |
| 2139 | } |
| 2140 | |
| 2141 | template <class _Tp, class _Compare, class _Allocator> |
| 2142 | template <class _Comp2> |
| 2143 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 2144 | __tree<_Tp, _Compare, _Allocator>::__node_handle_merge_unique(__tree<_Tp, _Comp2, _Allocator>& __source) { |
| 2145 | for (iterator __i = __source.begin(); __i != __source.end();) { |
| 2146 | __node_pointer __src_ptr = __i.__get_np(); |
| 2147 | auto [__parent, __child] = __find_equal(__src_ptr->__get_value()); |
| 2148 | ++__i; |
| 2149 | if (__child != nullptr) |
| 2150 | continue; |
| 2151 | __source.__remove_node_pointer(__src_ptr); |
| 2152 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__src_ptr)); |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | template <class _Tp, class _Compare, class _Allocator> |
| 2157 | template <class _NodeHandle> |
| 2158 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::iterator |
| 2159 | __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(_NodeHandle&& __nh) { |
| 2160 | if (__nh.empty()) |
| 2161 | return end(); |
| 2162 | __node_pointer __ptr = __nh.__ptr_; |
| 2163 | __end_node_pointer __parent; |
| 2164 | __node_base_pointer& __child = __find_leaf_high(__parent, v: __ptr->__get_value()); |
| 2165 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr)); |
| 2166 | __nh.__release_ptr(); |
| 2167 | return iterator(__ptr); |
| 2168 | } |
| 2169 | |
| 2170 | template <class _Tp, class _Compare, class _Allocator> |
| 2171 | template <class _NodeHandle> |
| 2172 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::iterator |
| 2173 | __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh) { |
| 2174 | if (__nh.empty()) |
| 2175 | return end(); |
| 2176 | |
| 2177 | __node_pointer __ptr = __nh.__ptr_; |
| 2178 | __end_node_pointer __parent; |
| 2179 | __node_base_pointer& __child = __find_leaf(__hint, __parent, v: __ptr->__get_value()); |
| 2180 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr)); |
| 2181 | __nh.__release_ptr(); |
| 2182 | return iterator(__ptr); |
| 2183 | } |
| 2184 | |
| 2185 | template <class _Tp, class _Compare, class _Allocator> |
| 2186 | template <class _Comp2> |
| 2187 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 2188 | __tree<_Tp, _Compare, _Allocator>::__node_handle_merge_multi(__tree<_Tp, _Comp2, _Allocator>& __source) { |
| 2189 | for (iterator __i = __source.begin(); __i != __source.end();) { |
| 2190 | __node_pointer __src_ptr = __i.__get_np(); |
| 2191 | __end_node_pointer __parent; |
| 2192 | __node_base_pointer& __child = __find_leaf_high(__parent, v: __src_ptr->__get_value()); |
| 2193 | ++__i; |
| 2194 | __source.__remove_node_pointer(__src_ptr); |
| 2195 | __insert_node_at(__parent, __child, new_node: std::__static_fancy_pointer_cast<__node_base_pointer>(__src_ptr)); |
| 2196 | } |
| 2197 | } |
| 2198 | #endif // _LIBCPP_STD_VER >= 17 |
| 2199 | |
| 2200 | template <class _Tp, class _Compare, class _Allocator> |
| 2201 | _LIBCPP_CONSTEXPR_SINCE_CXX26 |
| 2202 | typename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p) { |
| 2203 | __node_pointer __np = __p.__get_np(); |
| 2204 | iterator __r = __remove_node_pointer(ptr: __np); |
| 2205 | __node_allocator& __na = __node_alloc(); |
| 2206 | __node_traits::destroy(__na, std::addressof(const_cast<value_type&>(*__p))); |
| 2207 | __node_traits::deallocate(__na, __np, 1); |
| 2208 | return __r; |
| 2209 | } |
| 2210 | |
| 2211 | template <class _Tp, class _Compare, class _Allocator> |
| 2212 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::iterator |
| 2213 | __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l) { |
| 2214 | while (__f != __l) |
| 2215 | __f = erase(__f); |
| 2216 | return iterator(__l.__ptr_); |
| 2217 | } |
| 2218 | |
| 2219 | template <class _Tp, class _Compare, class _Allocator> |
| 2220 | template <class _Key> |
| 2221 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::size_type |
| 2222 | __tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k) { |
| 2223 | iterator __i = find(__k); |
| 2224 | if (__i == end()) |
| 2225 | return 0; |
| 2226 | erase(__i); |
| 2227 | return 1; |
| 2228 | } |
| 2229 | |
| 2230 | template <class _Tp, class _Compare, class _Allocator> |
| 2231 | template <class _Key> |
| 2232 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::size_type |
| 2233 | __tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k) { |
| 2234 | pair<iterator, iterator> __p = __equal_range_multi(__k); |
| 2235 | size_type __r = 0; |
| 2236 | for (; __p.first != __p.second; ++__r) |
| 2237 | __p.first = erase(__p.first); |
| 2238 | return __r; |
| 2239 | } |
| 2240 | |
| 2241 | template <class _Tp, class _Compare, class _Allocator> |
| 2242 | template <class _Key> |
| 2243 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::size_type |
| 2244 | __tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const { |
| 2245 | __node_pointer __rt = __root(); |
| 2246 | auto __comp = __lazy_synth_three_way_comparator<value_compare, _Key, value_type>(value_comp()); |
| 2247 | while (__rt != nullptr) { |
| 2248 | auto __comp_res = __comp(__k, __rt->__get_value()); |
| 2249 | if (__comp_res.__less()) { |
| 2250 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_); |
| 2251 | } else if (__comp_res.__greater()) |
| 2252 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_); |
| 2253 | else |
| 2254 | return 1; |
| 2255 | } |
| 2256 | return 0; |
| 2257 | } |
| 2258 | |
| 2259 | template <class _Tp, class _Compare, class _Allocator> |
| 2260 | template <class _Key> |
| 2261 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::size_type |
| 2262 | __tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const { |
| 2263 | __end_node_pointer __result = __end_node(); |
| 2264 | __node_pointer __rt = __root(); |
| 2265 | auto __comp = __lazy_synth_three_way_comparator<value_compare, _Key, value_type>(value_comp()); |
| 2266 | while (__rt != nullptr) { |
| 2267 | auto __comp_res = __comp(__k, __rt->__get_value()); |
| 2268 | if (__comp_res.__less()) { |
| 2269 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt); |
| 2270 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_); |
| 2271 | } else if (__comp_res.__greater()) |
| 2272 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_); |
| 2273 | else |
| 2274 | return std::distance( |
| 2275 | __lower_bound_multi(__k, |
| 2276 | std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_), |
| 2277 | std::__static_fancy_pointer_cast<__end_node_pointer>(__rt)), |
| 2278 | __upper_bound_multi(__k, std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_), __result)); |
| 2279 | } |
| 2280 | return 0; |
| 2281 | } |
| 2282 | |
| 2283 | template <class _Tp, class _Compare, class _Allocator> |
| 2284 | template <class _Key> |
| 2285 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::iterator |
| 2286 | __tree<_Tp, _Compare, _Allocator>::__lower_bound_multi( |
| 2287 | const _Key& __v, __node_pointer __root, __end_node_pointer __result) { |
| 2288 | while (__root != nullptr) { |
| 2289 | if (!value_comp()(__root->__get_value(), __v)) { |
| 2290 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__root); |
| 2291 | __root = std::__static_fancy_pointer_cast<__node_pointer>(__root->__left_); |
| 2292 | } else |
| 2293 | __root = std::__static_fancy_pointer_cast<__node_pointer>(__root->__right_); |
| 2294 | } |
| 2295 | return iterator(__result); |
| 2296 | } |
| 2297 | |
| 2298 | template <class _Tp, class _Compare, class _Allocator> |
| 2299 | template <class _Key> |
| 2300 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::const_iterator |
| 2301 | __tree<_Tp, _Compare, _Allocator>::__lower_bound_multi( |
| 2302 | const _Key& __v, __node_pointer __root, __end_node_pointer __result) const { |
| 2303 | while (__root != nullptr) { |
| 2304 | if (!value_comp()(__root->__get_value(), __v)) { |
| 2305 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__root); |
| 2306 | __root = std::__static_fancy_pointer_cast<__node_pointer>(__root->__left_); |
| 2307 | } else |
| 2308 | __root = std::__static_fancy_pointer_cast<__node_pointer>(__root->__right_); |
| 2309 | } |
| 2310 | return const_iterator(__result); |
| 2311 | } |
| 2312 | |
| 2313 | template <class _Tp, class _Compare, class _Allocator> |
| 2314 | template <class _Key> |
| 2315 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::iterator |
| 2316 | __tree<_Tp, _Compare, _Allocator>::__upper_bound_multi( |
| 2317 | const _Key& __v, __node_pointer __root, __end_node_pointer __result) { |
| 2318 | while (__root != nullptr) { |
| 2319 | if (value_comp()(__v, __root->__get_value())) { |
| 2320 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__root); |
| 2321 | __root = std::__static_fancy_pointer_cast<__node_pointer>(__root->__left_); |
| 2322 | } else |
| 2323 | __root = std::__static_fancy_pointer_cast<__node_pointer>(__root->__right_); |
| 2324 | } |
| 2325 | return iterator(__result); |
| 2326 | } |
| 2327 | |
| 2328 | template <class _Tp, class _Compare, class _Allocator> |
| 2329 | template <class _Key> |
| 2330 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::const_iterator |
| 2331 | __tree<_Tp, _Compare, _Allocator>::__upper_bound_multi( |
| 2332 | const _Key& __v, __node_pointer __root, __end_node_pointer __result) const { |
| 2333 | while (__root != nullptr) { |
| 2334 | if (value_comp()(__v, __root->__get_value())) { |
| 2335 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__root); |
| 2336 | __root = std::__static_fancy_pointer_cast<__node_pointer>(__root->__left_); |
| 2337 | } else |
| 2338 | __root = std::__static_fancy_pointer_cast<__node_pointer>(__root->__right_); |
| 2339 | } |
| 2340 | return const_iterator(__result); |
| 2341 | } |
| 2342 | |
| 2343 | template <class _Tp, class _Compare, class _Allocator> |
| 2344 | template <class _Key> |
| 2345 | _LIBCPP_CONSTEXPR_SINCE_CXX26 |
| 2346 | pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, typename __tree<_Tp, _Compare, _Allocator>::iterator> |
| 2347 | __tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) { |
| 2348 | using _Pp = pair<iterator, iterator>; |
| 2349 | __end_node_pointer __result = __end_node(); |
| 2350 | __node_pointer __rt = __root(); |
| 2351 | auto __comp = __lazy_synth_three_way_comparator<value_compare, _Key, value_type>(value_comp()); |
| 2352 | while (__rt != nullptr) { |
| 2353 | auto __comp_res = __comp(__k, __rt->__get_value()); |
| 2354 | if (__comp_res.__less()) { |
| 2355 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt); |
| 2356 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_); |
| 2357 | } else if (__comp_res.__greater()) |
| 2358 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_); |
| 2359 | else |
| 2360 | return _Pp(iterator(__rt), |
| 2361 | iterator(__rt->__right_ != nullptr |
| 2362 | ? std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_min(__rt->__right_)) |
| 2363 | : __result)); |
| 2364 | } |
| 2365 | return _Pp(iterator(__result), iterator(__result)); |
| 2366 | } |
| 2367 | |
| 2368 | template <class _Tp, class _Compare, class _Allocator> |
| 2369 | template <class _Key> |
| 2370 | _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator, |
| 2371 | typename __tree<_Tp, _Compare, _Allocator>::const_iterator> |
| 2372 | __tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const { |
| 2373 | using _Pp = pair<const_iterator, const_iterator>; |
| 2374 | __end_node_pointer __result = __end_node(); |
| 2375 | __node_pointer __rt = __root(); |
| 2376 | auto __comp = __lazy_synth_three_way_comparator<value_compare, _Key, value_type>(value_comp()); |
| 2377 | while (__rt != nullptr) { |
| 2378 | auto __comp_res = __comp(__k, __rt->__get_value()); |
| 2379 | if (__comp_res.__less()) { |
| 2380 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt); |
| 2381 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_); |
| 2382 | } else if (__comp_res.__greater()) |
| 2383 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_); |
| 2384 | else |
| 2385 | return _Pp( |
| 2386 | const_iterator(__rt), |
| 2387 | const_iterator(__rt->__right_ != nullptr |
| 2388 | ? std::__static_fancy_pointer_cast<__end_node_pointer>(std::__tree_min(__rt->__right_)) |
| 2389 | : __result)); |
| 2390 | } |
| 2391 | return _Pp(const_iterator(__result), const_iterator(__result)); |
| 2392 | } |
| 2393 | |
| 2394 | template <class _Tp, class _Compare, class _Allocator> |
| 2395 | template <class _Key> |
| 2396 | _LIBCPP_CONSTEXPR_SINCE_CXX26 |
| 2397 | pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, typename __tree<_Tp, _Compare, _Allocator>::iterator> |
| 2398 | __tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) { |
| 2399 | using _Pp = pair<iterator, iterator>; |
| 2400 | __end_node_pointer __result = __end_node(); |
| 2401 | __node_pointer __rt = __root(); |
| 2402 | auto __comp = __lazy_synth_three_way_comparator<value_compare, _Key, value_type>(value_comp()); |
| 2403 | while (__rt != nullptr) { |
| 2404 | auto __comp_res = __comp(__k, __rt->__get_value()); |
| 2405 | if (__comp_res.__less()) { |
| 2406 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt); |
| 2407 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_); |
| 2408 | } else if (__comp_res.__greater()) |
| 2409 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_); |
| 2410 | else |
| 2411 | return _Pp(__lower_bound_multi(__k, |
| 2412 | std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_), |
| 2413 | std::__static_fancy_pointer_cast<__end_node_pointer>(__rt)), |
| 2414 | __upper_bound_multi(__k, std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_), __result)); |
| 2415 | } |
| 2416 | return _Pp(iterator(__result), iterator(__result)); |
| 2417 | } |
| 2418 | |
| 2419 | template <class _Tp, class _Compare, class _Allocator> |
| 2420 | template <class _Key> |
| 2421 | _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator, |
| 2422 | typename __tree<_Tp, _Compare, _Allocator>::const_iterator> |
| 2423 | __tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const { |
| 2424 | using _Pp = pair<const_iterator, const_iterator>; |
| 2425 | __end_node_pointer __result = __end_node(); |
| 2426 | __node_pointer __rt = __root(); |
| 2427 | auto __comp = __lazy_synth_three_way_comparator<value_compare, _Key, value_type>(value_comp()); |
| 2428 | while (__rt != nullptr) { |
| 2429 | auto __comp_res = __comp(__k, __rt->__get_value()); |
| 2430 | if (__comp_res.__less()) { |
| 2431 | __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt); |
| 2432 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_); |
| 2433 | } else if (__comp_res.__greater()) |
| 2434 | __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_); |
| 2435 | else |
| 2436 | return _Pp(__lower_bound_multi(__k, |
| 2437 | std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_), |
| 2438 | std::__static_fancy_pointer_cast<__end_node_pointer>(__rt)), |
| 2439 | __upper_bound_multi(__k, std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_), __result)); |
| 2440 | } |
| 2441 | return _Pp(const_iterator(__result), const_iterator(__result)); |
| 2442 | } |
| 2443 | |
| 2444 | template <class _Tp, class _Compare, class _Allocator> |
| 2445 | _LIBCPP_CONSTEXPR_SINCE_CXX26 typename __tree<_Tp, _Compare, _Allocator>::__node_holder |
| 2446 | __tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT { |
| 2447 | __node_pointer __np = __p.__get_np(); |
| 2448 | if (__begin_node_ == __p.__ptr_) { |
| 2449 | if (__np->__right_ != nullptr) |
| 2450 | __begin_node_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__np->__right_); |
| 2451 | else |
| 2452 | __begin_node_ = std::__static_fancy_pointer_cast<__end_node_pointer>(__np->__parent_); |
| 2453 | } |
| 2454 | --__size_; |
| 2455 | std::__tree_remove(__end_node()->__left_, std::__static_fancy_pointer_cast<__node_base_pointer>(__np)); |
| 2456 | return __node_holder(__np, _Dp(__node_alloc(), true)); |
| 2457 | } |
| 2458 | |
| 2459 | template <class _Tp, class _Compare, class _Allocator> |
| 2460 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void |
| 2461 | swap(__tree<_Tp, _Compare, _Allocator>& __x, __tree<_Tp, _Compare, _Allocator>& __y) |
| 2462 | _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) { |
| 2463 | __x.swap(__y); |
| 2464 | } |
| 2465 | |
| 2466 | _LIBCPP_END_NAMESPACE_STD |
| 2467 | |
| 2468 | _LIBCPP_POP_MACROS |
| 2469 | |
| 2470 | #endif // _LIBCPP___TREE |
| 2471 | |