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 erase_fn_imps.hpp
42 * Contains an implementation for rb_tree_.
46 inline typename
PB_ASSOC_CLASS_C_DEC::size_type
47 PB_ASSOC_CLASS_C_DEC::
48 erase(const_key_reference r_key
)
50 iterator it
= find(r_key
);
52 if (it
== PB_ASSOC_BASE_C_DEC::find_end())
61 inline typename
PB_ASSOC_CLASS_C_DEC::const_iterator
62 PB_ASSOC_CLASS_C_DEC::
63 erase(const_iterator it
)
65 PB_ASSOC_DBG_ONLY(assert_valid());
66 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
68 if (it
== PB_ASSOC_BASE_C_DEC::find_end())
71 const_iterator ret_it
= it
;
75 erase_node(it
.m_p_nd
);
77 PB_ASSOC_DBG_ONLY(assert_valid());
78 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
83 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
85 inline typename
PB_ASSOC_CLASS_C_DEC::iterator
86 PB_ASSOC_CLASS_C_DEC::
89 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid());
90 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_iterators();)
92 if (it
== PB_ASSOC_BASE_C_DEC::find_end())
99 erase_node(it
.m_p_nd
);
101 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid());
102 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_iterators();)
106 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
109 inline typename
PB_ASSOC_CLASS_C_DEC::const_reverse_iterator
110 PB_ASSOC_CLASS_C_DEC::
111 erase(const_reverse_iterator it
)
113 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid());
114 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_iterators();)
116 if (it
== PB_ASSOC_BASE_C_DEC::find_rend())
119 const_reverse_iterator ret_it
= it
;
123 erase_node(it
.m_p_nd
);
125 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid());
126 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_iterators();)
131 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
133 inline typename
PB_ASSOC_CLASS_C_DEC::reverse_iterator
134 PB_ASSOC_CLASS_C_DEC::
135 erase(reverse_iterator it
)
137 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid());
139 if (it
== PB_ASSOC_BASE_C_DEC::find_rend())
142 reverse_iterator ret_it
= it
;
146 erase_node(it
.m_p_nd
);
148 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid());
152 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
156 inline typename
PB_ASSOC_CLASS_C_DEC::size_type
157 PB_ASSOC_CLASS_C_DEC::
160 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
162 size_type num_ersd
= 0;
164 iterator it
= PB_ASSOC_BASE_C_DEC::begin();
166 while (it
!= PB_ASSOC_BASE_C_DEC::end())
176 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
183 PB_ASSOC_CLASS_C_DEC::
184 erase_node(node_pointer p_nd
)
188 PB_ASSOC_BASE_C_DEC::actual_erase_node(p_nd
);
190 PB_ASSOC_DBG_ONLY(assert_valid());
191 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_valid(true, false);)
196 PB_ASSOC_CLASS_C_DEC::
197 remove_node(node_pointer p_z
)
199 update_min_max_for_erased_node(p_z
);
201 node_pointer p_y
= p_z
;
203 node_pointer p_x
= NULL
;
205 node_pointer p_new_x_parent
= NULL
;
207 if (p_y
->m_p_left
== NULL
)
208 p_x
= p_y
->m_p_right
;
209 else if (p_y
->m_p_right
== NULL
)
213 p_y
= p_y
->m_p_right
;
215 while (p_y
->m_p_left
!= NULL
)
218 p_x
= p_y
->m_p_right
;
223 p_new_x_parent
= p_y
->m_p_parent
;
226 p_x
->m_p_parent
= p_y
->m_p_parent
;
228 if (PB_ASSOC_BASE_C_DEC::m_p_head
->m_p_parent
== p_z
)
229 PB_ASSOC_BASE_C_DEC::m_p_head
->m_p_parent
= p_x
;
230 else if (p_z
->m_p_parent
->m_p_left
== p_z
)
232 p_y
->m_p_left
= p_z
->m_p_parent
;
234 p_z
->m_p_parent
->m_p_left
= p_x
;
238 p_y
->m_p_left
= NULL
;
240 p_z
->m_p_parent
->m_p_right
= p_x
;
245 p_z
->m_p_left
->m_p_parent
= p_y
;
247 p_y
->m_p_left
= p_z
->m_p_left
;
249 if (p_y
!= p_z
->m_p_right
)
251 p_new_x_parent
= p_y
->m_p_parent
;
254 p_x
->m_p_parent
= p_y
->m_p_parent
;
256 p_y
->m_p_parent
->m_p_left
= p_x
;
258 p_y
->m_p_right
= p_z
->m_p_right
;
260 p_z
->m_p_right
->m_p_parent
= p_y
;
263 p_new_x_parent
= p_y
;
265 if (PB_ASSOC_BASE_C_DEC::m_p_head
->m_p_parent
== p_z
)
266 PB_ASSOC_BASE_C_DEC::m_p_head
->m_p_parent
= p_y
;
267 else if (p_z
->m_p_parent
->m_p_left
== p_z
)
268 p_z
->m_p_parent
->m_p_left
= p_y
;
270 p_z
->m_p_parent
->m_p_right
= p_y
;
272 p_y
->m_p_parent
= p_z
->m_p_parent
;
274 std::swap(p_y
->m_red
, p_z
->m_red
);
279 update_to_top(p_new_x_parent
, (Node_Updator
* )this);
284 remove_fixup(p_x
, p_new_x_parent
);
289 PB_ASSOC_CLASS_C_DEC::
290 remove_fixup(node_pointer p_x
, node_pointer p_new_x_parent
)
292 PB_ASSOC_DBG_ASSERT(p_x
== NULL
|| p_x
->m_p_parent
== p_new_x_parent
);
294 while (p_x
!= PB_ASSOC_BASE_C_DEC::m_p_head
->m_p_parent
&&
295 is_effectively_black(p_x
))
296 if (p_x
== p_new_x_parent
->m_p_left
)
298 node_pointer p_w
= p_new_x_parent
->m_p_right
;
304 p_new_x_parent
->m_red
= true;
306 PB_ASSOC_BASE_C_DEC::rotate_left(p_new_x_parent
);
308 p_w
= p_new_x_parent
->m_p_right
;
311 if (is_effectively_black(p_w
->m_p_left
)&&
312 is_effectively_black(p_w
->m_p_right
))
316 p_x
= p_new_x_parent
;
318 p_new_x_parent
= p_new_x_parent
->m_p_parent
;
322 if (is_effectively_black(p_w
->m_p_right
))
324 if (p_w
->m_p_left
!= NULL
)
325 p_w
->m_p_left
->m_red
= false;
329 PB_ASSOC_BASE_C_DEC::rotate_right(p_w
);
331 p_w
= p_new_x_parent
->m_p_right
;
334 p_w
->m_red
= p_new_x_parent
->m_red
;
336 p_new_x_parent
->m_red
= false;
338 if (p_w
->m_p_right
!= NULL
)
339 p_w
->m_p_right
->m_red
= false;
341 PB_ASSOC_BASE_C_DEC::rotate_left(p_new_x_parent
);
343 update_to_top(p_new_x_parent
, (Node_Updator
* )this);
350 node_pointer p_w
= p_new_x_parent
->m_p_left
;
352 if (p_w
->m_red
== true)
356 p_new_x_parent
->m_red
= true;
358 PB_ASSOC_BASE_C_DEC::rotate_right(p_new_x_parent
);
360 p_w
= p_new_x_parent
->m_p_left
;
363 if (is_effectively_black(p_w
->m_p_right
)&&
364 is_effectively_black(p_w
->m_p_left
))
368 p_x
= p_new_x_parent
;
370 p_new_x_parent
= p_new_x_parent
->m_p_parent
;
374 if (is_effectively_black(p_w
->m_p_left
))
376 if (p_w
->m_p_right
!= NULL
)
377 p_w
->m_p_right
->m_red
= false;
381 PB_ASSOC_BASE_C_DEC::rotate_left(p_w
);
383 p_w
= p_new_x_parent
->m_p_left
;
386 p_w
->m_red
= p_new_x_parent
->m_red
;
388 p_new_x_parent
->m_red
= false;
390 if (p_w
->m_p_left
!= NULL
)
391 p_w
->m_p_left
->m_red
= false;
393 PB_ASSOC_BASE_C_DEC::rotate_right(p_new_x_parent
);
395 update_to_top(p_new_x_parent
, (Node_Updator
* )this);