source: trunk/src/emx/include/Attic/cpp/gen/SplayMap.ccP@ 18

Last change on this file since 18 was 18, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.8 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2/*
3Copyright (C) 1988 Free Software Foundation
4 written by Doug Lea ([email protected])
5
6This file is part of the GNU C++ Library. This library is free
7software; you can redistribute it and/or modify it under the terms of
8the GNU Library General Public License as published by the Free
9Software Foundation; either version 2 of the License, or (at your
10option) any later version. This library is distributed in the hope
11that it will be useful, but WITHOUT ANY WARRANTY; without even the
12implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13PURPOSE. See the GNU Library General Public License for more details.
14You should have received a copy of the GNU Library General Public
15License along with this library; if not, write to the Free Software
16Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifdef __GNUG__
20#pragma implementation
21#endif
22#include <stream.h>
23#include "<T>.<C>.SplayMap.h"
24
25
26/*
27
28 struct to simulate the special `null' node in the Sleater & Tarjan JACM 1985
29 splay tree algorithms
30
31 All routines use a version of their `simple top-down' splay alg. (p 669)
32
33*/
34
35struct _dummySplayNode
36{
37 <T><C>SplayNode* lt;
38 <T><C>SplayNode* rt;
39 <T><C>SplayNode* par;
40} _dummy_null;
41
42
43/*
44 traversal primitives
45*/
46
47
48<T><C>SplayNode* <T><C>SplayMap::leftmost()
49{
50 <T><C>SplayNode* t = root;
51 if (t != 0) while (t->lt != 0) t = t->lt;
52 return t;
53}
54
55<T><C>SplayNode* <T><C>SplayMap::rightmost()
56{
57 <T><C>SplayNode* t = root;
58 if (t != 0) while (t->rt != 0) t = t->rt;
59 return t;
60}
61
62<T><C>SplayNode* <T><C>SplayMap::succ(<T><C>SplayNode* t)
63{
64 if (t == 0)
65 return 0;
66 if (t->rt != 0)
67 {
68 t = t->rt;
69 while (t->lt != 0) t = t->lt;
70 return t;
71 }
72 else
73 {
74 for (;;)
75 {
76 if (t->par == 0 || t == t->par->lt)
77 return t->par;
78 else
79 t = t->par;
80 }
81 }
82}
83
84<T><C>SplayNode* <T><C>SplayMap::pred(<T><C>SplayNode* t)
85{
86 if (t == 0)
87 return 0;
88 else if (t->lt != 0)
89 {
90 t = t->lt;
91 while (t->rt != 0) t = t->rt;
92 return t;
93 }
94 else
95 {
96 for (;;)
97 {
98 if (t->par == 0 || t == t->par->rt)
99 return t->par;
100 else
101 t = t->par;
102 }
103 }
104}
105
106
107Pix <T><C>SplayMap::seek(<T&> key)
108{
109 <T><C>SplayNode* t = root;
110 if (t == 0)
111 return 0;
112
113 int comp = <T>CMP(key, t->item);
114 if (comp == 0)
115 return Pix(t);
116
117 <T><C>SplayNode* dummy = (<T><C>SplayNode*)(&_dummy_null);
118 <T><C>SplayNode* l = dummy;
119 <T><C>SplayNode* r = dummy;
120 dummy->rt = dummy->lt = dummy->par = 0;
121
122 while (comp != 0)
123 {
124 if (comp > 0)
125 {
126 <T><C>SplayNode* tr = t->rt;
127 if (tr == 0)
128 break;
129 else
130 {
131 comp = <T>CMP(key, tr->item);
132 if (comp <= 0 || tr->rt == 0)
133 {
134 l->rt = t; t->par = l;
135 l = t;
136 t = tr;
137 if (comp >= 0)
138 break;
139 }
140 else
141 {
142 if ((t->rt = tr->lt) != 0) t->rt->par = t;
143 tr->lt = t; t->par = tr;
144 l->rt = tr; tr->par = l;
145 l = tr;
146 t = tr->rt;
147 comp = <T>CMP(key, t->item);
148 }
149 }
150 }
151 else
152 {
153 <T><C>SplayNode* tl = t->lt;
154 if (tl == 0)
155 break;
156 else
157 {
158 comp = <T>CMP(key, tl->item);
159 if (comp >= 0 || tl->lt == 0)
160 {
161 r->lt = t; t->par = r;
162 r = t;
163 t = tl;
164 if (comp <= 0)
165 break;
166 }
167 else
168 {
169 if ((t->lt = tl->rt) != 0) t->lt->par = t;
170 tl->rt = t; t->par = tl;
171 r->lt = tl; tl->par = r;
172 r = tl;
173 t = tl->lt;
174 comp = <T>CMP(key, t->item);
175 }
176 }
177 }
178 }
179 if ((r->lt = t->rt) != 0) r->lt->par = r;
180 if ((l->rt = t->lt) != 0) l->rt->par = l;
181 if ((t->lt = dummy->rt) != 0) t->lt->par = t;
182 if ((t->rt = dummy->lt) != 0) t->rt->par = t;
183 t->par = 0;
184 root = t;
185 return (comp == 0) ? Pix(t) : 0;
186}
187
188
189<C>& <T><C>SplayMap::operator [] (<T&> item)
190{
191 <T><C>SplayNode* t = root;
192 if (t == 0)
193 {
194 ++count;
195 root = new <T><C>SplayNode(item, def);
196 return root->cont;
197 }
198 int comp = <T>CMP(item, t->item);
199 if (comp == 0)
200 return t->cont;
201
202 <T><C>SplayNode* dummy = (<T><C>SplayNode*)(&_dummy_null);
203 <T><C>SplayNode* l = dummy;
204 <T><C>SplayNode* r = dummy;
205 dummy->rt = dummy->lt = dummy->par = 0;
206
207 while (comp != 0)
208 {
209 if (comp > 0)
210 {
211 <T><C>SplayNode* tr = t->rt;
212 if (tr == 0)
213 {
214 ++count;
215 tr = new <T><C>SplayNode(item, def);
216 comp = 0;
217 }
218 else
219 comp = <T>CMP(item, tr->item);
220
221 if (comp <= 0)
222 {
223 l->rt = t; t->par = l;
224 l = t;
225 t = tr;
226 }
227 else
228 {
229 <T><C>SplayNode* trr = tr->rt;
230 if (trr == 0)
231 {
232 ++count;
233 trr = new <T><C>SplayNode(item, def);
234 comp = 0;
235 }
236 else
237 comp = <T>CMP(item, trr->item);
238
239 if ((t->rt = tr->lt) != 0) t->rt->par = t;
240 tr->lt = t; t->par = tr;
241 l->rt = tr; tr->par = l;
242 l = tr;
243 t = trr;
244 }
245 }
246 else
247 {
248 <T><C>SplayNode* tl = t->lt;
249 if (tl == 0)
250 {
251 ++count;
252 tl = new <T><C>SplayNode(item, def);
253 comp = 0;
254 }
255 else
256 comp = <T>CMP(item, tl->item);
257
258 if (comp >= 0)
259 {
260 r->lt = t; t->par = r;
261 r = t;
262 t = tl;
263 }
264 else
265 {
266 <T><C>SplayNode* tll = tl->lt;
267 if (tll == 0)
268 {
269 ++count;
270 tll = new <T><C>SplayNode(item, def);
271 comp = 0;
272 }
273 else
274 comp = <T>CMP(item, tll->item);
275
276 if ((t->lt = tl->rt) != 0) t->lt->par = t;
277 tl->rt = t; t->par = tl;
278 r->lt = tl; tl->par = r;
279 r = tl;
280 t = tll;
281 }
282 }
283 }
284 if ((r->lt = t->rt) != 0) r->lt->par = r;
285 if ((l->rt = t->lt) != 0) l->rt->par = l;
286 if ((t->lt = dummy->rt) != 0) t->lt->par = t;
287 if ((t->rt = dummy->lt) != 0) t->rt->par = t;
288 t->par = 0;
289 root = t;
290 return root->cont;
291}
292
293void <T><C>SplayMap::del(<T&> key)
294{
295 <T><C>SplayNode* t = (<T><C>SplayNode*)(seek(key));
296 if (t == 0) return;
297
298 <T><C>SplayNode* p = t->par;
299
300 --count;
301 if (t->rt == 0)
302 {
303 if (t == root)
304 {
305 if ((root = t->lt) != 0) root->par = 0;
306 }
307 else if (t == p->lt)
308 {
309 if ((p->lt = t->lt) != 0) p->lt->par = p;
310 }
311 else
312 if ((p->rt = t->lt) != 0) p->rt->par = p;
313 }
314 else
315 {
316 <T><C>SplayNode* r = t->rt;
317 <T><C>SplayNode* l = r->lt;
318 for(;;)
319 {
320 if (l == 0)
321 {
322 if (t == root)
323 {
324 root = r;
325 r->par = 0;
326 }
327 else if (t == p->lt)
328 {
329 p->lt = r;
330 r->par = p;
331 }
332 else
333 {
334 p->rt = r;
335 r->par = p;
336 }
337 if ((r->lt = t->lt) != 0) r->lt->par = r;
338 break;
339 }
340 else
341 {
342 if ((r->lt = l->rt) != 0) r->lt->par = r;
343 l->rt = r; r->par = l;
344 r = l;
345 l = l->lt;
346 }
347 }
348 }
349 delete t;
350}
351
352
353void <T><C>SplayMap::_kill(<T><C>SplayNode* t)
354{
355 if (t != 0)
356 {
357 _kill(t->lt);
358 _kill(t->rt);
359 delete t;
360 }
361}
362
363
364<T><C>SplayNode* <T><C>SplayMap::_copy(<T><C>SplayNode* t)
365{
366 if (t != 0)
367 {
368 <T><C>SplayNode* l = _copy(t->lt);
369 <T><C>SplayNode* r = _copy(t->rt);
370 <T><C>SplayNode* x = new <T><C>SplayNode(t->item, t->cont, l, r);
371 if (l != 0) l->par = x;
372 if (r != 0) r->par = x;
373 return x;
374 }
375 else
376 return 0;
377}
378
379
380int <T><C>SplayMap::OK()
381{
382 int v = 1;
383 if (root == 0)
384 v = count == 0;
385 else
386 {
387 int n = 1;
388 <T><C>SplayNode* trail = leftmost();
389 <T><C>SplayNode* t = succ(trail);
390 while (t != 0)
391 {
392 ++n;
393 v &= <T>CMP(trail->item, t->item) < 0;
394 trail = t;
395 t = succ(t);
396 }
397 v &= n == count;
398 }
399 if (!v) error("invariant failure");
400 return v;
401}
Note: See TracBrowser for help on using the repository browser.