template <typename T>
class CIDPoolEx
{
typedef struct Node
{
T obj;
UINT32 next;
}Ele;
public:
bool Init( unsigned int dwSize)
{
m_aObject = new Ele[dwSize];
memset(m_aObject,0,dwSize*sizeof(Ele));
for( unsigned int i = 0 ; i < dwSize ; i++)
{
m_aObject[i].next = i + 1;
}
m_dwSize = m_freeSize = dwSize;
m_FreeHead = 0;
m_FreeTail = dwSize - 1;
return true;
}
CIDPoolEx(void):m_aObject(NULL),m_dwSize(0){}
~CDPoolEx(void)
{
delete [] m_aObject;
}
UINT32 Add( T object)
{
if ( 0 == m_freeSize) return (UINT32)(-1);
UINT32 dwID = m_FreeHead;
m_aObject[m_FreeHead].object = object;
m_FreeHead = m_aObject[m_FreeHead].next;
m_aObject[dwID].next = (UINT32)-1; //表示在使用中
--m_freeSize;
return dwID;
}
bool Find( UINT32 dwID , T&pObject)
{
if( dwID >= m_dwSize)
return false;
if( m_aObject[dwID].next != (UINT32)-1 ) //不是在使用中
return false;
pObject = m_aObject[dwID].object;
return true;
}
void Del(UINT32 dwID)
{
if( dwID >= m_dwSize)
{
return ;
}
if( m_aObject[dwID].next != (UINT32)-1)
return ;
++m_freeSize;
if( m_freeeSize == 1)
{
m_FreeHead = m_FreeTail = dwID;
m_FreeHead[m_FreeHead].next = m_dwSize;
return;
}
m_aObject[dwID].next = m_dwSize; //表示无后继
m_aObject[m_FreeTail].next = dwID; //放回到队尾
}
std::list<UINT32>::size_type GetFreeCount(void) const
{
return m_freeSize;
}
bool Empty() const
{
return (m_freeSize == m_dwSize);
}
bool Full() const
{
return ( m_freeSize == 0);
}
std::list<UINT32>::size_type size() const
{
return m_freeSize;
}
std::list<UINT32>::size_type Capacity()
{
return m_dwSize;
}
UINT32 GetUSG()
{
return 100 - m_freeSize*100/m_dwSize;
}
private:
unsigned int m_dwSize;
UINT32 m_feeSize;
Ele* m_aObject;
UINT32 m_FreeHead;
UINT32 m_FreeTail;
};