MAP.CTT
资源名称:MSDN_VC98.zip [点击查看]
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:15k
源码类别:
Windows编程
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // class CMap - a mapping from 'KEY's to 'VALUE's, passed in as
- // ARG_KEY and/or ARG_VALUE parameters.
- //
- // optional definitions:
- // IS_SERIAL - enable serialization through CArchive extraction & insertion
- // HAS_CREATE - call constructors and destructors
- //
- // This is a part of the Microsoft Foundation Classes C++ library.
- // Copyright (C) 1994-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.
- /////////////////////////////////////////////////////////////////////////////
- //$DECLARE_TEMPLATE
- /////////////////////////////////////////////////////////////////////////////
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- class CMap : public CObject
- {
- $ifdef IS_SERIAL
- DECLARE_SERIAL(CMap)
- $else
- DECLARE_DYNAMIC(CMap)
- $endif //!IS_SERIAL
- protected:
- // Association
- struct CAssoc
- {
- CAssoc* pNext;
- $ifdef CACHE_HASH
- UINT nHashValue; // needed for efficient iteration
- $endif
- KEY key;
- VALUE value;
- };
- public:
- // Construction
- CMap(int nBlockSize = 10);
- // Attributes
- // number of elements
- int GetCount() const;
- BOOL IsEmpty() const;
- // Lookup
- BOOL Lookup(ARG_KEY key, VALUE& rValue) const;
- // Operations
- // Lookup and add if not there
- VALUE& operator[](ARG_KEY key);
- // add a new (key, value) pair
- void SetAt(ARG_KEY key, ARG_VALUE newValue);
- // removing existing (key, ?) pair
- BOOL RemoveKey(ARG_KEY key);
- void RemoveAll();
- // iterating all (key, value) pairs
- POSITION GetStartPosition() const;
- void GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const;
- // advanced features for derived classes
- UINT GetHashTableSize() const;
- void InitHashTable(UINT hashSize, BOOL bAllocNow = TRUE);
- // Overridables: special non-virtual (see map implementation for details)
- // Routine used to user-provided hash keys
- UINT HashKey(ARG_KEY key) const;
- // Implementation
- protected:
- CAssoc** m_pHashTable;
- UINT m_nHashTableSize;
- int m_nCount;
- CAssoc* m_pFreeList;
- struct CPlex* m_pBlocks;
- int m_nBlockSize;
- CAssoc* NewAssoc();
- void FreeAssoc(CAssoc*);
- CAssoc* GetAssocAt(ARG_KEY, UINT&) const;
- public:
- ~CMap();
- $ifdef IS_SERIAL
- void Serialize(CArchive&);
- $endif //IS_SERIAL
- #ifdef _DEBUG
- void Dump(CDumpContext&) const;
- void AssertValid() const;
- #endif
- $ifdef INCLUDE_GETVALUEAT
- VALUE GetValueAt(ARG_KEY key) const;
- $endif
- protected:
- // local typedefs for CTypedPtrMap class template
- typedef KEY BASE_KEY;
- typedef ARG_KEY BASE_ARG_KEY;
- typedef VALUE BASE_VALUE;
- typedef ARG_VALUE BASE_ARG_VALUE;
- };
- //$IMPLEMENT_TEMPLATE_INLINES
- ////////////////////////////////////////////////////////////////////////////
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- _AFXCOLL_INLINE int CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetCount() const
- { return m_nCount; }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- _AFXCOLL_INLINE BOOL CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::IsEmpty() const
- { return m_nCount == 0; }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- _AFXCOLL_INLINE void CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::SetAt(ARG_KEY key, ARG_VALUE newValue)
- { (*this)[key] = newValue; }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- _AFXCOLL_INLINE POSITION CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetStartPosition() const
- { return (m_nCount == 0) ? NULL : BEFORE_START_POSITION; }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- _AFXCOLL_INLINE UINT CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetHashTableSize() const
- { return m_nHashTableSize; }
- //$IMPLEMENT_TEMPLATE
- // This is a part of the Microsoft Foundation Classes C++ library.
- // Copyright (C) 1992-1997 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
- $ifdef HAS_CREATE
- #include "elements.h" // used for special creation
- $endif
- #define new DEBUG_NEW
- /////////////////////////////////////////////////////////////////////////////
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::CMap(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;
- }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- inline UINT CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::HashKey(ARG_KEY key) const
- {
- // default identity hash - works for most primitive values
- return ((UINT)(void*)(DWORD)key) >> 4;
- }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- void CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::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;
- }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- void CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::RemoveAll()
- {
- ASSERT_VALID(this);
- $ifdef HAS_CREATE
- if (m_pHashTable != NULL)
- {
- // destroy elements (values only)
- for (UINT nHash = 0; nHash < m_nHashTableSize; nHash++)
- {
- CAssoc* pAssoc;
- for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL;
- pAssoc = pAssoc->pNext)
- {
- DestructElement(&pAssoc->value);
- }
- }
- }
- $endif
- 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;
- }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::~CMap()
- {
- 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
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::CAssoc*
- CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::NewAssoc()
- {
- if (m_pFreeList == NULL)
- {
- // add another block
- CPlex* newBlock = CPlex::Create(m_pBlocks, m_nBlockSize, sizeof(CMap::CAssoc));
- // chain them into free list
- CMap::CAssoc* pAssoc = (CMap::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
- CMap::CAssoc* pAssoc = m_pFreeList;
- m_pFreeList = m_pFreeList->pNext;
- m_nCount++;
- ASSERT(m_nCount > 0); // make sure we don't overflow
- $ifdef USE_MEMSET_KEY
- memset(&pAssoc->key, 0, sizeof(KEY));
- $endif
- $ifdef USE_ASSIGN_KEY
- pAssoc->key = 0;
- $endif
- $ifdef HAS_CREATE
- ConstructElement(&pAssoc->value); // special construct values
- $endif
- $ifdef USE_MEMSET
- memset(&pAssoc->value, 0, sizeof(VALUE));
- $endif
- $ifdef USE_ASSIGN
- pAssoc->value = 0;
- $endif
- return pAssoc;
- }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- void CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::FreeAssoc(CMap::CAssoc* pAssoc)
- {
- $ifdef HAS_CREATE
- DestructElement(&pAssoc->value);
- $endif
- 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();
- }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::CAssoc*
- CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetAssocAt(ARG_KEY 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;
- }
- $ifdef INCLUDE_GETVALUEAT
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- VALUE CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetValueAt(ARG_KEY key) const
- // find value (or return NULL -- NULL values not different as a result)
- {
- if (m_pHashTable == NULL)
- return NULL;
- UINT nHash = HashKey(key) % m_nHashTableSize;
- // see if it exists
- CAssoc* pAssoc;
- for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext)
- {
- if (pAssoc->key == key)
- return pAssoc->value;
- }
- return NULL;
- }
- $endif
- /////////////////////////////////////////////////////////////////////////////
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- BOOL CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::Lookup(ARG_KEY key, VALUE& 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;
- }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- VALUE& CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::operator[](ARG_KEY 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();
- $ifdef CACHE_HASH
- pAssoc->nHashValue = nHash;
- $endif
- 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
- }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- BOOL CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::RemoveKey(ARG_KEY 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
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- void CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetNextAssoc(POSITION& rNextPosition,
- KEY& rKey, VALUE& 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
- $ifdef CACHE_HASH
- for (UINT nBucket = pAssocRet->nHashValue + 1;
- $else
- for (UINT nBucket = (HashKey(pAssocRet->key) % m_nHashTableSize) + 1;
- $endif
- nBucket < m_nHashTableSize; nBucket++)
- if ((pAssocNext = m_pHashTable[nBucket]) != NULL)
- break;
- }
- rNextPosition = (POSITION) pAssocNext;
- // fill in return data
- rKey = pAssocRet->key;
- rValue = pAssocRet->value;
- }
- $ifdef IS_SERIAL
- /////////////////////////////////////////////////////////////////////////////
- // Serialization
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- void CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::Serialize(CArchive& ar)
- {
- ASSERT_VALID(this);
- CObject::Serialize(ar);
- if (ar.IsStoring())
- {
- ar.WriteCount(m_nCount);
- if (m_nCount == 0)
- return; // nothing more to do
- ASSERT(m_pHashTable != NULL);
- for (UINT nHash = 0; nHash < m_nHashTableSize; nHash++)
- {
- CAssoc* pAssoc;
- for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL;
- pAssoc = pAssoc->pNext)
- {
- ar << pAssoc->key;
- ar << pAssoc->value;
- }
- }
- }
- else
- {
- DWORD nNewCount = ar.ReadCount();
- KEY newKey;
- VALUE newValue;
- while (nNewCount--)
- {
- ar >> newKey;
- ar >> newValue;
- SetAt(newKey, newValue);
- }
- }
- }
- $endif //IS_SERIAL
- /////////////////////////////////////////////////////////////////////////////
- // Diagnostics
- #ifdef _DEBUG
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- void CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::Dump(CDumpContext& dc) const
- {
- CObject::Dump(dc);
- dc << "with " << m_nCount << " elements";
- if (dc.GetDepth() > 0)
- {
- // Dump in format "[key] -> value"
- KEY key;
- VALUE val;
- POSITION pos = GetStartPosition();
- while (pos != NULL)
- {
- GetNextAssoc(pos, key, val);
- dc << "nt[" << key << "] = " << val;
- }
- }
- dc << "n";
- }
- template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
- void CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::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
- $ifdef IS_SERIAL
- IMPLEMENT_SERIAL(CMap, CObject, 0)
- $else
- IMPLEMENT_DYNAMIC(CMap, CObject)
- $endif //!IS_SERIAL
- /////////////////////////////////////////////////////////////////////////////