// Copyright (c) 1997-1999 Microsoft Corporation #if (_ATL_VER < 0x0300) ///////////////////////////////////////////////////////////////////////////// // Collection helpers - CSimpleArray & CSimpleMap #ifndef __SIMPLEARRAY__ #define __SIMPLEARRAY__ #ifndef ATLASSERT #define ATLASSERT(expr) _ASSERTE(expr) #endif template class CSimpleArray { public: T* m_aT; int m_nSize; int m_nAllocSize; // Construction/destruction CSimpleArray() : m_aT(NULL), m_nSize(0), m_nAllocSize(0) { } ~CSimpleArray() { RemoveAll(); } // Operations int GetSize() const { return m_nSize; } BOOL Add(T& t) { if(m_nSize == m_nAllocSize) { T* aT; int nNewAllocSize = (m_nAllocSize == 0) ? 1 : (m_nSize * 2); aT = (T*)realloc(m_aT, nNewAllocSize * sizeof(T)); if(aT == NULL) return FALSE; m_nAllocSize = nNewAllocSize; m_aT = aT; } m_nSize++; SetAtIndex(m_nSize - 1, t); return TRUE; } BOOL Remove(T& t) { int nIndex = Find(t); if(nIndex == -1) return FALSE; return RemoveAt(nIndex); } BOOL RemoveAt(int nIndex) { if(nIndex != (m_nSize - 1)) memmove((void*)&m_aT[nIndex], (void*)&m_aT[nIndex + 1], (m_nSize - (nIndex + 1)) * sizeof(T)); m_nSize--; return TRUE; } void RemoveAll() { if(m_aT != NULL) { free(m_aT); m_aT = NULL; } m_nSize = 0; m_nAllocSize = 0; } T& operator[] (int nIndex) const { ATLASSERT(nIndex >= 0 && nIndex < m_nSize); return m_aT[nIndex]; } T* GetData() const { return m_aT; } // Implementation void SetAtIndex(int nIndex, T& t) { ATLASSERT(nIndex >= 0 && nIndex < m_nSize); m_aT[nIndex] = t; } int Find(T& t) const { for(int i = 0; i < m_nSize; i++) { if(m_aT[i] == t) return i; } return -1; // not found } }; #endif __SIMPLEARRAY__ #endif