Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
6.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #ifndef ACTIVEITEMLIST_H
  14. #define ACTIVEITEMLIST_H
  15. #pragma once
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include <tier0/memdbgon.h>
  18. //=============================================================================
  19. typedef int ITEM_HANDLE;
  20. //=============================================================================
  21. template <class T> class ActiveItemList
  22. {
  23. public:
  24. ActiveItemList();
  25. ActiveItemList( int size );
  26. void SetSize( int size );
  27. int GetSize( void );
  28. int GetNumberOfItems( void );
  29. T* GetFirstItem( void );
  30. T* GetNextItem( void );
  31. ITEM_HANDLE GetEmptyItemHandle( void );
  32. T* GetItem( ITEM_HANDLE handle );
  33. void RemoveItem( ITEM_HANDLE handle );
  34. void SetActiveItem( ITEM_HANDLE handle );
  35. T* GetActiveItem( void );
  36. void Free( void );
  37. protected:
  38. int m_NumItems; // the number of items in the list
  39. int m_ActiveItem; // the active item index
  40. int m_CurrentItem;
  41. int m_ListSize; // size of the list
  42. T *m_pList; // the active item list
  43. bool *m_pEmptyList; // keep an empty list
  44. };
  45. //=============================================================================
  46. //-----------------------------------------------------------------------------
  47. //-----------------------------------------------------------------------------
  48. template <class T> ActiveItemList<T>::ActiveItemList()
  49. {
  50. m_NumItems = 0;
  51. m_ActiveItem = -1;
  52. m_CurrentItem = -1;
  53. m_ListSize = 0;
  54. m_pList = NULL;
  55. m_pEmptyList = NULL;
  56. }
  57. //-----------------------------------------------------------------------------
  58. //-----------------------------------------------------------------------------
  59. template <class T> ActiveItemList<T>::ActiveItemList( int size )
  60. {
  61. int i; // loop counter
  62. // set the size of the list
  63. m_ListSize = size;
  64. //
  65. // allocate memory for the list
  66. //
  67. if( !( m_pList = new T[size] ) )
  68. return;
  69. if( !( m_pEmptyList = new bool[size] ) )
  70. return;
  71. //
  72. // initialize the active item list
  73. //
  74. m_NumItems = 0;
  75. m_ActiveItem = -1;
  76. m_CurrentItem = -1;
  77. for( i = 0; i < size; i++ )
  78. m_pEmptyList[i] = true;
  79. }
  80. //-----------------------------------------------------------------------------
  81. //-----------------------------------------------------------------------------
  82. template <class T> void ActiveItemList<T>::SetSize( int size )
  83. {
  84. int i; // loop counter
  85. // set the size of the list
  86. m_ListSize = size;
  87. //
  88. // allocate memory for the list
  89. //
  90. if( !( m_pList = new T[size] ) )
  91. return;
  92. if( !( m_pEmptyList = new bool[size] ) )
  93. return;
  94. //
  95. // initialize the active item list
  96. //
  97. m_NumItems = 0;
  98. m_ActiveItem = -1;
  99. m_CurrentItem = -1;
  100. for( i = 0; i < size; i++ )
  101. m_pEmptyList[i] = true;
  102. }
  103. //-----------------------------------------------------------------------------
  104. //-----------------------------------------------------------------------------
  105. template <class T> int ActiveItemList<T>::GetSize( void )
  106. {
  107. return m_ListSize;
  108. }
  109. //-----------------------------------------------------------------------------
  110. //-----------------------------------------------------------------------------
  111. template <class T> int ActiveItemList<T>::GetNumberOfItems( void )
  112. {
  113. return m_NumItems;
  114. }
  115. //-----------------------------------------------------------------------------
  116. //-----------------------------------------------------------------------------
  117. template <class T> T* ActiveItemList<T>::GetFirstItem( void )
  118. {
  119. int i; // loop counter
  120. // reset current item index
  121. m_CurrentItem = -1;
  122. //
  123. // find the first item in the list
  124. //
  125. for( i = 0; i < m_ListSize; i++ )
  126. {
  127. if( !m_pEmptyList[i] )
  128. {
  129. m_CurrentItem = i;
  130. return &m_pList[i];
  131. }
  132. }
  133. // no items found
  134. return NULL;
  135. }
  136. //-----------------------------------------------------------------------------
  137. //-----------------------------------------------------------------------------
  138. template <class T> T* ActiveItemList<T>::GetNextItem( void )
  139. {
  140. int i; // loop counter
  141. //
  142. // find the next item in the list
  143. //
  144. for( i = m_CurrentItem + 1; i < m_ListSize; i++ )
  145. {
  146. if( !m_pEmptyList[i] )
  147. {
  148. m_CurrentItem = i;
  149. return &m_pList[i];
  150. }
  151. }
  152. // no more items found
  153. return NULL;
  154. }
  155. //-----------------------------------------------------------------------------
  156. //-----------------------------------------------------------------------------
  157. template <class T> ITEM_HANDLE ActiveItemList<T>::GetEmptyItemHandle( void )
  158. {
  159. int i; // loop counter
  160. //
  161. // find an empty item slot and return the handle
  162. //
  163. for( i = 0; i < m_ListSize; i++ )
  164. {
  165. if( m_pEmptyList[i] )
  166. {
  167. m_pEmptyList[i] = false;
  168. m_NumItems++;
  169. return i;
  170. }
  171. }
  172. // no empty item slot
  173. return -1;
  174. }
  175. //-----------------------------------------------------------------------------
  176. //-----------------------------------------------------------------------------
  177. template <class T> T* ActiveItemList<T>::GetItem( ITEM_HANDLE handle )
  178. {
  179. return &m_pList[handle];
  180. }
  181. //-----------------------------------------------------------------------------
  182. //-----------------------------------------------------------------------------
  183. template <class T> void ActiveItemList<T>::RemoveItem( ITEM_HANDLE handle )
  184. {
  185. //
  186. // set the item to empty and decrement the number of items in list
  187. //
  188. m_pEmptyList[handle] = true;
  189. m_NumItems--;
  190. }
  191. //-----------------------------------------------------------------------------
  192. //-----------------------------------------------------------------------------
  193. template <class T> void ActiveItemList<T>::SetActiveItem( ITEM_HANDLE handle )
  194. {
  195. // set the active item
  196. m_ActiveItem = handle;
  197. }
  198. //-----------------------------------------------------------------------------
  199. //-----------------------------------------------------------------------------
  200. template <class T> T* ActiveItemList<T>::GetActiveItem( void )
  201. {
  202. if( m_ActiveItem == -1 )
  203. return NULL;
  204. return &m_pList[m_ActiveItem];
  205. }
  206. //-----------------------------------------------------------------------------
  207. //-----------------------------------------------------------------------------
  208. template <class T> void ActiveItemList<T>::Free( void )
  209. {
  210. //
  211. // clean up lists
  212. //
  213. if( m_pList )
  214. delete [] m_pList;
  215. if( m_pEmptyList )
  216. delete [] m_pEmptyList;
  217. }
  218. #endif // ACTIVEITEMLIST_H