3 // Copyright (C) 2005 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
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file splay_fn_imps.hpp
42 * Contains an implementation class for splay_tree_.
47 PB_ASSOC_CLASS_C_DEC::
48 splay(node_pointer p_nd
)
50 while (p_nd
->m_p_parent
!= PB_ASSOC_BASE_C_DEC::m_p_head
)
52 PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_node_consistent(p_nd
);)
54 if (p_nd
->m_p_parent
->m_p_parent
==
55 PB_ASSOC_BASE_C_DEC::m_p_head
)
57 PB_ASSOC_BASE_C_DEC::rotate_parent(p_nd
);
59 PB_ASSOC_DBG_ASSERT(p_nd
==
60 PB_ASSOC_BASE_C_DEC::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
;
67 #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
68 const size_type total
=
69 PB_ASSOC_BASE_C_DEC::recursive_count(p_grandparent
);
71 PB_ASSOC_DBG_ASSERT(total
>= 3);
72 #endif // #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
74 if (p_parent
->m_p_left
== p_nd
&&
75 p_grandparent
->m_p_right
== p_parent
)
76 splay_zig_zag_left(p_nd
, p_parent
, p_grandparent
);
77 else if (p_parent
->m_p_right
== p_nd
&&
78 p_grandparent
->m_p_left
== p_parent
)
79 splay_zig_zag_right(p_nd
, p_parent
, p_grandparent
);
80 else if (p_parent
->m_p_left
== p_nd
&&
81 p_grandparent
->m_p_left
== p_parent
)
82 splay_zig_zig_left(p_nd
, p_parent
, p_grandparent
);
84 splay_zig_zig_right(p_nd
, p_parent
, p_grandparent
);
86 PB_ASSOC_DBG_ASSERT(total
==
87 PB_ASSOC_BASE_C_DEC::recursive_count(p_nd
));
90 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_nd
);)
96 PB_ASSOC_CLASS_C_DEC::
97 splay_zig_zag_left(node_pointer p_nd
, node_pointer p_parent
, node_pointer p_grandparent
)
99 PB_ASSOC_DBG_ASSERT(p_parent
== p_nd
->m_p_parent
);
100 PB_ASSOC_DBG_ASSERT(p_grandparent
== p_parent
->m_p_parent
);
102 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent
);)
104 PB_ASSOC_DBG_ASSERT(p_parent
->m_p_left
== p_nd
&&
105 p_grandparent
->m_p_right
== p_parent
);
107 splay_zz_start(p_nd
, p_parent
, p_grandparent
);
109 node_pointer p_b
= p_nd
->m_p_right
;
110 node_pointer p_c
= p_nd
->m_p_left
;
112 p_nd
->m_p_right
= p_parent
;
113 p_parent
->m_p_parent
= p_nd
;
115 p_nd
->m_p_left
= p_grandparent
;
116 p_grandparent
->m_p_parent
= p_nd
;
118 p_parent
->m_p_left
= p_b
;
120 p_b
->m_p_parent
= p_parent
;
122 p_grandparent
->m_p_right
= p_c
;
124 p_c
->m_p_parent
= p_grandparent
;
126 splay_zz_end(p_nd
, p_parent
, p_grandparent
);
131 PB_ASSOC_CLASS_C_DEC::
132 splay_zig_zag_right(node_pointer p_nd
, node_pointer p_parent
, node_pointer p_grandparent
)
134 PB_ASSOC_DBG_ASSERT(p_parent
== p_nd
->m_p_parent
);
135 PB_ASSOC_DBG_ASSERT(p_grandparent
== p_parent
->m_p_parent
);
137 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent
);)
139 PB_ASSOC_DBG_ASSERT(p_parent
->m_p_right
== p_nd
&&
140 p_grandparent
->m_p_left
== p_parent
);
142 splay_zz_start(p_nd
, p_parent
, p_grandparent
);
144 node_pointer p_b
= p_nd
->m_p_left
;
145 node_pointer p_c
= p_nd
->m_p_right
;
147 p_nd
->m_p_left
= p_parent
;
148 p_parent
->m_p_parent
= p_nd
;
150 p_nd
->m_p_right
= p_grandparent
;
151 p_grandparent
->m_p_parent
= p_nd
;
153 p_parent
->m_p_right
= p_b
;
155 p_b
->m_p_parent
= p_parent
;
157 p_grandparent
->m_p_left
= p_c
;
159 p_c
->m_p_parent
= p_grandparent
;
161 splay_zz_end(p_nd
, p_parent
, p_grandparent
);
166 PB_ASSOC_CLASS_C_DEC::
167 splay_zig_zig_left(node_pointer p_nd
, node_pointer p_parent
, node_pointer p_grandparent
)
169 PB_ASSOC_DBG_ASSERT(p_parent
== p_nd
->m_p_parent
);
170 PB_ASSOC_DBG_ASSERT(p_grandparent
== p_parent
->m_p_parent
);
172 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent
);)
174 PB_ASSOC_DBG_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
);
201 PB_ASSOC_CLASS_C_DEC::
202 splay_zig_zig_right(node_pointer p_nd
, node_pointer p_parent
, node_pointer p_grandparent
)
204 PB_ASSOC_DBG_ASSERT(p_parent
== p_nd
->m_p_parent
);
205 PB_ASSOC_DBG_ASSERT(p_grandparent
== p_parent
->m_p_parent
);
207 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent
);)
209 PB_ASSOC_DBG_ASSERT(p_parent
->m_p_right
== p_nd
&&
210 p_nd
->m_p_parent
->m_p_parent
->m_p_right
== p_nd
->m_p_parent
);
212 splay_zz_start(p_nd
, p_parent
, p_grandparent
);
214 node_pointer p_b
= p_nd
->m_p_left
;
215 node_pointer p_c
= p_parent
->m_p_left
;
217 p_nd
->m_p_left
= p_parent
;
218 p_parent
->m_p_parent
= p_nd
;
220 p_parent
->m_p_left
= p_grandparent
;
221 p_grandparent
->m_p_parent
= p_parent
;
223 p_parent
->m_p_right
= p_b
;
225 p_b
->m_p_parent
= p_parent
;
227 p_grandparent
->m_p_right
= p_c
;
229 p_c
->m_p_parent
= p_grandparent
;
231 PB_ASSOC_BASE_C_DEC::update_to_top(
232 p_grandparent
, (Node_Updator
* )this);
234 splay_zz_end(p_nd
, p_parent
, p_grandparent
);
239 PB_ASSOC_CLASS_C_DEC::
240 splay_zz_start(node_pointer p_nd
,
241 #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
242 node_pointer p_parent
,
243 #else // #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
244 node_pointer
/*p_parent*/,
245 #endif // #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
246 node_pointer p_grandparent
)
248 PB_ASSOC_DBG_ASSERT(p_nd
!= NULL
);
249 PB_ASSOC_DBG_ASSERT(p_parent
!= NULL
);
250 PB_ASSOC_DBG_ASSERT(p_grandparent
!= NULL
);
252 const bool grandparent_head
=
253 p_grandparent
->m_p_parent
== PB_ASSOC_BASE_C_DEC::m_p_head
;
255 if (grandparent_head
)
257 PB_ASSOC_BASE_C_DEC::m_p_head
->m_p_parent
=
258 PB_ASSOC_BASE_C_DEC::m_p_head
->m_p_parent
;
260 p_nd
->m_p_parent
= PB_ASSOC_BASE_C_DEC::m_p_head
;
265 node_pointer p_greatgrandparent
= p_grandparent
->m_p_parent
;
267 p_nd
->m_p_parent
= p_greatgrandparent
;
269 if (p_grandparent
== p_greatgrandparent
->m_p_left
)
270 p_greatgrandparent
->m_p_left
= p_nd
;
272 p_greatgrandparent
->m_p_right
= p_nd
;
277 PB_ASSOC_CLASS_C_DEC::
278 splay_zz_end(node_pointer p_nd
, node_pointer p_parent
, node_pointer p_grandparent
)
280 if (p_nd
->m_p_parent
== PB_ASSOC_BASE_C_DEC::m_p_head
)
281 PB_ASSOC_BASE_C_DEC::m_p_head
->m_p_parent
= p_nd
;
283 apply_update(p_grandparent
, (Node_Updator
* )this);
284 apply_update(p_parent
, (Node_Updator
* )this);
285 apply_update(p_nd
, (Node_Updator
* )this);
287 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_nd
);)