1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
// -*- C++ -*-
// Copyright (C) 2005-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software
// Foundation; either version 3, or (at your option) any later
// version.
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
// Permission to use, copy, modify, sell, and distribute this software
// is hereby granted without fee, provided that the above copyright
// notice appears in all copies, and that both that copyright notice
// and this permission notice appear in supporting documentation. None
// of the above authors, nor IBM Haifa Research Laboratories, make any
// representation about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.
/**
* @file rb_tree_map_/split_join_fn_imps.hpp
* Contains an implementation for rb_tree_.
*/
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
join(PB_DS_CLASS_C_DEC& other)
{
PB_DS_ASSERT_VALID((*this))
PB_DS_ASSERT_VALID(other)
if (base_type::join_prep(other) == false)
{
PB_DS_ASSERT_VALID((*this))
PB_DS_ASSERT_VALID(other)
return;
}
const node_pointer p_x = other.split_min();
join_imp(p_x, other.m_p_head->m_p_parent);
base_type::join_finish(other);
PB_DS_ASSERT_VALID((*this))
PB_DS_ASSERT_VALID(other)
}
PB_DS_CLASS_T_DEC
void
PB_DS_CLASS_C_DEC::
join_imp(node_pointer p_x, node_pointer p_r)
{
_GLIBCXX_DEBUG_ASSERT(p_x != 0);
if (p_r != 0)
p_r->m_red = false;
const size_type h = black_height(base_type::m_p_head->m_p_parent);
const size_type other_h = black_height(p_r);
node_pointer p_x_l;
node_pointer p_x_r;
std::pair<node_pointer, node_pointer> join_pos;
const bool right_join = h >= other_h;
if (right_join)
{
join_pos = find_join_pos_right(base_type::m_p_head->m_p_parent,
h, other_h);
p_x_l = join_pos.first;
p_x_r = p_r;
}
else
{
p_x_l = base_type::m_p_head->m_p_parent;
base_type::m_p_head->m_p_parent = p_r;
if (p_r != 0)
p_r->m_p_parent = base_type::m_p_head;
join_pos = find_join_pos_left(base_type::m_p_head->m_p_parent,
h, other_h);
p_x_r = join_pos.first;
}
node_pointer p_parent = join_pos.second;
if (p_parent == base_type::m_p_head)
{
base_type::m_p_head->m_p_parent = p_x;
p_x->m_p_parent = base_type::m_p_head;
}
else
{
p_x->m_p_parent = p_parent;
if (right_join)
p_x->m_p_parent->m_p_right = p_x;
else
p_x->m_p_parent->m_p_left = p_x;
}
p_x->m_p_left = p_x_l;
if (p_x_l != 0)
p_x_l->m_p_parent = p_x;
p_x->m_p_right = p_x_r;
if (p_x_r != 0)
p_x_r->m_p_parent = p_x;
p_x->m_red = true;
base_type::initialize_min_max();
PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
base_type::update_to_top(p_x, (node_update* )this);
insert_fixup(p_x);
PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
}
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::node_pointer
PB_DS_CLASS_C_DEC::
split_min()
{
node_pointer p_min = base_type::m_p_head->m_p_left;
#ifdef _GLIBCXX_DEBUG
const node_pointer p_head = base_type::m_p_head;
_GLIBCXX_DEBUG_ASSERT(p_min != p_head);
#endif
remove_node(p_min);
return p_min;
}
PB_DS_CLASS_T_DEC
std::pair<
typename PB_DS_CLASS_C_DEC::node_pointer,
typename PB_DS_CLASS_C_DEC::node_pointer>
PB_DS_CLASS_C_DEC::
find_join_pos_right(node_pointer p_l, size_type h_l, size_type h_r)
{
_GLIBCXX_DEBUG_ASSERT(h_l >= h_r);
if (base_type::m_p_head->m_p_parent == 0)
return (std::make_pair((node_pointer)0, base_type::m_p_head));
node_pointer p_l_parent = base_type::m_p_head;
while (h_l > h_r)
{
if (p_l->m_red == false)
{
_GLIBCXX_DEBUG_ASSERT(h_l > 0);
--h_l;
}
p_l_parent = p_l;
p_l = p_l->m_p_right;
}
if (!is_effectively_black(p_l))
{
p_l_parent = p_l;
p_l = p_l->m_p_right;
}
_GLIBCXX_DEBUG_ASSERT(is_effectively_black(p_l));
_GLIBCXX_DEBUG_ASSERT(black_height(p_l) == h_r);
_GLIBCXX_DEBUG_ASSERT(p_l == 0 || p_l->m_p_parent == p_l_parent);
return std::make_pair(p_l, p_l_parent);
}
PB_DS_CLASS_T_DEC
std::pair<
typename PB_DS_CLASS_C_DEC::node_pointer,
typename PB_DS_CLASS_C_DEC::node_pointer>
PB_DS_CLASS_C_DEC::
find_join_pos_left(node_pointer p_r, size_type h_l, size_type h_r)
{
_GLIBCXX_DEBUG_ASSERT(h_r > h_l);
if (base_type::m_p_head->m_p_parent == 0)
return (std::make_pair((node_pointer)0,
base_type::m_p_head));
node_pointer p_r_parent = base_type::m_p_head;
while (h_r > h_l)
{
if (p_r->m_red == false)
{
_GLIBCXX_DEBUG_ASSERT(h_r > 0);
--h_r;
}
p_r_parent = p_r;
p_r = p_r->m_p_left;
}
if (!is_effectively_black(p_r))
{
p_r_parent = p_r;
p_r = p_r->m_p_left;
}
_GLIBCXX_DEBUG_ASSERT(is_effectively_black(p_r));
_GLIBCXX_DEBUG_ASSERT(black_height(p_r) == h_l);
_GLIBCXX_DEBUG_ASSERT(p_r == 0 || p_r->m_p_parent == p_r_parent);
return std::make_pair(p_r, p_r_parent);
}
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::size_type
PB_DS_CLASS_C_DEC::
black_height(node_pointer p_nd)
{
size_type h = 1;
while (p_nd != 0)
{
if (p_nd->m_red == false)
++h;
p_nd = p_nd->m_p_left;
}
return h;
}
PB_DS_CLASS_T_DEC
void
PB_DS_CLASS_C_DEC::
split(key_const_reference r_key, PB_DS_CLASS_C_DEC& other)
{
PB_DS_ASSERT_VALID((*this))
PB_DS_ASSERT_VALID(other)
if (base_type::split_prep(r_key, other) == false)
{
PB_DS_ASSERT_VALID((*this))
PB_DS_ASSERT_VALID(other)
return;
}
PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
node_pointer p_nd = this->upper_bound(r_key).m_p_nd;
do
{
node_pointer p_next_nd = p_nd->m_p_parent;
if (Cmp_Fn::operator()(r_key, PB_DS_V2F(p_nd->m_value)))
split_at_node(p_nd, other);
PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
p_nd = p_next_nd;
}
while (p_nd != base_type::m_p_head);
base_type::split_finish(other);
PB_DS_ASSERT_VALID((*this))
}
PB_DS_CLASS_T_DEC
void
PB_DS_CLASS_C_DEC::
split_at_node(node_pointer p_nd, PB_DS_CLASS_C_DEC& other)
{
_GLIBCXX_DEBUG_ASSERT(p_nd != 0);
node_pointer p_l = p_nd->m_p_left;
node_pointer p_r = p_nd->m_p_right;
node_pointer p_parent = p_nd->m_p_parent;
if (p_parent == base_type::m_p_head)
{
base_type::m_p_head->m_p_parent = p_l;
if (p_l != 0)
{
p_l->m_p_parent = base_type::m_p_head;
p_l->m_red = false;
}
}
else
{
if (p_parent->m_p_left == p_nd)
p_parent->m_p_left = p_l;
else
p_parent->m_p_right = p_l;
if (p_l != 0)
p_l->m_p_parent = p_parent;
this->update_to_top(p_parent, (node_update* )this);
if (!p_nd->m_red)
remove_fixup(p_l, p_parent);
}
base_type::initialize_min_max();
other.join_imp(p_nd, p_r);
PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
}
|