StaticList.h 14 KB

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