3 // Copyright (C) 2005, 2006, 2009 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 erase_fn_imps.hpp
38 * Contains an implementation for rb_tree_.
44 erase(const_key_reference r_key
)
46 point_iterator it
= find(r_key
);
47 if (it
== base_type::end())
54 inline typename
PB_DS_CLASS_C_DEC::iterator
58 _GLIBCXX_DEBUG_ONLY(assert_valid());
59 if (it
== base_type::end())
64 erase_node(it
.m_p_nd
);
65 _GLIBCXX_DEBUG_ONLY(assert_valid());
70 inline typename
PB_DS_CLASS_C_DEC::reverse_iterator
72 erase(reverse_iterator it
)
74 _GLIBCXX_DEBUG_ONLY(assert_valid());
75 if (it
.m_p_nd
== base_type::m_p_head
)
78 reverse_iterator ret_it
= it
;
80 erase_node(it
.m_p_nd
);
81 _GLIBCXX_DEBUG_ONLY(assert_valid());
86 template<typename Pred
>
87 inline typename
PB_DS_CLASS_C_DEC::size_type
91 _GLIBCXX_DEBUG_ONLY(assert_valid();)
92 size_type num_ersd
= 0;
93 iterator it
= base_type::begin();
94 while (it
!= base_type::end())
105 _GLIBCXX_DEBUG_ONLY(assert_valid();)
112 erase_node(node_pointer p_nd
)
115 base_type::actual_erase_node(p_nd
);
116 _GLIBCXX_DEBUG_ONLY(assert_valid());
122 remove_node(node_pointer p_z
)
124 update_min_max_for_erased_node(p_z
);
125 node_pointer p_y
= p_z
;
126 node_pointer p_x
= NULL
;
127 node_pointer p_new_x_parent
= NULL
;
129 if (p_y
->m_p_left
== NULL
)
130 p_x
= p_y
->m_p_right
;
131 else if (p_y
->m_p_right
== NULL
)
135 p_y
= p_y
->m_p_right
;
136 while (p_y
->m_p_left
!= NULL
)
138 p_x
= p_y
->m_p_right
;
143 p_new_x_parent
= p_y
->m_p_parent
;
145 p_x
->m_p_parent
= p_y
->m_p_parent
;
147 if (base_type::m_p_head
->m_p_parent
== p_z
)
148 base_type::m_p_head
->m_p_parent
= p_x
;
149 else if (p_z
->m_p_parent
->m_p_left
== p_z
)
151 p_y
->m_p_left
= p_z
->m_p_parent
;
152 p_z
->m_p_parent
->m_p_left
= p_x
;
156 p_y
->m_p_left
= NULL
;
157 p_z
->m_p_parent
->m_p_right
= p_x
;
162 p_z
->m_p_left
->m_p_parent
= p_y
;
163 p_y
->m_p_left
= p_z
->m_p_left
;
164 if (p_y
!= p_z
->m_p_right
)
166 p_new_x_parent
= p_y
->m_p_parent
;
168 p_x
->m_p_parent
= p_y
->m_p_parent
;
169 p_y
->m_p_parent
->m_p_left
= p_x
;
170 p_y
->m_p_right
= p_z
->m_p_right
;
171 p_z
->m_p_right
->m_p_parent
= p_y
;
174 p_new_x_parent
= p_y
;
176 if (base_type::m_p_head
->m_p_parent
== p_z
)
177 base_type::m_p_head
->m_p_parent
= p_y
;
178 else if (p_z
->m_p_parent
->m_p_left
== p_z
)
179 p_z
->m_p_parent
->m_p_left
= p_y
;
181 p_z
->m_p_parent
->m_p_right
= p_y
;
183 p_y
->m_p_parent
= p_z
->m_p_parent
;
184 std::swap(p_y
->m_red
, p_z
->m_red
);
188 update_to_top(p_new_x_parent
, (node_update
* )this);
193 remove_fixup(p_x
, p_new_x_parent
);
199 remove_fixup(node_pointer p_x
, node_pointer p_new_x_parent
)
201 _GLIBCXX_DEBUG_ASSERT(p_x
== NULL
|| p_x
->m_p_parent
== p_new_x_parent
);
203 while (p_x
!= base_type::m_p_head
->m_p_parent
&& is_effectively_black(p_x
))
204 if (p_x
== p_new_x_parent
->m_p_left
)
206 node_pointer p_w
= p_new_x_parent
->m_p_right
;
210 p_new_x_parent
->m_red
= true;
211 base_type::rotate_left(p_new_x_parent
);
212 p_w
= p_new_x_parent
->m_p_right
;
215 if (is_effectively_black(p_w
->m_p_left
)
216 && is_effectively_black(p_w
->m_p_right
))
219 p_x
= p_new_x_parent
;
220 p_new_x_parent
= p_new_x_parent
->m_p_parent
;
224 if (is_effectively_black(p_w
->m_p_right
))
226 if (p_w
->m_p_left
!= NULL
)
227 p_w
->m_p_left
->m_red
= false;
230 base_type::rotate_right(p_w
);
231 p_w
= p_new_x_parent
->m_p_right
;
234 p_w
->m_red
= p_new_x_parent
->m_red
;
235 p_new_x_parent
->m_red
= false;
237 if (p_w
->m_p_right
!= NULL
)
238 p_w
->m_p_right
->m_red
= false;
240 base_type::rotate_left(p_new_x_parent
);
241 update_to_top(p_new_x_parent
, (node_update
* )this);
247 node_pointer p_w
= p_new_x_parent
->m_p_left
;
248 if (p_w
->m_red
== true)
251 p_new_x_parent
->m_red
= true;
252 base_type::rotate_right(p_new_x_parent
);
253 p_w
= p_new_x_parent
->m_p_left
;
256 if (is_effectively_black(p_w
->m_p_right
)
257 && is_effectively_black(p_w
->m_p_left
))
260 p_x
= p_new_x_parent
;
261 p_new_x_parent
= p_new_x_parent
->m_p_parent
;
265 if (is_effectively_black(p_w
->m_p_left
))
267 if (p_w
->m_p_right
!= NULL
)
268 p_w
->m_p_right
->m_red
= false;
271 base_type::rotate_left(p_w
);
272 p_w
= p_new_x_parent
->m_p_left
;
275 p_w
->m_red
= p_new_x_parent
->m_red
;
276 p_new_x_parent
->m_red
= false;
278 if (p_w
->m_p_left
!= NULL
)
279 p_w
->m_p_left
->m_red
= false;
281 base_type::rotate_right(p_new_x_parent
);
282 update_to_top(p_new_x_parent
, (node_update
* )this);