📄 spcoll.cpp
字号:
/*
Cross Platform Core Code.
Copyright(R) 2001-2002 Balang Software.
All rights reserved.
Using:
class CSPArray;
class CSPPtrArray;
class CSPDWordArray;
class CSPObArray;
class CSPStringArray;
class CSPMapStringToPtr;
*/
#include "StdAfx.h"
#include "SpColl.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#ifndef _SP_ENABLE_INLINES
#define _SPCOLL_INLINE
#include "SpColl.inl"
#undef _SPCOLL_INLINE
#endif
/////////////////////////////////////////////////////////////////////////////
// CSPPtrArray out-of-line functions
CSPPtrArray::CSPPtrArray()
{
m_pData = NULL;
m_nSize = m_nMaxSize = m_nGrowBy = 0;
}
CSPPtrArray::~CSPPtrArray()
{
delete [] (BYTE*)m_pData;
}
void CSPPtrArray::SetSize(int nNewSize, int nGrowBy /* = -1 */)
{
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(void*) <= SIZE_T_MAX); // no overflow
#endif
m_pData = (void**) new BYTE[nNewSize * sizeof(void*)];
memset(m_pData, 0, nNewSize * sizeof(void*)); // 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(void*));
}
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(void*) <= SIZE_T_MAX); // no overflow
#endif
void** pNewData = (void**) new BYTE[nNewMax * sizeof(void*)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(void*));
// construct remaining elements
SP_ASSERT(nNewSize > m_nSize);
memset(&pNewData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(void*));
// get rid of old stuff (note: no destructors called)
delete [] (BYTE*)m_pData;
m_pData = pNewData;
m_nSize = nNewSize;
m_nMaxSize = nNewMax;
}
}
void CSPPtrArray::FreeExtra()
{
if (m_nSize != m_nMaxSize)
{
// shrink to desired size
#ifdef SIZE_T_MAX
SP_ASSERT((long)m_nSize * sizeof(void*) <= SIZE_T_MAX); // no overflow
#endif
void** pNewData = NULL;
if (m_nSize != 0)
{
pNewData = (void**) new BYTE[m_nSize * sizeof(void*)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(void*));
}
// get rid of old stuff (note: no destructors called)
delete [] (BYTE*)m_pData;
m_pData = pNewData;
m_nMaxSize = m_nSize;
}
}
void CSPPtrArray::SetAtGrow(int nIndex, void* newElement)
{
SP_ASSERT(nIndex >= 0);
if (nIndex >= m_nSize)
SetSize(nIndex+1);
m_pData[nIndex] = newElement;
}
void CSPPtrArray::InsertAt(int nIndex, void* newElement, int nCount /*=1*/)
{
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(void*));
// re-init slots we copied from
memset(&m_pData[nIndex], 0, nCount * sizeof(void*));
}
// insert new value in the gap
SP_ASSERT(nIndex + nCount <= m_nSize);
while (nCount--)
m_pData[nIndex++] = newElement;
}
void CSPPtrArray::RemoveAt(int nIndex, int nCount /* = 1 */)
{
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(void*));
m_nSize -= nCount;
}
void CSPPtrArray::InsertAt(int nStartIndex, CSPPtrArray* pNewArray)
{
SP_ASSERT(pNewArray != NULL);
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));
}
}
int CSPPtrArray::Append(const CSPPtrArray& src)
{
SP_ASSERT_VALID(this);
SP_ASSERT(this != &src); // cannot append to itself
int nOldSize = m_nSize;
SetSize(m_nSize + src.m_nSize);
memcpy(m_pData + nOldSize, src.m_pData, src.m_nSize * sizeof(void*));
return nOldSize;
}
void CSPPtrArray::Copy(const CSPPtrArray& src)
{
SP_ASSERT_VALID(this);
SP_ASSERT(this != &src); // cannot append to itself
SetSize(src.m_nSize);
memcpy(m_pData, src.m_pData, src.m_nSize * sizeof(void*));
}
#ifdef _DEBUG
void CSPPtrArray::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(void*)) );
}
}
void CSPPtrArray::Dump( ) const
{
Object::Dump( );
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSPDWordArray out-of-line functions
CSPDWordArray::CSPDWordArray()
{
m_pData = NULL;
m_nSize = m_nMaxSize = m_nGrowBy = 0;
}
CSPDWordArray::~CSPDWordArray()
{
delete [] (BYTE*)m_pData;
}
void CSPDWordArray::SetSize(int nNewSize, int nGrowBy /* = -1 */)
{
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(DWORD) <= SIZE_T_MAX); // no overflow
#endif
m_pData = (DWORD*) new BYTE[nNewSize * sizeof(DWORD)];
memset(m_pData, 0, nNewSize * sizeof(DWORD)); // 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(DWORD));
}
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(DWORD) <= SIZE_T_MAX); // no overflow
#endif
DWORD* pNewData = (DWORD*) new BYTE[nNewMax * sizeof(DWORD)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(DWORD));
// construct remaining elements
SP_ASSERT(nNewSize > m_nSize);
memset(&pNewData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(DWORD));
// get rid of old stuff (note: no destructors called)
delete [] (BYTE*)m_pData;
m_pData = pNewData;
m_nSize = nNewSize;
m_nMaxSize = nNewMax;
}
}
void CSPDWordArray::Copy(const CSPDWordArray& src)
{
SP_ASSERT_VALID(this);
SP_ASSERT(this != &src); // cannot append to itself
SetSize(src.m_nSize);
memcpy(m_pData, src.m_pData, src.m_nSize * sizeof(DWORD));
}
void CSPDWordArray::FreeExtra()
{
if (m_nSize != m_nMaxSize)
{
// shrink to desired size
#ifdef SIZE_T_MAX
SP_ASSERT((long)m_nSize * sizeof(DWORD) <= SIZE_T_MAX); // no overflow
#endif
DWORD* pNewData = NULL;
if (m_nSize != 0)
{
pNewData = (DWORD*) new BYTE[m_nSize * sizeof(DWORD)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(DWORD));
}
// get rid of old stuff (note: no destructors called)
delete [] (BYTE*)m_pData;
m_pData = pNewData;
m_nMaxSize = m_nSize;
}
}
void CSPDWordArray::SetAtGrow(int nIndex, DWORD newElement)
{
SP_ASSERT(nIndex >= 0);
if (nIndex >= m_nSize)
SetSize(nIndex+1);
m_pData[nIndex] = newElement;
}
void CSPDWordArray::InsertAt(int nIndex, DWORD newElement, int nCount /*=1*/)
{
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(DWORD));
// re-init slots we copied from
memset(&m_pData[nIndex], 0, nCount * sizeof(DWORD));
}
// insert new value in the gap
SP_ASSERT(nIndex + nCount <= m_nSize);
while (nCount--)
m_pData[nIndex++] = newElement;
}
void CSPDWordArray::RemoveAt(int nIndex, int nCount /* = 1 */)
{
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(DWORD));
m_nSize -= nCount;
}
void CSPDWordArray::InsertAt(int nStartIndex, CSPDWordArray* pNewArray)
{
SP_ASSERT(pNewArray != NULL);
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));
}
}
int CSPDWordArray::AddUnique( DWORD newElement )
{
for( int i=0; i<GetSize(); i++ )
{
if( ElementAt(i) == newElement )
return i;
}
return Add( newElement );
}
int SortDWord(const void *p1,const void *p2)
{
DWORD *pTemp1 = (DWORD *)p1;
DWORD *pTemp2 = (DWORD *)p2;
if( pTemp1 && pTemp2 && *pTemp1 < *pTemp2 )
return -1;
else if( pTemp1 && pTemp2 && *pTemp1 > *pTemp2 )
return 1;
return 0;
}
void CSPDWordArray::Sort( )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -