3 // Copyright (C) 2005, 2006, 2009, 2010 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
27 // Permission to use, copy, modify, sell, and distribute this software
28 // is hereby granted without fee, provided that the above copyright
29 // notice appears in all copies, and that both that copyright notice
30 // and this permission notice appear in supporting documentation. None
31 // of the above authors, nor IBM Haifa Research Laboratories, make any
32 // representation about the suitability of this software for any
33 // purpose. It is provided "as is" without express or implied
37 * @file split_join_fn_imps.hpp
38 * Contains an implementation for rb_tree_.
44 join(PB_DS_CLASS_C_DEC& other)
46 _GLIBCXX_DEBUG_ONLY(assert_valid();)
47 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
48 _GLIBCXX_DEBUG_ONLY(other.base_type::assert_valid();)
49 if (base_type::join_prep(other) == false)
51 _GLIBCXX_DEBUG_ONLY(assert_valid();)
52 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
56 const node_pointer p_x = other.split_min();
57 join_imp(p_x, other.m_p_head->m_p_parent);
58 base_type::join_finish(other);
59 _GLIBCXX_DEBUG_ONLY(assert_valid();)
60 _GLIBCXX_DEBUG_ONLY(base_type::assert_valid();)
61 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
62 _GLIBCXX_DEBUG_ONLY(other.base_type::assert_valid();)
68 join_imp(node_pointer p_x, node_pointer p_r)
70 _GLIBCXX_DEBUG_ASSERT(p_x != 0);
74 const size_type h = black_height(base_type::m_p_head->m_p_parent);
75 const size_type other_h = black_height(p_r);
78 std::pair<node_pointer, node_pointer> join_pos;
79 const bool right_join = h >= other_h;
82 join_pos = find_join_pos_right(base_type::m_p_head->m_p_parent,
84 p_x_l = join_pos.first;
89 p_x_l = base_type::m_p_head->m_p_parent;
90 base_type::m_p_head->m_p_parent = p_r;
92 p_r->m_p_parent = base_type::m_p_head;
94 join_pos = find_join_pos_left(base_type::m_p_head->m_p_parent,
96 p_x_r = join_pos.first;
99 node_pointer p_parent = join_pos.second;
100 if (p_parent == base_type::m_p_head)
102 base_type::m_p_head->m_p_parent = p_x;
103 p_x->m_p_parent = base_type::m_p_head;
107 p_x->m_p_parent = p_parent;
109 p_x->m_p_parent->m_p_right = p_x;
111 p_x->m_p_parent->m_p_left = p_x;
114 p_x->m_p_left = p_x_l;
116 p_x_l->m_p_parent = p_x;
118 p_x->m_p_right = p_x_r;
120 p_x_r->m_p_parent = p_x;
124 base_type::initialize_min_max();
125 _GLIBCXX_DEBUG_ONLY(base_type::structure_only_assert_valid();)
126 base_type::update_to_top(p_x, (node_update* )this);
128 _GLIBCXX_DEBUG_ONLY(base_type::structure_only_assert_valid());
132 inline typename PB_DS_CLASS_C_DEC::node_pointer
136 node_pointer p_min = base_type::m_p_head->m_p_left;
138 #ifdef _GLIBCXX_DEBUG
139 const node_pointer p_head = base_type::m_p_head;
140 _GLIBCXX_DEBUG_ASSERT(p_min != p_head);
149 typename PB_DS_CLASS_C_DEC::node_pointer,
150 typename PB_DS_CLASS_C_DEC::node_pointer>
152 find_join_pos_right(node_pointer p_l, size_type h_l, size_type h_r)
154 _GLIBCXX_DEBUG_ASSERT(h_l >= h_r);
156 if (base_type::m_p_head->m_p_parent == 0)
157 return (std::make_pair((node_pointer)0, base_type::m_p_head));
159 node_pointer p_l_parent = base_type::m_p_head;
162 if (p_l->m_red == false)
164 _GLIBCXX_DEBUG_ASSERT(h_l > 0);
169 p_l = p_l->m_p_right;
172 if (!is_effectively_black(p_l))
175 p_l = p_l->m_p_right;
178 _GLIBCXX_DEBUG_ASSERT(is_effectively_black(p_l));
179 _GLIBCXX_DEBUG_ASSERT(black_height(p_l) == h_r);
180 _GLIBCXX_DEBUG_ASSERT(p_l == 0 || p_l->m_p_parent == p_l_parent);
181 return std::make_pair(p_l, p_l_parent);
186 typename PB_DS_CLASS_C_DEC::node_pointer,
187 typename PB_DS_CLASS_C_DEC::node_pointer>
189 find_join_pos_left(node_pointer p_r, size_type h_l, size_type h_r)
191 _GLIBCXX_DEBUG_ASSERT(h_r > h_l);
192 if (base_type::m_p_head->m_p_parent == 0)
193 return (std::make_pair((node_pointer)0,
194 base_type::m_p_head));
195 node_pointer p_r_parent = base_type::m_p_head;
198 if (p_r->m_red == false)
200 _GLIBCXX_DEBUG_ASSERT(h_r > 0);
208 if (!is_effectively_black(p_r))
214 _GLIBCXX_DEBUG_ASSERT(is_effectively_black(p_r));
215 _GLIBCXX_DEBUG_ASSERT(black_height(p_r) == h_l);
216 _GLIBCXX_DEBUG_ASSERT(p_r == 0 || p_r->m_p_parent == p_r_parent);
217 return std::make_pair(p_r, p_r_parent);
221 inline typename PB_DS_CLASS_C_DEC::size_type
223 black_height(node_pointer p_nd)
228 if (p_nd->m_red == false)
230 p_nd = p_nd->m_p_left;
238 split(const_key_reference r_key, PB_DS_CLASS_C_DEC& other)
240 _GLIBCXX_DEBUG_ONLY(assert_valid());
241 _GLIBCXX_DEBUG_ONLY(base_type::assert_valid();)
243 _GLIBCXX_DEBUG_ONLY(other.assert_valid());
244 _GLIBCXX_DEBUG_ONLY(other.base_type::assert_valid();)
246 if (base_type::split_prep(r_key, other) == false)
248 _GLIBCXX_DEBUG_ONLY(assert_valid());
249 _GLIBCXX_DEBUG_ONLY(other.assert_valid());
253 _GLIBCXX_DEBUG_ONLY(base_type::structure_only_assert_valid();)
254 _GLIBCXX_DEBUG_ONLY(other.base_type::structure_only_assert_valid();)
255 node_pointer p_nd = upper_bound(r_key).m_p_nd;
258 node_pointer p_next_nd = p_nd->m_p_parent;
259 if (Cmp_Fn::operator()(r_key, PB_DS_V2F(p_nd->m_value)))
260 split_at_node(p_nd, other);
262 _GLIBCXX_DEBUG_ONLY(base_type::structure_only_assert_valid();)
263 _GLIBCXX_DEBUG_ONLY(other.base_type::structure_only_assert_valid();)
266 while (p_nd != base_type::m_p_head);
268 base_type::split_finish(other);
269 _GLIBCXX_DEBUG_ONLY(assert_valid();)
270 _GLIBCXX_DEBUG_ONLY(assert_valid();)
276 split_at_node(node_pointer p_nd, PB_DS_CLASS_C_DEC& other)
278 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
280 node_pointer p_l = p_nd->m_p_left;
281 node_pointer p_r = p_nd->m_p_right;
282 node_pointer p_parent = p_nd->m_p_parent;
283 if (p_parent == base_type::m_p_head)
285 base_type::m_p_head->m_p_parent = p_l;
288 p_l->m_p_parent = base_type::m_p_head;
294 if (p_parent->m_p_left == p_nd)
295 p_parent->m_p_left = p_l;
297 p_parent->m_p_right = p_l;
300 p_l->m_p_parent = p_parent;
302 update_to_top(p_parent, (node_update* )this);
305 remove_fixup(p_l, p_parent);
308 base_type::initialize_min_max();
309 other.join_imp(p_nd, p_r);
310 _GLIBCXX_DEBUG_ONLY(base_type::structure_only_assert_valid());
311 _GLIBCXX_DEBUG_ONLY(other.base_type::structure_only_assert_valid());