📄 spcoll.cpp
字号:
{
if( m_pData )
qsort( m_pData, GetSize(), sizeof(DWORD), SortDWord );
}
BOOL CSPDWordArray::IsEqualTo( CSPDWordArray & adw )
{
if( GetSize() != adw.GetSize() )
return FALSE;
for( int i=0; i<GetSize(); i++ )
{
if( ElementAt(i) != adw.ElementAt(i) )
return FALSE;
}
return TRUE;
}
DWORD * CSPDWordArray::GetData()
{
return m_pData;
}
void CSPDWordArray::Serialize(CSPArchive& ar)
{
SP_ASSERT_VALID(this);
if (ar.IsStoring())
{
ar << GetSize();
// write each item individually so that it will be byte-swapped
for (int i = 0; i < m_nSize; i++)
ar << m_pData[i];
}
else
{
int size;
ar >> size;
SetSize(size);
for (int i = 0; i < size; i++)
{
DWORD dw;
ar >> dw;
SetAt(i,dw);
}
}
}
#ifdef _DEBUG
void CSPDWordArray::AssertValid() const
{
Object::AssertValid();
if (m_pData == NULL)
{
SP_ASSERT(m_nSize == 0);
SP_ASSERT(m_nMaxSize == 0);
}
else
{
SP_ASSERT(m_nSize >= 0);
SP_ASSERT(m_nMaxSize >= 0);
SP_ASSERT(m_nSize <= m_nMaxSize);
SP_ASSERT( SP_IsValidAddress(m_pData, m_nMaxSize * sizeof(DWORD)) );
}
}
void CSPDWordArray::Dump( ) const
{
Object::Dump( );
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSPObArray out-of-line functions
CSPObArray::CSPObArray()
{
m_pData = NULL;
m_nSize = m_nMaxSize = m_nGrowBy = 0;
}
CSPObArray::~CSPObArray()
{
SP_ASSERT_VALID(this);
delete [] (BYTE*)m_pData;
}
void CSPObArray::SetSize(int nNewSize, int nGrowBy /* = -1 */)
{
SP_ASSERT_VALID(this);
SP_ASSERT(nNewSize >= 0);
if (nGrowBy != -1)
m_nGrowBy = nGrowBy; // set new size
if (nNewSize == 0)
{
// shrink to nothing
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
SP_ASSERT((long)nNewSize * sizeof(Object*) <= SIZE_T_MAX); // no overflow
#endif
m_pData = (Object**) new BYTE[nNewSize * sizeof(Object*)];
memset(m_pData, 0, nNewSize * sizeof(Object*)); // zero fill
m_nSize = m_nMaxSize = nNewSize;
}
else if (nNewSize <= m_nMaxSize)
{
// it fits
if (nNewSize > m_nSize)
{
// initialize the new elements
memset(&m_pData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(Object*));
}
m_nSize = nNewSize;
}
else
{
// Otherwise grow array
int nNewMax;
if (nNewSize < m_nMaxSize + m_nGrowBy)
nNewMax = m_nMaxSize + m_nGrowBy; // granularity
else
nNewMax = nNewSize; // no slush
#ifdef SIZE_T_MAX
SP_ASSERT((long)nNewMax * sizeof(Object*) <= SIZE_T_MAX); // no overflow
#endif
Object** pNewData = (Object**) new BYTE[nNewMax * sizeof(Object*)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(Object*));
// construct remaining elements
SP_ASSERT(nNewSize > m_nSize);
memset(&pNewData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(Object*));
// get rid of old stuff (note: no destructors called)
delete [] (BYTE*)m_pData;
m_pData = pNewData;
m_nSize = nNewSize;
m_nMaxSize = nNewMax;
}
}
void CSPObArray::FreeExtra()
{
SP_ASSERT_VALID(this);
if (m_nSize != m_nMaxSize)
{
// shrink to desired size
#ifdef SIZE_T_MAX
SP_ASSERT((long)m_nSize * sizeof(Object*) <= SIZE_T_MAX); // no overflow
#endif
Object** pNewData = NULL;
if (m_nSize != 0)
{
pNewData = (Object**) new BYTE[m_nSize * sizeof(Object*)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(Object*));
}
// get rid of old stuff (note: no destructors called)
delete [] (BYTE*)m_pData;
m_pData = pNewData;
m_nMaxSize = m_nSize;
}
}
void CSPObArray::SetAtGrow(int nIndex, Object* newElement)
{
SP_ASSERT_VALID(this);
SP_ASSERT(nIndex >= 0);
if (nIndex >= m_nSize)
SetSize(nIndex+1);
m_pData[nIndex] = newElement;
}
void CSPObArray::InsertAt(int nIndex, Object* newElement, int nCount /*=1*/)
{
SP_ASSERT_VALID(this);
SP_ASSERT(nIndex >= 0); // will expand to meet need
SP_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(Object*));
// re-init slots we copied from
memset(&m_pData[nIndex], 0, nCount * sizeof(Object*));
}
// insert new value in the gap
SP_ASSERT(nIndex + nCount <= m_nSize);
while (nCount--)
m_pData[nIndex++] = newElement;
}
void CSPObArray::RemoveAt(int nIndex, int nCount /* = 1 */)
{
SP_ASSERT_VALID(this);
SP_ASSERT(nIndex >= 0);
SP_ASSERT(nCount >= 0);
SP_ASSERT(nIndex + nCount <= m_nSize);
// just remove a range
int nMoveCount = m_nSize - (nIndex + nCount);
if (nMoveCount)
memcpy(&m_pData[nIndex], &m_pData[nIndex + nCount],
nMoveCount * sizeof(Object*));
m_nSize -= nCount;
}
void CSPObArray::InsertAt(int nStartIndex, CSPObArray* pNewArray)
{
SP_ASSERT_VALID(this);
SP_ASSERT(pNewArray != NULL);
SP_ASSERT_VALID(pNewArray);
SP_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));
}
}
// Diagnostics
#ifdef _DEBUG
void CSPObArray::Dump() const
{
Object::Dump( );
}
void CSPObArray::AssertValid() const
{
Object::AssertValid();
if (m_pData == NULL)
{
SP_ASSERT(m_nSize == 0);
SP_ASSERT(m_nMaxSize == 0);
}
else
{
SP_ASSERT(m_nSize >= 0);
SP_ASSERT(m_nMaxSize >= 0);
SP_ASSERT(m_nSize <= m_nMaxSize);
SP_ASSERT(SP_IsValidAddress(m_pData, m_nMaxSize * sizeof(Object*)));
}
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSPStringArray out-of-line functions
CSPStringArray::CSPStringArray()
{
m_pData = NULL;
m_nSize = m_nMaxSize = m_nGrowBy = 0;
}
CSPStringArray::~CSPStringArray()
{
SP_ASSERT_VALID(this);
SPDestructElements(m_pData, m_nSize);
delete [] (BYTE*)m_pData;
}
void CSPStringArray::SetSize(int nNewSize, int nGrowBy /* = -1 */)
{
SP_ASSERT_VALID(this);
SP_ASSERT(nNewSize >= 0);
if (nGrowBy != -1)
m_nGrowBy = nGrowBy; // set new size
if (nNewSize == 0)
{
// shrink to nothing
SPDestructElements(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
SP_ASSERT((long)nNewSize * sizeof(CSPString) <= SIZE_T_MAX); // no overflow
#endif
m_pData = (CSPString*) new BYTE[nNewSize * sizeof(CSPString)];
SPConstructElements(m_pData, nNewSize);
m_nSize = m_nMaxSize = nNewSize;
}
else if (nNewSize <= m_nMaxSize)
{
// it fits
if (nNewSize > m_nSize)
{
// initialize the new elements
SPConstructElements(&m_pData[m_nSize], nNewSize-m_nSize);
}
else if (m_nSize > nNewSize) // destroy the old elements
SPDestructElements(&m_pData[nNewSize], m_nSize-nNewSize);
m_nSize = nNewSize;
}
else
{
// Otherwise grow array
int nNewMax;
if (nNewSize < m_nMaxSize + m_nGrowBy)
nNewMax = m_nMaxSize + m_nGrowBy; // granularity
else
nNewMax = nNewSize; // no slush
#ifdef SIZE_T_MAX
SP_ASSERT((long)nNewMax * sizeof(CSPString) <= SIZE_T_MAX); // no overflow
#endif
CSPString* pNewData = (CSPString*) new BYTE[nNewMax * sizeof(CSPString)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(CSPString));
// construct remaining elements
SP_ASSERT(nNewSize > m_nSize);
SPConstructElements(&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 CSPStringArray::Append(const CSPStringArray& src)
{
SP_ASSERT_VALID(this);
SP_ASSERT(this != &src); // cannot append to itself
int nOldSize = m_nSize;
SetSize(m_nSize + src.m_nSize);
SPCopyElements(m_pData + nOldSize, src.m_pData, src.m_nSize);
return nOldSize;
}
void CSPStringArray::Copy(const CSPStringArray& src)
{
SP_ASSERT_VALID(this);
SP_ASSERT(this != &src); // cannot append to itself
SetSize(src.m_nSize);
SPCopyElements(m_pData, src.m_pData, src.m_nSize);
}
void CSPStringArray::FreeExtra()
{
SP_ASSERT_VALID(this);
if (m_nSize != m_nMaxSize)
{
// shrink to desired size
#ifdef SIZE_T_MAX
SP_ASSERT((long)m_nSize * sizeof(CSPString) <= SIZE_T_MAX); // no overflow
#endif
CSPString* pNewData = NULL;
if (m_nSize != 0)
{
pNewData = (CSPString*) new BYTE[m_nSize * sizeof(CSPString)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(CSPString));
}
// get rid of old stuff (note: no destructors called)
delete [] (BYTE*)m_pData;
m_pData = pNewData;
m_nMaxSize = m_nSize;
}
}
void CSPStringArray::SetAtGrow(int nIndex, const char* newElement)
{
SP_ASSERT_VALID(this);
SP_ASSERT(nIndex >= 0);
if (nIndex >= m_nSize)
SetSize(nIndex+1);
m_pData[nIndex] = newElement;
}
void CSPStringArray::InsertAt(int nIndex, const char* newElement, int nCount /*=1*/)
{
SP_ASSERT_VALID(this);
SP_ASSERT(nIndex >= 0); // will expand to meet need
SP_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(CSPString));
// re-init slots we copied from
SPConstructElements(&m_pData[nIndex], nCount);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -