doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StaticList.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __STATICLIST_H__
30 #define __STATICLIST_H__
31 
32 /*
33 ===============================================================================
34 
35  Static list template
36  A non-growing, memset-able list using no memory allocation.
37 
38 ===============================================================================
39 */
40 
41 template<class type,int size>
42 class idStaticList {
43 public:
44 
45  idStaticList();
46  idStaticList( const idStaticList<type,size> &other );
48 
49  void Clear( void ); // marks the list as empty. does not deallocate or intialize data.
50  int Num( void ) const; // returns number of elements in list
51  int Max( void ) const; // returns the maximum number of elements in the list
52  void SetNum( int newnum ); // set number of elements in list
53 
54  size_t Allocated( void ) const; // returns total size of allocated memory
55  size_t Size( void ) const; // returns total size of allocated memory including size of list type
56  size_t MemoryUsed( void ) const; // returns size of the used elements in the list
57 
58  const type & operator[]( int index ) const;
59  type & operator[]( int index );
60 
61  type * Ptr( void ); // returns a pointer to the list
62  const type * Ptr( void ) const; // returns a pointer to the list
63  type * Alloc( void ); // returns reference to a new data element at the end of the list. returns NULL when full.
64  int Append( const type & obj ); // append element
65  int Append( const idStaticList<type,size> &other ); // append list
66  int AddUnique( const type & obj ); // add unique element
67  int Insert( const type & obj, int index ); // insert the element at the given index
68  int FindIndex( const type & obj ) const; // find the index for the given element
69  type * Find( type const & obj ) const; // find pointer to the given element
70  int FindNull( void ) const; // find the index for the first NULL pointer in the list
71  int IndexOf( const type *obj ) const; // returns the index for the pointer to an element in the list
72  bool RemoveIndex( int index ); // remove the element at the given index
73  bool Remove( const type & obj ); // remove the element
74  void Swap( idStaticList<type,size> &other ); // swap the contents of the lists
75  void DeleteContents( bool clear ); // delete the contents of the list
76 
77 private:
78  int num;
80 };
81 
82 /*
83 ================
84 idStaticList<type,size>::idStaticList()
85 ================
86 */
87 template<class type,int size>
89  num = 0;
90 }
91 
92 /*
93 ================
94 idStaticList<type,size>::idStaticList( const idStaticList<type,size> &other )
95 ================
96 */
97 template<class type,int size>
99  *this = other;
100 }
101 
102 /*
103 ================
104 idStaticList<type,size>::~idStaticList<type,size>
105 ================
106 */
107 template<class type,int size>
109 }
110 
111 /*
112 ================
113 idStaticList<type,size>::Clear
114 
115 Sets the number of elements in the list to 0. Assumes that type automatically handles freeing up memory.
116 ================
117 */
118 template<class type,int size>
119 ID_INLINE void idStaticList<type,size>::Clear( void ) {
120  num = 0;
121 }
122 
123 /*
124 ================
125 idStaticList<type,size>::DeleteContents
126 
127 Calls the destructor of all elements in the list. Conditionally frees up memory used by the list.
128 Note that this only works on lists containing pointers to objects and will cause a compiler error
129 if called with non-pointers. Since the list was not responsible for allocating the object, it has
130 no information on whether the object still exists or not, so care must be taken to ensure that
131 the pointers are still valid when this function is called. Function will set all pointers in the
132 list to NULL.
133 ================
134 */
135 template<class type,int size>
136 ID_INLINE void idStaticList<type,size>::DeleteContents( bool clear ) {
137  int i;
138 
139  for( i = 0; i < size; i++ ) {
140  delete list[ i ];
141  list[ i ] = NULL;
142  }
143 
144  if ( clear ) {
145  Clear();
146  } else {
147  memset( list, 0, sizeof( list ) );
148  }
149 }
150 
151 /*
152 ================
153 idStaticList<type,size>::Num
154 
155 Returns the number of elements currently contained in the list.
156 ================
157 */
158 template<class type,int size>
159 ID_INLINE int idStaticList<type,size>::Num( void ) const {
160  return num;
161 }
162 
163 /*
164 ================
165 idStaticList<type,size>::Num
166 
167 Returns the maximum number of elements in the list.
168 ================
169 */
170 template<class type,int size>
171 ID_INLINE int idStaticList<type,size>::Max( void ) const {
172  return size;
173 }
174 
175 /*
176 ================
177 idStaticList<type>::Allocated
178 ================
179 */
180 template<class type,int size>
181 ID_INLINE size_t idStaticList<type,size>::Allocated( void ) const {
182  return size * sizeof( type );
183 }
184 
185 /*
186 ================
187 idStaticList<type>::Size
188 ================
189 */
190 template<class type,int size>
191 ID_INLINE size_t idStaticList<type,size>::Size( void ) const {
192  return sizeof( idStaticList<type,size> ) + Allocated();
193 }
194 
195 /*
196 ================
197 idStaticList<type,size>::Num
198 ================
199 */
200 template<class type,int size>
201 ID_INLINE size_t idStaticList<type,size>::MemoryUsed( void ) const {
202  return num * sizeof( list[ 0 ] );
203 }
204 
205 /*
206 ================
207 idStaticList<type,size>::SetNum
208 
209 Set number of elements in list.
210 ================
211 */
212 template<class type,int size>
213 ID_INLINE void idStaticList<type,size>::SetNum( int newnum ) {
214  assert( newnum >= 0 );
215  assert( newnum <= size );
216  num = newnum;
217 }
218 
219 /*
220 ================
221 idStaticList<type,size>::operator[] const
222 
223 Access operator. Index must be within range or an assert will be issued in debug builds.
224 Release builds do no range checking.
225 ================
226 */
227 template<class type,int size>
228 ID_INLINE const type &idStaticList<type,size>::operator[]( int index ) const {
229  assert( index >= 0 );
230  assert( index < num );
231 
232  return list[ index ];
233 }
234 
235 /*
236 ================
237 idStaticList<type,size>::operator[]
238 
239 Access operator. Index must be within range or an assert will be issued in debug builds.
240 Release builds do no range checking.
241 ================
242 */
243 template<class type,int size>
245  assert( index >= 0 );
246  assert( index < num );
247 
248  return list[ index ];
249 }
250 
251 /*
252 ================
253 idStaticList<type,size>::Ptr
254 
255 Returns a pointer to the begining of the array. Useful for iterating through the list in loops.
256 
257 Note: may return NULL if the list is empty.
258 
259 FIXME: Create an iterator template for this kind of thing.
260 ================
261 */
262 template<class type,int size>
263 ID_INLINE type *idStaticList<type,size>::Ptr( void ) {
264  return &list[ 0 ];
265 }
266 
267 /*
268 ================
269 idStaticList<type,size>::Ptr
270 
271 Returns a pointer to the begining of the array. Useful for iterating through the list in loops.
272 
273 Note: may return NULL if the list is empty.
274 
275 FIXME: Create an iterator template for this kind of thing.
276 ================
277 */
278 template<class type,int size>
279 ID_INLINE const type *idStaticList<type,size>::Ptr( void ) const {
280  return &list[ 0 ];
281 }
282 
283 /*
284 ================
285 idStaticList<type,size>::Alloc
286 
287 Returns a pointer to a new data element at the end of the list.
288 ================
289 */
290 template<class type,int size>
292  if ( num >= size ) {
293  return NULL;
294  }
295 
296  return &list[ num++ ];
297 }
298 
299 /*
300 ================
301 idStaticList<type,size>::Append
302 
303 Increases the size of the list by one element and copies the supplied data into it.
304 
305 Returns the index of the new element, or -1 when list is full.
306 ================
307 */
308 template<class type,int size>
309 ID_INLINE int idStaticList<type,size>::Append( type const & obj ) {
310  assert( num < size );
311  if ( num < size ) {
312  list[ num ] = obj;
313  num++;
314  return num - 1;
315  }
316 
317  return -1;
318 }
319 
320 
321 /*
322 ================
323 idStaticList<type,size>::Insert
324 
325 Increases the size of the list by at leat one element if necessary
326 and inserts the supplied data into it.
327 
328 Returns the index of the new element, or -1 when list is full.
329 ================
330 */
331 template<class type,int size>
332 ID_INLINE int idStaticList<type,size>::Insert( type const & obj, int index ) {
333  int i;
334 
335  assert( num < size );
336  if ( num >= size ) {
337  return -1;
338  }
339 
340  assert( index >= 0 );
341  if ( index < 0 ) {
342  index = 0;
343  } else if ( index > num ) {
344  index = num;
345  }
346 
347  for( i = num; i > index; --i ) {
348  list[i] = list[i-1];
349  }
350 
351  num++;
352  list[index] = obj;
353  return index;
354 }
355 
356 /*
357 ================
358 idStaticList<type,size>::Append
359 
360 adds the other list to this one
361 
362 Returns the size of the new combined list
363 ================
364 */
365 template<class type,int size>
367  int i;
368  int n = other.Num();
369 
370  if ( num + n > size ) {
371  n = size - num;
372  }
373  for( i = 0; i < n; i++ ) {
374  list[i + num] = other.list[i];
375  }
376  num += n;
377  return Num();
378 }
379 
380 /*
381 ================
382 idStaticList<type,size>::AddUnique
383 
384 Adds the data to the list if it doesn't already exist. Returns the index of the data in the list.
385 ================
386 */
387 template<class type,int size>
388 ID_INLINE int idStaticList<type,size>::AddUnique( type const & obj ) {
389  int index;
390 
391  index = FindIndex( obj );
392  if ( index < 0 ) {
393  index = Append( obj );
394  }
395 
396  return index;
397 }
398 
399 /*
400 ================
401 idStaticList<type,size>::FindIndex
402 
403 Searches for the specified data in the list and returns it's index. Returns -1 if the data is not found.
404 ================
405 */
406 template<class type,int size>
407 ID_INLINE int idStaticList<type,size>::FindIndex( type const & obj ) const {
408  int i;
409 
410  for( i = 0; i < num; i++ ) {
411  if ( list[ i ] == obj ) {
412  return i;
413  }
414  }
415 
416  // Not found
417  return -1;
418 }
419 
420 /*
421 ================
422 idStaticList<type,size>::Find
423 
424 Searches for the specified data in the list and returns it's address. Returns NULL if the data is not found.
425 ================
426 */
427 template<class type,int size>
428 ID_INLINE type *idStaticList<type,size>::Find( type const & obj ) const {
429  int i;
430 
431  i = FindIndex( obj );
432  if ( i >= 0 ) {
433  return &list[ i ];
434  }
435 
436  return NULL;
437 }
438 
439 /*
440 ================
441 idStaticList<type,size>::FindNull
442 
443 Searches for a NULL pointer in the list. Returns -1 if NULL is not found.
444 
445 NOTE: This function can only be called on lists containing pointers. Calling it
446 on non-pointer lists will cause a compiler error.
447 ================
448 */
449 template<class type,int size>
450 ID_INLINE int idStaticList<type,size>::FindNull( void ) const {
451  int i;
452 
453  for( i = 0; i < num; i++ ) {
454  if ( list[ i ] == NULL ) {
455  return i;
456  }
457  }
458 
459  // Not found
460  return -1;
461 }
462 
463 /*
464 ================
465 idStaticList<type,size>::IndexOf
466 
467 Takes a pointer to an element in the list and returns the index of the element.
468 This is NOT a guarantee that the object is really in the list.
469 Function will assert in debug builds if pointer is outside the bounds of the list,
470 but remains silent in release builds.
471 ================
472 */
473 template<class type,int size>
474 ID_INLINE int idStaticList<type,size>::IndexOf( type const *objptr ) const {
475  int index;
476 
477  index = objptr - list;
478 
479  assert( index >= 0 );
480  assert( index < num );
481 
482  return index;
483 }
484 
485 /*
486 ================
487 idStaticList<type,size>::RemoveIndex
488 
489 Removes the element at the specified index and moves all data following the element down to fill in the gap.
490 The number of elements in the list is reduced by one. Returns false if the index is outside the bounds of the list.
491 Note that the element is not destroyed, so any memory used by it may not be freed until the destruction of the list.
492 ================
493 */
494 template<class type,int size>
496  int i;
497 
498  assert( index >= 0 );
499  assert( index < num );
500 
501  if ( ( index < 0 ) || ( index >= num ) ) {
502  return false;
503  }
504 
505  num--;
506  for( i = index; i < num; i++ ) {
507  list[ i ] = list[ i + 1 ];
508  }
509 
510  return true;
511 }
512 
513 /*
514 ================
515 idStaticList<type,size>::Remove
516 
517 Removes the element if it is found within the list and moves all data following the element down to fill in the gap.
518 The number of elements in the list is reduced by one. Returns false if the data is not found in the list. Note that
519 the element is not destroyed, so any memory used by it may not be freed until the destruction of the list.
520 ================
521 */
522 template<class type,int size>
523 ID_INLINE bool idStaticList<type,size>::Remove( type const & obj ) {
524  int index;
525 
526  index = FindIndex( obj );
527  if ( index >= 0 ) {
528  return RemoveIndex( index );
529  }
530 
531  return false;
532 }
533 
534 /*
535 ================
536 idStaticList<type,size>::Swap
537 
538 Swaps the contents of two lists
539 ================
540 */
541 template<class type,int size>
543  idStaticList<type,size> temp = *this;
544  *this = other;
545  other = temp;
546 }
547 
548 #endif /* !__STATICLIST_H__ */
type list[size]
Definition: StaticList.h:79
assert(prefInfo.fullscreenBtn)
void Swap(idStaticList< type, size > &other)
Definition: StaticList.h:542
GLenum GLsizei n
Definition: glext.h:3705
type * Alloc(void)
Definition: StaticList.h:291
~idStaticList(void)
Definition: StaticList.h:108
int Max(void) const
Definition: StaticList.h:171
size_t Size(void) const
Definition: StaticList.h:191
GLuint GLuint GLsizei GLenum type
Definition: glext.h:2845
bool Remove(const type &obj)
Definition: StaticList.h:523
int AddUnique(const type &obj)
Definition: StaticList.h:388
GLhandleARB obj
Definition: glext.h:3602
int i
Definition: process.py:33
GLuint GLuint num
Definition: glext.h:5390
GLuint index
Definition: glext.h:3476
void SetNum(int newnum)
Definition: StaticList.h:213
#define NULL
Definition: Lib.h:88
void Clear(void)
Definition: StaticList.h:119
size_t MemoryUsed(void) const
Definition: StaticList.h:201
int FindIndex(const type &obj) const
Definition: StaticList.h:407
const type & operator[](int index) const
Definition: StaticList.h:228
int Insert(const type &obj, int index)
Definition: StaticList.h:332
int IndexOf(const type *obj) const
Definition: StaticList.h:474
int Append(const type &obj)
Definition: StaticList.h:309
type * Ptr(void)
Definition: StaticList.h:263
GLsizeiptr size
Definition: glext.h:3112
type * Find(type const &obj) const
Definition: StaticList.h:428
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
void DeleteContents(bool clear)
Definition: StaticList.h:136
size_t Allocated(void) const
Definition: StaticList.h:181
int FindNull(void) const
Definition: StaticList.h:450
int Num(void) const
Definition: StaticList.h:159
bool RemoveIndex(int index)
Definition: StaticList.h:495