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