File: ptrlist.cpp

package info (click to toggle)
powder 117-2
  • links: PTS
  • area: non-free
  • in suites: stretch
  • size: 10,576 kB
  • ctags: 3,545
  • sloc: cpp: 55,002; makefile: 541; sh: 258; objc: 245; ansic: 107; csh: 54
file content (352 lines) | stat: -rw-r--r-- 5,432 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Licensed under BSD license.  See LICENCE.TXT  
 *
 * Produced by:	Jeff Lait
 *
 *      	7DRL Development
 *
 * NAME:        ptrlist.cpp ( Utility Library, C++ )
 *
 * COMMENTS:
 * 	Implementation of the ptrlist functions
 */

#include <string.h>
#include "assert.h"
#include "ptrlist.h"

template <typename PTR>
PTRLIST<PTR>::PTRLIST()
{
    myEntries = 0;
    mySize = 0;
    myList = 0;
    myStartPos = 0;
}

template <typename PTR>
PTRLIST<PTR>::~PTRLIST()
{
    delete [] myList;
}

template <typename PTR>
PTRLIST<PTR>::PTRLIST(const PTRLIST<PTR> &ref)
{
    myList = 0;
    *this = ref;
}

template <typename PTR>
PTRLIST<PTR> &
PTRLIST<PTR>::operator=(const PTRLIST<PTR> &ref)
{
    int			i;

    if (this == &ref)
	return *this;
    delete [] myList;
    myList = new PTR[ref.myEntries];
    mySize = ref.myEntries;
    myEntries = ref.myEntries;
    // Reset our list.
    myStartPos = 0;

    for (i = 0; i < myEntries; i++)
    {
	set(i, ref(i));
    }

    return *this;
}

template <typename PTR>
void
PTRLIST<PTR>::append(PTR item)
{
    if (myEntries + myStartPos >= mySize)
    {
	// Need to expand the extra stack.
	PTR		*newlist;

	mySize = mySize * 2 + 10;
	newlist = new PTR[mySize];
	if (myList)
	{
	    for (int i = 0; i < myEntries; i++)
		newlist[i] = myList[i+myStartPos];
	}
	delete [] myList;
	myList = newlist;
	// Take this chance to reset our leading edge.
	myStartPos = 0;
    }
    
    myList[myEntries+myStartPos] = item;

    myEntries++;
}

template <typename PTR>
void
PTRLIST<PTR>::append(const PTRLIST<PTR> &list)
{
    int		i;

    for (i = 0; i < list.entries(); i++)
	append(list(i));
}

template <typename PTR>
void
PTRLIST<PTR>::resize(int size)
{
    while (myEntries < size)
	append((PTR) 0);

    myEntries = size;
}

template <typename PTR>
void
PTRLIST<PTR>::set(int idx, PTR item)
{
    UT_ASSERT(idx < entries());
    UT_ASSERT(idx >= 0);
    if (idx < 0)
	return;
    if (idx >= entries())
	return;

    myList[idx + myStartPos] = item;
}

template <typename PTR>
void
PTRLIST<PTR>::insert(int idx, PTR item)
{
    int		offset;

    // Deal with simple case.
    if (idx == entries())
    {
	append(item);
	return;
    }

    UT_ASSERT(idx < entries());
    UT_ASSERT(idx >= 0);

    // Duplicate end.
    append(top());
    // Now make room
    for (offset = entries()-1; offset > idx; offset--)
    {
	set(offset, (*this)(offset-1));
    }
    set(idx, item);
}

template <typename PTR>
PTR
PTRLIST<PTR>::operator()(int idx) const
{
    UT_ASSERT(idx >= 0 && idx < myEntries);

    return myList[idx + myStartPos];
}


template <typename PTR>
int
PTRLIST<PTR>::entries() const
{
    return myEntries;
}

template <typename PTR>
void
PTRLIST<PTR>::clear()
{
    myEntries = 0;
    // Likewise, might as well reset our offset
    myStartPos = 0;
}

template <typename PTR>
void
PTRLIST<PTR>::constant(PTR val)
{
    int		i;

    for (i = 0; i < myEntries; i++)
	set(i, val);
}

template <typename PTR>
void
PTRLIST<PTR>::reverse()
{
    PTR		 tmp;
    int		 i, n, r;

    n = entries();
    for (i = 0; i < n / 2; i++)
    {
	// Reversed entry.
	r = n - 1 - i;
	tmp = (*this)(i);
	set(i, (*this)(r));
	set(r, tmp);
    }
}

template <typename PTR>
void
PTRLIST<PTR>::removePtr(PTR item)
{
    int		s, d, n;

    n = entries();
    s = d = 0;
    while (s < n)
    {
	if ((*this)(s) == item)
	{
	    // Don't copy this value.
	}
	else
	{
	    // Copy this value.
	    if (s != d)
		set(d, (*this)(s));
	    d++;
	}
	// Go to next value.
	s++;
    }
    // Final n is d.
    myEntries = d;
}

template <typename PTR>
void
PTRLIST<PTR>::removeAt(int idx)
{
    int		s, n;

    UT_ASSERT(idx >= 0);
    UT_ASSERT(idx < entries());

    n = entries();
    for (s = idx; s < n-1; s++)
    {
	set(s, (*this)(s+1));
    }
    myEntries--;
}

template <typename PTR>
PTR
PTRLIST<PTR>::pop()
{
    int		n;
    PTR		result;
    
    n = entries();
    if (!n)
    {
	// We should do an inplace new on result here so it
	// works with all types.
	// Inplace newis buggy for enums on AMD64, however, so I'll
	// play it safe.
	result = 0;
	return result;
    }
    
    result = (*this)(n-1);
    myEntries = n-1;
    return result;
}

template <typename PTR>
void
PTRLIST<PTR>::swapEntries(int i1, int i2)
{
    if (i1 == i2)
	return;

    PTR		tmp;

    tmp = (*this)(i1);
    set(i1, (*this)(i2));
    set(i2, tmp);
}

template <typename PTR>
PTR
PTRLIST<PTR>::removeFirst()
{
    int		n;
    PTR		result;
    
    n = entries();
    if (!n)
    {
	// We should do an inplace new on result here so it
	// works with all types.
	// Inplace newis buggy for enums on AMD64, however, so I'll
	// play it safe.
	return (PTR) 0;
    }
    
    result = (*this)(0);

    // A little bit more efficient than moving.
    // We wait until the next resize to bother with clearing up
    // since at that resize we have to move anyways.
    myStartPos++;
    myEntries--;
    return result;
}

template <typename PTR>
int
PTRLIST<PTR>::find(PTR item) const
{
    int		i, n;

    n = entries();
    for (i = 0; i < n; i++)
    {
	if ((*this)(i) == item)
	    return i;
    }
    return -1;
}

template <typename PTR>
void
PTRLIST<PTR>::collapse()
{
    int			i, j, n;

    n = entries();

    for (i = j = 0; i < n; i++)
    {
	if ((*this)(i))
	{
	    // Valid...
	    if (i != j)
		set(j, (*this)(i));
	    j++;
	}
	else
	{
	    // Do not copy over, do not inc j.
	}
    }

    myEntries = j;
}