3 // Copyright (C) 2005 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
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file split_join_fn_imps.hpp
42 * Contains an implementation for rb_tree_.
47 PB_ASSOC_CLASS_C_DEC::
48 join(PB_ASSOC_CLASS_C_DEC& r_other)
50 PB_ASSOC_DBG_ONLY(assert_valid();)
51 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, true);)
53 PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
54 PB_ASSOC_DBG_ONLY(r_other.PB_ASSOC_BASE_C_DEC::assert_valid(true, true);)
56 if (PB_ASSOC_BASE_C_DEC::join_prep(r_other) == false)
58 PB_ASSOC_DBG_ONLY(assert_valid();)
59 PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
64 const node_pointer p_x = r_other.split_min();
66 join_imp(p_x, r_other.m_p_head->m_p_parent);
68 PB_ASSOC_BASE_C_DEC::join_finish(r_other);
70 PB_ASSOC_DBG_ONLY(assert_valid();)
71 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, true);)
73 PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
74 PB_ASSOC_DBG_ONLY(r_other.PB_ASSOC_BASE_C_DEC::assert_valid(true, true);)
79 PB_ASSOC_CLASS_C_DEC::
80 join_imp(node_pointer p_x, node_pointer p_r)
82 PB_ASSOC_DBG_ASSERT(p_x != NULL);
88 black_height(PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent);
89 const size_type other_h = black_height(p_r);
94 std::pair<node_pointer, node_pointer> join_pos;
96 const bool right_join = h >= other_h;
100 join_pos = find_join_pos_right(PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent, h, other_h);
102 p_x_l = join_pos.first;
107 p_x_l = PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent;
109 PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent = p_r;
111 p_r->m_p_parent = PB_ASSOC_BASE_C_DEC::m_p_head;
113 join_pos = find_join_pos_left(PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent, h, other_h);
115 p_x_r = join_pos.first;
118 node_pointer p_parent = join_pos.second;
120 if (p_parent == PB_ASSOC_BASE_C_DEC::m_p_head)
122 PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent = p_x;
124 p_x->m_p_parent = PB_ASSOC_BASE_C_DEC::m_p_head;
128 p_x->m_p_parent = p_parent;
131 p_x->m_p_parent->m_p_right = p_x;
133 p_x->m_p_parent->m_p_left = p_x;
136 p_x->m_p_left = p_x_l;
138 p_x_l->m_p_parent = p_x;
140 p_x->m_p_right = p_x_r;
142 p_x_r->m_p_parent = p_x;
146 PB_ASSOC_BASE_C_DEC::initialize_min_max();
148 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
150 PB_ASSOC_BASE_C_DEC::update_to_top(p_x, (Node_Updator* )this);
154 PB_ASSOC_DBG_ONLY(assert_valid());
155 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
159 inline typename PB_ASSOC_CLASS_C_DEC::node_pointer
160 PB_ASSOC_CLASS_C_DEC::
163 PB_ASSOC_DBG_ASSERT(PB_ASSOC_BASE_C_DEC::m_p_head->m_p_left !=
164 PB_ASSOC_BASE_C_DEC::m_p_head);
166 node_pointer p_min = PB_ASSOC_BASE_C_DEC::m_p_head->m_p_left;
175 typename PB_ASSOC_CLASS_C_DEC::node_pointer,
176 typename PB_ASSOC_CLASS_C_DEC::node_pointer>
177 PB_ASSOC_CLASS_C_DEC::
178 find_join_pos_right(node_pointer p_l, size_type h_l, size_type h_r)
180 PB_ASSOC_DBG_ASSERT(h_l >= h_r);
182 if (PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent == NULL)
183 return (std::make_pair((node_pointer)NULL,
184 PB_ASSOC_BASE_C_DEC::m_p_head));
186 node_pointer p_l_parent = PB_ASSOC_BASE_C_DEC::m_p_head;
190 if (p_l->m_red == false)
192 PB_ASSOC_DBG_ASSERT(h_l > 0);
199 p_l = p_l->m_p_right;
202 if (!is_effectively_black(p_l))
206 p_l = p_l->m_p_right;
209 PB_ASSOC_DBG_ASSERT(is_effectively_black(p_l));
210 PB_ASSOC_DBG_ASSERT(black_height(p_l) == h_r);
211 PB_ASSOC_DBG_ASSERT(p_l == NULL || p_l->m_p_parent == p_l_parent);
213 return (std::make_pair(p_l, p_l_parent));
218 typename PB_ASSOC_CLASS_C_DEC::node_pointer,
219 typename PB_ASSOC_CLASS_C_DEC::node_pointer>
220 PB_ASSOC_CLASS_C_DEC::
221 find_join_pos_left(node_pointer p_r, size_type h_l, size_type h_r)
223 PB_ASSOC_DBG_ASSERT(h_r > h_l);
225 if (PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent == NULL)
226 return (std::make_pair((node_pointer)NULL,
227 PB_ASSOC_BASE_C_DEC::m_p_head));
229 node_pointer p_r_parent = PB_ASSOC_BASE_C_DEC::m_p_head;
233 if (p_r->m_red == false)
235 PB_ASSOC_DBG_ASSERT(h_r > 0);
245 if (!is_effectively_black(p_r))
252 PB_ASSOC_DBG_ASSERT(is_effectively_black(p_r));
253 PB_ASSOC_DBG_ASSERT(black_height(p_r) == h_l);
254 PB_ASSOC_DBG_ASSERT(p_r == NULL || p_r->m_p_parent == p_r_parent);
256 return (std::make_pair(p_r, p_r_parent));
260 inline typename PB_ASSOC_CLASS_C_DEC::size_type
261 PB_ASSOC_CLASS_C_DEC::
262 black_height(node_pointer p_nd)
268 if (p_nd->m_red == false)
271 p_nd = p_nd->m_p_left;
279 PB_ASSOC_CLASS_C_DEC::
280 split(const_key_reference r_key, PB_ASSOC_CLASS_C_DEC& r_other)
282 PB_ASSOC_DBG_ONLY(assert_valid());
283 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, true);)
285 PB_ASSOC_DBG_ONLY(r_other.assert_valid());
286 PB_ASSOC_DBG_ONLY(r_other.PB_ASSOC_BASE_C_DEC::assert_valid(true, true);)
288 if (PB_ASSOC_BASE_C_DEC::split_prep(r_key, r_other) == false)
290 PB_ASSOC_DBG_ONLY(assert_valid());
291 PB_ASSOC_DBG_ONLY(r_other.assert_valid());
296 PB_ASSOC_DBG_ONLY(assert_valid());
297 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
299 PB_ASSOC_DBG_ONLY(r_other.assert_valid());
300 PB_ASSOC_DBG_ONLY(r_other.PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
302 node_pointer p_nd = upper_bound(r_key).m_p_nd;
306 node_pointer p_next_nd = p_nd->m_p_parent;
308 if (Cmp_Fn::operator()(
310 PB_ASSOC_V2F(p_nd->m_value)))
311 split_at_node(p_nd, r_other);
313 PB_ASSOC_DBG_ONLY(assert_valid());
314 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
316 PB_ASSOC_DBG_ONLY(r_other.assert_valid());
317 PB_ASSOC_DBG_ONLY(r_other.PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
321 while (p_nd != PB_ASSOC_BASE_C_DEC::m_p_head);
323 PB_ASSOC_BASE_C_DEC::split_finish(r_other);
325 PB_ASSOC_DBG_ONLY(assert_valid());
326 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, true);)
328 PB_ASSOC_DBG_ONLY(r_other.assert_valid());
329 PB_ASSOC_DBG_ONLY(r_other.PB_ASSOC_BASE_C_DEC::assert_valid(true, true);)
334 PB_ASSOC_CLASS_C_DEC::
335 split_at_node(node_pointer p_nd, PB_ASSOC_CLASS_C_DEC& r_other)
337 PB_ASSOC_DBG_ASSERT(p_nd != NULL);
339 node_pointer p_l = p_nd->m_p_left;
340 node_pointer p_r = p_nd->m_p_right;
342 node_pointer p_parent = p_nd->m_p_parent;
344 if (p_parent == PB_ASSOC_BASE_C_DEC::m_p_head)
346 PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent = p_l;
350 p_l->m_p_parent = PB_ASSOC_BASE_C_DEC::m_p_head;
357 if (p_parent->m_p_left == p_nd)
358 p_parent->m_p_left = p_l;
360 p_parent->m_p_right = p_l;
363 p_l->m_p_parent = p_parent;
365 update_to_top(p_parent, (Node_Updator* )this);
368 remove_fixup(p_l, p_parent);
371 PB_ASSOC_BASE_C_DEC::initialize_min_max();
373 PB_ASSOC_DBG_ONLY(assert_valid());
374 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
376 PB_ASSOC_DBG_ONLY(r_other.assert_valid());
377 PB_ASSOC_DBG_ONLY(r_other.PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
379 r_other.join_imp(p_nd, p_r);