]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/rb_tree_map_/erase_fn_imps.hpp
c++config (std::size_t, [...]): Provide typedefs.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / rb_tree_map_ / erase_fn_imps.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
748086b7 3// Copyright (C) 2005, 2006, 2009 Free Software Foundation, Inc.
4569a895
AT
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
748086b7 8// Foundation; either version 3, or (at your option) any later
4569a895
AT
9// version.
10
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.
15
748086b7
JJ
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.
19
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/>.
4569a895
AT
24
25// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26
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.
35
36/**
37 * @file erase_fn_imps.hpp
38 * Contains an implementation for rb_tree_.
39 */
40
41PB_DS_CLASS_T_DEC
42inline bool
43PB_DS_CLASS_C_DEC::
44erase(const_key_reference r_key)
45{
46 point_iterator it = find(r_key);
1b24692f
BK
47 if (it == base_type::end())
48 return false;
4569a895 49 erase(it);
1b24692f 50 return true;
4569a895
AT
51}
52
53PB_DS_CLASS_T_DEC
54inline typename PB_DS_CLASS_C_DEC::iterator
55PB_DS_CLASS_C_DEC::
56erase(iterator it)
57{
47bea7b8 58 _GLIBCXX_DEBUG_ONLY(assert_valid());
1b24692f
BK
59 if (it == base_type::end())
60 return it;
4569a895
AT
61
62 iterator ret_it = it;
4569a895 63 ++ret_it;
4569a895 64 erase_node(it.m_p_nd);
47bea7b8 65 _GLIBCXX_DEBUG_ONLY(assert_valid());
1b24692f 66 return ret_it;
4569a895
AT
67}
68
69PB_DS_CLASS_T_DEC
70inline typename PB_DS_CLASS_C_DEC::reverse_iterator
71PB_DS_CLASS_C_DEC::
72erase(reverse_iterator it)
73{
47bea7b8 74 _GLIBCXX_DEBUG_ONLY(assert_valid());
1b24692f
BK
75 if (it.m_p_nd == base_type::m_p_head)
76 return it;
4569a895
AT
77
78 reverse_iterator ret_it = it;
4569a895 79 ++ret_it;
4569a895 80 erase_node(it.m_p_nd);
47bea7b8 81 _GLIBCXX_DEBUG_ONLY(assert_valid());
1b24692f 82 return ret_it;
4569a895
AT
83}
84
85PB_DS_CLASS_T_DEC
86template<typename Pred>
87inline typename PB_DS_CLASS_C_DEC::size_type
88PB_DS_CLASS_C_DEC::
89erase_if(Pred pred)
90{
47bea7b8 91 _GLIBCXX_DEBUG_ONLY(assert_valid();)
1b24692f
BK
92 size_type num_ersd = 0;
93 iterator it = base_type::begin();
94 while (it != base_type::end())
4569a895
AT
95 {
96 if (pred(*it))
97 {
98 ++num_ersd;
4569a895
AT
99 it = erase(it);
100 }
101 else
102 ++it;
103 }
104
47bea7b8 105 _GLIBCXX_DEBUG_ONLY(assert_valid();)
1b24692f 106 return num_ersd;
4569a895
AT
107}
108
109PB_DS_CLASS_T_DEC
110void
111PB_DS_CLASS_C_DEC::
112erase_node(node_pointer p_nd)
113{
114 remove_node(p_nd);
1b24692f 115 base_type::actual_erase_node(p_nd);
47bea7b8 116 _GLIBCXX_DEBUG_ONLY(assert_valid());
4569a895
AT
117}
118
119PB_DS_CLASS_T_DEC
120void
121PB_DS_CLASS_C_DEC::
122remove_node(node_pointer p_z)
123{
124 update_min_max_for_erased_node(p_z);
4569a895 125 node_pointer p_y = p_z;
8fc81078
PC
126 node_pointer p_x = 0;
127 node_pointer p_new_x_parent = 0;
4569a895 128
8fc81078 129 if (p_y->m_p_left == 0)
4569a895 130 p_x = p_y->m_p_right;
8fc81078 131 else if (p_y->m_p_right == 0)
4569a895
AT
132 p_x = p_y->m_p_left;
133 else
134 {
135 p_y = p_y->m_p_right;
8fc81078 136 while (p_y->m_p_left != 0)
4569a895 137 p_y = p_y->m_p_left;
4569a895
AT
138 p_x = p_y->m_p_right;
139 }
140
141 if (p_y == p_z)
142 {
143 p_new_x_parent = p_y->m_p_parent;
8fc81078 144 if (p_x != 0)
4569a895
AT
145 p_x->m_p_parent = p_y->m_p_parent;
146
1b24692f
BK
147 if (base_type::m_p_head->m_p_parent == p_z)
148 base_type::m_p_head->m_p_parent = p_x;
4569a895
AT
149 else if (p_z->m_p_parent->m_p_left == p_z)
150 {
151 p_y->m_p_left = p_z->m_p_parent;
4569a895
AT
152 p_z->m_p_parent->m_p_left = p_x;
153 }
154 else
155 {
8fc81078 156 p_y->m_p_left = 0;
4569a895
AT
157 p_z->m_p_parent->m_p_right = p_x;
158 }
159 }
160 else
161 {
162 p_z->m_p_left->m_p_parent = p_y;
4569a895 163 p_y->m_p_left = p_z->m_p_left;
4569a895
AT
164 if (p_y != p_z->m_p_right)
165 {
166 p_new_x_parent = p_y->m_p_parent;
8fc81078 167 if (p_x != 0)
4569a895 168 p_x->m_p_parent = p_y->m_p_parent;
4569a895 169 p_y->m_p_parent->m_p_left = p_x;
4569a895 170 p_y->m_p_right = p_z->m_p_right;
4569a895
AT
171 p_z->m_p_right->m_p_parent = p_y;
172 }
173 else
174 p_new_x_parent = p_y;
175
1b24692f
BK
176 if (base_type::m_p_head->m_p_parent == p_z)
177 base_type::m_p_head->m_p_parent = p_y;
4569a895
AT
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;
182
183 p_y->m_p_parent = p_z->m_p_parent;
4569a895 184 std::swap(p_y->m_red, p_z->m_red);
4569a895
AT
185 p_y = p_z;
186 }
187
188 update_to_top(p_new_x_parent, (node_update* )this);
189
190 if (p_y->m_red)
191 return;
192
193 remove_fixup(p_x, p_new_x_parent);
194}
195
196PB_DS_CLASS_T_DEC
197void
198PB_DS_CLASS_C_DEC::
199remove_fixup(node_pointer p_x, node_pointer p_new_x_parent)
200{
8fc81078 201 _GLIBCXX_DEBUG_ASSERT(p_x == 0 || p_x->m_p_parent == p_new_x_parent);
4569a895 202
1b24692f 203 while (p_x != base_type::m_p_head->m_p_parent && is_effectively_black(p_x))
4569a895
AT
204 if (p_x == p_new_x_parent->m_p_left)
205 {
206 node_pointer p_w = p_new_x_parent->m_p_right;
4569a895
AT
207 if (p_w->m_red)
208 {
209 p_w->m_red = false;
4569a895 210 p_new_x_parent->m_red = true;
1b24692f 211 base_type::rotate_left(p_new_x_parent);
4569a895
AT
212 p_w = p_new_x_parent->m_p_right;
213 }
214
1b24692f
BK
215 if (is_effectively_black(p_w->m_p_left)
216 && is_effectively_black(p_w->m_p_right))
4569a895
AT
217 {
218 p_w->m_red = true;
4569a895 219 p_x = p_new_x_parent;
4569a895
AT
220 p_new_x_parent = p_new_x_parent->m_p_parent;
221 }
222 else
223 {
224 if (is_effectively_black(p_w->m_p_right))
225 {
8fc81078 226 if (p_w->m_p_left != 0)
4569a895
AT
227 p_w->m_p_left->m_red = false;
228
229 p_w->m_red = true;
1b24692f 230 base_type::rotate_right(p_w);
4569a895
AT
231 p_w = p_new_x_parent->m_p_right;
232 }
233
234 p_w->m_red = p_new_x_parent->m_red;
4569a895
AT
235 p_new_x_parent->m_red = false;
236
8fc81078 237 if (p_w->m_p_right != 0)
4569a895
AT
238 p_w->m_p_right->m_red = false;
239
1b24692f 240 base_type::rotate_left(p_new_x_parent);
4569a895 241 update_to_top(p_new_x_parent, (node_update* )this);
4569a895
AT
242 break;
243 }
244 }
245 else
246 {
247 node_pointer p_w = p_new_x_parent->m_p_left;
4569a895
AT
248 if (p_w->m_red == true)
249 {
250 p_w->m_red = false;
4569a895 251 p_new_x_parent->m_red = true;
1b24692f 252 base_type::rotate_right(p_new_x_parent);
4569a895
AT
253 p_w = p_new_x_parent->m_p_left;
254 }
255
1b24692f
BK
256 if (is_effectively_black(p_w->m_p_right)
257 && is_effectively_black(p_w->m_p_left))
4569a895
AT
258 {
259 p_w->m_red = true;
4569a895 260 p_x = p_new_x_parent;
4569a895
AT
261 p_new_x_parent = p_new_x_parent->m_p_parent;
262 }
263 else
264 {
265 if (is_effectively_black(p_w->m_p_left))
266 {
8fc81078 267 if (p_w->m_p_right != 0)
4569a895
AT
268 p_w->m_p_right->m_red = false;
269
270 p_w->m_red = true;
1b24692f 271 base_type::rotate_left(p_w);
4569a895
AT
272 p_w = p_new_x_parent->m_p_left;
273 }
274
275 p_w->m_red = p_new_x_parent->m_red;
4569a895
AT
276 p_new_x_parent->m_red = false;
277
8fc81078 278 if (p_w->m_p_left != 0)
4569a895
AT
279 p_w->m_p_left->m_red = false;
280
1b24692f 281 base_type::rotate_right(p_new_x_parent);
4569a895 282 update_to_top(p_new_x_parent, (node_update* )this);
4569a895
AT
283 break;
284 }
285 }
286
8fc81078 287 if (p_x != 0)
4569a895
AT
288 p_x->m_red = false;
289}