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
|
#include "ptrlist.h"
#include <bfc/bfc_assert.h>
// if this symbol is enabled, do sanity checking in a bunch of methods
//#define DO_VERIFY_PTRLIST
#if defined(ASSERTS_ENABLED) && defined(DO_VERIFY_PTRLIST)
#define VERIFY_PTRLIST verify()
#else
#define VERIFY_PTRLIST
#endif
#ifdef _DEBUG
int ptrlist_totalnitems = 0;
#endif
PtrListRoot::PtrListRoot( int initial_size )
{
nitems = nslots = 0;
items = NULL;
//FG>why? we might as well save initial_size*4 bytes if nobody inserts anything in the list, no?
//BU No. initial_size defaults to 0 and therefore no memory is actually
//allocated in a typical construction. But, sometimes you want to start with
//a specific size, generally when you know the exact size you want,
//so that you can avoid REALLOCs when you initially populate the list.
//FG>Ah, makes sense.
setMinimumSize( initial_size );
}
PtrListRoot::PtrListRoot( const PtrListRoot *from )
{
nitems = nslots = 0;
items = NULL;
copyFrom( from );
}
PtrListRoot::~PtrListRoot()
{
removeAll();
}
void PtrListRoot::copyFrom( const PtrListRoot *from )
{
ASSERT( from != NULL );
removeAll();
appendFrom( from );
VERIFY_PTRLIST;
}
void PtrListRoot::appendFrom( const PtrListRoot *from )
{
ASSERT( from != NULL );
int n = from->getNumItems();
if ( n <= 0 )
return;
void **mem = from->getItemList();
ASSERT( mem != NULL ); // can't be NULL if getNumItems() >= 1
setMinimumSize( getNumItems() + n );
MEMCPY( items + getNumItems(), from->getItemList(), sizeof( void * ) * n );
nitems += n;
#ifdef _DEBUG
ptrlist_totalnitems += n;
#endif
VERIFY_PTRLIST;
}
void PtrListRoot::setMinimumSize( int _nslots )
{
ASSERT( _nslots >= 0 );
#ifdef DECREASE_PTRLISTS_SIZE
if ( _nslots + 2 * DEF_PTRLIST_INCREMENT < nslots )
{
nslots = ( ( _nslots / DEF_PTRLIST_INCREMENT ) + 2 ) * DEF_PTRLIST_INCREMENT;
items = (void **) REALLOC( items, sizeof( void * ) * nslots );
}
#endif
if ( _nslots == 0 || _nslots <= nslots )
return;
nslots = _nslots;
if ( items )
items = (void **) REALLOC( items, sizeof( void * ) * nslots );
else
items = (void **) MALLOC( sizeof( void * ) * nslots );
VERIFY_PTRLIST;
}
int PtrListRoot::getNumItems() const
{
return nitems;
}
void *PtrListRoot::enumItem( int n ) const
{
if ( items == NULL || n < 0 || n >= nitems )
return NULL;
return items[ n ];
}
void *PtrListRoot::addItem( void *item, int pos, int increment )
{
ASSERT( increment > 0 );
ASSERTPR( item != NULL, "can't put NULLs into ptrlists" );
ASSERT( nitems <= nslots );
#ifdef DECREASE_PTRLISTS_SIZE
// expand or shrink as necessary
if ( items == NULL || nslots == nitems )
setMinimumSize( nslots + increment );
else
setMinimumSize( nitems );
#else
// expand if necessary
if ( items == NULL || nslots == nitems )
setMinimumSize( nslots + increment );
#endif
items[ nitems++ ] = item;
if ( pos != PTRLIST_POS_LAST )
moveItem( nitems - 1, pos );
VERIFY_PTRLIST;
#ifdef _DEBUG
ptrlist_totalnitems++;
#endif
return item;
}
// replace what's in the slot with the new value
void *PtrListRoot::setItem( void *item, int pos )
{
ASSERT( nitems >= pos );
items[ pos ] = item;
VERIFY_PTRLIST;
return item;
}
void PtrListRoot::reverse()
{
if ( nitems <= 1 )
return;
MemBlock<void *> block( nitems, items );
void **blockmem = block.getMemory();
for ( int i = 0, j = nitems - 1; j >= 0; i++, j-- )
{
items[ i ] = blockmem[ j ];
}
}
void PtrListRoot::moveItem( int from, int to )
{
if ( from == to )
return;
void *ptr = items[ from ];
if ( nitems > from + 1 ) // if moving from the end, there is nothing to shift behind our src position
{
//IMPORTANT note for future ports: This assumes MEMCPY accepts overlapping segments.
MEMCPY( &items[ from ], &items[ from + 1 ], ( nitems - from ) * sizeof( void * ) );
}
if ( to > from )
to--;
if ( nitems > to ) // if moving to the end, there is nothing to shift behind our target position
MEMCPY( &items[ to + 1 ], &items[ to ], ( nitems - to - 1 ) * sizeof( void * ) );
items[ to ] = ptr;
VERIFY_PTRLIST;
}
int PtrListRoot::removeItem( void *item )
{
int c = 0;
if ( item == NULL || items == NULL || nitems <= 0 )
return 0;
// count occurences of item in items, remember the first one
void **p = items;
int first = -1;
for ( int i = 0; i < nitems; i++, p++ )
{
if ( *p == item )
{
if ( c++ == 0 )
first = i;
}
}
// if we found one, remove it
if ( c > 0 )
{
removeByPos( first ); // delByPos is faaast
c--; // decrease count
}
VERIFY_PTRLIST;
return c; // returns how many occurences of this item left
}
void PtrListRoot::removeEveryItem( void *item )
{
while ( removeItem( item ) );
VERIFY_PTRLIST;
}
void PtrListRoot::removeByPos( int pos )
{
if ( pos < 0 || pos >= nitems )
return; //JC
--nitems;
#ifdef _DEBUG
ptrlist_totalnitems--;
#endif
if ( pos == nitems )
return; // last one? nothing to copy over
MEMCPY( items + pos, items + ( pos + 1 ), sizeof( void * ) * ( nitems - pos ) ); // CT:not (nitems-(pos+1)) as nitems has been decremented earlier
#ifdef DECREASE_PTRLISTS_SIZE
// shrink if necessary
setMinimumSize( nitems );
#endif
VERIFY_PTRLIST;
}
void PtrListRoot::removeLastItem()
{
if ( nitems == 0 || items == NULL ) return;
nitems--; // hee hee
#ifdef _DEBUG
ptrlist_totalnitems--;
#endif
#ifdef DECREASE_PTRLISTS_SIZE
// shrink if necessary
setMinimumSize( nitems );
#endif
VERIFY_PTRLIST;
}
void PtrListRoot::removeAll()
{
FREE( items ); items = NULL;
#ifdef _DEBUG
ptrlist_totalnitems -= nitems;
#endif
#ifdef DECREASE_PTRLISTS_SIZE
// shrink if necessary
setMinimumSize( nitems );
#endif
nitems = 0;
nslots = 0;
VERIFY_PTRLIST;
}
void PtrListRoot::freeAll()
{
for ( int i = 0; i < nitems; i++ ) //JC
if ( items ) FREE( items[ i ] );
removeAll();
VERIFY_PTRLIST;
}
int PtrListRoot::searchItem( void *item ) const
{
for ( int i = 0; i < nitems; i++ )
if ( items[ i ] == item )
return i;
return -1;
}
void PtrListRoot::verify()
{
#ifdef DO_VERIFY_PTRLIST
ASSERT( nitems >= 0 );
ASSERT( nslots >= 0 );
ASSERT( nitems <= nslots );
#endif
}
#if 0
int PtrListRoot::bsearchItem( void *item ) const
{
// do binary search
if ( nitems == 0 || items == NULL ) return -1;
int bot = 0, top = nitems - 1, mid;
for ( int c = 0; c < nitems + 16; c++ )
{
if ( bot > top ) return -1;
mid = ( bot + top ) / 2;
if ( item == items[ mid ] ) return mid;
if ( item < items[ mid ] ) top = mid - 1;
else bot = mid + 1;
}
ASSERTPR( 0, "binary search fucked up" );
return -1;
}
#endif
void PtrListRoot::purge()
{
ASSERT( nitems == 0 );
if ( items != NULL )
{
FREE( items );
items = NULL;
}
}
|