modules/up/src/gnug++/NormalTermPtr.DLList.cc
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- error
- length
- NormalTermPtrDLList
- clear
- prepend
- append
- ins_after
- ins_before
- join
- owns
- del
- del_after
- remove_front
- del_front
- remove_rear
- del_rear
- OK
1 // This may look like C code, but it is really -*- C++ -*-
2 // WARNING: This file is obsolete. Use ../DLList.cc, if you can.
3 /*
4 Copyright (C) 1988 Free Software Foundation
5 written by Doug Lea (dl@rocky.oswego.edu)
6
7 This file is part of the GNU C++ Library. This library is free
8 software; you can redistribute it and/or modify it under the terms of
9 the GNU Library General Public License as published by the Free
10 Software Foundation; either version 2 of the License, or (at your
11 option) any later version. This library is distributed in the hope
12 that it will be useful, but WITHOUT ANY WARRANTY; without even the
13 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifdef __GNUG__
21 #pragma implementation
22 #endif
23
24 #include "config.h"
25 #include <cstring>
26 #include <climits>
27 #include "NormalTermPtr.DLList.h"
28 #include <builtin.h>
29
30 // error handling
31
32
33
34 void NormalTermPtrDLList::error(const char* msg)
/* [<][>][^][v][top][bottom][index][help] */
35 {
36 (*lib_error_handler)("DLList", msg);
37 }
38
39 int NormalTermPtrDLList::length()
/* [<][>][^][v][top][bottom][index][help] */
40 {
41 int l = 0;
42 NormalTermPtrDLListNode* t = h;
43 if (t != 0) do { ++l; t = t->fd; } while (t != h);
44 return l;
45 }
46
47 NormalTermPtrDLList::NormalTermPtrDLList(const NormalTermPtrDLList& a)
/* [<][>][^][v][top][bottom][index][help] */
48 {
49 if (a.h == 0)
50 h = 0;
51 else
52 {
53 NormalTermPtrDLListNode* p = a.h;
54 NormalTermPtrDLListNode* t = new NormalTermPtrDLListNode(p->hd);
55 h = t;
56 p = p->fd;
57 while (p != a.h)
58 {
59 NormalTermPtrDLListNode* n = new NormalTermPtrDLListNode(p->hd);
60 t->fd = n;
61 n->bk = t;
62 t = n;
63 p = p->fd;
64 }
65 t->fd = h;
66 h->bk = t;
67 return;
68 }
69 }
70
71 NormalTermPtrDLList& NormalTermPtrDLList::operator = (const NormalTermPtrDLList& a)
72 {
73 if (h != a.h)
74 {
75 clear();
76 if (a.h != 0)
77 {
78 NormalTermPtrDLListNode* p = a.h;
79 NormalTermPtrDLListNode* t = new NormalTermPtrDLListNode(p->hd);
80 h = t;
81 p = p->fd;
82 while (p != a.h)
83 {
84 NormalTermPtrDLListNode* n = new NormalTermPtrDLListNode(p->hd);
85 t->fd = n;
86 n->bk = t;
87 t = n;
88 p = p->fd;
89 }
90 t->fd = h;
91 h->bk = t;
92 }
93 }
94 return *this;
95 }
96
97 void NormalTermPtrDLList::clear()
/* [<][>][^][v][top][bottom][index][help] */
98 {
99 if (h == 0)
100 return;
101
102 NormalTermPtrDLListNode* p = h->fd;
103 h->fd = 0;
104 h = 0;
105
106 while (p != 0)
107 {
108 NormalTermPtrDLListNode* nxt = p->fd;
109 // cengiz
110 delete p->hd;
111 delete(p);
112 p = nxt;
113 }
114 }
115
116
117 Pix NormalTermPtrDLList::prepend(NormalTermPtr item)
/* [<][>][^][v][top][bottom][index][help] */
118 {
119 NormalTermPtrDLListNode* t = new NormalTermPtrDLListNode(item);
120 if (h == 0)
121 t->fd = t->bk = h = t;
122 else
123 {
124 t->fd = h;
125 t->bk = h->bk;
126 h->bk->fd = t;
127 h->bk = t;
128 h = t;
129 }
130 return Pix(t);
131 }
132
133 Pix NormalTermPtrDLList::append(NormalTermPtr item)
/* [<][>][^][v][top][bottom][index][help] */
134 {
135 NormalTermPtrDLListNode* t = new NormalTermPtrDLListNode(item);
136 if (h == 0)
137 t->fd = t->bk = h = t;
138 else
139 {
140 t->bk = h->bk;
141 t->bk->fd = t;
142 t->fd = h;
143 h->bk = t;
144 }
145 return Pix(t);
146 }
147
148 Pix NormalTermPtrDLList::ins_after(Pix p, NormalTermPtr item)
/* [<][>][^][v][top][bottom][index][help] */
149 {
150 if (p == 0) return prepend(item);
151 NormalTermPtrDLListNode* u = (NormalTermPtrDLListNode*) p;
152 NormalTermPtrDLListNode* t = new NormalTermPtrDLListNode(item, u, u->fd);
153 u->fd->bk = t;
154 u->fd = t;
155 return Pix(t);
156 }
157
158 Pix NormalTermPtrDLList::ins_before(Pix p, NormalTermPtr item)
/* [<][>][^][v][top][bottom][index][help] */
159 {
160 if (p == 0) error("null Pix");
161 NormalTermPtrDLListNode* u = (NormalTermPtrDLListNode*) p;
162 NormalTermPtrDLListNode* t = new NormalTermPtrDLListNode(item, u->bk, u);
163 u->bk->fd = t;
164 u->bk = t;
165 if (u == h) h = t;
166 return Pix(t);
167 }
168
169 void NormalTermPtrDLList::join(NormalTermPtrDLList& b)
/* [<][>][^][v][top][bottom][index][help] */
170 {
171 NormalTermPtrDLListNode* t = b.h;
172 b.h = 0;
173 if (h == 0)
174 h = t;
175 else if (t != 0)
176 {
177 NormalTermPtrDLListNode* l = t->bk;
178 h->bk->fd = t;
179 t->bk = h->bk;
180 h->bk = l;
181 l->fd = h;
182 }
183 }
184
185 int NormalTermPtrDLList::owns(Pix p)
/* [<][>][^][v][top][bottom][index][help] */
186 {
187 NormalTermPtrDLListNode* t = h;
188 if (t != 0 && p != 0)
189 {
190 do
191 {
192 if (Pix(t) == p) return 1;
193 t = t->fd;
194 } while (t != h);
195 }
196 return 0;
197 }
198
199 void NormalTermPtrDLList::del(Pix& p, int dir)
/* [<][>][^][v][top][bottom][index][help] */
200 {
201 if (p == 0) error("null Pix");
202 NormalTermPtrDLListNode* t = (NormalTermPtrDLListNode*) p;
203 if (t->fd == t)
204 {
205 h = 0;
206 p = 0;
207 }
208 else
209 {
210 if (dir < 0)
211 {
212 if (t == h)
213 p = 0;
214 else
215 p = Pix(t->bk);
216 }
217 else
218 {
219 if (t == h->bk)
220 p = 0;
221 else
222 p = Pix(t->fd);
223 }
224 t->bk->fd = t->fd;
225 t->fd->bk = t->bk;
226 if (t == h) h = t->fd;
227 }
228 delete t;
229 }
230
231 void NormalTermPtrDLList::del_after(Pix& p)
/* [<][>][^][v][top][bottom][index][help] */
232 {
233 if (p == 0)
234 {
235 del_front();
236 return;
237 }
238
239 NormalTermPtrDLListNode* b = (NormalTermPtrDLListNode*) p;
240 NormalTermPtrDLListNode* t = b->fd;
241
242 if (b == t)
243 {
244 h = 0;
245 p = 0;
246 }
247 else
248 {
249 t->bk->fd = t->fd;
250 t->fd->bk = t->bk;
251 if (t == h) h = t->fd;
252 }
253 delete t;
254 }
255
256 NormalTermPtr NormalTermPtrDLList::remove_front()
/* [<][>][^][v][top][bottom][index][help] */
257 {
258 if (h == 0)
259 error("remove_front of empty list");
260 NormalTermPtrDLListNode* t = h;
261 NormalTermPtr res = t->hd;
262 if (h->fd == h)
263 h = 0;
264 else
265 {
266 h->fd->bk = h->bk;
267 h->bk->fd = h->fd;
268 h = h->fd;
269 }
270 delete t;
271 return res;
272 }
273
274
275 void NormalTermPtrDLList::del_front()
/* [<][>][^][v][top][bottom][index][help] */
276 {
277 if (h == 0)
278 error("del_front of empty list");
279 NormalTermPtrDLListNode* t = h;
280 if (h->fd == h)
281 h = 0;
282 else
283 {
284 h->fd->bk = h->bk;
285 h->bk->fd = h->fd;
286 h = h->fd;
287 }
288 delete t;
289 }
290
291 NormalTermPtr NormalTermPtrDLList::remove_rear()
/* [<][>][^][v][top][bottom][index][help] */
292 {
293 if (h == 0)
294 error("remove_rear of empty list");
295 NormalTermPtrDLListNode* t = h->bk;
296 NormalTermPtr res = t->hd;
297 if (h->fd == h)
298 h = 0;
299 else
300 {
301 t->fd->bk = t->bk;
302 t->bk->fd = t->fd;
303 }
304 delete t;
305 return res;
306 }
307
308
309 void NormalTermPtrDLList::del_rear()
/* [<][>][^][v][top][bottom][index][help] */
310 {
311 if (h == 0)
312 error("del_rear of empty list");
313 NormalTermPtrDLListNode* t = h->bk;
314 if (h->fd == h)
315 h = 0;
316 else
317 {
318 t->fd->bk = t->bk;
319 t->bk->fd = t->fd;
320 }
321 delete t;
322 }
323
324
325 int NormalTermPtrDLList::OK()
/* [<][>][^][v][top][bottom][index][help] */
326 {
327 int v = 1;
328 if (h != 0)
329 {
330 NormalTermPtrDLListNode* t = h;
331 long count = LONG_MAX; // Lots of chances to find h!
332 do
333 {
334 count--;
335 v &= t->bk->fd == t;
336 v &= t->fd->bk == t;
337 t = t->fd;
338 } while (v && count > 0 && t != h);
339 v &= count > 0;
340 }
341 if (!v) error("invariant failure");
342 return v;
343 }