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.

246 lines
6.0 KiB

  1. //--------------------------------------------------------------------------------------------------
  2. // qhArray.inl
  3. //
  4. // Copyright(C) 2011 by D. Gregorius. All rights reserved.
  5. //--------------------------------------------------------------------------------------------------
  6. //--------------------------------------------------------------------------------------------------
  7. // qhArray
  8. //--------------------------------------------------------------------------------------------------
  9. template < typename T > inline
  10. qhArray< T >::qhArray( void )
  11. : mBegin( NULL )
  12. , mEnd( NULL )
  13. , mCapacity( NULL )
  14. {
  15. }
  16. //--------------------------------------------------------------------------------------------------
  17. template < typename T > inline
  18. qhArray< T >::~qhArray( void )
  19. {
  20. qhDestroy( mBegin, Size() );
  21. qhFree( mBegin );
  22. }
  23. //--------------------------------------------------------------------------------------------------
  24. template < typename T > inline
  25. int qhArray< T >::Capacity( void ) const
  26. {
  27. return int( mCapacity - mBegin );
  28. }
  29. //--------------------------------------------------------------------------------------------------
  30. template < typename T > inline
  31. int qhArray< T >::Size( void ) const
  32. {
  33. return int( mEnd - mBegin );
  34. }
  35. //--------------------------------------------------------------------------------------------------
  36. template < typename T > inline
  37. bool qhArray< T >::Empty( void ) const
  38. {
  39. return mEnd == mBegin;
  40. }
  41. //--------------------------------------------------------------------------------------------------
  42. template < typename T > inline
  43. void qhArray< T >::Clear( void )
  44. {
  45. qhDestroy( mBegin, Size() );
  46. mEnd = mBegin;
  47. }
  48. //--------------------------------------------------------------------------------------------------
  49. template < typename T > inline
  50. void qhArray< T >::Reserve( int Count )
  51. {
  52. if ( Count > Capacity() )
  53. {
  54. T* Begin = (T*)qhAlloc( Count * sizeof( T ) );
  55. qhMove( Begin, mBegin, mEnd );
  56. qhFree( mBegin );
  57. mCapacity = Begin + Count;
  58. mEnd = Begin + Size();
  59. mBegin = Begin;
  60. }
  61. }
  62. //--------------------------------------------------------------------------------------------------
  63. template < typename T > inline
  64. void qhArray< T >::Resize( int Count )
  65. {
  66. Reserve( Count );
  67. qhDestroy( mBegin + Count, Size() - Count );
  68. qhConstruct( mEnd, Count - Size() );
  69. mEnd = mBegin + Count;
  70. }
  71. //--------------------------------------------------------------------------------------------------
  72. template < typename T > inline
  73. T& qhArray< T >::Expand( void )
  74. {
  75. if ( mEnd == mCapacity )
  76. {
  77. Reserve( 2 * Capacity() + 1 );
  78. }
  79. qhConstruct( mEnd );
  80. return *mEnd++;
  81. }
  82. //--------------------------------------------------------------------------------------------------
  83. template < typename T > inline
  84. void qhArray< T >::PushBack( const T& Other )
  85. {
  86. if ( mEnd == mCapacity )
  87. {
  88. Reserve( 2 * Capacity() + 1 );
  89. }
  90. qhCopyConstruct( mEnd++, Other );
  91. }
  92. //--------------------------------------------------------------------------------------------------
  93. template < typename T > inline
  94. void qhArray< T >::PopBack( void )
  95. {
  96. qhDestroy( --mEnd );
  97. }
  98. //--------------------------------------------------------------------------------------------------
  99. template < typename T > inline
  100. int qhArray< T >::IndexOf( const T& Element ) const
  101. {
  102. for ( int i = 0; i < Size(); ++i )
  103. {
  104. if ( mBegin[ i ] == Element )
  105. {
  106. return i;
  107. }
  108. }
  109. return -1;
  110. }
  111. //--------------------------------------------------------------------------------------------------
  112. template < typename T > inline
  113. T& qhArray< T >::operator[]( int Offset )
  114. {
  115. QH_ASSERT( 0 <= Offset && Offset < Size() );
  116. return *( mBegin + Offset );
  117. }
  118. //--------------------------------------------------------------------------------------------------
  119. template < typename T > inline
  120. const T& qhArray< T >::operator[]( int Offset ) const
  121. {
  122. QH_ASSERT( 0 <= Offset && Offset < Size() );
  123. return *( mBegin + Offset );
  124. }
  125. //--------------------------------------------------------------------------------------------------
  126. template < typename T > inline
  127. T& qhArray< T >::Front( void )
  128. {
  129. QH_ASSERT( !Empty() );
  130. return *mBegin;
  131. }
  132. //--------------------------------------------------------------------------------------------------
  133. template < typename T > inline
  134. const T& qhArray< T >::Front( void ) const
  135. {
  136. QH_ASSERT( !Empty() );
  137. return *mBegin;
  138. }
  139. //--------------------------------------------------------------------------------------------------
  140. template < typename T > inline
  141. T& qhArray< T >::Back( void )
  142. {
  143. QH_ASSERT( !Empty() );
  144. return *( mEnd - 1 );
  145. }
  146. //--------------------------------------------------------------------------------------------------
  147. template < typename T > inline
  148. T* qhArray< T >::Begin( void )
  149. {
  150. return mBegin;
  151. }
  152. //--------------------------------------------------------------------------------------------------
  153. template < typename T > inline
  154. const T* qhArray< T >::Begin( void ) const
  155. {
  156. return mBegin;
  157. }
  158. //--------------------------------------------------------------------------------------------------
  159. template < typename T > inline
  160. T* qhArray< T >::End( void )
  161. {
  162. return mEnd;
  163. }
  164. //--------------------------------------------------------------------------------------------------
  165. template < typename T > inline
  166. const T* qhArray< T >::End( void ) const
  167. {
  168. return mEnd;
  169. }
  170. //--------------------------------------------------------------------------------------------------
  171. template < typename T > inline
  172. const T& qhArray< T >::Back( void ) const
  173. {
  174. QH_ASSERT( !Empty() );
  175. return *( mEnd - 1 );
  176. }
  177. //--------------------------------------------------------------------------------------------------
  178. template < typename T > inline
  179. void qhArray< T >::Swap( qhArray< T >& Other )
  180. {
  181. qhSwap( mBegin, Other.mBegin );
  182. qhSwap( mEnd, Other.mEnd );
  183. qhSwap( mCapacity, Other.mCapacity );
  184. }
  185. //--------------------------------------------------------------------------------------------------
  186. template < typename T >
  187. void qhSwap( qhArray< T >& Lhs, qhArray< T >& Rhs )
  188. {
  189. Lhs.Swap( Rhs );
  190. }