3 // Copyright (C) 2005, 2006 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
43 * @file splay_fn_imps.hpp
44 * Contains an implementation class for splay_tree_.
50 splay(node_pointer p_nd)
52 while (p_nd->m_p_parent != PB_DS_BASE_C_DEC::m_p_head)
56 node_pointer p_head = PB_DS_BASE_C_DEC::m_p_head;
57 assert_special_imp(p_head);
61 _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_nd);)
63 if (p_nd->m_p_parent->m_p_parent == PB_DS_BASE_C_DEC::m_p_head)
65 PB_DS_BASE_C_DEC::rotate_parent(p_nd);
66 _GLIBCXX_DEBUG_ASSERT(p_nd == this->m_p_head->m_p_parent);
70 const node_pointer p_parent = p_nd->m_p_parent;
71 const node_pointer p_grandparent = p_parent->m_p_parent;
74 const size_type total =
75 PB_DS_BASE_C_DEC::recursive_count(p_grandparent);
76 _GLIBCXX_DEBUG_ASSERT(total >= 3);
79 if (p_parent->m_p_left == p_nd &&
80 p_grandparent->m_p_right == p_parent)
81 splay_zig_zag_left(p_nd, p_parent, p_grandparent);
82 else if (p_parent->m_p_right == p_nd &&
83 p_grandparent->m_p_left == p_parent)
84 splay_zig_zag_right(p_nd, p_parent, p_grandparent);
85 else if (p_parent->m_p_left == p_nd &&
86 p_grandparent->m_p_left == p_parent)
87 splay_zig_zig_left(p_nd, p_parent, p_grandparent);
89 splay_zig_zig_right(p_nd, p_parent, p_grandparent);
90 _GLIBCXX_DEBUG_ASSERT(total ==this->recursive_count(p_nd));
93 _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_nd);)
100 splay_zig_zag_left(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
102 _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
103 _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
105 _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_grandparent);)
107 _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_left == p_nd &&
108 p_grandparent->m_p_right == p_parent);
110 splay_zz_start(p_nd, p_parent, p_grandparent);
112 node_pointer p_b = p_nd->m_p_right;
113 node_pointer p_c = p_nd->m_p_left;
115 p_nd->m_p_right = p_parent;
116 p_parent->m_p_parent = p_nd;
118 p_nd->m_p_left = p_grandparent;
119 p_grandparent->m_p_parent = p_nd;
121 p_parent->m_p_left = p_b;
123 p_b->m_p_parent = p_parent;
125 p_grandparent->m_p_right = p_c;
127 p_c->m_p_parent = p_grandparent;
129 splay_zz_end(p_nd, p_parent, p_grandparent);
135 splay_zig_zag_right(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
137 _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
138 _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
140 _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_grandparent);)
142 _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_right == p_nd &&
143 p_grandparent->m_p_left == p_parent);
145 splay_zz_start(p_nd, p_parent, p_grandparent);
147 node_pointer p_b = p_nd->m_p_left;
148 node_pointer p_c = p_nd->m_p_right;
150 p_nd->m_p_left = p_parent;
151 p_parent->m_p_parent = p_nd;
153 p_nd->m_p_right = p_grandparent;
154 p_grandparent->m_p_parent = p_nd;
156 p_parent->m_p_right = p_b;
158 p_b->m_p_parent = p_parent;
160 p_grandparent->m_p_left = p_c;
162 p_c->m_p_parent = p_grandparent;
164 splay_zz_end(p_nd, p_parent, p_grandparent);
170 splay_zig_zig_left(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
172 _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
173 _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
175 _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_grandparent);)
177 _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_left == p_nd &&
178 p_nd->m_p_parent->m_p_parent->m_p_left == p_nd->m_p_parent);
180 splay_zz_start(p_nd, p_parent, p_grandparent);
182 node_pointer p_b = p_nd->m_p_right;
183 node_pointer p_c = p_parent->m_p_right;
185 p_nd->m_p_right = p_parent;
186 p_parent->m_p_parent = p_nd;
188 p_parent->m_p_right = p_grandparent;
189 p_grandparent->m_p_parent = p_parent;
191 p_parent->m_p_left = p_b;
193 p_b->m_p_parent = p_parent;
195 p_grandparent->m_p_left = p_c;
197 p_c->m_p_parent = p_grandparent;
199 splay_zz_end(p_nd, p_parent, p_grandparent);
205 splay_zig_zig_right(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
207 _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
208 _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
210 _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_grandparent);)
212 _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_right == p_nd&&
213 p_nd->m_p_parent->m_p_parent->m_p_right == p_nd->m_p_parent);
215 splay_zz_start(p_nd, p_parent, p_grandparent);
217 node_pointer p_b = p_nd->m_p_left;
218 node_pointer p_c = p_parent->m_p_left;
220 p_nd->m_p_left = p_parent;
221 p_parent->m_p_parent = p_nd;
223 p_parent->m_p_left = p_grandparent;
224 p_grandparent->m_p_parent = p_parent;
226 p_parent->m_p_right = p_b;
228 p_b->m_p_parent = p_parent;
230 p_grandparent->m_p_right = p_c;
232 p_c->m_p_parent = p_grandparent;
234 PB_DS_BASE_C_DEC::update_to_top(p_grandparent, (node_update* )this);
235 splay_zz_end(p_nd, p_parent, p_grandparent);
241 splay_zz_start(node_pointer p_nd,
242 #ifdef _GLIBCXX_DEBUG
243 node_pointer p_parent,
245 node_pointer /*p_parent*/,
247 node_pointer p_grandparent)
249 _GLIBCXX_DEBUG_ASSERT(p_nd != NULL);
250 _GLIBCXX_DEBUG_ASSERT(p_parent != NULL);
251 _GLIBCXX_DEBUG_ASSERT(p_grandparent != NULL);
253 const bool grandparent_head =
254 p_grandparent->m_p_parent == PB_DS_BASE_C_DEC::m_p_head;
256 if (grandparent_head)
258 PB_DS_BASE_C_DEC::m_p_head->m_p_parent =
259 PB_DS_BASE_C_DEC::m_p_head->m_p_parent;
261 p_nd->m_p_parent = PB_DS_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;
278 splay_zz_end(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
280 if (p_nd->m_p_parent == PB_DS_BASE_C_DEC::m_p_head)
281 PB_DS_BASE_C_DEC::m_p_head->m_p_parent = p_nd;
283 apply_update(p_grandparent, (node_update* )this);
284 apply_update(p_parent, (node_update* )this);
285 apply_update(p_nd, (node_update* )this);
287 _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_nd);)