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.

293 lines
7.3 KiB

  1. //====== Copyright (c) 1996-2005, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. // A growable array class that maintains a free list and keeps elements
  8. // in the same location
  9. //=============================================================================//
  10. #ifndef UTLARRAY_H
  11. #define UTLARRAY_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. #include "tier0/platform.h"
  16. #include "tier0/dbg.h"
  17. #include "vstdlib/random.h"
  18. #define FOR_EACH_ARRAY( vecName, iteratorName ) \
  19. for ( int iteratorName = 0; (vecName).IsUtlArray && iteratorName < (vecName).Count(); iteratorName++ )
  20. #define FOR_EACH_ARRAY_BACK( vecName, iteratorName ) \
  21. for ( int iteratorName = (vecName).Count()-1; (vecName).IsUtlArray && iteratorName >= 0; iteratorName-- )
  22. // utlarray derives from this so we can do the type check above
  23. struct base_array_t
  24. {
  25. public:
  26. enum { IsUtlArray = true }; // Used to match this at compiletime
  27. };
  28. //-----------------------------------------------------------------------------
  29. template< class T, size_t MAX_SIZE >
  30. class CUtlArray : public base_array_t
  31. {
  32. public:
  33. typedef T ElemType_t;
  34. CUtlArray();
  35. CUtlArray( T* pMemory, size_t count );
  36. ~CUtlArray();
  37. CUtlArray<T, MAX_SIZE>& operator=( const CUtlArray<T, MAX_SIZE> &other );
  38. CUtlArray( CUtlArray const& vec );
  39. // element access
  40. T& operator[]( int i );
  41. const T& operator[]( int i ) const;
  42. T& Element( int i );
  43. const T& Element( int i ) const;
  44. T& Random();
  45. const T& Random() const;
  46. T* Base();
  47. const T* Base() const;
  48. // Returns the number of elements in the array, NumAllocated() is included for consistency with UtlVector
  49. int Count() const;
  50. int NumAllocated() const;
  51. // Is element index valid?
  52. bool IsValidIndex( int i ) const;
  53. static int InvalidIndex();
  54. void CopyArray( const T *pArray, size_t count );
  55. void Swap( CUtlArray< T, MAX_SIZE > &vec );
  56. // Finds an element (element needs operator== defined)
  57. int Find( const T& src ) const;
  58. void FillWithValue( const T& src );
  59. bool HasElement( const T& src ) const;
  60. // calls delete on each element in it.
  61. void DeleteElements();
  62. void Sort( int (__cdecl *pfnCompare)(const T *, const T *) );
  63. protected:
  64. T m_Memory[ MAX_SIZE ];
  65. };
  66. //-----------------------------------------------------------------------------
  67. // constructor, destructor
  68. //-----------------------------------------------------------------------------
  69. template< typename T, size_t MAX_SIZE >
  70. inline CUtlArray<T, MAX_SIZE>::CUtlArray()
  71. {
  72. }
  73. template< typename T, size_t MAX_SIZE >
  74. inline CUtlArray<T, MAX_SIZE>::CUtlArray( T* pMemory, size_t count )
  75. {
  76. CopyArray( pMemory, count );
  77. }
  78. template< typename T, size_t MAX_SIZE >
  79. inline CUtlArray<T, MAX_SIZE>::~CUtlArray()
  80. {
  81. }
  82. template< typename T, size_t MAX_SIZE >
  83. inline CUtlArray<T, MAX_SIZE>& CUtlArray<T, MAX_SIZE>::operator=( const CUtlArray<T, MAX_SIZE> &other )
  84. {
  85. if ( this != &other )
  86. {
  87. for ( size_t n = 0; n < MAX_SIZE; ++n )
  88. {
  89. m_Memory[n] = other.m_Memory[n];
  90. }
  91. }
  92. return *this;
  93. }
  94. template< typename T, size_t MAX_SIZE >
  95. inline CUtlArray<T, MAX_SIZE>::CUtlArray( CUtlArray const& vec )
  96. {
  97. for ( size_t n = 0; n < MAX_SIZE; ++n )
  98. {
  99. m_Memory[n] = vec.m_Memory[n];
  100. }
  101. }
  102. template< typename T, size_t MAX_SIZE >
  103. inline T *CUtlArray<T, MAX_SIZE>::Base()
  104. {
  105. return &m_Memory[0];
  106. }
  107. template< typename T, size_t MAX_SIZE >
  108. inline const T *CUtlArray<T, MAX_SIZE>::Base() const
  109. {
  110. return &m_Memory[0];
  111. }
  112. //-----------------------------------------------------------------------------
  113. // element access
  114. //-----------------------------------------------------------------------------
  115. template< typename T, size_t MAX_SIZE >
  116. inline T& CUtlArray<T, MAX_SIZE>::operator[]( int i )
  117. {
  118. Assert( IsValidIndex( i ) );
  119. return m_Memory[ i ];
  120. }
  121. template< typename T, size_t MAX_SIZE >
  122. inline const T& CUtlArray<T, MAX_SIZE>::operator[]( int i ) const
  123. {
  124. Assert( IsValidIndex( i ) );
  125. return m_Memory[ i ];
  126. }
  127. template< typename T, size_t MAX_SIZE >
  128. inline T& CUtlArray<T, MAX_SIZE>::Element( int i )
  129. {
  130. Assert( IsValidIndex( i ) );
  131. return m_Memory[ i ];
  132. }
  133. template< typename T, size_t MAX_SIZE >
  134. inline const T& CUtlArray<T, MAX_SIZE>::Element( int i ) const
  135. {
  136. Assert( IsValidIndex( i ) );
  137. return m_Memory[ i ];
  138. }
  139. template< typename T, size_t MAX_SIZE >
  140. inline T& CUtlArray<T, MAX_SIZE>::Random()
  141. {
  142. Assert( MAX_SIZE > 0 );
  143. return m_Memory[ RandomInt( 0, MAX_SIZE - 1 ) ];
  144. }
  145. template< typename T, size_t MAX_SIZE >
  146. inline const T& CUtlArray<T, MAX_SIZE>::Random() const
  147. {
  148. Assert( MAX_SIZE > 0 );
  149. return m_Memory[ RandomInt( 0, MAX_SIZE - 1 ) ];
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Count
  153. //-----------------------------------------------------------------------------
  154. template< typename T, size_t MAX_SIZE >
  155. inline int CUtlArray<T, MAX_SIZE>::Count() const
  156. {
  157. return (int)MAX_SIZE;
  158. }
  159. //-----------------------------------------------------------------------------
  160. template< typename T, size_t MAX_SIZE >
  161. inline int CUtlArray<T, MAX_SIZE>::NumAllocated() const
  162. {
  163. return (int)MAX_SIZE;
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Is element index valid?
  167. //-----------------------------------------------------------------------------
  168. template< typename T, size_t MAX_SIZE >
  169. inline bool CUtlArray<T, MAX_SIZE>::IsValidIndex( int i ) const
  170. {
  171. return (i >= 0) && (i < MAX_SIZE);
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Returns in invalid index
  175. //-----------------------------------------------------------------------------
  176. template< typename T, size_t MAX_SIZE >
  177. inline int CUtlArray<T, MAX_SIZE>::InvalidIndex()
  178. {
  179. return -1;
  180. }
  181. //-----------------------------------------------------------------------------
  182. // Sorts the vector
  183. //-----------------------------------------------------------------------------
  184. template< typename T, size_t MAX_SIZE >
  185. void CUtlArray<T, MAX_SIZE>::Sort( int (__cdecl *pfnCompare)(const T *, const T *) )
  186. {
  187. typedef int (__cdecl *QSortCompareFunc_t)(const void *, const void *);
  188. if ( Count() <= 1 )
  189. return;
  190. qsort( Base(), Count(), sizeof(T), (QSortCompareFunc_t)(pfnCompare) );
  191. }
  192. template< typename T, size_t MAX_SIZE >
  193. void CUtlArray<T, MAX_SIZE>::CopyArray( const T *pArray, size_t count )
  194. {
  195. Assert( count < MAX_SIZE );
  196. for ( size_t n = 0; n < count; ++n )
  197. {
  198. m_Memory[n] = pArray[n];
  199. }
  200. }
  201. template< typename T, size_t MAX_SIZE >
  202. void CUtlArray<T, MAX_SIZE>::Swap( CUtlArray< T, MAX_SIZE > &vec )
  203. {
  204. for ( size_t n = 0; n < MAX_SIZE; ++n )
  205. {
  206. V_swap( m_Memory[n], vec.m_Memory[n] );
  207. }
  208. }
  209. //-----------------------------------------------------------------------------
  210. // Finds an element (element needs operator== defined)
  211. //-----------------------------------------------------------------------------
  212. template< typename T, size_t MAX_SIZE >
  213. int CUtlArray<T, MAX_SIZE>::Find( const T& src ) const
  214. {
  215. for ( int i = 0; i < Count(); ++i )
  216. {
  217. if (Element(i) == src)
  218. return i;
  219. }
  220. return -1;
  221. }
  222. template< typename T, size_t MAX_SIZE >
  223. void CUtlArray<T, MAX_SIZE>::FillWithValue( const T& src )
  224. {
  225. for ( int i = 0; i < Count(); i++ )
  226. {
  227. Element(i) = src;
  228. }
  229. }
  230. template< typename T, size_t MAX_SIZE >
  231. bool CUtlArray<T, MAX_SIZE>::HasElement( const T& src ) const
  232. {
  233. return ( Find(src) >= 0 );
  234. }
  235. template< typename T, size_t MAX_SIZE >
  236. inline void CUtlArray<T, MAX_SIZE>::DeleteElements()
  237. {
  238. for( int i=0; i < MAX_SIZE; i++ )
  239. {
  240. delete Element(i);
  241. }
  242. }
  243. #endif // UTLARRAY_H