]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/rb_tree_map_/split_join_fn_imps.hpp
* semantics.c (finish_call_expr): SET_EXPR_LOCATION.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / rb_tree_map_ / split_join_fn_imps.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
f5886803 3// Copyright (C) 2005, 2006, 2009, 2010, 2011 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.
4569a895 19
748086b7
JJ
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 split_join_fn_imps.hpp
38 * Contains an implementation for rb_tree_.
39 */
40
41PB_DS_CLASS_T_DEC
42inline void
43PB_DS_CLASS_C_DEC::
44join(PB_DS_CLASS_C_DEC& other)
45{
f5886803
FD
46 PB_DS_ASSERT_VALID((*this))
47 PB_DS_ASSERT_VALID(other)
1b24692f 48 if (base_type::join_prep(other) == false)
47bea7b8 49 {
f5886803
FD
50 PB_DS_ASSERT_VALID((*this))
51 PB_DS_ASSERT_VALID(other)
47bea7b8
BK
52 return;
53 }
4569a895
AT
54
55 const node_pointer p_x = other.split_min();
4569a895 56 join_imp(p_x, other.m_p_head->m_p_parent);
1b24692f 57 base_type::join_finish(other);
f5886803
FD
58 PB_DS_ASSERT_VALID((*this))
59 PB_DS_ASSERT_VALID(other)
47bea7b8 60 }
4569a895
AT
61
62PB_DS_CLASS_T_DEC
63void
64PB_DS_CLASS_C_DEC::
65join_imp(node_pointer p_x, node_pointer p_r)
66{
8fc81078
PC
67 _GLIBCXX_DEBUG_ASSERT(p_x != 0);
68 if (p_r != 0)
4569a895
AT
69 p_r->m_red = false;
70
1b24692f 71 const size_type h = black_height(base_type::m_p_head->m_p_parent);
4569a895 72 const size_type other_h = black_height(p_r);
4569a895
AT
73 node_pointer p_x_l;
74 node_pointer p_x_r;
4569a895 75 std::pair<node_pointer, node_pointer> join_pos;
4569a895 76 const bool right_join = h >= other_h;
4569a895
AT
77 if (right_join)
78 {
1b24692f 79 join_pos = find_join_pos_right(base_type::m_p_head->m_p_parent,
47bea7b8 80 h, other_h);
4569a895
AT
81 p_x_l = join_pos.first;
82 p_x_r = p_r;
83 }
84 else
85 {
1b24692f
BK
86 p_x_l = base_type::m_p_head->m_p_parent;
87 base_type::m_p_head->m_p_parent = p_r;
8fc81078 88 if (p_r != 0)
1b24692f 89 p_r->m_p_parent = base_type::m_p_head;
4569a895 90
1b24692f 91 join_pos = find_join_pos_left(base_type::m_p_head->m_p_parent,
47bea7b8 92 h, other_h);
4569a895
AT
93 p_x_r = join_pos.first;
94 }
95
96 node_pointer p_parent = join_pos.second;
1b24692f 97 if (p_parent == base_type::m_p_head)
4569a895 98 {
1b24692f
BK
99 base_type::m_p_head->m_p_parent = p_x;
100 p_x->m_p_parent = base_type::m_p_head;
4569a895
AT
101 }
102 else
103 {
104 p_x->m_p_parent = p_parent;
4569a895
AT
105 if (right_join)
106 p_x->m_p_parent->m_p_right = p_x;
107 else
108 p_x->m_p_parent->m_p_left = p_x;
109 }
110
111 p_x->m_p_left = p_x_l;
8fc81078 112 if (p_x_l != 0)
4569a895
AT
113 p_x_l->m_p_parent = p_x;
114
115 p_x->m_p_right = p_x_r;
8fc81078 116 if (p_x_r != 0)
4569a895
AT
117 p_x_r->m_p_parent = p_x;
118
119 p_x->m_red = true;
120
1b24692f 121 base_type::initialize_min_max();
f5886803 122 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
1b24692f 123 base_type::update_to_top(p_x, (node_update* )this);
4569a895 124 insert_fixup(p_x);
f5886803 125 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
4569a895
AT
126}
127
128PB_DS_CLASS_T_DEC
129inline typename PB_DS_CLASS_C_DEC::node_pointer
130PB_DS_CLASS_C_DEC::
131split_min()
132{
1b24692f 133 node_pointer p_min = base_type::m_p_head->m_p_left;
4569a895 134
47bea7b8 135#ifdef _GLIBCXX_DEBUG
1b24692f 136 const node_pointer p_head = base_type::m_p_head;
47bea7b8
BK
137 _GLIBCXX_DEBUG_ASSERT(p_min != p_head);
138#endif
4569a895
AT
139
140 remove_node(p_min);
1b24692f 141 return p_min;
4569a895
AT
142}
143
144PB_DS_CLASS_T_DEC
145std::pair<
146 typename PB_DS_CLASS_C_DEC::node_pointer,
147 typename PB_DS_CLASS_C_DEC::node_pointer>
148PB_DS_CLASS_C_DEC::
149find_join_pos_right(node_pointer p_l, size_type h_l, size_type h_r)
150{
47bea7b8 151 _GLIBCXX_DEBUG_ASSERT(h_l >= h_r);
4569a895 152
8fc81078
PC
153 if (base_type::m_p_head->m_p_parent == 0)
154 return (std::make_pair((node_pointer)0, base_type::m_p_head));
4569a895 155
1b24692f 156 node_pointer p_l_parent = base_type::m_p_head;
4569a895
AT
157 while (h_l > h_r)
158 {
159 if (p_l->m_red == false)
160 {
47bea7b8 161 _GLIBCXX_DEBUG_ASSERT(h_l > 0);
4569a895
AT
162 --h_l;
163 }
164
165 p_l_parent = p_l;
4569a895
AT
166 p_l = p_l->m_p_right;
167 }
168
169 if (!is_effectively_black(p_l))
170 {
171 p_l_parent = p_l;
4569a895
AT
172 p_l = p_l->m_p_right;
173 }
174
47bea7b8
BK
175 _GLIBCXX_DEBUG_ASSERT(is_effectively_black(p_l));
176 _GLIBCXX_DEBUG_ASSERT(black_height(p_l) == h_r);
8fc81078 177 _GLIBCXX_DEBUG_ASSERT(p_l == 0 || p_l->m_p_parent == p_l_parent);
47bea7b8 178 return std::make_pair(p_l, p_l_parent);
4569a895
AT
179}
180
181PB_DS_CLASS_T_DEC
182std::pair<
183 typename PB_DS_CLASS_C_DEC::node_pointer,
184 typename PB_DS_CLASS_C_DEC::node_pointer>
185PB_DS_CLASS_C_DEC::
186find_join_pos_left(node_pointer p_r, size_type h_l, size_type h_r)
187{
47bea7b8 188 _GLIBCXX_DEBUG_ASSERT(h_r > h_l);
8fc81078
PC
189 if (base_type::m_p_head->m_p_parent == 0)
190 return (std::make_pair((node_pointer)0,
1b24692f
BK
191 base_type::m_p_head));
192 node_pointer p_r_parent = base_type::m_p_head;
4569a895
AT
193 while (h_r > h_l)
194 {
195 if (p_r->m_red == false)
196 {
47bea7b8 197 _GLIBCXX_DEBUG_ASSERT(h_r > 0);
4569a895
AT
198 --h_r;
199 }
200
201 p_r_parent = p_r;
4569a895
AT
202 p_r = p_r->m_p_left;
203 }
204
205 if (!is_effectively_black(p_r))
206 {
207 p_r_parent = p_r;
4569a895
AT
208 p_r = p_r->m_p_left;
209 }
210
47bea7b8
BK
211 _GLIBCXX_DEBUG_ASSERT(is_effectively_black(p_r));
212 _GLIBCXX_DEBUG_ASSERT(black_height(p_r) == h_l);
8fc81078 213 _GLIBCXX_DEBUG_ASSERT(p_r == 0 || p_r->m_p_parent == p_r_parent);
47bea7b8 214 return std::make_pair(p_r, p_r_parent);
4569a895
AT
215}
216
217PB_DS_CLASS_T_DEC
218inline typename PB_DS_CLASS_C_DEC::size_type
219PB_DS_CLASS_C_DEC::
220black_height(node_pointer p_nd)
221{
222 size_type h = 1;
8fc81078 223 while (p_nd != 0)
4569a895
AT
224 {
225 if (p_nd->m_red == false)
226 ++h;
4569a895
AT
227 p_nd = p_nd->m_p_left;
228 }
47bea7b8 229 return h;
4569a895
AT
230}
231
232PB_DS_CLASS_T_DEC
233void
234PB_DS_CLASS_C_DEC::
235split(const_key_reference r_key, PB_DS_CLASS_C_DEC& other)
236{
f5886803
FD
237 PB_DS_ASSERT_VALID((*this))
238 PB_DS_ASSERT_VALID(other)
4569a895 239
f5886803
FD
240 if (base_type::split_prep(r_key, other) == false)
241 {
242 PB_DS_ASSERT_VALID((*this))
243 PB_DS_ASSERT_VALID(other)
244 return;
245 }
4569a895 246
f5886803
FD
247 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
248 PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
47bea7b8 249 node_pointer p_nd = upper_bound(r_key).m_p_nd;
4569a895
AT
250 do
251 {
252 node_pointer p_next_nd = p_nd->m_p_parent;
47bea7b8 253 if (Cmp_Fn::operator()(r_key, PB_DS_V2F(p_nd->m_value)))
4569a895
AT
254 split_at_node(p_nd, other);
255
f5886803
FD
256 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
257 PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
47bea7b8 258 p_nd = p_next_nd;
4569a895 259 }
1b24692f 260 while (p_nd != base_type::m_p_head);
4569a895 261
1b24692f 262 base_type::split_finish(other);
f5886803 263 PB_DS_ASSERT_VALID((*this))
47bea7b8 264}
4569a895
AT
265
266PB_DS_CLASS_T_DEC
267void
268PB_DS_CLASS_C_DEC::
269split_at_node(node_pointer p_nd, PB_DS_CLASS_C_DEC& other)
270{
8fc81078 271 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
4569a895
AT
272
273 node_pointer p_l = p_nd->m_p_left;
274 node_pointer p_r = p_nd->m_p_right;
4569a895 275 node_pointer p_parent = p_nd->m_p_parent;
1b24692f 276 if (p_parent == base_type::m_p_head)
4569a895 277 {
1b24692f 278 base_type::m_p_head->m_p_parent = p_l;
8fc81078 279 if (p_l != 0)
4569a895 280 {
1b24692f 281 p_l->m_p_parent = base_type::m_p_head;
4569a895
AT
282 p_l->m_red = false;
283 }
284 }
285 else
286 {
287 if (p_parent->m_p_left == p_nd)
288 p_parent->m_p_left = p_l;
289 else
290 p_parent->m_p_right = p_l;
291
8fc81078 292 if (p_l != 0)
4569a895
AT
293 p_l->m_p_parent = p_parent;
294
295 update_to_top(p_parent, (node_update* )this);
296
297 if (!p_nd->m_red)
298 remove_fixup(p_l, p_parent);
299 }
300
1b24692f 301 base_type::initialize_min_max();
4569a895 302 other.join_imp(p_nd, p_r);
f5886803
FD
303 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
304 PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
4569a895
AT
305}
306