source: trunk/src/emx/include/Attic/cpp/gen/SplayPQ.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: 9.7 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>.SplayPQ.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>SplayNode* lt;
38 <T>SplayNode* rt;
39 <T>SplayNode* par;
40} _dummy_null;
41
42
43/*
44 traversal primitives
45*/
46
47
48<T>SplayNode* <T>SplayPQ::leftmost()
49{
50 <T>SplayNode* t = root;
51 if (t != 0) while (t->lt != 0) t = t->lt;
52 return t;
53}
54
55<T>SplayNode* <T>SplayPQ::rightmost()
56{
57 <T>SplayNode* t = root;
58 if (t != 0) while (t->rt != 0) t = t->rt;
59 return t;
60}
61
62<T>SplayNode* <T>SplayPQ::succ(<T>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>SplayNode* <T>SplayPQ::pred(<T>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>SplayPQ::seek(<T&> key)
108{
109 <T>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>SplayNode* dummy = (<T>SplayNode*)(&_dummy_null);
118 <T>SplayNode* l = dummy;
119 <T>SplayNode* r = dummy;
120 dummy->rt = dummy->lt = dummy->par = 0;
121
122 while (comp != 0)
123 {
124 if (comp > 0)
125 {
126 <T>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>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
189Pix <T>SplayPQ::enq(<T&> item)
190{
191 ++count;
192 <T>SplayNode* newnode = new <T>SplayNode(item);
193 <T>SplayNode* t = root;
194 if (t == 0)
195 {
196 root = newnode;
197 return Pix(root);
198 }
199
200 int comp = <T>CMP(item, t->item);
201
202 <T>SplayNode* dummy = (<T>SplayNode*)(&_dummy_null);
203 <T>SplayNode* l = dummy;
204 <T>SplayNode* r = dummy;
205 dummy->rt = dummy->lt = dummy->par = 0;
206
207 int done = 0;