]>
Commit | Line | Data |
---|---|---|
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. | |
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 splay_fn_imps.hpp | |
38 | * Contains an implementation class for splay_tree_. | |
39 | */ | |
40 | ||
41 | PB_DS_CLASS_T_DEC | |
42 | void | |
43 | PB_DS_CLASS_C_DEC:: | |
44 | splay(node_pointer p_nd) | |
45 | { | |
1b24692f | 46 | while (p_nd->m_p_parent != base_type::m_p_head) |
4569a895 | 47 | { |
47bea7b8 | 48 | #ifdef _GLIBCXX_DEBUG |
4569a895 | 49 | { |
1b24692f | 50 | node_pointer p_head = base_type::m_p_head; |
4569a895 AT |
51 | assert_special_imp(p_head); |
52 | } | |
47bea7b8 | 53 | #endif |
4569a895 | 54 | |
1b24692f | 55 | _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_nd);) |
4569a895 | 56 | |
1b24692f | 57 | if (p_nd->m_p_parent->m_p_parent == base_type::m_p_head) |
4569a895 | 58 | { |
1b24692f | 59 | base_type::rotate_parent(p_nd); |
47bea7b8 | 60 | _GLIBCXX_DEBUG_ASSERT(p_nd == this->m_p_head->m_p_parent); |
4569a895 AT |
61 | } |
62 | else | |
63 | { | |
64 | const node_pointer p_parent = p_nd->m_p_parent; | |
65 | const node_pointer p_grandparent = p_parent->m_p_parent; | |
66 | ||
47bea7b8 | 67 | #ifdef _GLIBCXX_DEBUG |
4569a895 | 68 | const size_type total = |
1b24692f | 69 | base_type::recursive_count(p_grandparent); |
47bea7b8 BK |
70 | _GLIBCXX_DEBUG_ASSERT(total >= 3); |
71 | #endif | |
4569a895 | 72 | |
47bea7b8 | 73 | if (p_parent->m_p_left == p_nd && |
4569a895 AT |
74 | p_grandparent->m_p_right == p_parent) |
75 | splay_zig_zag_left(p_nd, p_parent, p_grandparent); | |
47bea7b8 | 76 | else if (p_parent->m_p_right == p_nd && |
4569a895 AT |
77 | p_grandparent->m_p_left == p_parent) |
78 | splay_zig_zag_right(p_nd, p_parent, p_grandparent); | |
47bea7b8 | 79 | else if (p_parent->m_p_left == p_nd && |
4569a895 AT |
80 | p_grandparent->m_p_left == p_parent) |
81 | splay_zig_zig_left(p_nd, p_parent, p_grandparent); | |
82 | else | |
83 | splay_zig_zig_right(p_nd, p_parent, p_grandparent); | |
47bea7b8 | 84 | _GLIBCXX_DEBUG_ASSERT(total ==this->recursive_count(p_nd)); |
4569a895 AT |
85 | } |
86 | ||
1b24692f | 87 | _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_nd);) |
47bea7b8 | 88 | } |
4569a895 AT |
89 | } |
90 | ||
91 | PB_DS_CLASS_T_DEC | |
92 | inline void | |
93 | PB_DS_CLASS_C_DEC:: | |
1b24692f BK |
94 | splay_zig_zag_left(node_pointer p_nd, node_pointer p_parent, |
95 | node_pointer p_grandparent) | |
4569a895 | 96 | { |
47bea7b8 BK |
97 | _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent); |
98 | _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent); | |
4569a895 | 99 | |
1b24692f | 100 | _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_grandparent);) |
4569a895 | 101 | |
47bea7b8 BK |
102 | _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_left == p_nd && |
103 | p_grandparent->m_p_right == p_parent); | |
4569a895 AT |
104 | |
105 | splay_zz_start(p_nd, p_parent, p_grandparent); | |
106 | ||
107 | node_pointer p_b = p_nd->m_p_right; | |
108 | node_pointer p_c = p_nd->m_p_left; | |
109 | ||
110 | p_nd->m_p_right = p_parent; | |
111 | p_parent->m_p_parent = p_nd; | |
112 | ||
113 | p_nd->m_p_left = p_grandparent; | |
114 | p_grandparent->m_p_parent = p_nd; | |
115 | ||
116 | p_parent->m_p_left = p_b; | |
8fc81078 | 117 | if (p_b != 0) |
4569a895 AT |
118 | p_b->m_p_parent = p_parent; |
119 | ||
120 | p_grandparent->m_p_right = p_c; | |
8fc81078 | 121 | if (p_c != 0) |
4569a895 AT |
122 | p_c->m_p_parent = p_grandparent; |
123 | ||
124 | splay_zz_end(p_nd, p_parent, p_grandparent); | |
125 | } | |
126 | ||
127 | PB_DS_CLASS_T_DEC | |
128 | inline void | |
129 | PB_DS_CLASS_C_DEC:: | |
1b24692f BK |
130 | splay_zig_zag_right(node_pointer p_nd, node_pointer p_parent, |
131 | node_pointer p_grandparent) | |
4569a895 | 132 | { |
47bea7b8 BK |
133 | _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent); |
134 | _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent); | |
4569a895 | 135 | |
1b24692f | 136 | _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_grandparent);) |
4569a895 | 137 | |
47bea7b8 BK |
138 | _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_right == p_nd && |
139 | p_grandparent->m_p_left == p_parent); | |
4569a895 AT |
140 | |
141 | splay_zz_start(p_nd, p_parent, p_grandparent); | |
142 | ||
143 | node_pointer p_b = p_nd->m_p_left; | |
144 | node_pointer p_c = p_nd->m_p_right; | |
145 | ||
146 | p_nd->m_p_left = p_parent; | |
147 | p_parent->m_p_parent = p_nd; | |
148 | ||
149 | p_nd->m_p_right = p_grandparent; | |
150 | p_grandparent->m_p_parent = p_nd; | |
151 | ||
152 | p_parent->m_p_right = p_b; | |
8fc81078 | 153 | if (p_b != 0) |
4569a895 AT |
154 | p_b->m_p_parent = p_parent; |
155 | ||
156 | p_grandparent->m_p_left = p_c; | |
8fc81078 | 157 | if (p_c != 0) |
4569a895 AT |
158 | p_c->m_p_parent = p_grandparent; |
159 | ||
160 | splay_zz_end(p_nd, p_parent, p_grandparent); | |
161 | } | |
162 | ||
163 | PB_DS_CLASS_T_DEC | |
164 | inline void | |
165 | PB_DS_CLASS_C_DEC:: | |
1b24692f BK |
166 | splay_zig_zig_left(node_pointer p_nd, node_pointer p_parent, |
167 | node_pointer p_grandparent) | |
4569a895 | 168 | { |
47bea7b8 BK |
169 | _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent); |
170 | _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent); | |
4569a895 | 171 | |
1b24692f | 172 | _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_grandparent);) |
4569a895 | 173 | |
47bea7b8 | 174 | _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_left == p_nd && |
4569a895 AT |
175 | p_nd->m_p_parent->m_p_parent->m_p_left == p_nd->m_p_parent); |
176 | ||
177 | splay_zz_start(p_nd, p_parent, p_grandparent); | |
178 | ||
179 | node_pointer p_b = p_nd->m_p_right; | |
180 | node_pointer p_c = p_parent->m_p_right; | |
181 | ||
182 | p_nd->m_p_right = p_parent; | |
183 | p_parent->m_p_parent = p_nd; | |
184 | ||
185 | p_parent->m_p_right = p_grandparent; | |
186 | p_grandparent->m_p_parent = p_parent; | |
187 | ||
188 | p_parent->m_p_left = p_b; | |
8fc81078 | 189 | if (p_b != 0) |
4569a895 AT |
190 | p_b->m_p_parent = p_parent; |
191 | ||
192 | p_grandparent->m_p_left = p_c; | |
8fc81078 | 193 | if (p_c != 0) |
4569a895 AT |
194 | p_c->m_p_parent = p_grandparent; |
195 | ||
196 | splay_zz_end(p_nd, p_parent, p_grandparent); | |
197 | } | |
198 | ||
199 | PB_DS_CLASS_T_DEC | |
200 | inline void | |
201 | PB_DS_CLASS_C_DEC:: | |
1b24692f BK |
202 | splay_zig_zig_right(node_pointer p_nd, node_pointer p_parent, |
203 | node_pointer p_grandparent) | |
4569a895 | 204 | { |
47bea7b8 BK |
205 | _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent); |
206 | _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent); | |
1b24692f BK |
207 | _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_grandparent);) |
208 | _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_right == p_nd && | |
209 | p_nd->m_p_parent->m_p_parent->m_p_right == p_nd->m_p_parent); | |
4569a895 AT |
210 | |
211 | splay_zz_start(p_nd, p_parent, p_grandparent); | |
212 | ||
213 | node_pointer p_b = p_nd->m_p_left; | |
214 | node_pointer p_c = p_parent->m_p_left; | |
215 | ||
216 | p_nd->m_p_left = p_parent; | |
217 | p_parent->m_p_parent = p_nd; | |
218 | ||
219 | p_parent->m_p_left = p_grandparent; | |
220 | p_grandparent->m_p_parent = p_parent; | |
221 | ||
222 | p_parent->m_p_right = p_b; | |
8fc81078 | 223 | if (p_b != 0) |
4569a895 AT |
224 | p_b->m_p_parent = p_parent; |
225 | ||
226 | p_grandparent->m_p_right = p_c; | |
8fc81078 | 227 | if (p_c != 0) |
4569a895 AT |
228 | p_c->m_p_parent = p_grandparent; |
229 | ||
1b24692f | 230 | base_type::update_to_top(p_grandparent, (node_update* )this); |
4569a895 AT |
231 | splay_zz_end(p_nd, p_parent, p_grandparent); |
232 | } | |
233 | ||
234 | PB_DS_CLASS_T_DEC | |
235 | inline void | |
236 | PB_DS_CLASS_C_DEC:: | |
237 | splay_zz_start(node_pointer p_nd, | |
47bea7b8 | 238 | #ifdef _GLIBCXX_DEBUG |
4569a895 | 239 | node_pointer p_parent, |
47bea7b8 | 240 | #else |
4569a895 | 241 | node_pointer /*p_parent*/, |
47bea7b8 | 242 | #endif |
4569a895 AT |
243 | node_pointer p_grandparent) |
244 | { | |
8fc81078 PC |
245 | _GLIBCXX_DEBUG_ASSERT(p_nd != 0); |
246 | _GLIBCXX_DEBUG_ASSERT(p_parent != 0); | |
247 | _GLIBCXX_DEBUG_ASSERT(p_grandparent != 0); | |
4569a895 | 248 | |
1b24692f | 249 | const bool grandparent_head = p_grandparent->m_p_parent == base_type::m_p_head; |
4569a895 AT |
250 | |
251 | if (grandparent_head) | |
252 | { | |
1b24692f BK |
253 | base_type::m_p_head->m_p_parent = base_type::m_p_head->m_p_parent; |
254 | p_nd->m_p_parent = base_type::m_p_head; | |
4569a895 AT |
255 | return; |
256 | } | |
257 | ||
258 | node_pointer p_greatgrandparent = p_grandparent->m_p_parent; | |
259 | ||
260 | p_nd->m_p_parent = p_greatgrandparent; | |
261 | ||
262 | if (p_grandparent == p_greatgrandparent->m_p_left) | |
263 | p_greatgrandparent->m_p_left = p_nd; | |
264 | else | |
265 | p_greatgrandparent->m_p_right = p_nd; | |
266 | } | |
267 | ||
268 | PB_DS_CLASS_T_DEC | |
269 | inline void | |
270 | PB_DS_CLASS_C_DEC:: | |
1b24692f BK |
271 | splay_zz_end(node_pointer p_nd, node_pointer p_parent, |
272 | node_pointer p_grandparent) | |
4569a895 | 273 | { |
1b24692f BK |
274 | if (p_nd->m_p_parent == base_type::m_p_head) |
275 | base_type::m_p_head->m_p_parent = p_nd; | |
4569a895 AT |
276 | |
277 | apply_update(p_grandparent, (node_update* )this); | |
278 | apply_update(p_parent, (node_update* )this); | |
279 | apply_update(p_nd, (node_update* )this); | |
280 | ||
1b24692f | 281 | _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_nd);) |
47bea7b8 | 282 | } |
4569a895 | 283 |