forked from jimdose/SiN_110_Source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.h
464 lines (400 loc) · 9.17 KB
/
container.h
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//-----------------------------------------------------------------------------
//
// $Logfile:: /Quake 2 Engine/Sin/code/game/container.h $
// $Revision:: 17 $
// $Author:: Jimdose $
// $Date:: 10/25/98 11:53p $
//
// Copyright (C) 1997 by Ritual Entertainment, Inc.
// All rights reserved.
//
// This source is may not be distributed and/or modified without
// expressly written permission by Ritual Entertainment, Inc.
//
// $Log:: /Quake 2 Engine/Sin/code/game/container.h $
//
// 17 10/25/98 11:53p Jimdose
// added EXPORT_TEMPLATE
//
// 16 10/07/98 11:45p Jimdose
// Added \n to dprintf's
// Made resize assert only when size was negative
//
// 15 9/21/98 6:01p Markd
// tried putting archiving back in, but took it out again, when I realized it
// wouldn't work because the class hierarchy hasn't been created yet.
//
// 14 9/21/98 4:21p Markd
// Put in archive functions and rewrote all archive routines
//
// 13 8/20/98 4:42p Jimdose
// Added Sort function.
//
// 12 7/09/98 1:40a Jimdose
// ClearObjectList now only reallocates the list if there are any objects in
// the list
//
// 11 5/25/98 2:29p Aldie
// Fixed issues with not loading game dll
//
// 10 3/23/98 1:33p Jimdose
// Revamped event and command system
//
// 9 3/04/98 1:43p Jimdose
// Changed IndexOfObject so that it did a comparison instead of a memcmp
//
// 8 3/02/98 5:27p Jimdose
// Changed Container to a template to make it more flexible
//
// 7 2/03/98 10:51a Jimdose
// Updated to work with Quake 2 engine
//
// 5 11/20/97 9:03p Jimdose
// Changed Container class to use entity # (int) instead of void *
//
// 4 11/07/97 6:37p Jimdose
// Added FreeObjectList to empty the list and free up the memory.
// Changed ClearObjectList to only empty the list, but keep the memory
// allocated.
// Changed all functions from being virtual to being exported by the DLL.
//
// 3 10/27/97 2:49p Jimdose
// Removed dependency on quakedef.h
//
// 2 9/26/97 6:13p Jimdose
// Added standard Ritual headers
//
// DESCRIPTION:
// Base class for a dynamic array. Allows adding, removing, index of,
// and finding of entries with specified value. Originally created for
// cataloging entities, but pointers to objects that may be removed at
// any time are bad to keep around, so only entity numbers should be
// used in the future.
//
#ifndef __CONTAINER_H__
#define __CONTAINER_H__
#include "g_local.h"
#include <stdlib.h>
template< class Type >
class EXPORT_FROM_DLL Container
{
private:
Type *objlist;
int numobjects;
int maxobjects;
public:
Container();
~Container<Type>();
void FreeObjectList( void );
void ClearObjectList( void );
int NumObjects( void );
void Resize( int maxelements );
void SetObjectAt( int index, Type& obj );
int AddObject( Type& obj );
int AddUniqueObject( Type& obj );
void AddObjectAt( int index, Type& obj );
int IndexOfObject( Type& obj );
qboolean ObjectInList( Type& obj );
Type& ObjectAt( int index );
Type *AddressOfObjectAt( int index );
void RemoveObjectAt( int index );
void RemoveObject( Type& obj );
void Sort( int ( __cdecl *compare )( const void *elem1, const void *elem2 ) );
// virtual void Archive( Archiver &arc );
// virtual void Unarchive( Archiver &arc );
};
/*
template< class Type >
EXPORT_FROM_DLL void Container<Type>::Archive
(
Archiver &arc
)
{
int i;
arc.WriteInteger( maxobjects );
arc.WriteInteger( numobjects );
for ( i = 0; i < numobjects; i++ )
{
arc.WriteRaw( objlist[ i ], sizeof( Type ) );
}
}
template< class Type >
EXPORT_FROM_DLL void Container<Type>::Unarchive
(
Archiver &arc
)
{
int i;
FreeObjectList();
maxobjects = arc.ReadInteger();
numobjects = arc.ReadInteger();
objlist = new Type[ maxobjects ];
for ( i = 0; i < numobjects; i++ )
{
arc.ReadRaw( &objlist[ i ], sizeof( Type ) );
}
}
*/
template< class Type >
Container<Type>::Container()
{
objlist = NULL;
FreeObjectList();
}
template< class Type >
Container<Type>::~Container<Type>()
{
FreeObjectList();
}
template< class Type >
EXPORT_FROM_DLL void Container<Type>::FreeObjectList
(
void
)
{
if ( objlist )
{
delete[] objlist;
}
objlist = NULL;
numobjects = 0;
maxobjects = 0;
}
template< class Type >
EXPORT_FROM_DLL void Container<Type>::ClearObjectList
(
void
)
{
// only delete the list if we have objects in it
if ( objlist && numobjects )
{
delete[] objlist;
objlist = new Type[ maxobjects ];
numobjects = 0;
}
}
template< class Type >
EXPORT_FROM_DLL int Container<Type>::NumObjects
(
void
)
{
return numobjects;
}
template< class Type >
EXPORT_FROM_DLL void Container<Type>::Resize
(
int maxelements
)
{
Type *temp;
int i;
assert( maxelements >= 0 );
if ( maxelements <= 0 )
{
FreeObjectList();
return;
}
if ( !objlist )
{
maxobjects = maxelements;
objlist = new Type[ maxobjects ];
}
else
{
temp = objlist;
maxobjects = maxelements;
if ( maxobjects < numobjects )
{
maxobjects = numobjects;
}
objlist = new Type[ maxobjects ];
for( i = 0; i < numobjects; i++ )
{
objlist[ i ] = temp[ i ];
}
delete[] temp;
}
}
template< class Type >
EXPORT_FROM_DLL void Container<Type>::SetObjectAt
(
int index,
Type& obj
)
{
if ( ( index <= 0 ) || ( index > numobjects ) )
{
gi.error( "Container::SetObjectAt : index out of range" );
}
objlist[ index - 1 ] = obj;
}
template< class Type >
EXPORT_FROM_DLL int Container<Type>::AddObject
(
Type& obj
)
{
if ( !objlist )
{
Resize( 10 );
}
if ( numobjects == maxobjects )
{
Resize( maxobjects * 2 );
}
objlist[ numobjects ] = obj;
numobjects++;
return numobjects;
}
template< class Type >
EXPORT_FROM_DLL int Container<Type>::AddUniqueObject
(
Type& obj
)
{
int index;
index = IndexOfObject( obj );
if ( !index )
index = AddObject( obj );
return index;
}
template< class Type >
EXPORT_FROM_DLL void Container<Type>::AddObjectAt
(
int index,
Type& obj
)
{
//
// this should only be used when reconstructing a list that has to be identical to the original
//
if ( index > maxobjects )
{
Resize( index );
}
if ( index > numobjects )
{
numobjects = index;
}
SetObjectAt( index, obj );
}
template< class Type >
EXPORT_FROM_DLL int Container<Type>::IndexOfObject
(
Type& obj
)
{
int i;
for( i = 0; i < numobjects; i++ )
{
if ( objlist[ i ] == obj )
{
return i + 1;
}
}
return 0;
}
template< class Type >
EXPORT_FROM_DLL qboolean Container<Type>::ObjectInList
(
Type& obj
)
{
if ( !IndexOfObject( obj ) )
{
return false;
}
return true;
}
template< class Type >
EXPORT_FROM_DLL Type& Container<Type>::ObjectAt
(
int index
)
{
if ( ( index <= 0 ) || ( index > numobjects ) )
{
gi.error( "Container::ObjectAt : index out of range" );
}
return objlist[ index - 1 ];
}
template< class Type >
EXPORT_FROM_DLL Type * Container<Type>::AddressOfObjectAt
(
int index
)
{
//
// this should only be used when reconstructing a list that has to be identical to the original
//
if ( index > maxobjects )
{
gi.error( "Container::AddressOfObjectAt : index is greater than maxobjects" );
}
if ( index > numobjects )
{
numobjects = index;
}
return &objlist[ index - 1 ];
}
template< class Type >
EXPORT_FROM_DLL void Container<Type>::RemoveObjectAt
(
int index
)
{
int i;
if ( !objlist )
{
gi.dprintf( "Container::RemoveObjectAt : Empty list\n" );
return;
}
if ( ( index <= 0 ) || ( index > numobjects ) )
{
gi.error( "Container::RemoveObjectAt : index out of range" );
return;
}
i = index - 1;
numobjects--;
for( i = index - 1; i < numobjects; i++ )
{
objlist[ i ] = objlist[ i + 1 ];
}
}
template< class Type >
EXPORT_FROM_DLL void Container<Type>::RemoveObject
(
Type& obj
)
{
int index;
index = IndexOfObject( obj );
if ( !index )
{
gi.dprintf( "Container::RemoveObject : Object not in list\n" );
return;
}
RemoveObjectAt( index );
}
template< class Type >
EXPORT_FROM_DLL void Container<Type>::Sort
(
int ( __cdecl *compare )( const void *elem1, const void *elem2 )
)
{
if ( !objlist )
{
gi.dprintf( "Container::RemoveObjectAt : Empty list\n" );
return;
}
qsort( ( void * )objlist, ( size_t )numobjects, sizeof( Type ), compare );
}
//
// Exported templated classes must be explicitly instantiated
//
#ifdef EXPORT_TEMPLATE
template class EXPORT_FROM_DLL Container<int>;
#endif
#endif /* container.h */