OSDN Git Service

2006-09-08 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / ext / pb_ds / detail / splay_tree_ / splay_fn_imps.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
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
8 // Foundation; either version 2, or (at your option) any later
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
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.
20
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
29 // Public License.
30
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
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
40 // warranty.
41
42 /**
43  * @file splay_fn_imps.hpp
44  * Contains an implementation class for splay_tree_.
45  */
46
47 PB_DS_CLASS_T_DEC
48 void
49 PB_DS_CLASS_C_DEC::
50 splay(node_pointer p_nd)
51 {
52   while (p_nd->m_p_parent != PB_DS_BASE_C_DEC::m_p_head)
53     {
54 #ifdef _GLIBCXX_DEBUG
55       {
56         node_pointer p_head = PB_DS_BASE_C_DEC::m_p_head;
57         assert_special_imp(p_head);
58       }
59 #endif
60
61       _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_nd);)
62
63         if (p_nd->m_p_parent->m_p_parent == PB_DS_BASE_C_DEC::m_p_head)
64           {
65             PB_DS_BASE_C_DEC::rotate_parent(p_nd);
66             _GLIBCXX_DEBUG_ASSERT(p_nd == this->m_p_head->m_p_parent);
67           }
68         else
69           {
70             const node_pointer p_parent = p_nd->m_p_parent;
71             const node_pointer p_grandparent = p_parent->m_p_parent;
72
73 #ifdef _GLIBCXX_DEBUG
74             const size_type total =
75               PB_DS_BASE_C_DEC::recursive_count(p_grandparent);
76             _GLIBCXX_DEBUG_ASSERT(total >= 3);
77 #endif 
78
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);
88             else
89               splay_zig_zig_right(p_nd, p_parent, p_grandparent);
90             _GLIBCXX_DEBUG_ASSERT(total ==this->recursive_count(p_nd));
91           }
92
93       _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_nd);)
94   }
95 }
96
97 PB_DS_CLASS_T_DEC
98 inline void
99 PB_DS_CLASS_C_DEC::
100 splay_zig_zag_left(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
101 {
102   _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
103   _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
104
105   _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_grandparent);)
106
107   _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_left == p_nd && 
108                         p_grandparent->m_p_right == p_parent);
109
110   splay_zz_start(p_nd, p_parent, p_grandparent);
111
112   node_pointer p_b = p_nd->m_p_right;
113   node_pointer p_c = p_nd->m_p_left;
114
115   p_nd->m_p_right = p_parent;
116   p_parent->m_p_parent = p_nd;
117
118   p_nd->m_p_left = p_grandparent;
119   p_grandparent->m_p_parent = p_nd;
120
121   p_parent->m_p_left = p_b;
122   if (p_b != NULL)
123     p_b->m_p_parent = p_parent;
124
125   p_grandparent->m_p_right = p_c;
126   if (p_c != NULL)
127     p_c->m_p_parent = p_grandparent;
128
129   splay_zz_end(p_nd, p_parent, p_grandparent);
130 }
131
132 PB_DS_CLASS_T_DEC
133 inline void
134 PB_DS_CLASS_C_DEC::
135 splay_zig_zag_right(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
136 {
137   _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
138   _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
139
140   _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_grandparent);)
141
142   _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_right == p_nd && 
143                         p_grandparent->m_p_left == p_parent);
144
145   splay_zz_start(p_nd, p_parent, p_grandparent);
146
147   node_pointer p_b = p_nd->m_p_left;
148   node_pointer p_c = p_nd->m_p_right;
149
150   p_nd->m_p_left = p_parent;
151   p_parent->m_p_parent = p_nd;
152
153   p_nd->m_p_right = p_grandparent;
154   p_grandparent->m_p_parent = p_nd;
155
156   p_parent->m_p_right = p_b;
157   if (p_b != NULL)
158     p_b->m_p_parent = p_parent;
159
160   p_grandparent->m_p_left = p_c;
161   if (p_c != NULL)
162     p_c->m_p_parent = p_grandparent;
163
164   splay_zz_end(p_nd, p_parent, p_grandparent);
165 }
166
167 PB_DS_CLASS_T_DEC
168 inline void
169 PB_DS_CLASS_C_DEC::
170 splay_zig_zig_left(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
171 {
172   _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
173   _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
174
175   _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_grandparent);)
176
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);
179
180   splay_zz_start(p_nd, p_parent, p_grandparent);
181
182   node_pointer p_b = p_nd->m_p_right;
183   node_pointer p_c = p_parent->m_p_right;
184
185   p_nd->m_p_right = p_parent;
186   p_parent->m_p_parent = p_nd;
187
188   p_parent->m_p_right = p_grandparent;
189   p_grandparent->m_p_parent = p_parent;
190
191   p_parent->m_p_left = p_b;
192   if (p_b != NULL)
193     p_b->m_p_parent = p_parent;
194
195   p_grandparent->m_p_left = p_c;
196   if (p_c != NULL)
197     p_c->m_p_parent = p_grandparent;
198
199   splay_zz_end(p_nd, p_parent, p_grandparent);
200 }
201
202 PB_DS_CLASS_T_DEC
203 inline void
204 PB_DS_CLASS_C_DEC::
205 splay_zig_zig_right(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
206 {
207   _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
208   _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
209
210   _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_grandparent);)
211
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);
214
215   splay_zz_start(p_nd, p_parent, p_grandparent);
216
217   node_pointer p_b = p_nd->m_p_left;
218   node_pointer p_c = p_parent->m_p_left;
219
220   p_nd->m_p_left = p_parent;
221   p_parent->m_p_parent = p_nd;
222
223   p_parent->m_p_left = p_grandparent;
224   p_grandparent->m_p_parent = p_parent;
225
226   p_parent->m_p_right = p_b;
227   if (p_b != NULL)
228     p_b->m_p_parent = p_parent;
229
230   p_grandparent->m_p_right = p_c;
231   if (p_c != NULL)
232     p_c->m_p_parent = p_grandparent;
233
234   PB_DS_BASE_C_DEC::update_to_top(p_grandparent, (node_update* )this);
235   splay_zz_end(p_nd, p_parent, p_grandparent);
236 }
237
238 PB_DS_CLASS_T_DEC
239 inline void
240 PB_DS_CLASS_C_DEC::
241 splay_zz_start(node_pointer p_nd,
242 #ifdef _GLIBCXX_DEBUG
243                node_pointer p_parent,
244 #else 
245                node_pointer /*p_parent*/,
246 #endif
247                node_pointer p_grandparent)
248 {
249   _GLIBCXX_DEBUG_ASSERT(p_nd != NULL);
250   _GLIBCXX_DEBUG_ASSERT(p_parent != NULL);
251   _GLIBCXX_DEBUG_ASSERT(p_grandparent != NULL);
252
253   const bool grandparent_head =
254     p_grandparent->m_p_parent == PB_DS_BASE_C_DEC::m_p_head;
255
256   if (grandparent_head)
257     {
258       PB_DS_BASE_C_DEC::m_p_head->m_p_parent =
259         PB_DS_BASE_C_DEC::m_p_head->m_p_parent;
260
261       p_nd->m_p_parent = PB_DS_BASE_C_DEC::m_p_head;
262       return;
263     }
264
265   node_pointer p_greatgrandparent = p_grandparent->m_p_parent;
266
267   p_nd->m_p_parent = p_greatgrandparent;
268
269   if (p_grandparent == p_greatgrandparent->m_p_left)
270     p_greatgrandparent->m_p_left = p_nd;
271   else
272     p_greatgrandparent->m_p_right = p_nd;
273 }
274
275 PB_DS_CLASS_T_DEC
276 inline void
277 PB_DS_CLASS_C_DEC::
278 splay_zz_end(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
279 {
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;
282
283   apply_update(p_grandparent, (node_update* )this);
284   apply_update(p_parent, (node_update* )this);
285   apply_update(p_nd, (node_update* )this);
286
287   _GLIBCXX_DEBUG_ONLY(PB_DS_BASE_C_DEC::assert_node_consistent(p_nd);)
288 }
289