| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- // This is a part of the Microsoft Foundation Classes C++ library.
- // Copyright (C) 1992-1998 Microsoft Corporation
- // All rights reserved.
- //
- // This source code is only intended as a supplement to the
- // Microsoft Foundation Classes Reference and related
- // electronic documentation provided with the library.
- // See these sources for detailed information regarding the
- // Microsoft Foundation Classes product.
- /////////////////////////////////////////////////////////////////////////////
- //
- // Implementation of parmeterized Map
- //
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #ifdef AFX_COLL2_SEG
- #pragma code_seg(AFX_COLL2_SEG)
- #endif
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define new DEBUG_NEW
- /////////////////////////////////////////////////////////////////////////////
- CMapWordToPtr::CMapWordToPtr(int nBlockSize)
- {
- ASSERT(nBlockSize > 0);
- m_pHashTable = NULL;
- m_nHashTableSize = 17; // default size
- m_nCount = 0;
- m_pFreeList = NULL;
- m_pBlocks = NULL;
- m_nBlockSize = nBlockSize;
- }
- inline UINT CMapWordToPtr::HashKey(WORD key) const
- {
- // default identity hash - works for most primitive values
- return ((UINT)(void*)(DWORD)key) >> 4;
- }
- void CMapWordToPtr::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;
- }
- void CMapWordToPtr::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;
- }
- CMapWordToPtr::~CMapWordToPtr()
- {
- RemoveAll();
- ASSERT(m_nCount == 0);
- }
- /////////////////////////////////////////////////////////////////////////////
- // Assoc helpers
- // same as CList implementation except we store CAssoc's not CNode's
- // and CAssoc's are singly linked all the time
- CMapWordToPtr::CAssoc*
- CMapWordToPtr::NewAssoc()
- {
- if (m_pFreeList == NULL)
- {
- // add another block
- CPlex* newBlock = CPlex::Create(m_pBlocks, m_nBlockSize, sizeof(CMapWordToPtr::CAssoc));
- // chain them into free list
- CMapWordToPtr::CAssoc* pAssoc = (CMapWordToPtr::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
- CMapWordToPtr::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;
- }
- void CMapWordToPtr::FreeAssoc(CMapWordToPtr::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();
- }
- CMapWordToPtr::CAssoc*
- CMapWordToPtr::GetAssocAt(WORD 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;
- }
- /////////////////////////////////////////////////////////////////////////////
- BOOL CMapWordToPtr::Lookup(WORD 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;
- }
- void*& CMapWordToPtr::operator[](WORD 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
- }
- BOOL CMapWordToPtr::RemoveKey(WORD 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
- }
- /////////////////////////////////////////////////////////////////////////////
- // Iterating
- void CMapWordToPtr::GetNextAssoc(POSITION& rNextPosition,
- WORD& rKey, void*& rValue) const
- {
- ASSERT_VALID(this);
- ASSERT(m_pHashTable != NULL); // never call on empty map
- CAssoc* pAssocRet = (CAssoc*)rNextPosition;
- ASSERT(pAssocRet != NULL);
- if (pAssocRet == (CAssoc*) BEFORE_START_POSITION)
- {
- // find the first association
- for (UINT nBucket = 0; nBucket < m_nHashTableSize; nBucket++)
- if ((pAssocRet = m_pHashTable[nBucket]) != NULL)
- break;
- ASSERT(pAssocRet != NULL); // must find something
- }
- // find next association
- ASSERT(AfxIsValidAddress(pAssocRet, sizeof(CAssoc)));
- CAssoc* pAssocNext;
- if ((pAssocNext = pAssocRet->pNext) == NULL)
- {
- // go to next bucket
- for (UINT nBucket = (HashKey(pAssocRet->key) % m_nHashTableSize) + 1;
- nBucket < m_nHashTableSize; nBucket++)
- if ((pAssocNext = m_pHashTable[nBucket]) != NULL)
- break;
- }
- rNextPosition = (POSITION) pAssocNext;
- // fill in return data
- rKey = pAssocRet->key;
- rValue = pAssocRet->value;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Diagnostics
- #ifdef _DEBUG
- void CMapWordToPtr::Dump(CDumpContext& dc) const
- {
- CObject::Dump(dc);
- dc << "with " << m_nCount << " elements";
- if (dc.GetDepth() > 0)
- {
- // Dump in format "[key] -> value"
- WORD key;
- void* val;
- POSITION pos = GetStartPosition();
- while (pos != NULL)
- {
- GetNextAssoc(pos, key, val);
- dc << "\n\t[" << key << "] = " << val;
- }
- }
- dc << "\n";
- }
- void CMapWordToPtr::AssertValid() const
- {
- CObject::AssertValid();
- ASSERT(m_nHashTableSize > 0);
- ASSERT(m_nCount == 0 || m_pHashTable != NULL);
- // non-empty map should have hash table
- }
- #endif //_DEBUG
- #ifdef AFX_INIT_SEG
- #pragma code_seg(AFX_INIT_SEG)
- #endif
- IMPLEMENT_DYNAMIC(CMapWordToPtr, CObject)
- /////////////////////////////////////////////////////////////////////////////
|