/****************************************************************************\ * * ARRAY.H ---Class declaration for the array structure * \****************************************************************************/ #ifndef _ARRAY_H #define _ARRAY_H /*Interface-------------------------------------------------------------------*/ template class array { private: int nLen, nMax; T *pData; void Destruct(); public: array(); ~array(); BOOL Append(T v); int Length() const; void ClearAll(); void DeleteAll(); T& operator[](int index); }; /*definitions of everything*/ #ifndef ARRAY_CXX #define ARRAY_CXX /*Implementation------------------------------------------------------------*/ template array::array(){ nLen = nMax = 0; pData = NULL; } template inline array::~array() { if (pData) ::MemFree(pData); pData = NULL; nMax = nLen = 0; } template inline int array::Length() const{ return nLen; } template inline T& array::operator[](int index){ ASSERT(index=0); ASSERT(pData); return pData[index]; } template BOOL array::Append(T v) { if (nLen == nMax){ nMax = nMax + 8; /* grow by bigger chunks */ T* pNew = (T*)::MemReAlloc(pData, sizeof(T)*nMax); if (pNew == NULL) return FALSE; pData = pNew; } ASSERT(pData); ASSERT(nMax); pData[nLen++] = v; return TRUE; } template void array::Destruct(){ while (nLen){ delete pData[--nLen]; } } template inline void array::ClearAll() { nLen = 0; } template inline void array::DeleteAll() { Destruct(); } #endif /* ARRAY_CXX */ #endif /* _ARRAY_H */