BSD 4_3_Net_2 release
[unix-history] / usr / src / lib / libg++ / g++-include / gen / DLList.ccP
CommitLineData
afe17391
C
1// This may look like C code, but it is really -*- C++ -*-
2/*
3Copyright (C) 1988 Free Software Foundation
4 written by Doug Lea (dl@rocky.oswego.edu)
5
6This file is part of GNU CC.
7
8GNU CC is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY. No author or distributor
10accepts responsibility to anyone for the consequences of using it
11or for whether it serves any particular purpose or works at all,
12unless he says so in writing. Refer to the GNU CC General Public
13License for full details.
14
15Everyone is granted permission to copy, modify and redistribute
16GNU CC, but only under the conditions described in the
17GNU CC General Public License. A copy of this license is
18supposed to have been given to you along with GNU CC so you
19can know your rights and responsibilities. It should be in a
20file named COPYING. Among other things, the copyright notice
21and this notice must be preserved on all copies.
22*/
23
24#ifdef __GNUG__
25#pragma implementation
26#endif
27#include <values.h>
28#include <stream.h>
29#include "<T>.DLList.h"
30
31// error handling
32
33
34
35void <T>DLList::error(const char* msg)
36{
37 (*lib_error_handler)("DLList", msg);
38}
39
40int <T>DLList::length()
41{
42 int l = 0;
43 <T>DLListNode* t = h;
44 if (t != 0) do { ++l; t = t->fd; } while (t != h);
45 return l;
46}
47
48<T>DLList::<T>DLList(<T>DLList& a)
49{
50 if (a.h == 0)
51 h = 0;
52 else
53 {
54 <T>DLListNode* p = a.h;
55 <T>DLListNode* t = new <T>DLListNode(p->hd);
56 h = t;
57 p = p->fd;
58 while (p != a.h)
59 {
60 <T>DLListNode* n = new <T>DLListNode(p->hd);
61 t->fd = n;
62 n->bk = t;
63 t = n;
64 p = p->fd;
65 }
66 t->fd = h;
67 h->bk = t;
68 return;
69 }
70}
71
72<T>DLList& <T>DLList::operator = (<T>DLList& a)
73{
74 if (h != a.h)
75 {
76 clear();
77 if (a.h != 0)
78 {
79 <T>DLListNode* p = a.h;
80 <T>DLListNode* t = new <T>DLListNode(p->hd);
81 h = t;
82 p = p->fd;
83 while (p != a.h)
84 {
85 <T>DLListNode* n = new <T>DLListNode(p->hd);
86 t->fd = n;
87 n->bk = t;
88 t = n;
89 p = p->fd;
90 }
91 t->fd = h;
92 h->bk = t;
93 }
94 }
95 return *this;
96}
97
98void <T>DLList::clear()
99{
100 if (h == 0)
101 return;
102
103 <T>DLListNode* p = h->fd;
104 h->fd = 0;
105 h = 0;
106
107 while (p != 0)
108 {
109 <T>DLListNode* nxt = p->fd;
110 delete(p);
111 p = nxt;
112 }
113}
114
115
116Pix <T>DLList::prepend(<T&> item)
117{
118 <T>DLListNode* t = new <T>DLListNode(item);
119 if (h == 0)
120 t->fd = t->bk = h = t;
121 else
122 {
123 t->fd = h;
124 t->bk = h->bk;
125 h->bk->fd = t;
126 h->bk = t;
127 h = t;
128 }
129 return Pix(t);
130}
131
132Pix <T>DLList::append(<T&> item)
133{
134 <T>DLListNode* t = new <T>DLListNode(item);
135 if (h == 0)
136 t->fd = t->bk = h = t;
137 else
138 {
139 t->bk = h->bk;
140 t->bk->fd = t;
141 t->fd = h;
142 h->bk = t;
143 }
144 return Pix(t);
145}
146
147Pix <T>DLList::ins_after(Pix p, <T&> item)
148{
149 if (p == 0) return prepend(item);
150 <T>DLListNode* u = (<T>DLListNode*) p;
151 <T>DLListNode* t = new <T>DLListNode(item, u, u->fd);
152 u->fd->bk = t;
153 u->fd = t;
154 return Pix(t);
155}
156
157Pix <T>DLList::ins_before(Pix p, <T&> item)
158{
159 if (p == 0) error("null Pix");
160 <T>DLListNode* u = (<T>DLListNode*) p;
161 <T>DLListNode* t = new <T>DLListNode(item, u->bk, u);
162 u->bk->fd = t;
163 u->bk = t;
164 if (u == h) h = t;
165 return Pix(t);
166}
167
168void <T>DLList::join(<T>DLList& b)
169{
170 <T>DLListNode* t = b.h;
171 b.h = 0;
172 if (h == 0)
173 h = t;
174 else if (t != 0)
175 {
176 <T>DLListNode* l = t->bk;
177 h->bk->fd = t;
178 t->bk = h->bk;
179 h->bk = l;
180 l->fd = h;
181 }
182}
183
184int <T>DLList::owns(Pix p)
185{
186 <T>DLListNode* t = h;
187 if (t != 0 && p != 0)
188 {
189 do
190 {
191 if (Pix(t) == p) return 1;
192 t = t->fd;
193 } while (t != h);
194 }
195 return 0;
196}
197
198void <T>DLList::del(Pix& p, int dir)
199{
200 if (p == 0) error("null Pix");
201 <T>DLListNode* t = (<T>DLListNode*) p;
202 if (t->fd == t)
203 {
204 h = 0;
205 p = 0;
206 }
207 else
208 {
209 if (dir < 0)
210 {
211 if (t == h)
212 p = 0;
213 else
214 p = Pix(t->bk);
215 }
216 else
217 {
218 if (t == h->bk)
219 p = 0;
220 else
221 p = Pix(t->fd);
222 }
223 t->bk->fd = t->fd;
224 t->fd->bk = t->bk;
225 if (t == h) h = t->fd;
226 }
227 delete t;
228}
229
230<T> <T>DLList::remove_front()
231{
232 if (h == 0)
233 error("remove_front of empty list");
234 <T>DLListNode* t = h;
235 <T> res = t->hd;
236 if (h->fd == h)
237 h = 0;
238 else
239 {
240 h->fd->bk = h->bk;
241 h->bk->fd = h->fd;
242 h = h->fd;
243 }
244 delete t;
245 return res;
246}
247
248
249void <T>DLList::del_front()
250{
251 if (h == 0)
252 error("del_front of empty list");
253 <T>DLListNode* t = h;
254 if (h->fd == h)
255 h = 0;
256 else
257 {
258 h->fd->bk = h->bk;
259 h->bk->fd = h->fd;
260 h = h->fd;
261 }
262 delete t;
263}
264
265<T> <T>DLList::remove_rear()
266{
267 if (h == 0)
268 error("remove_rear of empty list");
269 <T>DLListNode* t = h->bk;
270 <T> res = t->hd;
271 if (h->fd == h)
272 h = 0;
273 else
274 {
275 t->fd->bk = t->bk;
276 t->bk->fd = t->fd;
277 }
278 delete t;
279 return res;
280}
281
282
283void <T>DLList::del_rear()
284{
285 if (h == 0)
286 error("del_rear of empty list");
287 <T>DLListNode* t = h->bk;
288 if (h->fd == h)
289 h = 0;
290 else
291 {
292 t->fd->bk = t->bk;
293 t->bk->fd = t->fd;
294 }
295 delete t;
296}
297
298
299int <T>DLList::OK()
300{
301 int v = 1;
302 if (h != 0)
303 {
304 <T>DLListNode* t = h;
305 long count = MAXLONG; // Lots of chances to find h!
306 do
307 {
308 count--;
309 v &= t->bk->fd == t;
310 v &= t->fd->bk == t;
311 t = t->fd;
312 } while (v && count > 0 && t != h);
313 v &= count > 0;
314 }
315 if (!v) error("invariant failure");
316 return v;
317}