/******************************************************************************* * * (C) COPYRIGHT MICROSOFT CORPORATION, 1998 * * TITLE: SIMARRAY.H * * VERSION: 1.0 * * AUTHOR: ShaunIv * * DATE: 5/4/1999 * * DESCRIPTION: Dynamic array template class * *******************************************************************************/ #ifndef __SIMARRAY_H_INCLUDED #define __SIMARRAY_H_INCLUDED template class CSimpleDynamicArray { private: int m_nSize; int m_nMaxSize; int m_nGrowSize; T *m_pArray; enum { eGrowSize = 10 // The number of items to add each time the array grows. }; public: CSimpleDynamicArray(void) : m_nSize(0), m_nMaxSize(0), m_nGrowSize(eGrowSize), m_pArray(NULL) { } CSimpleDynamicArray( int nInitialSize, int nGrowSize=0 ) : m_nSize(0), m_nMaxSize(0), m_nGrowSize(nGrowSize ? nGrowSize : eGrowSize), m_pArray(NULL) { GrowTo(nInitialSize); } CSimpleDynamicArray( const CSimpleDynamicArray &other ) : m_nSize(0), m_nMaxSize(0), m_nGrowSize(eGrowSize), m_pArray(NULL) { Append(other); } virtual ~CSimpleDynamicArray(void) { Destroy(); } CSimpleDynamicArray &operator=( const CSimpleDynamicArray &other ) { if (this != &other) { Destroy(); Append(other); } return *this; } void Destroy(void) { if (m_pArray) { delete[] m_pArray; m_pArray = NULL; } m_nSize = m_nMaxSize = 0; } void Append( const CSimpleDynamicArray &other ) { if (GrowTo( m_nSize + other.Size() )) { for (int i=0;i= 0 && nIndex <= m_nSize) { // // Make room for the new item by moving all items above up by one slot // for (int i=Size();i>nIndex;i--) { m_pArray[i] = m_pArray[i-1]; } // // Save the new item // m_pArray[nIndex] = element; // // We're now one larger // m_nSize++; // // Return the index of the slot we used // return nIndex; } } // // Return an error // return -1; } void Delete( int nItem ) { if (nItem >= 0 && nItem < m_nSize && m_pArray) { T *pTmpArray = new T[m_nMaxSize]; if (pTmpArray) { T *pSrc, *pTgt; pSrc = m_pArray; pTgt = pTmpArray; for (int i=0;i= 0);} void Size( int nSize ) { m_nSize = nSize;} void MaxSize( int nMaxSize ) { m_nMaxSize = nMaxSize;} void GrowSize( int nGrowSize ) { m_nGrowSize = nGrowSize;} int Size(void) const { return m_nSize;} int MaxSize(void) const { return m_nMaxSize;} int GrowSize(void) const { return m_nGrowSize;} const T *Array(void) const { return m_pArray;} const T &operator[](int nIndex) const { return m_pArray[nIndex];} T &operator[](int nIndex) { return m_pArray[nIndex];} }; #endif