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