Debian Codegraph @

ptrlist.cpp

raw symbols SCIP
1 /*
2 * Licensed under BSD license. See LICENCE.TXT
3 *
4 * Produced by: Jeff Lait
5 *
6 * 7DRL Development
7 *
8 * NAME: ptrlist.cpp ( Utility Library, C++ )
9 *
10 * COMMENTS:
11 * Implementation of the ptrlist functions
12 */
13
14 #include <string.h>
15 #include "assert.h"
16 #include "ptrlist.h"
17
18 template <typename PTR>
19 PTRLIST<PTR>::PTRLIST()
20 {
21 myEntries = 0;
22 mySize = 0;
23 myList = 0;
24 myStartPos = 0;
25 }
26
27 template <typename PTR>
28 PTRLIST<PTR>::~PTRLIST()
29 {
30 delete [] myList;
31 }
32
33 template <typename PTR>
34 PTRLIST<PTR>::PTRLIST(const PTRLIST<PTR> &ref)
35 {
36 myList = 0;
37 *this = ref;
38 }
39
40 template <typename PTR>
41 PTRLIST<PTR> &
42 PTRLIST<PTR>::operator=(const PTRLIST<PTR> &ref)
43 {
44 int i;
45
46 if (this == &ref)
47 return *this;
48 delete [] myList;
49 myList = new PTR[ref.myEntries];
50 mySize = ref.myEntries;
51 myEntries = ref.myEntries;
52 // Reset our list.
53 myStartPos = 0;
54
55 for (i = 0; i < myEntries; i++)
56 {
57 set(i, ref(i));
58 }
59
60 return *this;
61 }
62
63 template <typename PTR>
64 void
65 PTRLIST<PTR>::append(PTR item)
66 {
67 if (myEntries + myStartPos >= mySize)
68 {
69 // Need to expand the extra stack.
70 PTR *newlist;
71
72 mySize = mySize * 2 + 10;
73 newlist = new PTR[mySize];
74 if (myList)
75 {
76 for (int i = 0; i < myEntries; i++)
77 newlist[i] = myList[i+myStartPos];
78 }
79 delete [] myList;
80 myList = newlist;
81 // Take this chance to reset our leading edge.
82 myStartPos = 0;
83 }
84
85 myList[myEntries+myStartPos] = item;
86
87 myEntries++;
88 }
89
90 template <typename PTR>
91 void
92 PTRLIST<PTR>::append(const PTRLIST<PTR> &list)
93 {
94 int i;
95
96 for (i = 0; i < list.entries(); i++)
97 append(list(i));
98 }
99
100 template <typename PTR>
101 void
102 PTRLIST<PTR>::resize(int size)
103 {
104 while (myEntries < size)
105 append((PTR) 0);
106
107 myEntries = size;
108 }
109
110 template <typename PTR>
111 void
112 PTRLIST<PTR>::set(int idx, PTR item)
113 {
114 UT_ASSERT(idx < entries());
115 UT_ASSERT(idx >= 0);
116 if (idx < 0)
117 return;
118 if (idx >= entries())
119 return;
120
121 myList[idx + myStartPos] = item;
122 }
123
124 template <typename PTR>
125 void
126 PTRLIST<PTR>::insert(int idx, PTR item)
127 {
128 int offset;
129
130 // Deal with simple case.
131 if (idx == entries())
132 {
133 append(item);
134 return;
135 }
136
137 UT_ASSERT(idx < entries());
138 UT_ASSERT(idx >= 0);
139
140 // Duplicate end.
141 append(top());
142 // Now make room
143 for (offset = entries()-1; offset > idx; offset--)
144 {
145 set(offset, (*this)(offset-1));
146 }
147 set(idx, item);
148 }
149
150 template <typename PTR>
151 PTR
152 PTRLIST<PTR>::operator()(int idx) const
153 {
154 UT_ASSERT(idx >= 0 && idx < myEntries);
155
156 return myList[idx + myStartPos];
157 }
158
159
160 template <typename PTR>
161 int
162 PTRLIST<PTR>::entries() const
163 {
164 return myEntries;
165 }
166
167 template <typename PTR>
168 void
169 PTRLIST<PTR>::clear()
170 {
171 myEntries = 0;
172 // Likewise, might as well reset our offset
173 myStartPos = 0;
174 }
175
176 template <typename PTR>
177 void
178 PTRLIST<PTR>::constant(PTR val)
179 {
180 int i;
181
182 for (i = 0; i < myEntries; i++)
183 set(i, val);
184 }
185
186 template <typename PTR>
187 void
188 PTRLIST<PTR>::reverse()
189 {
190 PTR tmp;
191 int i, n, r;
192
193 n = entries();
194 for (i = 0; i < n / 2; i++)
195 {
196 // Reversed entry.
197 r = n - 1 - i;
198 tmp = (*this)(i);
199 set(i, (*this)(r));
200 set(r, tmp);
201 }
202 }
203
204 template <typename PTR>
205 void
206 PTRLIST<PTR>::removePtr(PTR item)
207 {
208 int s, d, n;
209
210 n = entries();
211 s = d = 0;
212 while (s < n)
213 {
214 if ((*this)(s) == item)
215 {
216 // Don't copy this value.
217 }
218 else
219 {
220 // Copy this value.
221 if (s != d)
222 set(d, (*this)(s));
223 d++;
224 }
225 // Go to next value.
226 s++;
227 }
228 // Final n is d.
229 myEntries = d;
230 }
231
232 template <typename PTR>
233 void
234 PTRLIST<PTR>::removeAt(int idx)
235 {
236 int s, n;
237
238 UT_ASSERT(idx >= 0);
239 UT_ASSERT(idx < entries());
240
241 n = entries();
242 for (s = idx; s < n-1; s++)
243 {
244 set(s, (*this)(s+1));
245 }
246 myEntries--;
247 }
248
249 template <typename PTR>
250 PTR
251 PTRLIST<PTR>::pop()
252 {
253 int n;
254 PTR result;
255
256 n = entries();
257 if (!n)
258 {
259 // We should do an inplace new on result here so it
260 // works with all types.
261 // Inplace newis buggy for enums on AMD64, however, so I'll
262 // play it safe.
263 result = 0;
264 return result;
265 }
266
267 result = (*this)(n-1);
268 myEntries = n-1;
269 return result;
270 }
271
272 template <typename PTR>
273 void
274 PTRLIST<PTR>::swapEntries(int i1, int i2)
275 {
276 if (i1 == i2)
277 return;
278
279 PTR tmp;
280
281 tmp = (*this)(i1);
282 set(i1, (*this)(i2));
283 set(i2, tmp);
284 }
285
286 template <typename PTR>
287 PTR
288 PTRLIST<PTR>::removeFirst()
289 {
290 int n;
291 PTR result;
292
293 n = entries();
294 if (!n)
295 {
296 // We should do an inplace new on result here so it
297 // works with all types.
298 // Inplace newis buggy for enums on AMD64, however, so I'll
299 // play it safe.
300 return (PTR) 0;
301 }
302
303 result = (*this)(0);
304
305 // A little bit more efficient than moving.
306 // We wait until the next resize to bother with clearing up
307 // since at that resize we have to move anyways.
308 myStartPos++;
309 myEntries--;
310 return result;
311 }
312
313 template <typename PTR>
314 int
315 PTRLIST<PTR>::find(PTR item) const
316 {
317 int i, n;
318
319 n = entries();
320 for (i = 0; i < n; i++)
321 {
322 if ((*this)(i) == item)
323 return i;
324 }
325 return -1;
326 }
327
328 template <typename PTR>
329 void
330 PTRLIST<PTR>::collapse()
331 {
332 int i, j, n;
333
334 n = entries();
335
336 for (i = j = 0; i < n; i++)
337 {
338 if ((*this)(i))
339 {
340 // Valid...
341 if (i != j)
342 set(j, (*this)(i));
343 j++;
344 }
345 else
346 {
347 // Do not copy over, do not inc j.
348 }
349 }
350
351 myEntries = j;
352 }