3 // Copyright (C) 2005-2018 Free Software Foundation, Inc.
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
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
37 * @file splay_tree_/splay_fn_imps.hpp
38 * Contains an implementation class for splay_tree_.
44 splay(node_pointer p_nd
)
46 while (p_nd
->m_p_parent
!= base_type::m_p_head
)
50 node_pointer p_head
= base_type::m_p_head
;
51 assert_special_imp(p_head
, __FILE__
, __LINE__
);
55 PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_nd
)
57 if (p_nd
->m_p_parent
->m_p_parent
== base_type::m_p_head
)
59 base_type::rotate_parent(p_nd
);
60 _GLIBCXX_DEBUG_ASSERT(p_nd
== this->m_p_head
->m_p_parent
);
64 const node_pointer p_parent
= p_nd
->m_p_parent
;
65 const node_pointer p_grandparent
= p_parent
->m_p_parent
;
68 const size_type total
=
69 base_type::recursive_count(p_grandparent
);
70 _GLIBCXX_DEBUG_ASSERT(total
>= 3);
73 if (p_parent
->m_p_left
== p_nd
&&
74 p_grandparent
->m_p_right
== p_parent
)
75 splay_zig_zag_left(p_nd
, p_parent
, p_grandparent
);
76 else if (p_parent
->m_p_right
== p_nd
&&
77 p_grandparent
->m_p_left
== p_parent
)
78 splay_zig_zag_right(p_nd
, p_parent
, p_grandparent
);
79 else if (p_parent
->m_p_left
== p_nd
&&
80 p_grandparent
->m_p_left
== p_parent
)
81 splay_zig_zig_left(p_nd
, p_parent
, p_grandparent
);
83 splay_zig_zig_right(p_nd
, p_parent
, p_grandparent
);
84 _GLIBCXX_DEBUG_ASSERT(total
==this->recursive_count(p_nd
));
87 PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_nd
)
94 splay_zig_zag_left(node_pointer p_nd
, node_pointer p_parent
,
95 node_pointer p_grandparent
)
97 _GLIBCXX_DEBUG_ASSERT(p_parent
== p_nd
->m_p_parent
);
98 _GLIBCXX_DEBUG_ASSERT(p_grandparent
== p_parent
->m_p_parent
);
100 PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_grandparent
)
102 _GLIBCXX_DEBUG_ASSERT(p_parent
->m_p_left
== p_nd
&&
103 p_grandparent
->m_p_right
== p_parent
);
105 splay_zz_start(p_nd
, p_parent
, p_grandparent
);
107 node_pointer p_b
= p_nd
->m_p_right
;
108 node_pointer p_c
= p_nd
->m_p_left
;
110 p_nd
->m_p_right
= p_parent
;
111 p_parent
->m_p_parent
= p_nd
;
113 p_nd
->m_p_left
= p_grandparent
;
114 p_grandparent
->m_p_parent
= p_nd
;
116 p_parent
->m_p_left
= p_b
;
118 p_b
->m_p_parent
= p_parent
;
120 p_grandparent
->m_p_right
= p_c
;
122 p_c
->m_p_parent
= p_grandparent
;
124 splay_zz_end(p_nd
, p_parent
, p_grandparent
);
130 splay_zig_zag_right(node_pointer p_nd
, node_pointer p_parent
,
131 node_pointer p_grandparent
)
133 _GLIBCXX_DEBUG_ASSERT(p_parent
== p_nd
->m_p_parent
);
134 _GLIBCXX_DEBUG_ASSERT(p_grandparent
== p_parent
->m_p_parent
);
136 PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_grandparent
)
138 _GLIBCXX_DEBUG_ASSERT(p_parent
->m_p_right
== p_nd
&&
139 p_grandparent
->m_p_left
== p_parent
);
141 splay_zz_start(p_nd
, p_parent
, p_grandparent
);
143 node_pointer p_b
= p_nd
->m_p_left
;
144 node_pointer p_c
= p_nd
->m_p_right
;
146 p_nd
->m_p_left
= p_parent
;
147 p_parent
->m_p_parent
= p_nd
;
149 p_nd
->m_p_right
= p_grandparent
;
150 p_grandparent
->m_p_parent
= p_nd
;
152 p_parent
->m_p_right
= p_b
;
154 p_b
->m_p_parent
= p_parent
;
156 p_grandparent
->m_p_left
= p_c
;
158 p_c
->m_p_parent
= p_grandparent
;
160 splay_zz_end(p_nd
, p_parent
, p_grandparent
);
166 splay_zig_zig_left(node_pointer p_nd
, node_pointer p_parent
,
167 node_pointer p_grandparent
)
169 _GLIBCXX_DEBUG_ASSERT(p_parent
== p_nd
->m_p_parent
);
170 _GLIBCXX_DEBUG_ASSERT(p_grandparent
== p_parent
->m_p_parent
);
172 PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_grandparent
)
174 _GLIBCXX_DEBUG_ASSERT(p_parent
->m_p_left
== p_nd
&&
175 p_nd
->m_p_parent
->m_p_parent
->m_p_left
== p_nd
->m_p_parent
);
177 splay_zz_start(p_nd
, p_parent
, p_grandparent
);
179 node_pointer p_b
= p_nd
->m_p_right
;
180 node_pointer p_c
= p_parent
->m_p_right
;
182 p_nd
->m_p_right
= p_parent
;
183 p_parent
->m_p_parent
= p_nd
;
185 p_parent
->m_p_right
= p_grandparent
;
186 p_grandparent
->m_p_parent
= p_parent
;
188 p_parent
->m_p_left
= p_b
;
190 p_b
->m_p_parent
= p_parent
;
192 p_grandparent
->m_p_left
= p_c
;
194 p_c
->m_p_parent
= p_grandparent
;
196 splay_zz_end(p_nd
, p_parent
, p_grandparent
);
202 splay_zig_zig_right(node_pointer p_nd
, node_pointer p_parent
,
203 node_pointer p_grandparent
)
205 _GLIBCXX_DEBUG_ASSERT(p_parent
== p_nd
->m_p_parent
);
206 _GLIBCXX_DEBUG_ASSERT(p_grandparent
== p_parent
->m_p_parent
);
207 PB_DS_ASSERT_BASE_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
);
211 splay_zz_start(p_nd
, p_parent
, p_grandparent
);
213 node_pointer p_b
= p_nd
->m_p_left
;
214 node_pointer p_c
= p_parent
->m_p_left
;
216 p_nd
->m_p_left
= p_parent
;
217 p_parent
->m_p_parent
= p_nd
;
219 p_parent
->m_p_left
= p_grandparent
;
220 p_grandparent
->m_p_parent
= p_parent
;
222 p_parent
->m_p_right
= p_b
;
224 p_b
->m_p_parent
= p_parent
;
226 p_grandparent
->m_p_right
= p_c
;
228 p_c
->m_p_parent
= p_grandparent
;
230 base_type::update_to_top(p_grandparent
, (node_update
*)this);
231 splay_zz_end(p_nd
, p_parent
, p_grandparent
);
237 splay_zz_start(node_pointer p_nd
,
238 #ifdef _GLIBCXX_DEBUG
239 node_pointer p_parent
,
241 node_pointer
/*p_parent*/,
243 node_pointer p_grandparent
)
245 _GLIBCXX_DEBUG_ASSERT(p_nd
!= 0);
246 _GLIBCXX_DEBUG_ASSERT(p_parent
!= 0);
247 _GLIBCXX_DEBUG_ASSERT(p_grandparent
!= 0);
249 const bool grandparent_head
= p_grandparent
->m_p_parent
== base_type::m_p_head
;
251 if (grandparent_head
)
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
;
258 node_pointer p_greatgrandparent
= p_grandparent
->m_p_parent
;
260 p_nd
->m_p_parent
= p_greatgrandparent
;
262 if (p_grandparent
== p_greatgrandparent
->m_p_left
)
263 p_greatgrandparent
->m_p_left
= p_nd
;
265 p_greatgrandparent
->m_p_right
= p_nd
;
271 splay_zz_end(node_pointer p_nd
, node_pointer p_parent
,
272 node_pointer p_grandparent
)
274 if (p_nd
->m_p_parent
== base_type::m_p_head
)
275 base_type::m_p_head
->m_p_parent
= p_nd
;
277 this->apply_update(p_grandparent
, (node_update
*)this);
278 this->apply_update(p_parent
, (node_update
*)this);
279 this->apply_update(p_nd
, (node_update
*)this);
280 PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_nd
)