| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- // This is a part of the Microsoft Foundation Classes C++ library.
- // Copyright (C) 1992-1998 Microsoft Corporation
- // All rights reserved.
- //
- // This source code is only intended as a supplement to the
- // Microsoft Foundation Classes Reference and related
- // electronic documentation provided with the library.
- // See these sources for detailed information regarding the
- // Microsoft Foundation Classes product.
- /////////////////////////////////////////////////////////////////////////////
- //
- // Implementation of parameterized Array
- //
- /////////////////////////////////////////////////////////////////////////////
- // NOTE: we allocate an array of 'm_nMaxSize' elements, but only
- // the current size 'm_nSize' contains properly constructed
- // objects.
- #include "stdafx.h"
- #ifdef AFX_COLL_SEG
- #pragma code_seg(AFX_COLL_SEG)
- #endif
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define new DEBUG_NEW
- /////////////////////////////////////////////////////////////////////////////
- #include "elements.h" // used for special creation
- static void ConstructElements(CString* pNewData, int nCount)
- {
- ASSERT(nCount >= 0);
- while (nCount--)
- {
- ConstructElement(pNewData);
- pNewData++;
- }
- }
- static void DestructElements(CString* pOldData, int nCount)
- {
- ASSERT(nCount >= 0);
- while (nCount--)
- {
- DestructElement(pOldData);
- pOldData++;
- }
- }
- static void CopyElements(CString* pDest, CString* pSrc, int nCount)
- {
- ASSERT(nCount >= 0);
- while (nCount--)
- {
- *pDest = *pSrc;
- ++pDest;
- ++pSrc;
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- CStringArray::CStringArray()
- {
- m_pData = NULL;
- m_nSize = m_nMaxSize = m_nGrowBy = 0;
- }
- CStringArray::~CStringArray()
- {
- ASSERT_VALID(this);
- DestructElements(m_pData, m_nSize);
- delete[] (BYTE*)m_pData;
- }
- void CStringArray::SetSize(int nNewSize, int nGrowBy)
- {
- ASSERT_VALID(this);
- ASSERT(nNewSize >= 0);
- if (nGrowBy != -1)
- m_nGrowBy = nGrowBy; // set new size
- if (nNewSize == 0)
- {
- // shrink to nothing
- DestructElements(m_pData, m_nSize);
- delete[] (BYTE*)m_pData;
- m_pData = NULL;
- m_nSize = m_nMaxSize = 0;
- }
- else if (m_pData == NULL)
- {
- // create one with exact size
- #ifdef SIZE_T_MAX
- ASSERT(nNewSize <= SIZE_T_MAX/sizeof(CString)); // no overflow
- #endif
- m_pData = (CString*) new BYTE[nNewSize * sizeof(CString)];
- ConstructElements(m_pData, nNewSize);
- m_nSize = m_nMaxSize = nNewSize;
- }
- else if (nNewSize <= m_nMaxSize)
- {
- // it fits
- if (nNewSize > m_nSize)
- {
- // initialize the new elements
- ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize);
- }
- else if (m_nSize > nNewSize) // destroy the old elements
- DestructElements(&m_pData[nNewSize], m_nSize-nNewSize);
- m_nSize = nNewSize;
- }
- else
- {
- // otherwise, grow array
- int nGrowBy = m_nGrowBy;
- if (nGrowBy == 0)
- {
- // heuristically determine growth when nGrowBy == 0
- // (this avoids heap fragmentation in many situations)
- nGrowBy = min(1024, max(4, m_nSize / 8));
- }
- int nNewMax;
- if (nNewSize < m_nMaxSize + nGrowBy)
- nNewMax = m_nMaxSize + nGrowBy; // granularity
- else
- nNewMax = nNewSize; // no slush
- ASSERT(nNewMax >= m_nMaxSize); // no wrap around
- #ifdef SIZE_T_MAX
- ASSERT(nNewMax <= SIZE_T_MAX/sizeof(CString)); // no overflow
- #endif
- CString* pNewData = (CString*) new BYTE[nNewMax * sizeof(CString)];
- // copy new data from old
- memcpy(pNewData, m_pData, m_nSize * sizeof(CString));
- // construct remaining elements
- ASSERT(nNewSize > m_nSize);
- ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize);
- // get rid of old stuff (note: no destructors called)
- delete[] (BYTE*)m_pData;
- m_pData = pNewData;
- m_nSize = nNewSize;
- m_nMaxSize = nNewMax;
- }
- }
- int CStringArray::Append(const CStringArray& src)
- {
- ASSERT_VALID(this);
- ASSERT(this != &src); // cannot append to itself
- int nOldSize = m_nSize;
- SetSize(m_nSize + src.m_nSize);
- CopyElements(m_pData + nOldSize, src.m_pData, src.m_nSize);
- return nOldSize;
- }
- void CStringArray::Copy(const CStringArray& src)
- {
- ASSERT_VALID(this);
- ASSERT(this != &src); // cannot append to itself
- SetSize(src.m_nSize);
- CopyElements(m_pData, src.m_pData, src.m_nSize);
- }
- void CStringArray::FreeExtra()
- {
- ASSERT_VALID(this);
- if (m_nSize != m_nMaxSize)
- {
- // shrink to desired size
- #ifdef SIZE_T_MAX
- ASSERT(m_nSize <= SIZE_T_MAX/sizeof(CString)); // no overflow
- #endif
- CString* pNewData = NULL;
- if (m_nSize != 0)
- {
- pNewData = (CString*) new BYTE[m_nSize * sizeof(CString)];
- // copy new data from old
- memcpy(pNewData, m_pData, m_nSize * sizeof(CString));
- }
- // get rid of old stuff (note: no destructors called)
- delete[] (BYTE*)m_pData;
- m_pData = pNewData;
- m_nMaxSize = m_nSize;
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- void CStringArray::SetAtGrow(int nIndex, LPCTSTR newElement)
- {
- ASSERT_VALID(this);
- ASSERT(nIndex >= 0);
- if (nIndex >= m_nSize)
- SetSize(nIndex+1);
- m_pData[nIndex] = newElement;
- }
- void CStringArray::SetAtGrow(int nIndex, const CString& newElement)
- {
- ASSERT_VALID(this);
- ASSERT(nIndex >= 0);
- if (nIndex >= m_nSize)
- SetSize(nIndex+1);
- m_pData[nIndex] = newElement;
- }
- void CStringArray::InsertEmpty(int nIndex, int nCount)
- {
- ASSERT_VALID(this);
- ASSERT(nIndex >= 0); // will expand to meet need
- ASSERT(nCount > 0); // zero or negative size not allowed
- if (nIndex >= m_nSize)
- {
- // adding after the end of the array
- SetSize(nIndex + nCount); // grow so nIndex is valid
- }
- else
- {
- // inserting in the middle of the array
- int nOldSize = m_nSize;
- SetSize(m_nSize + nCount); // grow it to new size
- // shift old data up to fill gap
- memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],
- (nOldSize-nIndex) * sizeof(CString));
- // re-init slots we copied from
- ConstructElements(&m_pData[nIndex], nCount);
- }
- // insert new value in the gap
- ASSERT(nIndex + nCount <= m_nSize);
- }
- void CStringArray::InsertAt(int nIndex, LPCTSTR newElement, int nCount)
- {
- // make room for new elements
- InsertEmpty(nIndex, nCount);
- // copy elements into the empty space
- CString temp = newElement;
- while (nCount--)
- m_pData[nIndex++] = temp;
- }
- void CStringArray::InsertAt(int nIndex, const CString& newElement, int nCount)
- {
- // make room for new elements
- InsertEmpty(nIndex, nCount);
- // copy elements into the empty space
- while (nCount--)
- m_pData[nIndex++] = newElement;
- }
- void CStringArray::RemoveAt(int nIndex, int nCount)
- {
- ASSERT_VALID(this);
- ASSERT(nIndex >= 0);
- ASSERT(nCount >= 0);
- ASSERT(nIndex + nCount <= m_nSize);
- // just remove a range
- int nMoveCount = m_nSize - (nIndex + nCount);
- DestructElements(&m_pData[nIndex], nCount);
- if (nMoveCount)
- memmove(&m_pData[nIndex], &m_pData[nIndex + nCount],
- nMoveCount * sizeof(CString));
- m_nSize -= nCount;
- }
- void CStringArray::InsertAt(int nStartIndex, CStringArray* pNewArray)
- {
- ASSERT_VALID(this);
- ASSERT(pNewArray != NULL);
- ASSERT_KINDOF(CStringArray, pNewArray);
- ASSERT_VALID(pNewArray);
- ASSERT(nStartIndex >= 0);
- if (pNewArray->GetSize() > 0)
- {
- InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
- for (int i = 0; i < pNewArray->GetSize(); i++)
- SetAt(nStartIndex + i, pNewArray->GetAt(i));
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // Serialization
- void CStringArray::Serialize(CArchive& ar)
- {
- ASSERT_VALID(this);
- CObject::Serialize(ar);
- if (ar.IsStoring())
- {
- ar.WriteCount(m_nSize);
- for (int i = 0; i < m_nSize; i++)
- ar << m_pData[i];
- }
- else
- {
- DWORD nOldSize = ar.ReadCount();
- SetSize(nOldSize);
- for (int i = 0; i < m_nSize; i++)
- ar >> m_pData[i];
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // Diagnostics
- #ifdef _DEBUG
- void CStringArray::Dump(CDumpContext& dc) const
- {
- CObject::Dump(dc);
- dc << "with " << m_nSize << " elements";
- if (dc.GetDepth() > 0)
- {
- for (int i = 0; i < m_nSize; i++)
- dc << "\n\t[" << i << "] = " << m_pData[i];
- }
- dc << "\n";
- }
- void CStringArray::AssertValid() const
- {
- CObject::AssertValid();
- if (m_pData == NULL)
- {
- ASSERT(m_nSize == 0);
- ASSERT(m_nMaxSize == 0);
- }
- else
- {
- ASSERT(m_nSize >= 0);
- ASSERT(m_nMaxSize >= 0);
- ASSERT(m_nSize <= m_nMaxSize);
- ASSERT(AfxIsValidAddress(m_pData, m_nMaxSize * sizeof(CString)));
- }
- }
- #endif //_DEBUG
- #ifdef AFX_INIT_SEG
- #pragma code_seg(AFX_INIT_SEG)
- #endif
- IMPLEMENT_SERIAL(CStringArray, CObject, 0)
- /////////////////////////////////////////////////////////////////////////////
|