CMapPtrtoPtr的數據項由3部分組成,指向下一個數據項的指針,兩個相關聯的指針。定義如下:
struct CAssoc
{
CAssoc* pNext;
void* key;
void* value;
};
這裏相關聯的含義是單向關聯,即給某個value指針關聯了一個key指針,我們可以通過key指針查找或刪除對應value指針。現在我們思考一下CMapPtrtoPtr應該支持的基本操作,很明顯的是我們應該提供添加一個關聯項,查找某個key對應的value,刪除key對應的關聯項,刪除所有數據項。
首先我們來考慮添加一個關聯項,根據前面的數據項定義,我們很容易想到可以申請一塊數據項內存初始化我們的數據並以鏈表的形式串聯起來。這樣想來是比較容易,但如果我們的數據量比較大,我們就有可能大量的進行分配內存的操作,與之對應的當然還有釋放內存的操作,這些操作的開銷是會非常大的,而且我們分配的內存是不連續的空間,那麼當分配的內存之間的大小不足以分配數據項時就會出現內存碎片。爲了解決這個問題,我們希望能夠分配一整塊連續的內存,當爲CAssoc分配空間的時候使用我們事先分配好的空間,直到該空間使用完再申請, 當然這一整塊空間的大小,可以由用戶根據自己的需要指定。爲此我們引入了一個輔助的管理內存的數據結構:CPlex,定義如下:
StructCPlex
{
CPlex* pNext;
void* data() {return this+1;}
static CPlex* Create(CPlex* pHead, UINT nMax, UINT cbElement);
void FreeDataChain();
}
其中靜態函數Create實現了分配nMax個大小爲cbElement的連續空間,並將該空間添加到以pHead爲鏈表頭的鏈表中。
CPlex* CPlex::Create(CPlex* pHead,UINT nMax, UINT cbElement)
{
CPlex* p = (CPlex*)new BYTE[sizeof(CPlex) +nMax* cbElement];
p->pNext = pHead;
pHead =p;
return p;
}
void FreeDataChain()
{
CPlex * p = this;
while(p != NULL)
{
BYTE* pBytes = (BYTE*)p;
CPlex *pNext = p->Next;
delete [] pBytes;
p=pNext;
}
}
上面的兩個函數實現了分配和釋放整體的內存的功能,在此基礎上看看如何實現分配數據項內存。由函數NewAssoc()完成。
CMapPtrToPtr::CAssoc*
CMapPtrToPtr::NewAssoc()
{
if (m_pFreeList == NULL)
{
// add another block
CPlex* newBlock = CPlex::Create(m_pBlocks, m_nBlockSize, sizeof(CMapPtrToPtr::CAssoc));
// chain them into free list
CMapPtrToPtr::CAssoc* pAssoc = (CMapPtrToPtr::CAssoc*) newBlock->data();
// free in reverse order to make it easier to debug
pAssoc += m_nBlockSize - 1;
for (int i = m_nBlockSize-1; i >= 0; i--, pAssoc--)
{
pAssoc->pNext = m_pFreeList;
m_pFreeList = pAssoc;
}
}
ASSERT(m_pFreeList != NULL); // we must have something
CMapPtrToPtr::CAssoc* pAssoc = m_pFreeList;
m_pFreeList = m_pFreeList->pNext;
m_nCount++;
ASSERT(m_nCount > 0); // make sure we don't overflow
pAssoc->key = 0;
pAssoc->value = 0;
return pAssoc;
}
對於當前可用的內存我們使用了m_pFreeList作爲其線索接入點。每次通過這個指針來尋找可以分配數據項的內存。
現在我們再來考慮CMapPtrtoPtr的基本操作,由於我們實現的是關聯操作我們希望能夠通過將key作爲下標來對value進行查找和賦值。因此我們需要重載[]運算符。當我們去查找key對應的value時,最普通的方法就是遍歷整個鏈表,這個做法在數據量大時是很不划算的,因此我們希望能夠有一種方法通過key可以直接定位到value,爲此我們需要建立一張hash表。
hash表的初始化:
void CMapPtrToPtr::InitHashTable(
UINT nHashSize, BOOL bAllocNow)
//
// Used to force allocation of a hash table or to override the default
// hash table size of (which is fairly small)
{
ASSERT_VALID(this);
ASSERT(m_nCount == 0);
ASSERT(nHashSize > 0);
if (m_pHashTable != NULL)
{
// free hash table
delete[] m_pHashTable;
m_pHashTable = NULL;
}
if (bAllocNow)
{
m_pHashTable = new CAssoc* [nHashSize];
memset(m_pHashTable, 0, sizeof(CAssoc*) * nHashSize);
}
m_nHashTableSize = nHashSize;
}
通過key來查找相應的value,這裏mfc的源碼中的函數返回的並非是value值而是整個數據項,其實效果是一樣的,我們可以通過數據項很容易的得到value值,返回數據項起到了一箭雙鵰的效果,我們可以通過返回項是否爲空來判斷是否查找成功,(注:我們不能通過value值是否爲空判斷是否查找成功,必須增加一個布爾返回值的方式,所以返回數據項更加簡單)。
//hash function
inline UINT CMapPtrToPtr::HashKey(void* key) const
{
// default identity hash - works for most primitive values
return ((UINT)(void*)(DWORD)key) >> 4;
}
CMapPtrToPtr::CAssoc*
CMapPtrToPtr::GetAssocAt(void* key, UINT& nHash) const
// find association (or return NULL)
{
nHash = HashKey(key) % m_nHashTableSize;
if (m_pHashTable == NULL)
return NULL;
// see if it exists
CAssoc* pAssoc;
for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext)
{
if (pAssoc->key == key)
return pAssoc;
}
return NULL;
}
在此基礎上就可以很容易得到我們想要的value值
BOOL CMapPtrToPtr::Lookup(void* key, void*& rValue) const
{
ASSERT_VALID(this);
UINT nHash;
CAssoc* pAssoc = GetAssocAt(key, nHash);
if (pAssoc == NULL)
return FALSE; // not in map
rValue = pAssoc->value;
return TRUE;
}
我們期望和普通數組一樣使用map於是重載了[]運算符
void*& CMapPtrToPtr::operator[](void* key)
{
ASSERT_VALID(this);
UINT nHash;
CAssoc* pAssoc;
if ((pAssoc = GetAssocAt(key, nHash)) == NULL)
{
if (m_pHashTable == NULL)
InitHashTable(m_nHashTableSize);
// it doesn't exist, add a new Association
pAssoc = NewAssoc();
pAssoc->key = key;
// 'pAssoc->value' is a constructed object, nothing more
// put into hash table
pAssoc->pNext = m_pHashTable[nHash];
m_pHashTable[nHash] = pAssoc;
}
return pAssoc->value; // return new reference
}
增加一個新的映射項
_AFXCOLL_INLINE void CMapPtrToPtr::SetAt(void* key, void* newValue)
{ (*this)[key] = newValue; }
刪除一個數據項:
void CMapPtrToPtr::FreeAssoc(CMapPtrToPtr::CAssoc* pAssoc)
{
pAssoc->pNext = m_pFreeList;
m_pFreeList = pAssoc;
m_nCount--;
ASSERT(m_nCount >= 0); // make sure we don't underflow
// if no more elements, cleanup completely
if (m_nCount == 0)
RemoveAll();
}
刪除一個key值對應的數據項
BOOL CMapPtrToPtr::RemoveKey(void* key)
// remove key - return TRUE if removed
{
ASSERT_VALID(this);
if (m_pHashTable == NULL)
return FALSE; // nothing in the table
CAssoc** ppAssocPrev;
ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize];
CAssoc* pAssoc;
for (pAssoc = *ppAssocPrev; pAssoc != NULL; pAssoc = pAssoc->pNext)
{
if (pAssoc->key == key)
{
// remove it
*ppAssocPrev = pAssoc->pNext; // remove from list
FreeAssoc(pAssoc);
return TRUE;
}
ppAssocPrev = &pAssoc->pNext;
}
return FALSE; // not found
}
刪除整個Map
void CMapPtrToPtr::RemoveAll()
{
ASSERT_VALID(this);
if (m_pHashTable != NULL)
{
// free hash table
delete[] m_pHashTable;
m_pHashTable = NULL;
}
m_nCount = 0;
m_pFreeList = NULL;
m_pBlocks->FreeDataChain();
m_pBlocks = NULL;
}