Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libstdc++-v3 / include / ext / pb_assoc / detail / rb_tree_map_ / erase_fn_imps.hpp
blobe137fb7cad2089eb735fecb018a085d762191704
1 // -*- C++ -*-
3 // Copyright (C) 2005 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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.
40 /**
41 * @file erase_fn_imps.hpp
42 * Contains an implementation for rb_tree_.
45 PB_ASSOC_CLASS_T_DEC
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())
53 return (0);
55 erase(it);
57 return (1);
60 PB_ASSOC_CLASS_T_DEC
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())
69 return (it);
71 const_iterator ret_it = it;
73 ++ret_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);)
80 return (ret_it);
83 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
84 PB_ASSOC_CLASS_T_DEC
85 inline typename PB_ASSOC_CLASS_C_DEC::iterator
86 PB_ASSOC_CLASS_C_DEC::
87 erase(iterator it)
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())
93 return (it);
95 iterator ret_it = it;
97 ++ret_it;
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();)
104 return (ret_it);
106 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
108 PB_ASSOC_CLASS_T_DEC
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())
117 return (it);
119 const_reverse_iterator ret_it = it;
121 ++ret_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();)
128 return (ret_it);
131 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
132 PB_ASSOC_CLASS_T_DEC
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())
140 return (it);
142 reverse_iterator ret_it = it;
144 ++ret_it;
146 erase_node(it.m_p_nd);
148 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid());
150 return (ret_it);
152 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
154 PB_ASSOC_CLASS_T_DEC
155 template<class Pred>
156 inline typename PB_ASSOC_CLASS_C_DEC::size_type
157 PB_ASSOC_CLASS_C_DEC::
158 erase_if(Pred pred)
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())
167 if (pred(*it))
169 ++num_ersd;
171 it = erase(it);
173 else
174 ++it;
176 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
178 return (num_ersd);
181 PB_ASSOC_CLASS_T_DEC
182 void
183 PB_ASSOC_CLASS_C_DEC::
184 erase_node(node_pointer p_nd)
186 remove_node(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);)
194 PB_ASSOC_CLASS_T_DEC
195 void
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)
210 p_x = p_y->m_p_left;
211 else
213 p_y = p_y->m_p_right;
215 while (p_y->m_p_left != NULL)
216 p_y = p_y->m_p_left;
218 p_x = p_y->m_p_right;
221 if (p_y == p_z)
223 p_new_x_parent = p_y->m_p_parent;
225 if (p_x != NULL)
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;
236 else
238 p_y->m_p_left = NULL;
240 p_z->m_p_parent->m_p_right = p_x;
243 else
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;
253 if (p_x != NULL)
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;
262 else
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;
269 else
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);
276 p_y = p_z;
279 update_to_top(p_new_x_parent, (Node_Updator* )this);
281 if (p_y->m_red)
282 return;
284 remove_fixup(p_x, p_new_x_parent);
287 PB_ASSOC_CLASS_T_DEC
288 void
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;
300 if (p_w->m_red)
302 p_w->m_red = false;
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))
314 p_w->m_red = true;
316 p_x = p_new_x_parent;
318 p_new_x_parent = p_new_x_parent->m_p_parent;
320 else
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;
327 p_w->m_red = true;
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);
345 break;
348 else
350 node_pointer p_w = p_new_x_parent->m_p_left;
352 if (p_w->m_red == true)
354 p_w->m_red = false;
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))
366 p_w->m_red = true;
368 p_x = p_new_x_parent;
370 p_new_x_parent = p_new_x_parent->m_p_parent;
372 else
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;
379 p_w->m_red = true;
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);
397 break;
401 if (p_x != NULL)
402 p_x->m_red = false;