📄 spcoll.h
字号:
// just remove a range
int nMoveCount = m_nSize - (nIndex + nCount);
SPDestructElements<TYPE>(&m_pData[nIndex], nCount);
if (nMoveCount)
memmove(&m_pData[nIndex], &m_pData[nIndex + nCount],
nMoveCount * sizeof(TYPE));
m_nSize -= nCount;
}
template<class TYPE, class ARG_TYPE>
void CSPArray<TYPE, ARG_TYPE>::InsertAt(int nStartIndex, CSPArray* 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));
}
}
template<class TYPE, class ARG_TYPE>
void CSPArray<TYPE, ARG_TYPE>::Serialize(CSPArchive& ar)
{
SP_ASSERT_VALID(this);
// Object::Serialize(ar);
if (ar.IsStoring())
{
ar.WriteCount(m_nSize);
}
else
{
DWORD nOldSize = ar.ReadCount();
SetSize(nOldSize, -1);
}
SPSerializeElements<TYPE>(ar, m_pData, m_nSize);
}
#ifdef _DEBUG
template<class TYPE, class ARG_TYPE>
void CSPArray<TYPE, ARG_TYPE>::Dump() const
{
Object::Dump();
}
template<class TYPE, class ARG_TYPE>
void CSPArray<TYPE, ARG_TYPE>::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((void*)m_pData, m_nMaxSize * sizeof(TYPE)));
}
}
#endif //_DEBUG
////////////////////////////////////////////////////////////////////////////
// CSPDWordArray
class CSPDWordArray : public Object
{
public:
// Construction
CSPDWordArray();
// Attributes
int GetSize() const;
int GetUpperBound() const;
void SetSize(int nNewSize, int nGrowBy = -1);
void Copy( const CSPDWordArray & src );
// Operations
// Clean up
void FreeExtra();
void RemoveAll();
// Accessing elements
DWORD GetAt(int nIndex) const;
void SetAt(int nIndex, DWORD newElement);
DWORD& ElementAt(int nIndex);
// Potentially growing the array
void SetAtGrow(int nIndex, DWORD newElement);
int Add(DWORD newElement);
// overloaded operator helpers
DWORD operator[](int nIndex) const;
DWORD& operator[](int nIndex);
// Operations that move elements around
void InsertAt(int nIndex, DWORD newElement, int nCount = 1);
void RemoveAt(int nIndex, int nCount = 1);
void InsertAt(int nStartIndex, CSPDWordArray* pNewArray);
//
int AddUnique( DWORD newElement );
void Sort( );
BOOL IsEqualTo( CSPDWordArray & adw );
DWORD * GetData();
// Implementation
protected:
DWORD* m_pData; // the actual array of data
int m_nSize; // # of elements (upperBound - 1)
int m_nMaxSize; // max allocated
int m_nGrowBy; // grow amount
public:
~CSPDWordArray();
void Serialize(CSPArchive&);
#ifdef _DEBUG
public:
virtual void AssertValid() const;
virtual void Dump( ) const;
#endif
};
////////////////////////////////////////////////////////////////////////////
// CSPPrtArray
class CSPPtrArray : public Object
{
public:
// Construction
CSPPtrArray();
// Attributes
int GetSize() const;
int GetUpperBound() const;
void SetSize(int nNewSize, int nGrowBy = -1);
// Operations
// Clean up
void FreeExtra();
void RemoveAll();
// Accessing elements
void* GetAt(int nIndex) const;
void SetAt(int nIndex, void* newElement);
void*& ElementAt(int nIndex);
// Potentially growing the array
void SetAtGrow(int nIndex, void* newElement);
int Add(void* newElement);
// overloaded operator helpers
void* operator[](int nIndex) const;
void*& operator[](int nIndex);
// Operations that move elements around
void InsertAt(int nIndex, void* newElement, int nCount = 1);
void RemoveAt(int nIndex, int nCount = 1);
void InsertAt(int nStartIndex, CSPPtrArray* pNewArray);
int Append(const CSPPtrArray& src);
void Copy(const CSPPtrArray& src);
// Implementation
protected:
void** m_pData; // the actual array of data
int m_nSize; // # of elements (upperBound - 1)
int m_nMaxSize; // max allocated
int m_nGrowBy; // grow amount
public:
~CSPPtrArray();
#ifdef _DEBUG
public:
virtual void AssertValid() const;
virtual void Dump( ) const;
#endif
};
////////////////////////////////////////////////////////////////////////////
// CSPObArray
class CSPObArray : public Object
{
public:
// Construction
CSPObArray();
// Attributes
int GetSize() const;
int GetUpperBound() const;
void SetSize(int nNewSize, int nGrowBy = -1);
// Operations
// Clean up
void FreeExtra();
void RemoveAll();
// Accessing elements
Object* GetAt(int nIndex) const;
void SetAt(int nIndex, Object* newElement);
Object*& ElementAt(int nIndex);
// Potentially growing the array
void SetAtGrow(int nIndex, Object* newElement);
int Add(Object* newElement);
// overloaded operator helpers
Object* operator[](int nIndex) const;
Object*& operator[](int nIndex);
// Operations that move elements around
void InsertAt(int nIndex, Object* newElement, int nCount = 1);
void RemoveAt(int nIndex, int nCount = 1);
void InsertAt(int nStartIndex, CSPObArray* pNewArray);
// Implementation
protected:
Object** m_pData; // the actual array of data
int m_nSize; // # of elements (upperBound - 1)
int m_nMaxSize; // max allocated
int m_nGrowBy; // grow amount
public:
~CSPObArray();
#ifdef _DEBUG
void Dump( ) const;
void AssertValid() const;
#endif
};
////////////////////////////////////////////////////////////////////////////
// CSPStringArray
class CSPStringArray : public Object
{
public:
// Construction
CSPStringArray();
static CSPStringArray * m_pSortSPStringArray;
// Attributes
int GetSize() const;
int GetUpperBound() const;
void SetSize(int nNewSize, int nGrowBy = -1);
int Append(const CSPStringArray& src);
void Copy(const CSPStringArray& src);
// Operations
// Clean up
void FreeExtra();
void RemoveAll();
// Accessing elements
CSPString GetAt(int nIndex) const;
void SetAt(int nIndex, const char* newElement);
CSPString& ElementAt(int nIndex);
// Potentially growing the array
void SetAtGrow(int nIndex, const char* newElement);
int Add(const char* newElement);
// overloaded operator helpers
CSPString operator[](int nIndex) const;
CSPString& operator[](int nIndex);
// Operations that move elements around
void InsertAt(int nIndex, const char* newElement, int nCount = 1);
void RemoveAt(int nIndex, int nCount = 1);
void InsertAt(int nStartIndex, CSPStringArray* pNewArray);
BOOL GetSortIDArray( CSPDWordArray & adwSortID );
// Implementation
protected:
CSPString* m_pData; // the actual array of data
int m_nSize; // # of elements (upperBound - 1)
int m_nMaxSize; // max allocated
int m_nGrowBy; // grow amount
void SPConstructElements(CSPString* pElements, int nCount);
void SPDestructElements(CSPString* pElements, int nCount);
public:
~CSPStringArray();
void Serialize(CSPArchive&);
#ifdef _DEBUG
void Dump() const;
void AssertValid() const;
#endif
};
/////////////////////////////////////////////////////////////////////////////
// CSPPlex
struct CSPPlex // warning variable length structure
{
CSPPlex* pNext;
#if (_AFX_PACKING >= 8)
DWORD dwReserved[1]; // align on 8 byte boundary
#endif
// BYTE data[maxNum*elementSize];
void* data() { return this+1; }
static CSPPlex* PASCAL Create(CSPPlex*& head, UINT nMax, UINT cbElement);
// like 'calloc' but no zero fill
// may throw memory exceptions
void FreeDataChain(); // free this one and links
};
/////////////////////////////////////////////////////////////////////////////
// CSPMapStringToPtr
// abstract iteration position
struct __SPPOSITION { };
typedef __SPPOSITION* SPPOSITION;
#define BEFORE_START_SPPOSITION ((SPPOSITION)-1L)
class CSPMapStringToPtr : public Object
{
protected:
// Association
struct CAssoc
{
CAssoc* pNext;
UINT nHashValue; // needed for efficient iteration
CSPString key;
void* value;
};
public:
// Construction
CSPMapStringToPtr(int nBlockSize = 10);
// Attributes
// number of elements
int GetCount() const;
BOOL IsEmpty() const;
// Lookup
BOOL Lookup(LPCTSTR key, void*& rValue) const;
BOOL LookupKey(LPCTSTR key, LPCTSTR& rKey) const;
// Operations
// Lookup and add if not there
void*& operator[](LPCTSTR key);
// add a new (key, value) pair
void SetAt(LPCTSTR key, void* newValue);
// removing existing (key, ?) pair
BOOL RemoveKey(LPCTSTR key);
void RemoveAll();
// iterating all (key, value) pairs
SPPOSITION GetStartPosition() const;
void GetNextAssoc(SPPOSITION& rNextPosition, CSPString& rKey, void*& rValue) const;
// advanced features for derived classes
UINT GetHashTableSize() const;
void InitHashTable(UINT hashSize, BOOL bAllocNow = TRUE);
// Overridables: special non-virtual (see map implementation for details)
// Routine used to user-provided hash keys
UINT HashKey(LPCTSTR key) const;
// Implementation
protected:
CAssoc** m_pHashTable;
UINT m_nHashTableSize;
int m_nCount;
CAssoc* m_pFreeList;
struct CSPPlex* m_pBlocks;
int m_nBlockSize;
CAssoc* NewAssoc();
void FreeAssoc(CAssoc*);
CAssoc* GetAssocAt(LPCTSTR, UINT&) const;
public:
~CSPMapStringToPtr();
#ifdef _DEBUG
void Dump() const;
void AssertValid() const;
#endif
protected:
// local typedefs for CTypedPtrMap class template
typedef CSPString BASE_KEY;
typedef LPCTSTR BASE_ARG_KEY;
typedef void* BASE_VALUE;
typedef void* BASE_ARG_VALUE;
};
#ifdef _SP_ENABLE_INLINES
#define _SPCOLL_INLINE inline
#include "SpColl.inl"
#undef _SPCOLL_INLINE
#endif
#endif //__SP_COLLECT_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -