MFC中CMapPtrtoPtr的实现

来源:互联网 发布:知否txt书包网 编辑:程序博客网 时间:2024/05/22 17:49

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;
}

原创粉丝点击