CGrowableArray的声明 in DXUTmisc.h
1 //-------------------------------------------------------------------------------------- 2 // A growable array . 3 //-------------------------------------------------------------------------------------- 4 template<typename TYPE> class CGrowableArray 5 { 6 public: 7 CGrowableArray() 8 { 9 m_pData = NULL; m_nSize = 0; m_nMaxSize = 0; 10 } 11 CGrowableArray( const CGrowableArray <TYPE>& a ) 12 { 13 for( int i = 0; i < a.m_nSize; i++ ) Add( a.m_pData[i] ); 14 } 15 ~CGrowableArray() 16 { 17 RemoveAll(); 18 } 19 20 const TYPE& operator[]( int nIndex ) const 21 { 22 return GetAt( nIndex ); 23 } 24 TYPE& operator[]( int nIndex ) 25 { 26 return GetAt( nIndex ); 27 } 28 29 CGrowableArray& operator=( const CGrowableArray <TYPE>& a ) 30 { 31 if( this == &a ) return *this; RemoveAll(); for( int i = 0; i < a.m_nSize; 32 i++ ) Add( a.m_pData[i] ); return *this; 33 } 34 35 HRESULT SetSize( int nNewMaxSize ); 36 HRESULT Add( const TYPE& value ); 37 HRESULT Insert( int nIndex, const TYPE& value ); 38 HRESULT SetAt( int nIndex, const TYPE& value ); 39 TYPE& GetAt( int nIndex ) const 40 { 41 assert( nIndex >= 0 && nIndex < m_nSize ); return m_pData[nIndex]; 42 } 43 int GetSize() const 44 { 45 return m_nSize; 46 } 47 TYPE* GetData() 48 { 49 return m_pData; 50 } 51 bool Contains( const TYPE& value ) 52 { 53 return ( -1 != IndexOf( value ) ); 54 } 55 56 int IndexOf( const TYPE& value ) 57 { 58 return ( m_nSize > 0 ) ? IndexOf( value, 0, m_nSize ) : -1; 59 } 60 int IndexOf( const TYPE& value, int iStart ) 61 { 62 return IndexOf( value, iStart, m_nSize - iStart ); 63 } 64 int IndexOf( const TYPE& value, int nIndex, int nNumElements ); 65 66 int LastIndexOf( const TYPE& value ) 67 { 68 return ( m_nSize > 0 ) ? LastIndexOf( value, m_nSize - 1, m_nSize ) : -1; 69 } 70 int LastIndexOf( const TYPE& value, int nIndex ) 71 { 72 return LastIndexOf( value, nIndex, nIndex + 1 ); 73 } 74 int LastIndexOf( const TYPE& value, int nIndex, int nNumElements ); 75 76 HRESULT Remove( int nIndex ); 77 void RemoveAll() 78 { 79 SetSize( 0 ); 80 } 81 void Reset() 82 { 83 m_nSize = 0; 84 } 85 86 protected: 87 TYPE* m_pData; // the actual array of data 88 int m_nSize; // # of elements (upperBound - 1) 89 int m_nMaxSize; // max allocated 90 91 HRESULT SetSizeInternal( int nNewMaxSize ); // This version doesn‘t call ctor or dtor. 92 };
时间: 2024-10-06 01:19:06