2568
|
1 // This may look like C code, but it is really -*- C++ -*- |
|
2 /* |
|
3 Copyright (C) 1988, 1992 Free Software Foundation |
|
4 written by Doug Lea (dl@rocky.oswego.edu) |
|
5 |
|
6 This file is part of the GNU C++ Library. This library is free |
|
7 software; you can redistribute it and/or modify it under the terms of |
|
8 the GNU Library General Public License as published by the Free |
|
9 Software Foundation; either version 2 of the License, or (at your |
|
10 option) any later version. This library is distributed in the hope |
|
11 that it will be useful, but WITHOUT ANY WARRANTY; without even the |
|
12 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|
13 PURPOSE. See the GNU Library General Public License for more details. |
|
14 You should have received a copy of the GNU Library General Public |
|
15 License along with this library; if not, write to the Free Software |
|
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 */ |
|
18 |
|
19 #if defined (__GNUG__) |
|
20 #pragma implementation |
|
21 #endif |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <limits.h> |
|
28 #include <stream.h> |
|
29 #include <builtin.h> |
|
30 #include "SLList.h" |
|
31 |
|
32 void BaseSLList::error(const char* msg) const |
|
33 { |
|
34 (*lib_error_handler)("SLList", msg); |
|
35 } |
|
36 |
|
37 int BaseSLList::length() const |
|
38 { |
|
39 int l = 0; |
|
40 BaseSLNode* t = last; |
|
41 if (t != 0) do { ++l; t = t->tl; } while (t != last); |
|
42 return l; |
|
43 } |
|
44 |
|
45 void BaseSLList::clear() |
|
46 { |
|
47 if (last == 0) |
|
48 return; |
|
49 |
|
50 BaseSLNode* p = last->tl; |
|
51 last->tl = 0; |
|
52 last = 0; |
|
53 |
|
54 while (p != 0) |
|
55 { |
|
56 BaseSLNode* nxt = p->tl; |
|
57 delete_node(p); |
|
58 p = nxt; |
|
59 } |
|
60 } |
|
61 |
|
62 |
|
63 // Note: This is an internal method. It does *not* free old contents! |
|
64 |
|
65 void BaseSLList::copy(const BaseSLList& a) |
|
66 { |
|
67 if (a.last == 0) |
|
68 last = 0; |
|
69 else |
|
70 { |
|
71 BaseSLNode* p = a.last->tl; |
|
72 BaseSLNode* h = copy_node(p->item()); |
|
73 last = h; |
|
74 for (;;) |
|
75 { |
|
76 if (p == a.last) |
|
77 { |
|
78 last->tl = h; |
|
79 return; |
|
80 } |
|
81 p = p->tl; |
|
82 BaseSLNode* n = copy_node(p->item()); |
|
83 last->tl = n; |
|
84 last = n; |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 BaseSLList& BaseSLList::operator = (const BaseSLList& a) |
|
90 { |
|
91 if (last != a.last) |
|
92 { |
|
93 clear(); |
|
94 copy(a); |
|
95 } |
|
96 return *this; |
|
97 } |
|
98 |
|
99 Pix BaseSLList::prepend(const void *datum) |
|
100 { |
|
101 return prepend(copy_node(datum)); |
|
102 } |
|
103 |
|
104 |
|
105 Pix BaseSLList::prepend(BaseSLNode* t) |
|
106 { |
|
107 if (t == 0) return 0; |
|
108 if (last == 0) |
|
109 t->tl = last = t; |
|
110 else |
|
111 { |
|
112 t->tl = last->tl; |
|
113 last->tl = t; |
|
114 } |
|
115 return Pix(t); |
|
116 } |
|
117 |
|
118 |
|
119 Pix BaseSLList::append(const void *datum) |
|
120 { |
|
121 return append(copy_node(datum)); |
|
122 } |
|
123 |
|
124 Pix BaseSLList::append(BaseSLNode* t) |
|
125 { |
|
126 if (t == 0) return 0; |
|
127 if (last == 0) |
|
128 t->tl = last = t; |
|
129 else |
|
130 { |
|
131 t->tl = last->tl; |
|
132 last->tl = t; |
|
133 last = t; |
|
134 } |
|
135 return Pix(t); |
|
136 } |
|
137 |
|
138 void BaseSLList::join(BaseSLList& b) |
|
139 { |
|
140 BaseSLNode* t = b.last; |
|
141 b.last = 0; |
|
142 if (last == 0) |
|
143 last = t; |
|
144 else if (t != 0) |
|
145 { |
|
146 BaseSLNode* f = last->tl; |
|
147 last->tl = t->tl; |
|
148 t->tl = f; |
|
149 last = t; |
|
150 } |
|
151 } |
|
152 |
|
153 Pix BaseSLList::ins_after(Pix p, const void *datum) |
|
154 { |
|
155 BaseSLNode* u = (BaseSLNode*)p; |
|
156 BaseSLNode* t = copy_node(datum); |
|
157 if (last == 0) |
|
158 t->tl = last = t; |
|
159 else if (u == 0) // ins_after 0 means prepend |
|
160 { |
|
161 t->tl = last->tl; |
|
162 last->tl = t; |
|
163 } |
|
164 else |
|
165 { |
|
166 t->tl = u->tl; |
|
167 u->tl = t; |
|
168 if (u == last) |
|
169 last = t; |
|
170 } |
|
171 return Pix(t); |
|
172 } |
|
173 |
|
174 void BaseSLList::del_after(Pix p) |
|
175 { |
|
176 BaseSLNode* u = (BaseSLNode*)p; |
|
177 if (last == 0 || u == last) error("cannot del_after last"); |
|
178 if (u == 0) u = last; // del_after 0 means delete first |
|
179 BaseSLNode* t = u->tl; |
|
180 if (u == t) |
|
181 last = 0; |
|
182 else |
|
183 { |
|
184 u->tl = t->tl; |
|
185 if (last == t) |
|
186 last = u; |
|
187 } |
|
188 delete_node(t); |
|
189 } |
|
190 |
|
191 int BaseSLList::owns(Pix p) const |
|
192 { |
|
193 BaseSLNode* t = last; |
|
194 if (t != 0 && p != 0) |
|
195 { |
|
196 do |
|
197 { |
|
198 if (Pix(t) == p) return 1; |
|
199 t = t->tl; |
|
200 } while (t != last); |
|
201 } |
|
202 return 0; |
|
203 } |
|
204 |
|
205 int BaseSLList::remove_front(void *dst, int signal_error) |
|
206 { |
|
207 if (last) |
|
208 { |
|
209 BaseSLNode* t = last->tl; |
|
210 copy_item(dst, t->item()); |
|
211 if (t == last) |
|
212 last = 0; |
|
213 else |
|
214 last->tl = t->tl; |
|
215 delete_node(t); |
|
216 return 1; |
|
217 } |
|
218 if (signal_error) |
|
219 error("remove_front of empty list"); |
|
220 return 0; |
|
221 } |
|
222 |
|
223 void BaseSLList::del_front() |
|
224 { |
|
225 if (last == 0) error("del_front of empty list"); |
|
226 BaseSLNode* t = last->tl; |
|
227 if (t == last) |
|
228 last = 0; |
|
229 else |
|
230 last->tl = t->tl; |
|
231 delete_node(t); |
|
232 } |
|
233 |
|
234 int BaseSLList::OK() const |
|
235 { |
|
236 int v = 1; |
|
237 if (last != 0) |
|
238 { |
|
239 BaseSLNode* t = last; |
|
240 long count = LONG_MAX; // Lots of chances to find last! |
|
241 do |
|
242 { |
|
243 count--; |
|
244 t = t->tl; |
|
245 } while (count > 0 && t != last); |
|
246 v &= count > 0; |
|
247 } |
|
248 if (!v) error("invariant failure"); |
|
249 return v; |
|
250 } |
|
251 |
|
252 /* |
|
253 ;;; Local Variables: *** |
|
254 ;;; mode: C++ *** |
|
255 ;;; End: *** |
|
256 */ |