Import a stripped down version of gcc-4.1.1
[dragonfly.git] / contrib / gcc-4.1 / libstdc++-v3 / include / ext / pb_assoc / detail / splay_tree_ / splay_fn_imps.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005 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
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
31
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.
39
40 /**
41  * @file splay_fn_imps.hpp
42  * Contains an implementation class for splay_tree_.
43  */
44
45 PB_ASSOC_CLASS_T_DEC
46 void
47 PB_ASSOC_CLASS_C_DEC::
48 splay(node_pointer p_nd)
49 {
50   while (p_nd->m_p_parent != PB_ASSOC_BASE_C_DEC::m_p_head)
51     {
52       PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_node_consistent(p_nd);)
53
54         if (p_nd->m_p_parent->m_p_parent ==
55             PB_ASSOC_BASE_C_DEC::m_p_head)
56           {
57             PB_ASSOC_BASE_C_DEC::rotate_parent(p_nd);
58
59             PB_ASSOC_DBG_ASSERT(p_nd ==
60                                 PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent);
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
67 #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
68             const size_type total =
69               PB_ASSOC_BASE_C_DEC::recursive_count(p_grandparent);
70
71             PB_ASSOC_DBG_ASSERT(total >= 3);
72 #endif // #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
73
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);
83             else
84               splay_zig_zig_right(p_nd, p_parent, p_grandparent);
85
86             PB_ASSOC_DBG_ASSERT(total ==
87                                 PB_ASSOC_BASE_C_DEC::recursive_count(p_nd));
88           }
89
90       PB_ASSOC_DBG_ONLY(assert_node_consistent(p_nd);)
91         }
92 }
93
94 PB_ASSOC_CLASS_T_DEC
95 inline void
96 PB_ASSOC_CLASS_C_DEC::
97 splay_zig_zag_left(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
98 {
99   PB_ASSOC_DBG_ASSERT(p_parent == p_nd->m_p_parent);
100   PB_ASSOC_DBG_ASSERT(p_grandparent == p_parent->m_p_parent);
101
102   PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent);)
103
104     PB_ASSOC_DBG_ASSERT(p_parent->m_p_left == p_nd&& 
105                         p_grandparent->m_p_right == p_parent);
106
107   splay_zz_start(p_nd, p_parent, p_grandparent);
108
109   node_pointer p_b = p_nd->m_p_right;
110   node_pointer p_c = p_nd->m_p_left;
111
112   p_nd->m_p_right = p_parent;
113   p_parent->m_p_parent = p_nd;
114
115   p_nd->m_p_left = p_grandparent;
116   p_grandparent->m_p_parent = p_nd;
117
118   p_parent->m_p_left = p_b;
119   if (p_b != NULL)
120     p_b->m_p_parent = p_parent;
121
122   p_grandparent->m_p_right = p_c;
123   if (p_c != NULL)
124     p_c->m_p_parent = p_grandparent;
125
126   splay_zz_end(p_nd, p_parent, p_grandparent);
127 }
128
129 PB_ASSOC_CLASS_T_DEC
130 inline void
131 PB_ASSOC_CLASS_C_DEC::
132 splay_zig_zag_right(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
133 {
134   PB_ASSOC_DBG_ASSERT(p_parent == p_nd->m_p_parent);
135   PB_ASSOC_DBG_ASSERT(p_grandparent == p_parent->m_p_parent);
136
137   PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent);)
138
139     PB_ASSOC_DBG_ASSERT(p_parent->m_p_right == p_nd&& 
140                         p_grandparent->m_p_left == p_parent);
141
142   splay_zz_start(p_nd, p_parent, p_grandparent);
143
144   node_pointer p_b = p_nd->m_p_left;
145   node_pointer p_c = p_nd->m_p_right;
146
147   p_nd->m_p_left = p_parent;
148   p_parent->m_p_parent = p_nd;
149
150   p_nd->m_p_right = p_grandparent;
151   p_grandparent->m_p_parent = p_nd;
152
153   p_parent->m_p_right = p_b;
154   if (p_b != NULL)
155     p_b->m_p_parent = p_parent;
156
157   p_grandparent->m_p_left = p_c;
158   if (p_c != NULL)
159     p_c->m_p_parent = p_grandparent;
160
161   splay_zz_end(p_nd, p_parent, p_grandparent);
162 }
163
164 PB_ASSOC_CLASS_T_DEC
165 inline void
166 PB_ASSOC_CLASS_C_DEC::
167 splay_zig_zig_left(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
168 {
169   PB_ASSOC_DBG_ASSERT(p_parent == p_nd->m_p_parent);
170   PB_ASSOC_DBG_ASSERT(p_grandparent == p_parent->m_p_parent);
171
172   PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent);)
173
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);
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;
189   if (p_b != NULL)
190     p_b->m_p_parent = p_parent;
191
192   p_grandparent->m_p_left = p_c;
193   if (p_c != NULL)
194     p_c->m_p_parent = p_grandparent;
195
196   splay_zz_end(p_nd, p_parent, p_grandparent);
197 }
198
199 PB_ASSOC_CLASS_T_DEC
200 inline void
201 PB_ASSOC_CLASS_C_DEC::
202 splay_zig_zig_right(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
203 {
204   PB_ASSOC_DBG_ASSERT(p_parent == p_nd->m_p_parent);
205   PB_ASSOC_DBG_ASSERT(p_grandparent == p_parent->m_p_parent);
206
207   PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent);)
208
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);
211
212   splay_zz_start(p_nd, p_parent, p_grandparent);
213
214   node_pointer p_b = p_nd->m_p_left;
215   node_pointer p_c = p_parent->m_p_left;
216
217   p_nd->m_p_left = p_parent;
218   p_parent->m_p_parent = p_nd;
219
220   p_parent->m_p_left = p_grandparent;
221   p_grandparent->m_p_parent = p_parent;
222
223   p_parent->m_p_right = p_b;
224   if (p_b != NULL)
225     p_b->m_p_parent = p_parent;
226
227   p_grandparent->m_p_right = p_c;
228   if (p_c != NULL)
229     p_c->m_p_parent = p_grandparent;
230
231   PB_ASSOC_BASE_C_DEC::update_to_top(
232                                      p_grandparent, (Node_Updator* )this);
233
234   splay_zz_end(p_nd, p_parent, p_grandparent);
235 }
236
237 PB_ASSOC_CLASS_T_DEC
238 inline void
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)
247 {
248   PB_ASSOC_DBG_ASSERT(p_nd != NULL);
249   PB_ASSOC_DBG_ASSERT(p_parent != NULL);
250   PB_ASSOC_DBG_ASSERT(p_grandparent != NULL);
251
252   const bool grandparent_head =
253     p_grandparent->m_p_parent == PB_ASSOC_BASE_C_DEC::m_p_head;
254
255   if (grandparent_head)
256     {
257       PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent =
258         PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent;
259
260       p_nd->m_p_parent = PB_ASSOC_BASE_C_DEC::m_p_head;
261
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_ASSOC_CLASS_T_DEC
276 inline void
277 PB_ASSOC_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_ASSOC_BASE_C_DEC::m_p_head)
281     PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent = p_nd;
282
283   apply_update(p_grandparent, (Node_Updator* )this);
284   apply_update(p_parent, (Node_Updator* )this);
285   apply_update(p_nd, (Node_Updator* )this);
286
287   PB_ASSOC_DBG_ONLY(assert_node_consistent(p_nd);)
288     }
289