Team Fortress 2 Source Code as on 22/4/2020
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.

1513 lines
41 KiB

  1. //========= Copyright 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 UTLVECTOR_H
  11. #define UTLVECTOR_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. #include <algorithm>
  16. #include <string.h>
  17. #include "tier0/platform.h"
  18. #include "tier0/dbg.h"
  19. #include "tier0/threadtools.h"
  20. #include "tier1/utlmemory.h"
  21. #include "tier1/utlblockmemory.h"
  22. #include "tier1/strtools.h"
  23. #include "vstdlib/random.h"
  24. #define FOR_EACH_VEC( vecName, iteratorName ) \
  25. for ( int iteratorName = 0; (vecName).IsUtlVector && iteratorName < (vecName).Count(); iteratorName++ )
  26. #define FOR_EACH_VEC_BACK( vecName, iteratorName ) \
  27. for ( int iteratorName = (vecName).Count()-1; (vecName).IsUtlVector && iteratorName >= 0; iteratorName-- )
  28. // UtlVector derives from this so we can do the type check above
  29. struct base_vector_t
  30. {
  31. public:
  32. enum { IsUtlVector = true }; // Used to match this at compiletime
  33. };
  34. //-----------------------------------------------------------------------------
  35. // The CUtlVector class:
  36. // A growable array class which doubles in size by default.
  37. // It will always keep all elements consecutive in memory, and may move the
  38. // elements around in memory (via a PvRealloc) when elements are inserted or
  39. // removed. Clients should therefore refer to the elements of the vector
  40. // by index (they should *never* maintain pointers to elements in the vector).
  41. //-----------------------------------------------------------------------------
  42. template< class T, class A = CUtlMemory<T> >
  43. class CUtlVector : public base_vector_t
  44. {
  45. typedef A CAllocator;
  46. public:
  47. typedef T ElemType_t;
  48. typedef T* iterator;
  49. typedef const T* const_iterator;
  50. // Set the growth policy and initial capacity. Count will always be zero. This is different from std::vector
  51. // where the constructor sets count as well as capacity.
  52. // growSize of zero implies the default growth pattern which is exponential.
  53. explicit CUtlVector( int growSize = 0, int initialCapacity = 0 );
  54. // Initialize with separately allocated buffer, setting the capacity and count.
  55. // The container will not be growable.
  56. CUtlVector( T* pMemory, int initialCapacity, int initialCount = 0 );
  57. ~CUtlVector();
  58. // Copy the array.
  59. CUtlVector<T, A>& operator=( const CUtlVector<T, A> &other );
  60. // element access
  61. T& operator[]( int i );
  62. const T& operator[]( int i ) const;
  63. T& Element( int i );
  64. const T& Element( int i ) const;
  65. T& Head();
  66. const T& Head() const;
  67. T& Tail();
  68. const T& Tail() const;
  69. T& Random();
  70. const T& Random() const;
  71. // STL compatible member functions. These allow easier use of std::sort
  72. // and they are forward compatible with the C++ 11 range-based for loops.
  73. iterator begin() { return Base(); }
  74. const_iterator begin() const { return Base(); }
  75. iterator end() { return Base() + Count(); }
  76. const_iterator end() const { return Base() + Count(); }
  77. // Gets the base address (can change when adding elements!)
  78. T* Base() { return m_Memory.Base(); }
  79. const T* Base() const { return m_Memory.Base(); }
  80. // Returns the number of elements in the vector
  81. // SIZE IS DEPRECATED!
  82. int Count() const;
  83. int Size() const; // don't use me!
  84. /// are there no elements? For compatibility with lists.
  85. inline bool IsEmpty( void ) const
  86. {
  87. return ( Count() == 0 );
  88. }
  89. // Is element index valid?
  90. bool IsValidIndex( int i ) const;
  91. static int InvalidIndex();
  92. // Adds an element, uses default constructor
  93. int AddToHead();
  94. int AddToTail();
  95. T *AddToTailGetPtr();
  96. int InsertBefore( int elem );
  97. int InsertAfter( int elem );
  98. // Adds an element, uses copy constructor
  99. int AddToHead( const T& src );
  100. int AddToTail( const T& src );
  101. int InsertBefore( int elem, const T& src );
  102. int InsertAfter( int elem, const T& src );
  103. // Adds multiple elements, uses default constructor
  104. int AddMultipleToHead( int num );
  105. int AddMultipleToTail( int num );
  106. int AddMultipleToTail( int num, const T *pToCopy );
  107. int InsertMultipleBefore( int elem, int num );
  108. int InsertMultipleBefore( int elem, int num, const T *pToCopy );
  109. int InsertMultipleAfter( int elem, int num );
  110. // Calls RemoveAll() then AddMultipleToTail.
  111. // SetSize is a synonym for SetCount
  112. void SetSize( int size );
  113. // SetCount deletes the previous contents of the container and sets the
  114. // container to have this many elements.
  115. // Use GetCount to retrieve the current count.
  116. void SetCount( int count );
  117. void SetCountNonDestructively( int count ); //sets count by adding or removing elements to tail TODO: This should probably be the default behavior for SetCount
  118. // Calls SetSize and copies each element.
  119. void CopyArray( const T *pArray, int size );
  120. // Fast swap
  121. void Swap( CUtlVector< T, A > &vec );
  122. // Add the specified array to the tail.
  123. int AddVectorToTail( CUtlVector<T, A> const &src );
  124. // Finds an element (element needs operator== defined)
  125. int Find( const T& src ) const;
  126. // Helper to find using std::find_if with a predicate
  127. // e.g. [] -> bool ( T &a ) { return a.IsTheThingIWant(); }
  128. //
  129. // Useful if your object doesn't define a ==
  130. template < typename F >
  131. int FindPredicate( F&& predicate ) const;
  132. void FillWithValue( const T& src );
  133. bool HasElement( const T& src ) const;
  134. // Makes sure we have enough memory allocated to store a requested # of elements
  135. // Use NumAllocated() to retrieve the current capacity.
  136. void EnsureCapacity( int num );
  137. // Makes sure we have at least this many elements
  138. // Use GetCount to retrieve the current count.
  139. void EnsureCount( int num );
  140. // Element removal
  141. void FastRemove( int elem ); // doesn't preserve order
  142. void Remove( int elem ); // preserves order, shifts elements
  143. bool FindAndRemove( const T& src ); // removes first occurrence of src, preserves order, shifts elements
  144. bool FindAndFastRemove( const T& src ); // removes first occurrence of src, doesn't preserve order
  145. void RemoveMultiple( int elem, int num ); // preserves order, shifts elements
  146. void RemoveMultipleFromHead(int num); // removes num elements from tail
  147. void RemoveMultipleFromTail(int num); // removes num elements from tail
  148. void RemoveAll(); // doesn't deallocate memory
  149. // Memory deallocation
  150. void Purge();
  151. // Purges the list and calls delete on each element in it.
  152. void PurgeAndDeleteElements();
  153. // Compacts the vector to the number of elements actually in use
  154. void Compact();
  155. // Set the size by which it grows when it needs to allocate more memory.
  156. void SetGrowSize( int size ) { m_Memory.SetGrowSize( size ); }
  157. int NumAllocated() const; // Only use this if you really know what you're doing!
  158. void Sort( int (__cdecl *pfnCompare)(const T *, const T *) );
  159. void Shuffle( IUniformRandomStream* pSteam = NULL );
  160. // Call this to quickly sort non-contiguously allocated vectors
  161. void InPlaceQuickSort( int (__cdecl *pfnCompare)(const T *, const T *) );
  162. // reverse the order of elements
  163. void Reverse( );
  164. #ifdef DBGFLAG_VALIDATE
  165. void Validate( CValidator &validator, char *pchName ); // Validate our internal structures
  166. #endif // DBGFLAG_VALIDATE
  167. /// sort using std:: and expecting a "<" function to be defined for the type
  168. void Sort( void );
  169. /// sort using std:: with a predicate. e.g. [] -> bool ( T &a, T &b ) { return a < b; }
  170. template <class F> void SortPredicate( F &&predicate );
  171. protected:
  172. // Can't copy this unless we explicitly do it!
  173. CUtlVector( CUtlVector const& vec ) { Assert(0); }
  174. // Grows the vector
  175. void GrowVector( int num = 1 );
  176. // Shifts elements....
  177. void ShiftElementsRight( int elem, int num = 1 );
  178. void ShiftElementsLeft( int elem, int num = 1 );
  179. CAllocator m_Memory;
  180. int m_Size;
  181. #ifndef _X360
  182. // For easier access to the elements through the debugger
  183. // it's in release builds so this can be used in libraries correctly
  184. T *m_pElements;
  185. inline void ResetDbgInfo()
  186. {
  187. m_pElements = Base();
  188. }
  189. #else
  190. inline void ResetDbgInfo() {}
  191. #endif
  192. private:
  193. void InPlaceQuickSort_r( int (__cdecl *pfnCompare)(const T *, const T *), int nLeft, int nRight );
  194. };
  195. // this is kind of ugly, but until C++ gets templatized typedefs in C++0x, it's our only choice
  196. template < class T >
  197. class CUtlBlockVector : public CUtlVector< T, CUtlBlockMemory< T, int > >
  198. {
  199. public:
  200. explicit CUtlBlockVector( int growSize = 0, int initSize = 0 )
  201. : CUtlVector< T, CUtlBlockMemory< T, int > >( growSize, initSize ) {}
  202. };
  203. //-----------------------------------------------------------------------------
  204. // The CUtlVectorMT class:
  205. // An array class with spurious mutex protection. Nothing is actually protected
  206. // unless you call Lock and Unlock. Also, the Mutex_t is actually not a type
  207. // but a member which probably isn't used.
  208. //-----------------------------------------------------------------------------
  209. template< class BASE_UTLVECTOR, class MUTEX_TYPE = CThreadFastMutex >
  210. class CUtlVectorMT : public BASE_UTLVECTOR, public MUTEX_TYPE
  211. {
  212. typedef BASE_UTLVECTOR BaseClass;
  213. public:
  214. // MUTEX_TYPE Mutex_t;
  215. // constructor, destructor
  216. explicit CUtlVectorMT( int growSize = 0, int initSize = 0 ) : BaseClass( growSize, initSize ) {}
  217. CUtlVectorMT( typename BaseClass::ElemType_t* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
  218. };
  219. //-----------------------------------------------------------------------------
  220. // The CUtlVectorFixed class:
  221. // A array class with a fixed allocation scheme
  222. //-----------------------------------------------------------------------------
  223. template< class T, size_t MAX_SIZE >
  224. class CUtlVectorFixed : public CUtlVector< T, CUtlMemoryFixed<T, MAX_SIZE > >
  225. {
  226. typedef CUtlVector< T, CUtlMemoryFixed<T, MAX_SIZE > > BaseClass;
  227. public:
  228. // constructor, destructor
  229. explicit CUtlVectorFixed( int growSize = 0, int initSize = 0 ) : BaseClass( growSize, initSize ) {}
  230. CUtlVectorFixed( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
  231. };
  232. //-----------------------------------------------------------------------------
  233. // The CUtlVectorFixedGrowable class:
  234. // A array class with a fixed allocation scheme backed by a dynamic one
  235. //-----------------------------------------------------------------------------
  236. template< class T, size_t MAX_SIZE >
  237. class CUtlVectorFixedGrowable : public CUtlVector< T, CUtlMemoryFixedGrowable<T, MAX_SIZE > >
  238. {
  239. typedef CUtlVector< T, CUtlMemoryFixedGrowable<T, MAX_SIZE > > BaseClass;
  240. public:
  241. // constructor, destructor
  242. explicit CUtlVectorFixedGrowable( int growSize = 0 ) : BaseClass( growSize, MAX_SIZE ) {}
  243. };
  244. //-----------------------------------------------------------------------------
  245. // The CUtlVectorConservative class:
  246. // A array class with a conservative allocation scheme
  247. //-----------------------------------------------------------------------------
  248. template< class T >
  249. class CUtlVectorConservative : public CUtlVector< T, CUtlMemoryConservative<T> >
  250. {
  251. typedef CUtlVector< T, CUtlMemoryConservative<T> > BaseClass;
  252. public:
  253. // constructor, destructor
  254. explicit CUtlVectorConservative( int growSize = 0, int initSize = 0 ) : BaseClass( growSize, initSize ) {}
  255. CUtlVectorConservative( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
  256. };
  257. //-----------------------------------------------------------------------------
  258. // The CUtlVectorUltra Conservative class:
  259. // A array class with a very conservative allocation scheme, with customizable allocator
  260. // Especialy useful if you have a lot of vectors that are sparse, or if you're
  261. // carefully packing holders of vectors
  262. //-----------------------------------------------------------------------------
  263. #pragma warning(push)
  264. #pragma warning(disable : 4200) // warning C4200: nonstandard extension used : zero-sized array in struct/union
  265. #pragma warning(disable : 4815 ) // warning C4815: 'staticData' : zero-sized array in stack object will have no elements
  266. class CUtlVectorUltraConservativeAllocator
  267. {
  268. public:
  269. static void *Alloc( size_t nSize )
  270. {
  271. return malloc( nSize );
  272. }
  273. static void *Realloc( void *pMem, size_t nSize )
  274. {
  275. return realloc( pMem, nSize );
  276. }
  277. static void Free( void *pMem )
  278. {
  279. free( pMem );
  280. }
  281. static size_t GetSize( void *pMem )
  282. {
  283. return mallocsize( pMem );
  284. }
  285. };
  286. template <typename T, typename A = CUtlVectorUltraConservativeAllocator >
  287. class CUtlVectorUltraConservative : private A
  288. {
  289. public:
  290. // Don't inherit from base_vector_t because multiple-inheritance increases
  291. // class size!
  292. enum { IsUtlVector = true }; // Used to match this at compiletime
  293. CUtlVectorUltraConservative()
  294. {
  295. m_pData = StaticData();
  296. }
  297. ~CUtlVectorUltraConservative()
  298. {
  299. RemoveAll();
  300. }
  301. int Count() const
  302. {
  303. return m_pData->m_Size;
  304. }
  305. static int InvalidIndex()
  306. {
  307. return -1;
  308. }
  309. inline bool IsValidIndex( int i ) const
  310. {
  311. return (i >= 0) && (i < Count());
  312. }
  313. T& operator[]( int i )
  314. {
  315. Assert( IsValidIndex( i ) );
  316. return m_pData->m_Elements[i];
  317. }
  318. const T& operator[]( int i ) const
  319. {
  320. Assert( IsValidIndex( i ) );
  321. return m_pData->m_Elements[i];
  322. }
  323. T& Element( int i )
  324. {
  325. Assert( IsValidIndex( i ) );
  326. return m_pData->m_Elements[i];
  327. }
  328. const T& Element( int i ) const
  329. {
  330. Assert( IsValidIndex( i ) );
  331. return m_pData->m_Elements[i];
  332. }
  333. void EnsureCapacity( int num )
  334. {
  335. int nCurCount = Count();
  336. if ( num <= nCurCount )
  337. {
  338. return;
  339. }
  340. if ( m_pData == StaticData() )
  341. {
  342. m_pData = (Data_t *)A::Alloc( sizeof(Data_t) + ( num * sizeof(T) ) );
  343. m_pData->m_Size = 0;
  344. }
  345. else
  346. {
  347. int nNeeded = sizeof(Data_t) + ( num * sizeof(T) );
  348. int nHave = A::GetSize( m_pData );
  349. if ( nNeeded > nHave )
  350. {
  351. m_pData = (Data_t *)A::Realloc( m_pData, nNeeded );
  352. }
  353. }
  354. }
  355. int AddToTail( const T& src )
  356. {
  357. int iNew = Count();
  358. EnsureCapacity( Count() + 1 );
  359. m_pData->m_Elements[iNew] = src;
  360. m_pData->m_Size++;
  361. return iNew;
  362. }
  363. void RemoveAll()
  364. {
  365. if ( Count() )
  366. {
  367. for (int i = m_pData->m_Size; --i >= 0; )
  368. {
  369. // Global scope to resolve conflict with Scaleform 4.0
  370. ::Destruct(&m_pData->m_Elements[i]);
  371. }
  372. }
  373. if ( m_pData != StaticData() )
  374. {
  375. A::Free( m_pData );
  376. m_pData = StaticData();
  377. }
  378. }
  379. void PurgeAndDeleteElements()
  380. {
  381. if ( m_pData != StaticData() )
  382. {
  383. for( int i=0; i < m_pData->m_Size; i++ )
  384. {
  385. delete Element(i);
  386. }
  387. RemoveAll();
  388. }
  389. }
  390. void FastRemove( int elem )
  391. {
  392. Assert( IsValidIndex(elem) );
  393. // Global scope to resolve conflict with Scaleform 4.0
  394. ::Destruct( &Element(elem) );
  395. if (Count() > 0)
  396. {
  397. if ( elem != m_pData->m_Size -1 )
  398. memcpy( &Element(elem), &Element(m_pData->m_Size-1), sizeof(T) );
  399. --m_pData->m_Size;
  400. }
  401. if ( !m_pData->m_Size )
  402. {
  403. A::Free( m_pData );
  404. m_pData = StaticData();
  405. }
  406. }
  407. void Remove( int elem )
  408. {
  409. // Global scope to resolve conflict with Scaleform 4.0
  410. ::Destruct( &Element(elem) );
  411. ShiftElementsLeft(elem);
  412. --m_pData->m_Size;
  413. if ( !m_pData->m_Size )
  414. {
  415. A::Free( m_pData );
  416. m_pData = StaticData();
  417. }
  418. }
  419. int Find( const T& src ) const
  420. {
  421. int nCount = Count();
  422. for ( int i = 0; i < nCount; ++i )
  423. {
  424. if (Element(i) == src)
  425. return i;
  426. }
  427. return -1;
  428. }
  429. bool FindAndRemove( const T& src )
  430. {
  431. int elem = Find( src );
  432. if ( elem != -1 )
  433. {
  434. Remove( elem );
  435. return true;
  436. }
  437. return false;
  438. }
  439. bool FindAndFastRemove( const T& src )
  440. {
  441. int elem = Find( src );
  442. if ( elem != -1 )
  443. {
  444. FastRemove( elem );
  445. return true;
  446. }
  447. return false;
  448. }
  449. bool DebugCompileError_ANonVectorIsUsedInThe_FOR_EACH_VEC_Macro( void ) const { return true; }
  450. struct Data_t
  451. {
  452. int m_Size;
  453. T m_Elements[0];
  454. };
  455. Data_t *m_pData;
  456. private:
  457. void ShiftElementsLeft( int elem, int num = 1 )
  458. {
  459. int Size = Count();
  460. Assert( IsValidIndex(elem) || ( Size == 0 ) || ( num == 0 ));
  461. int numToMove = Size - elem - num;
  462. if ((numToMove > 0) && (num > 0))
  463. {
  464. Q_memmove( &Element(elem), &Element(elem+num), numToMove * sizeof(T) );
  465. #ifdef _DEBUG
  466. Q_memset( &Element(Size-num), 0xDD, num * sizeof(T) );
  467. #endif
  468. }
  469. }
  470. static Data_t *StaticData()
  471. {
  472. static Data_t staticData;
  473. Assert( staticData.m_Size == 0 );
  474. return &staticData;
  475. }
  476. };
  477. #pragma warning(pop)
  478. // Make sure nobody adds multiple inheritance and makes this class bigger.
  479. COMPILE_TIME_ASSERT( sizeof(CUtlVectorUltraConservative<int>) == sizeof(void*) );
  480. //-----------------------------------------------------------------------------
  481. // The CCopyableUtlVector class:
  482. // A array class that allows copy construction (so you can nest a CUtlVector inside of another one of our containers)
  483. // WARNING - this class lets you copy construct which can be an expensive operation if you don't carefully control when it happens
  484. // Only use this when nesting a CUtlVector() inside of another one of our container classes (i.e a CUtlMap)
  485. //-----------------------------------------------------------------------------
  486. template< class T >
  487. class CCopyableUtlVector : public CUtlVector< T, CUtlMemory<T> >
  488. {
  489. typedef CUtlVector< T, CUtlMemory<T> > BaseClass;
  490. public:
  491. explicit CCopyableUtlVector( int growSize = 0, int initSize = 0 ) : BaseClass( growSize, initSize ) {}
  492. CCopyableUtlVector( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
  493. virtual ~CCopyableUtlVector() {}
  494. CCopyableUtlVector( CCopyableUtlVector const& vec ) { this->CopyArray( vec.Base(), vec.Count() ); }
  495. };
  496. //-----------------------------------------------------------------------------
  497. // The CCopyableUtlVector class:
  498. // A array class that allows copy construction (so you can nest a CUtlVector inside of another one of our containers)
  499. // WARNING - this class lets you copy construct which can be an expensive operation if you don't carefully control when it happens
  500. // Only use this when nesting a CUtlVector() inside of another one of our container classes (i.e a CUtlMap)
  501. //-----------------------------------------------------------------------------
  502. template< class T, size_t MAX_SIZE >
  503. class CCopyableUtlVectorFixed : public CUtlVectorFixed< T, MAX_SIZE >
  504. {
  505. typedef CUtlVectorFixed< T, MAX_SIZE > BaseClass;
  506. public:
  507. explicit CCopyableUtlVectorFixed( int growSize = 0, int initSize = 0 ) : BaseClass( growSize, initSize ) {}
  508. CCopyableUtlVectorFixed( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
  509. virtual ~CCopyableUtlVectorFixed() {}
  510. CCopyableUtlVectorFixed( CCopyableUtlVectorFixed const& vec ) { this->CopyArray( vec.Base(), vec.Count() ); }
  511. };
  512. // TODO (Ilya): It seems like all the functions in CUtlVector are simple enough that they should be inlined.
  513. //-----------------------------------------------------------------------------
  514. // constructor, destructor
  515. //-----------------------------------------------------------------------------
  516. template< typename T, class A >
  517. inline CUtlVector<T, A>::CUtlVector( int growSize, int initSize ) :
  518. m_Memory(growSize, initSize), m_Size(0)
  519. {
  520. ResetDbgInfo();
  521. }
  522. template< typename T, class A >
  523. inline CUtlVector<T, A>::CUtlVector( T* pMemory, int allocationCount, int numElements ) :
  524. m_Memory(pMemory, allocationCount), m_Size(numElements)
  525. {
  526. ResetDbgInfo();
  527. }
  528. template< typename T, class A >
  529. inline CUtlVector<T, A>::~CUtlVector()
  530. {
  531. Purge();
  532. }
  533. template< typename T, class A >
  534. inline CUtlVector<T, A>& CUtlVector<T, A>::operator=( const CUtlVector<T, A> &other )
  535. {
  536. int nCount = other.Count();
  537. SetSize( nCount );
  538. for ( int i = 0; i < nCount; i++ )
  539. {
  540. (*this)[ i ] = other[ i ];
  541. }
  542. return *this;
  543. }
  544. #ifdef STAGING_ONLY
  545. inline void StagingUtlVectorBoundsCheck( int i, int size )
  546. {
  547. if ( (unsigned)i >= (unsigned)size )
  548. {
  549. Msg( "Array access error: %d / %d\n", i, size );
  550. DebuggerBreak();
  551. }
  552. }
  553. #else
  554. #define StagingUtlVectorBoundsCheck( _i, _size )
  555. #endif
  556. //-----------------------------------------------------------------------------
  557. // element access
  558. //-----------------------------------------------------------------------------
  559. template< typename T, class A >
  560. inline T& CUtlVector<T, A>::operator[]( int i )
  561. {
  562. // Do an inline unsigned check for maximum debug-build performance.
  563. Assert( (unsigned)i < (unsigned)m_Size );
  564. StagingUtlVectorBoundsCheck( i, m_Size );
  565. return m_Memory[ i ];
  566. }
  567. template< typename T, class A >
  568. inline const T& CUtlVector<T, A>::operator[]( int i ) const
  569. {
  570. // Do an inline unsigned check for maximum debug-build performance.
  571. Assert( (unsigned)i < (unsigned)m_Size );
  572. StagingUtlVectorBoundsCheck( i, m_Size );
  573. return m_Memory[ i ];
  574. }
  575. template< typename T, class A >
  576. inline T& CUtlVector<T, A>::Element( int i )
  577. {
  578. // Do an inline unsigned check for maximum debug-build performance.
  579. Assert( (unsigned)i < (unsigned)m_Size );
  580. StagingUtlVectorBoundsCheck( i, m_Size );
  581. return m_Memory[ i ];
  582. }
  583. template< typename T, class A >
  584. inline const T& CUtlVector<T, A>::Element( int i ) const
  585. {
  586. // Do an inline unsigned check for maximum debug-build performance.
  587. Assert( (unsigned)i < (unsigned)m_Size );
  588. StagingUtlVectorBoundsCheck( i, m_Size );
  589. return m_Memory[ i ];
  590. }
  591. template< typename T, class A >
  592. inline T& CUtlVector<T, A>::Head()
  593. {
  594. Assert( m_Size > 0 );
  595. StagingUtlVectorBoundsCheck( 0, m_Size );
  596. return m_Memory[ 0 ];
  597. }
  598. template< typename T, class A >
  599. inline const T& CUtlVector<T, A>::Head() const
  600. {
  601. Assert( m_Size > 0 );
  602. StagingUtlVectorBoundsCheck( 0, m_Size );
  603. return m_Memory[ 0 ];
  604. }
  605. template< typename T, class A >
  606. inline T& CUtlVector<T, A>::Tail()
  607. {
  608. Assert( m_Size > 0 );
  609. StagingUtlVectorBoundsCheck( 0, m_Size );
  610. return m_Memory[ m_Size - 1 ];
  611. }
  612. template< typename T, class A >
  613. inline const T& CUtlVector<T, A>::Tail() const
  614. {
  615. Assert( m_Size > 0 );
  616. StagingUtlVectorBoundsCheck( 0, m_Size );
  617. return m_Memory[ m_Size - 1 ];
  618. }
  619. //-----------------------------------------------------------------------------
  620. // Count
  621. //-----------------------------------------------------------------------------
  622. template< typename T, class A >
  623. inline int CUtlVector<T, A>::Size() const
  624. {
  625. return m_Size;
  626. }
  627. template< typename T, class A >
  628. inline T& CUtlVector<T, A>::Random()
  629. {
  630. Assert( m_Size > 0 );
  631. return m_Memory[ RandomInt( 0, m_Size - 1 ) ];
  632. }
  633. template< typename T, class A >
  634. inline const T& CUtlVector<T, A>::Random() const
  635. {
  636. Assert( m_Size > 0 );
  637. return m_Memory[ RandomInt( 0, m_Size - 1 ) ];
  638. }
  639. //-----------------------------------------------------------------------------
  640. // Shuffle - Knuth/Fisher-Yates
  641. //-----------------------------------------------------------------------------
  642. template< typename T, class A >
  643. void CUtlVector<T, A>::Shuffle( IUniformRandomStream* pSteam )
  644. {
  645. for ( int i = 0; i < m_Size; i++ )
  646. {
  647. int j = pSteam ? pSteam->RandomInt( i, m_Size - 1 ) : RandomInt( i, m_Size - 1 );
  648. if ( i != j )
  649. {
  650. V_swap( m_Memory[ i ], m_Memory[ j ] );
  651. }
  652. }
  653. }
  654. template< typename T, class A >
  655. inline int CUtlVector<T, A>::Count() const
  656. {
  657. return m_Size;
  658. }
  659. //-----------------------------------------------------------------------------
  660. //-----------------------------------------------------------------------------
  661. // Reverse - reverse the order of elements, akin to std::vector<>::reverse()
  662. //-----------------------------------------------------------------------------
  663. template< typename T, class A >
  664. void CUtlVector<T, A>::Reverse( )
  665. {
  666. for ( int i = 0; i < m_Size / 2; i++ )
  667. {
  668. V_swap( m_Memory[ i ], m_Memory[ m_Size - 1 - i ] );
  669. #if defined( UTLVECTOR_TRACK_STACKS )
  670. if ( bTrackingEnabled )
  671. {
  672. V_swap( m_pElementStackStatsIndices[ i ], m_pElementStackStatsIndices[ m_Size - 1 - i ] );
  673. }
  674. #endif
  675. }
  676. }
  677. //-----------------------------------------------------------------------------
  678. // Is element index valid?
  679. //-----------------------------------------------------------------------------
  680. template< typename T, class A >
  681. inline bool CUtlVector<T, A>::IsValidIndex( int i ) const
  682. {
  683. return (i >= 0) && (i < m_Size);
  684. }
  685. //-----------------------------------------------------------------------------
  686. // Returns in invalid index
  687. //-----------------------------------------------------------------------------
  688. template< typename T, class A >
  689. inline int CUtlVector<T, A>::InvalidIndex()
  690. {
  691. return -1;
  692. }
  693. //-----------------------------------------------------------------------------
  694. // Grows the vector
  695. //-----------------------------------------------------------------------------
  696. template< typename T, class A >
  697. void CUtlVector<T, A>::GrowVector( int num )
  698. {
  699. if (m_Size + num > m_Memory.NumAllocated())
  700. {
  701. MEM_ALLOC_CREDIT_CLASS();
  702. m_Memory.Grow( m_Size + num - m_Memory.NumAllocated() );
  703. }
  704. m_Size += num;
  705. ResetDbgInfo();
  706. }
  707. //-----------------------------------------------------------------------------
  708. // Sorts the vector
  709. //-----------------------------------------------------------------------------
  710. template< typename T, class A >
  711. void CUtlVector<T, A>::Sort( int (__cdecl *pfnCompare)(const T *, const T *) )
  712. {
  713. typedef int (__cdecl *QSortCompareFunc_t)(const void *, const void *);
  714. if ( Count() <= 1 )
  715. return;
  716. if ( Base() )
  717. {
  718. qsort( Base(), Count(), sizeof(T), (QSortCompareFunc_t)(pfnCompare) );
  719. }
  720. else
  721. {
  722. Assert( 0 );
  723. // this path is untested
  724. // if you want to sort vectors that use a non-sequential memory allocator,
  725. // you'll probably want to patch in a quicksort algorithm here
  726. // I just threw in this bubble sort to have something just in case...
  727. for ( int i = m_Size - 1; i >= 0; --i )
  728. {
  729. for ( int j = 1; j <= i; ++j )
  730. {
  731. if ( pfnCompare( &Element( j - 1 ), &Element( j ) ) < 0 )
  732. {
  733. V_swap( Element( j - 1 ), Element( j ) );
  734. }
  735. }
  736. }
  737. }
  738. }
  739. //----------------------------------------------------------------------------------------------
  740. // Private function that does the in-place quicksort for non-contiguously allocated vectors.
  741. //----------------------------------------------------------------------------------------------
  742. template< typename T, class A >
  743. void CUtlVector<T, A>::InPlaceQuickSort_r( int (__cdecl *pfnCompare)(const T *, const T *), int nLeft, int nRight )
  744. {
  745. int nPivot;
  746. int nLeftIdx = nLeft;
  747. int nRightIdx = nRight;
  748. if ( nRight - nLeft > 0 )
  749. {
  750. nPivot = ( nLeft + nRight ) / 2;
  751. while ( ( nLeftIdx <= nPivot ) && ( nRightIdx >= nPivot ) )
  752. {
  753. while ( ( pfnCompare( &Element( nLeftIdx ), &Element( nPivot ) ) < 0 ) && ( nLeftIdx <= nPivot ) )
  754. {
  755. nLeftIdx++;
  756. }
  757. while ( ( pfnCompare( &Element( nRightIdx ), &Element( nPivot ) ) > 0 ) && ( nRightIdx >= nPivot ) )
  758. {
  759. nRightIdx--;
  760. }
  761. V_swap( Element( nLeftIdx ), Element( nRightIdx ) );
  762. nLeftIdx++;
  763. nRightIdx--;
  764. if ( ( nLeftIdx - 1 ) == nPivot )
  765. {
  766. nPivot = nRightIdx = nRightIdx + 1;
  767. }
  768. else if ( nRightIdx + 1 == nPivot )
  769. {
  770. nPivot = nLeftIdx = nLeftIdx - 1;
  771. }
  772. }
  773. InPlaceQuickSort_r( pfnCompare, nLeft, nPivot - 1 );
  774. InPlaceQuickSort_r( pfnCompare, nPivot + 1, nRight );
  775. }
  776. }
  777. //----------------------------------------------------------------------------------------------
  778. // Call this to quickly sort non-contiguously allocated vectors. Sort uses a slower bubble sort.
  779. //----------------------------------------------------------------------------------------------
  780. template< typename T, class A >
  781. void CUtlVector<T, A>::InPlaceQuickSort( int (__cdecl *pfnCompare)(const T *, const T *) )
  782. {
  783. InPlaceQuickSort_r( pfnCompare, 0, Count() - 1 );
  784. }
  785. template< typename T, class A >
  786. void CUtlVector<T, A>::Sort( void )
  787. {
  788. //STACK STATS TODO: Do we care about allocation tracking precision enough to match element origins across a sort?
  789. std::sort( Base(), Base() + Count() );
  790. }
  791. template< typename T, class A >
  792. template <class F>
  793. void CUtlVector<T, A>::SortPredicate( F &&predicate )
  794. {
  795. std::sort( Base(), Base() + Count(), predicate );
  796. }
  797. //-----------------------------------------------------------------------------
  798. // Makes sure we have enough memory allocated to store a requested # of elements
  799. //-----------------------------------------------------------------------------
  800. template< typename T, class A >
  801. void CUtlVector<T, A>::EnsureCapacity( int num )
  802. {
  803. MEM_ALLOC_CREDIT_CLASS();
  804. m_Memory.EnsureCapacity(num);
  805. ResetDbgInfo();
  806. }
  807. //-----------------------------------------------------------------------------
  808. // Makes sure we have at least this many elements
  809. //-----------------------------------------------------------------------------
  810. template< typename T, class A >
  811. void CUtlVector<T, A>::EnsureCount( int num )
  812. {
  813. if (Count() < num)
  814. {
  815. AddMultipleToTail( num - Count() );
  816. }
  817. }
  818. //-----------------------------------------------------------------------------
  819. // Shifts elements
  820. //-----------------------------------------------------------------------------
  821. template< typename T, class A >
  822. void CUtlVector<T, A>::ShiftElementsRight( int elem, int num )
  823. {
  824. Assert( IsValidIndex(elem) || ( m_Size == 0 ) || ( num == 0 ));
  825. int numToMove = m_Size - elem - num;
  826. if ((numToMove > 0) && (num > 0))
  827. Q_memmove( &Element(elem+num), &Element(elem), numToMove * sizeof(T) );
  828. }
  829. template< typename T, class A >
  830. void CUtlVector<T, A>::ShiftElementsLeft( int elem, int num )
  831. {
  832. Assert( IsValidIndex(elem) || ( m_Size == 0 ) || ( num == 0 ));
  833. int numToMove = m_Size - elem - num;
  834. if ((numToMove > 0) && (num > 0))
  835. {
  836. Q_memmove( &Element(elem), &Element(elem+num), numToMove * sizeof(T) );
  837. #ifdef _DEBUG
  838. Q_memset( &Element(m_Size-num), 0xDD, num * sizeof(T) );
  839. #endif
  840. }
  841. }
  842. //-----------------------------------------------------------------------------
  843. // Adds an element, uses default constructor
  844. //-----------------------------------------------------------------------------
  845. template< typename T, class A >
  846. inline int CUtlVector<T, A>::AddToHead()
  847. {
  848. return InsertBefore(0);
  849. }
  850. template< typename T, class A >
  851. inline int CUtlVector<T, A>::AddToTail()
  852. {
  853. return InsertBefore( m_Size );
  854. }
  855. template< typename T, class A >
  856. inline T *CUtlVector<T, A>::AddToTailGetPtr()
  857. {
  858. return &Element( AddToTail() );
  859. }
  860. template< typename T, class A >
  861. inline int CUtlVector<T, A>::InsertAfter( int elem )
  862. {
  863. return InsertBefore( elem + 1 );
  864. }
  865. template< typename T, class A >
  866. int CUtlVector<T, A>::InsertBefore( int elem )
  867. {
  868. // Can insert at the end
  869. Assert( (elem == Count()) || IsValidIndex(elem) );
  870. GrowVector();
  871. ShiftElementsRight(elem);
  872. Construct( &Element(elem) );
  873. return elem;
  874. }
  875. //-----------------------------------------------------------------------------
  876. // Adds an element, uses copy constructor
  877. //-----------------------------------------------------------------------------
  878. template< typename T, class A >
  879. inline int CUtlVector<T, A>::AddToHead( const T& src )
  880. {
  881. // Can't insert something that's in the list... reallocation may hose us
  882. Assert( (Base() == NULL) || (&src < Base()) || (&src >= (Base() + Count()) ) );
  883. return InsertBefore( 0, src );
  884. }
  885. template< typename T, class A >
  886. inline int CUtlVector<T, A>::AddToTail( const T& src )
  887. {
  888. // Can't insert something that's in the list... reallocation may hose us
  889. Assert( (Base() == NULL) || (&src < Base()) || (&src >= (Base() + Count()) ) );
  890. return InsertBefore( m_Size, src );
  891. }
  892. template< typename T, class A >
  893. inline int CUtlVector<T, A>::InsertAfter( int elem, const T& src )
  894. {
  895. // Can't insert something that's in the list... reallocation may hose us
  896. Assert( (Base() == NULL) || (&src < Base()) || (&src >= (Base() + Count()) ) );
  897. return InsertBefore( elem + 1, src );
  898. }
  899. template< typename T, class A >
  900. int CUtlVector<T, A>::InsertBefore( int elem, const T& src )
  901. {
  902. // Can't insert something that's in the list... reallocation may hose us
  903. Assert( (Base() == NULL) || (&src < Base()) || (&src >= (Base() + Count()) ) );
  904. // Can insert at the end
  905. Assert( (elem == Count()) || IsValidIndex(elem) );
  906. GrowVector();
  907. ShiftElementsRight(elem);
  908. CopyConstruct( &Element(elem), src );
  909. return elem;
  910. }
  911. //-----------------------------------------------------------------------------
  912. // Adds multiple elements, uses default constructor
  913. //-----------------------------------------------------------------------------
  914. template< typename T, class A >
  915. inline int CUtlVector<T, A>::AddMultipleToHead( int num )
  916. {
  917. return InsertMultipleBefore( 0, num );
  918. }
  919. template< typename T, class A >
  920. inline int CUtlVector<T, A>::AddMultipleToTail( int num )
  921. {
  922. return InsertMultipleBefore( m_Size, num );
  923. }
  924. template< typename T, class A >
  925. inline int CUtlVector<T, A>::AddMultipleToTail( int num, const T *pToCopy )
  926. {
  927. // Can't insert something that's in the list... reallocation may hose us
  928. Assert( (Base() == NULL) || !pToCopy || (pToCopy + num <= Base()) || (pToCopy >= (Base() + Count()) ) );
  929. return InsertMultipleBefore( m_Size, num, pToCopy );
  930. }
  931. template< typename T, class A >
  932. int CUtlVector<T, A>::InsertMultipleAfter( int elem, int num )
  933. {
  934. return InsertMultipleBefore( elem + 1, num );
  935. }
  936. template< typename T, class A >
  937. void CUtlVector<T, A>::SetCount( int count )
  938. {
  939. RemoveAll();
  940. AddMultipleToTail( count );
  941. }
  942. template< typename T, class A >
  943. inline void CUtlVector<T, A>::SetSize( int size )
  944. {
  945. SetCount( size );
  946. }
  947. template< typename T, class A >
  948. void CUtlVector<T, A>::SetCountNonDestructively( int count )
  949. {
  950. int delta = count - m_Size;
  951. if(delta > 0) AddMultipleToTail( delta );
  952. else if(delta < 0) RemoveMultipleFromTail( -delta );
  953. }
  954. template< typename T, class A >
  955. void CUtlVector<T, A>::CopyArray( const T *pArray, int size )
  956. {
  957. // Can't insert something that's in the list... reallocation may hose us
  958. Assert( (Base() == NULL) || !pArray || (Base() >= (pArray + size)) || (pArray >= (Base() + Count()) ) );
  959. SetSize( size );
  960. for( int i=0; i < size; i++ )
  961. {
  962. (*this)[i] = pArray[i];
  963. }
  964. }
  965. template< typename T, class A >
  966. void CUtlVector<T, A>::Swap( CUtlVector< T, A > &vec )
  967. {
  968. m_Memory.Swap( vec.m_Memory );
  969. V_swap( m_Size, vec.m_Size );
  970. #ifndef _X360
  971. V_swap( m_pElements, vec.m_pElements );
  972. #endif
  973. }
  974. template< typename T, class A >
  975. int CUtlVector<T, A>::AddVectorToTail( CUtlVector const &src )
  976. {
  977. Assert( &src != this );
  978. int base = Count();
  979. // Make space.
  980. int nSrcCount = src.Count();
  981. EnsureCapacity( base + nSrcCount );
  982. // Copy the elements.
  983. m_Size += nSrcCount;
  984. for ( int i=0; i < nSrcCount; i++ )
  985. {
  986. CopyConstruct( &Element(base+i), src[i] );
  987. }
  988. return base;
  989. }
  990. template< typename T, class A >
  991. inline int CUtlVector<T, A>::InsertMultipleBefore( int elem, int num )
  992. {
  993. if( num == 0 )
  994. return elem;
  995. // Can insert at the end
  996. Assert( (elem == Count()) || IsValidIndex(elem) );
  997. GrowVector(num);
  998. ShiftElementsRight( elem, num );
  999. // Invoke default constructors
  1000. for (int i = 0; i < num; ++i )
  1001. {
  1002. Construct( &Element( elem+i ) );
  1003. }
  1004. return elem;
  1005. }
  1006. template< typename T, class A >
  1007. inline int CUtlVector<T, A>::InsertMultipleBefore( int elem, int num, const T *pToInsert )
  1008. {
  1009. if( num == 0 )
  1010. return elem;
  1011. // Can insert at the end
  1012. Assert( (elem == Count()) || IsValidIndex(elem) );
  1013. GrowVector(num);
  1014. ShiftElementsRight( elem, num );
  1015. // Invoke default constructors
  1016. if ( !pToInsert )
  1017. {
  1018. for (int i = 0; i < num; ++i )
  1019. {
  1020. Construct( &Element( elem+i ) );
  1021. }
  1022. }
  1023. else
  1024. {
  1025. for ( int i=0; i < num; i++ )
  1026. {
  1027. CopyConstruct( &Element( elem+i ), pToInsert[i] );
  1028. }
  1029. }
  1030. return elem;
  1031. }
  1032. //-----------------------------------------------------------------------------
  1033. // Finds an element (element needs operator== defined)
  1034. //-----------------------------------------------------------------------------
  1035. template< typename T, class A >
  1036. int CUtlVector<T, A>::Find( const T& src ) const
  1037. {
  1038. for ( int i = 0; i < Count(); ++i )
  1039. {
  1040. if (Element(i) == src)
  1041. return i;
  1042. }
  1043. return -1;
  1044. }
  1045. //-----------------------------------------------------------------------------
  1046. // Finds an element using a predicate, using std::find_if
  1047. //-----------------------------------------------------------------------------
  1048. template< typename T, class A >
  1049. template< class F >
  1050. int CUtlVector<T, A>::FindPredicate( F &&predicate ) const
  1051. {
  1052. const T * begin = Base();
  1053. const T * end = begin + Count();
  1054. const T * const &elem = std::find_if( begin, end, predicate );
  1055. if ( elem != end )
  1056. {
  1057. int idx = (int)std::distance( begin, elem );
  1058. StagingUtlVectorBoundsCheck( idx, m_Size );
  1059. return idx;
  1060. }
  1061. return InvalidIndex();
  1062. }
  1063. template< typename T, class A >
  1064. void CUtlVector<T, A>::FillWithValue( const T& src )
  1065. {
  1066. for ( int i = 0; i < Count(); i++ )
  1067. {
  1068. Element(i) = src;
  1069. }
  1070. }
  1071. template< typename T, class A >
  1072. bool CUtlVector<T, A>::HasElement( const T& src ) const
  1073. {
  1074. return ( Find(src) >= 0 );
  1075. }
  1076. //-----------------------------------------------------------------------------
  1077. // Element removal
  1078. //-----------------------------------------------------------------------------
  1079. template< typename T, class A >
  1080. void CUtlVector<T, A>::FastRemove( int elem )
  1081. {
  1082. Assert( IsValidIndex(elem) );
  1083. // Global scope to resolve conflict with Scaleform 4.0
  1084. ::Destruct( &Element(elem) );
  1085. if (m_Size > 0)
  1086. {
  1087. if ( elem != m_Size -1 )
  1088. memcpy( &Element(elem), &Element(m_Size-1), sizeof(T) );
  1089. --m_Size;
  1090. }
  1091. }
  1092. template< typename T, class A >
  1093. void CUtlVector<T, A>::Remove( int elem )
  1094. {
  1095. // Global scope to resolve conflict with Scaleform 4.0
  1096. ::Destruct( &Element(elem) );
  1097. ShiftElementsLeft(elem);
  1098. --m_Size;
  1099. }
  1100. template< typename T, class A >
  1101. bool CUtlVector<T, A>::FindAndRemove( const T& src )
  1102. {
  1103. int elem = Find( src );
  1104. if ( elem != -1 )
  1105. {
  1106. Remove( elem );
  1107. return true;
  1108. }
  1109. return false;
  1110. }
  1111. template< typename T, class A >
  1112. bool CUtlVector<T, A>::FindAndFastRemove( const T& src )
  1113. {
  1114. int elem = Find( src );
  1115. if ( elem != -1 )
  1116. {
  1117. FastRemove( elem );
  1118. return true;
  1119. }
  1120. return false;
  1121. }
  1122. template< typename T, class A >
  1123. void CUtlVector<T, A>::RemoveMultiple( int elem, int num )
  1124. {
  1125. Assert( elem >= 0 );
  1126. Assert( elem + num <= Count() );
  1127. // Global scope to resolve conflict with Scaleform 4.0
  1128. for (int i = elem + num; --i >= elem; )
  1129. ::Destruct(&Element(i));
  1130. ShiftElementsLeft(elem, num);
  1131. m_Size -= num;
  1132. }
  1133. template< typename T, class A >
  1134. void CUtlVector<T, A>::RemoveMultipleFromHead( int num )
  1135. {
  1136. Assert( num <= Count() );
  1137. // Global scope to resolve conflict with Scaleform 4.0
  1138. for (int i = num; --i >= 0; )
  1139. ::Destruct(&Element(i));
  1140. ShiftElementsLeft(0, num);
  1141. m_Size -= num;
  1142. }
  1143. template< typename T, class A >
  1144. void CUtlVector<T, A>::RemoveMultipleFromTail( int num )
  1145. {
  1146. Assert( num <= Count() );
  1147. // Global scope to resolve conflict with Scaleform 4.0
  1148. for (int i = m_Size-num; i < m_Size; i++)
  1149. ::Destruct(&Element(i));
  1150. m_Size -= num;
  1151. }
  1152. template< typename T, class A >
  1153. void CUtlVector<T, A>::RemoveAll()
  1154. {
  1155. for (int i = m_Size; --i >= 0; )
  1156. {
  1157. // Global scope to resolve conflict with Scaleform 4.0
  1158. ::Destruct(&Element(i));
  1159. }
  1160. m_Size = 0;
  1161. }
  1162. //-----------------------------------------------------------------------------
  1163. // Memory deallocation
  1164. //-----------------------------------------------------------------------------
  1165. template< typename T, class A >
  1166. inline void CUtlVector<T, A>::Purge()
  1167. {
  1168. RemoveAll();
  1169. m_Memory.Purge();
  1170. ResetDbgInfo();
  1171. }
  1172. template< typename T, class A >
  1173. inline void CUtlVector<T, A>::PurgeAndDeleteElements()
  1174. {
  1175. for( int i=0; i < m_Size; i++ )
  1176. {
  1177. delete Element(i);
  1178. }
  1179. Purge();
  1180. }
  1181. template< typename T, class A >
  1182. inline void CUtlVector<T, A>::Compact()
  1183. {
  1184. m_Memory.Purge(m_Size);
  1185. }
  1186. template< typename T, class A >
  1187. inline int CUtlVector<T, A>::NumAllocated() const
  1188. {
  1189. return m_Memory.NumAllocated();
  1190. }
  1191. //-----------------------------------------------------------------------------
  1192. // Data and memory validation
  1193. //-----------------------------------------------------------------------------
  1194. #ifdef DBGFLAG_VALIDATE
  1195. template< typename T, class A >
  1196. void CUtlVector<T, A>::Validate( CValidator &validator, char *pchName )
  1197. {
  1198. validator.Push( typeid(*this).name(), this, pchName );
  1199. m_Memory.Validate( validator, "m_Memory" );
  1200. validator.Pop();
  1201. }
  1202. #endif // DBGFLAG_VALIDATE
  1203. // A vector class for storing pointers, so that the elements pointed to by the pointers are deleted
  1204. // on exit.
  1205. template<class T> class CUtlVectorAutoPurge : public CUtlVector< T, CUtlMemory< T, int> >
  1206. {
  1207. public:
  1208. ~CUtlVectorAutoPurge( void )
  1209. {
  1210. this->PurgeAndDeleteElements();
  1211. }
  1212. };
  1213. // easy string list class with dynamically allocated strings. For use with V_SplitString, etc.
  1214. // Frees the dynamic strings in destructor.
  1215. class CUtlStringList : public CUtlVectorAutoPurge< char *>
  1216. {
  1217. public:
  1218. void CopyAndAddToTail( char const *pString ) // clone the string and add to the end
  1219. {
  1220. char *pNewStr = new char[1 + strlen( pString )];
  1221. V_strcpy( pNewStr, pString );
  1222. AddToTail( pNewStr );
  1223. }
  1224. static int __cdecl SortFunc( char * const * sz1, char * const * sz2 )
  1225. {
  1226. return strcmp( *sz1, *sz2 );
  1227. }
  1228. CUtlStringList(){}
  1229. CUtlStringList( char const *pString, char const *pSeparator )
  1230. {
  1231. SplitString( pString, pSeparator );
  1232. }
  1233. CUtlStringList( char const *pString, const char **pSeparators, int nSeparators )
  1234. {
  1235. SplitString2( pString, pSeparators, nSeparators );
  1236. }
  1237. void SplitString( char const *pString, char const *pSeparator )
  1238. {
  1239. V_SplitString( pString, pSeparator, *this );
  1240. }
  1241. void SplitString2( char const *pString, const char **pSeparators, int nSeparators )
  1242. {
  1243. V_SplitString2( pString, pSeparators, nSeparators, *this );
  1244. }
  1245. private:
  1246. CUtlStringList( const CUtlStringList &other ); // copying directly will cause double-release of the same strings; maybe we need to do a deep copy, but unless and until such need arises, this will guard against double-release
  1247. };
  1248. // <Sergiy> placing it here a few days before Cert to minimize disruption to the rest of codebase
  1249. class CSplitString: public CUtlVector<char*, CUtlMemory<char*, int> >
  1250. {
  1251. public:
  1252. CSplitString(const char *pString, const char *pSeparator);
  1253. CSplitString(const char *pString, const char **pSeparators, int nSeparators);
  1254. ~CSplitString();
  1255. //
  1256. // NOTE: If you want to make Construct() public and implement Purge() here, you'll have to free m_szBuffer there
  1257. //
  1258. private:
  1259. void Construct(const char *pString, const char **pSeparators, int nSeparators);
  1260. void PurgeAndDeleteElements();
  1261. private:
  1262. char *m_szBuffer; // a copy of original string, with '\0' instead of separators
  1263. };
  1264. #endif // CCVECTOR_H