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