Install gcc-4.4.0-tdm-1-g++-2.tar.gz
[git/jnareb-git.git] / mingw / lib / gcc / mingw32 / 4.4.0 / include / c++ / ext / pb_ds / detail / rb_tree_map_ / erase_fn_imps.hpp
blob685b8a51b6baa869d3a29b2fa5d12fda1abd3a5e
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2009 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 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
9 // version.
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
34 // warranty.
36 /**
37 * @file erase_fn_imps.hpp
38 * Contains an implementation for rb_tree_.
41 PB_DS_CLASS_T_DEC
42 inline bool
43 PB_DS_CLASS_C_DEC::
44 erase(const_key_reference r_key)
46 point_iterator it = find(r_key);
47 if (it == base_type::end())
48 return false;
49 erase(it);
50 return true;
53 PB_DS_CLASS_T_DEC
54 inline typename PB_DS_CLASS_C_DEC::iterator
55 PB_DS_CLASS_C_DEC::
56 erase(iterator it)
58 _GLIBCXX_DEBUG_ONLY(assert_valid());
59 if (it == base_type::end())
60 return it;
62 iterator ret_it = it;
63 ++ret_it;
64 erase_node(it.m_p_nd);
65 _GLIBCXX_DEBUG_ONLY(assert_valid());
66 return ret_it;
69 PB_DS_CLASS_T_DEC
70 inline typename PB_DS_CLASS_C_DEC::reverse_iterator
71 PB_DS_CLASS_C_DEC::
72 erase(reverse_iterator it)
74 _GLIBCXX_DEBUG_ONLY(assert_valid());
75 if (it.m_p_nd == base_type::m_p_head)
76 return it;
78 reverse_iterator ret_it = it;
79 ++ret_it;
80 erase_node(it.m_p_nd);
81 _GLIBCXX_DEBUG_ONLY(assert_valid());
82 return ret_it;
85 PB_DS_CLASS_T_DEC
86 template<typename Pred>
87 inline typename PB_DS_CLASS_C_DEC::size_type
88 PB_DS_CLASS_C_DEC::
89 erase_if(Pred pred)
91 _GLIBCXX_DEBUG_ONLY(assert_valid();)
92 size_type num_ersd = 0;
93 iterator it = base_type::begin();
94 while (it != base_type::end())
96 if (pred(*it))
98 ++num_ersd;
99 it = erase(it);
101 else
102 ++it;
105 _GLIBCXX_DEBUG_ONLY(assert_valid();)
106 return num_ersd;
109 PB_DS_CLASS_T_DEC
110 void
111 PB_DS_CLASS_C_DEC::
112 erase_node(node_pointer p_nd)
114 remove_node(p_nd);
115 base_type::actual_erase_node(p_nd);
116 _GLIBCXX_DEBUG_ONLY(assert_valid());
119 PB_DS_CLASS_T_DEC
120 void
121 PB_DS_CLASS_C_DEC::
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)
132 p_x = p_y->m_p_left;
133 else
135 p_y = p_y->m_p_right;
136 while (p_y->m_p_left != NULL)
137 p_y = p_y->m_p_left;
138 p_x = p_y->m_p_right;
141 if (p_y == p_z)
143 p_new_x_parent = p_y->m_p_parent;
144 if (p_x != NULL)
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;
154 else
156 p_y->m_p_left = NULL;
157 p_z->m_p_parent->m_p_right = p_x;
160 else
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;
167 if (p_x != NULL)
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;
173 else
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;
180 else
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);
185 p_y = p_z;
188 update_to_top(p_new_x_parent, (node_update* )this);
190 if (p_y->m_red)
191 return;
193 remove_fixup(p_x, p_new_x_parent);
196 PB_DS_CLASS_T_DEC
197 void
198 PB_DS_CLASS_C_DEC::
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;
207 if (p_w->m_red)
209 p_w->m_red = false;
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))
218 p_w->m_red = true;
219 p_x = p_new_x_parent;
220 p_new_x_parent = p_new_x_parent->m_p_parent;
222 else
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;
229 p_w->m_red = true;
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);
242 break;
245 else
247 node_pointer p_w = p_new_x_parent->m_p_left;
248 if (p_w->m_red == true)
250 p_w->m_red = false;
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))
259 p_w->m_red = true;
260 p_x = p_new_x_parent;
261 p_new_x_parent = p_new_x_parent->m_p_parent;
263 else
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;
270 p_w->m_red = true;
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);
283 break;
287 if (p_x != NULL)
288 p_x->m_red = false;