(1)CObArray 简介
CObArray类支持CObject指针数组。这些对象数组近似于C中的数组,但不同的是它们可以动态地增减。
(2)成员函数
|
CObArray
|
为CObject指针构造空数组
|
|
GetSize
|
获取数组中的元素数
|
|
GetUpperBound
|
返回最大有效索引
|
|
SetSize
|
设置数组中包含的元素数
|
|
FreeExtra
|
释放当前数组上界以上的所有没有使用的内存
|
|
RemoveAll
|
删除数组中所有元素
|
|
GetAt
|
返回给定索引的值
|
|
SetAt
|
设定给定索引的值;并且数组不允许扩展
|
|
ElementAt
|
返回数组内部元素指针的临时参考
|
|
GetData
|
允许访问数组中的元素,它不能为NULL
|
|
SetAtGrow
|
设置给定索引的值,如果需要就扩展数组
|
|
Add
|
在数组尾部增加元素,如果需要就扩展数组
|
|
Append
|
为该数组附加另一个数组,如果需要就扩展数组
|
|
Copy
|
拷贝另一个数组到该数组中,如果需要就扩展数组
|
|
InsertAt
|
在指定索引处插入一个元素(或在另一个数组中插入所有元素)
|
|
RemoveAt
|
删除指定索引处的数组元素
|
(3)源码
// CObArray.h
/////////////////////////////////////////////////////////////////////////////
//
//
// 整理的微软CObArray源码,删除了部分功能(如Serialize/Dump/AssertValid等)
//
/////////////////////////////////////////////////////////////////////////////
#ifndef _CObArray_H__
#define _CObArray_H__
class CObArray : public CObject
{
public:
// Construction
CObArray();
// Attributes
INT_PTR GetSize() const;
INT_PTR GetCount() const;
BOOL IsEmpty() const;
INT_PTR GetUpperBound() const;
void SetSize(INT_PTR nNewSize, INT_PTR nGrowBy = -1);
// Operations
// Clean up
void FreeExtra();
void RemoveAll();
// Accessing elements
CObject* GetAt(INT_PTR nIndex) const;
void SetAt(INT_PTR nIndex, CObject* newElement);
CObject*& ElementAt(INT_PTR nIndex);
// Direct Access to the element data (may return NULL)
const CObject** GetData() const;
CObject** GetData();
// Potentially growing the array
void SetAtGrow(INT_PTR nIndex, CObject* newElement);
INT_PTR Add(CObject* newElement);
INT_PTR Append(const CObArray& src);
void Copy(const CObArray& src);
// overloaded operator helpers
CObject* operator[](INT_PTR nIndex) const;
CObject*& operator[](INT_PTR nIndex);
// Operations that move elements around
void InsertAt(INT_PTR nIndex, CObject* newElement, INT_PTR nCount = 1);
void RemoveAt(INT_PTR nIndex, INT_PTR nCount = 1);
void InsertAt(INT_PTR nStartIndex, CObArray* pNewArray);
// Implementation
protected:
CObject** m_pData; // the actual array of data
INT_PTR m_nSize; // # of elements (upperBound - 1)
INT_PTR m_nMaxSize; // max allocated
INT_PTR m_nGrowBy; // grow amount
public:
~CObArray();
protected:
// local typedefs for class templates
typedef CObject* BASE_TYPE;
typedef CObject* BASE_ARG_TYPE;
};
#endif
CObArray.cpp
/////////////////////////////////////////////////////////////////////////////
//
// 整理的微软CObArray源码,删除了部分功能(如Serialize/Dump/AssertValid等)
// 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"
#include <wchar.h>
#include "CObArray.h"
/////////////////////////////////////////////////////////////////////////////
CObArray::CObArray()
{
m_pData = NULL;
m_nSize = m_nMaxSize = m_nGrowBy = 0;
}
CObArray::~CObArray()
{
ASSERT_VALID(this);
delete[] (BYTE*)m_pData;
}
void CObArray::SetSize(INT_PTR nNewSize, INT_PTR nGrowBy)
{
ASSERT_VALID(this);
ASSERT(nNewSize >= 0);
if(nNewSize < 0 )
AfxThrowInvalidArgException();
if (nGrowBy >= 0)
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
ASSERT(nNewSize <= SIZE_T_MAX/sizeof(CObject*)); // no overflow
#endif
m_pData = (CObject**) new BYTE[nNewSize * sizeof(CObject*)];
memset(m_pData, 0, nNewSize * sizeof(CObject*)); // 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(CObject*));
}
m_nSize = nNewSize;
}
else
{
// otherwise, grow array
INT_PTR nGrowBySize = m_nGrowBy;
if (nGrowBySize == 0)
{
// heuristically determine growth when nGrowBy == 0
// (this avoids heap fragmentation in many situations)
nGrowBySize = min(1024, max(4, m_nSize / 8));
}
INT_PTR nNewMax;
if (nNewSize < m_nMaxSize + nGrowBySize)
nNewMax = m_nMaxSize + nGrowBySize; // granularity
else
nNewMax = nNewSize; // no slush
ASSERT(nNewMax >= m_nMaxSize); // no wrap around
if(nNewMax < m_nMaxSize)
AfxThrowInvalidArgException();
#ifdef SIZE_T_MAX
ASSERT(nNewMax <= SIZE_T_MAX/sizeof(CObject*)); // no overflow
#endif
CObject** pNewData = (CObject**) new BYTE[nNewMax * sizeof(CObject*)];
// copy new data from old
Checked::memcpy_s(pNewData, nNewMax * sizeof(CObject*),
m_pData, m_nSize * sizeof(CObject*));
// construct remaining elements
ASSERT(nNewSize > m_nSize);
memset(&pNewData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(CObject*));
// get rid of old stuff (note: no destructors called)
delete[] (BYTE*)m_pData;
m_pData = pNewData;
m_nSize = nNewSize;
m_nMaxSize = nNewMax;
}
}
INT_PTR CObArray::Append(const CObArray& src)
{
ASSERT_VALID(this);
ASSERT(this != &src); // cannot append to itself
if(this == &src)
AfxThrowInvalidArgException();
INT_PTR nOldSize = m_nSize;
SetSize(m_nSize + src.m_nSize);
Checked::memcpy_s(m_pData + nOldSize, src.m_nSize * sizeof(CObject*),
src.m_pData, src.m_nSize * sizeof(CObject*));
return nOldSize;
}
void CObArray::Copy(const CObArray& src)
{
ASSERT_VALID(this);
ASSERT(this != &src); // cannot append to itself
if(this != &src)
{
SetSize(src.m_nSize);
Checked::memcpy_s(m_pData, src.m_nSize * sizeof(CObject*),
src.m_pData, src.m_nSize * sizeof(CObject*));
}
}
void CObArray::FreeExtra()
{
ASSERT_VALID(this);
if (m_nSize != m_nMaxSize)
{
// shrink to desired size
#ifdef SIZE_T_MAX
ASSERT(m_nSize <= SIZE_T_MAX/sizeof(CObject*)); // no overflow
#endif
CObject** pNewData = NULL;
if (m_nSize != 0)
{
pNewData = (CObject**) new BYTE[m_nSize * sizeof(CObject*)];
// copy new data from old
Checked::memcpy_s(pNewData, m_nSize * sizeof(CObject*),
m_pData, m_nSize * sizeof(CObject*));
}
// get rid of old stuff (note: no destructors called)
delete[] (BYTE*)m_pData;
m_pData = pNewData;
m_nMaxSize = m_nSize;
}
}
/////////////////////////////////////////////////////////////////////////////
void CObArray::SetAtGrow(INT_PTR nIndex, CObject* newElement)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0);
if(nIndex < 0)
AfxThrowInvalidArgException();
if (nIndex >= m_nSize)
SetSize(nIndex+1);
m_pData[nIndex] = newElement;
}
void CObArray::InsertAt(INT_PTR nIndex, CObject* newElement, INT_PTR nCount)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0); // will expand to meet need
ASSERT(nCount > 0); // zero or negative size not allowed
if(nIndex < 0 || nCount <= 0)
AfxThrowInvalidArgException();
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_PTR nOldSize = m_nSize;
SetSize(m_nSize + nCount); // grow it to new size
// shift old data up to fill gap
Checked::memmove_s(&m_pData[nIndex+nCount], (m_nSize-(nIndex+nCount)) * sizeof(CObject*),
&m_pData[nIndex], (nOldSize-nIndex) * sizeof(CObject*));
// re-init slots we copied from
memset(&m_pData[nIndex], 0, nCount * sizeof(CObject*));
}
// insert new value in the gap
ASSERT(nIndex + nCount <= m_nSize);
// copy elements into the empty space
while (nCount--)
m_pData[nIndex++] = newElement;
}
void CObArray::RemoveAt(INT_PTR nIndex, INT_PTR nCount)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0);
ASSERT(nCount >= 0);
INT_PTR nUpperBound = nIndex + nCount;
ASSERT(nUpperBound <= m_nSize && nUpperBound >= nIndex && nUpperBound >= nCount);
if(nIndex < 0 || nCount < 0 || (nUpperBound > m_nSize) || (nUpperBound < nIndex) || (nUpperBound < nCount))
AfxThrowInvalidArgException();
// just remove a range
INT_PTR nMoveCount = m_nSize - (nUpperBound);
if (nMoveCount)
{
Checked::memmove_s(&m_pData[nIndex], nMoveCount * sizeof(CObject*),
&m_pData[nUpperBound], nMoveCount * sizeof(CObject*));
}
m_nSize -= nCount;
}
void CObArray::InsertAt(INT_PTR nStartIndex, CObArray* pNewArray)
{
ASSERT_VALID(this);
ASSERT(pNewArray != NULL);
ASSERT_KINDOF(CObArray, pNewArray);
ASSERT_VALID(pNewArray);
ASSERT(nStartIndex >= 0);
if(pNewArray == NULL || nStartIndex < 0)
AfxThrowInvalidArgException();
if (pNewArray->GetSize() > 0)
{
InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
for (INT_PTR i = 0; i < pNewArray->GetSize(); i++)
SetAt(nStartIndex + i, pNewArray->GetAt(i));
}
}
INT_PTR CObArray::GetSize() const
{
return m_nSize;
}
INT_PTR CObArray::GetCount() const
{
return m_nSize;
}
BOOL CObArray::IsEmpty() const
{
return m_nSize == 0;
}
INT_PTR CObArray::GetUpperBound() const
{
return m_nSize-1;
}
void CObArray::RemoveAll()
{
SetSize(0);
}
CObject* CObArray::GetAt(INT_PTR nIndex) const
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
if( nIndex < 0 || nIndex >= m_nSize )
AfxThrowInvalidArgException();
return m_pData[nIndex];
}
void CObArray::SetAt(INT_PTR nIndex, CObject* newElement)
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
if( nIndex < 0 || nIndex >= m_nSize )
AfxThrowInvalidArgException();
m_pData[nIndex] = newElement;
}
CObject*& CObArray::ElementAt(INT_PTR nIndex)
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
if( nIndex < 0 || nIndex >= m_nSize )
AfxThrowInvalidArgException();
return m_pData[nIndex];
}
const CObject** CObArray::GetData() const
{
return (const CObject**)m_pData;
}
CObject** CObArray::GetData()
{
return (CObject**)m_pData;
}
INT_PTR CObArray::Add(CObject* newElement)
{
INT_PTR nIndex = m_nSize;
SetAtGrow(nIndex, newElement);
return nIndex;
}
CObject* CObArray::operator[](INT_PTR nIndex) const
{
return GetAt(nIndex);
}
CObject*& CObArray::operator[](INT_PTR nIndex)
{
return ElementAt(nIndex);
}
/////////////////////////////////////////////////////////////////////////////
注:成员函数参考百度百科
6481




被折叠的 条评论
为什么被折叠?



