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