map_wp.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. /////////////////////////////////////////////////////////////////////////////
  11. //
  12. // Implementation of parmeterized Map
  13. //
  14. /////////////////////////////////////////////////////////////////////////////
  15. #include "stdafx.h"
  16. #ifdef AFX_COLL2_SEG
  17. #pragma code_seg(AFX_COLL2_SEG)
  18. #endif
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. #define new DEBUG_NEW
  24. /////////////////////////////////////////////////////////////////////////////
  25. CMapWordToPtr::CMapWordToPtr(int nBlockSize)
  26. {
  27. ASSERT(nBlockSize > 0);
  28. m_pHashTable = NULL;
  29. m_nHashTableSize = 17; // default size
  30. m_nCount = 0;
  31. m_pFreeList = NULL;
  32. m_pBlocks = NULL;
  33. m_nBlockSize = nBlockSize;
  34. }
  35. inline UINT CMapWordToPtr::HashKey(WORD key) const
  36. {
  37. // default identity hash - works for most primitive values
  38. return ((UINT)(void*)(DWORD)key) >> 4;
  39. }
  40. void CMapWordToPtr::InitHashTable(
  41. UINT nHashSize, BOOL bAllocNow)
  42. //
  43. // Used to force allocation of a hash table or to override the default
  44. // hash table size of (which is fairly small)
  45. {
  46. ASSERT_VALID(this);
  47. ASSERT(m_nCount == 0);
  48. ASSERT(nHashSize > 0);
  49. if (m_pHashTable != NULL)
  50. {
  51. // free hash table
  52. delete[] m_pHashTable;
  53. m_pHashTable = NULL;
  54. }
  55. if (bAllocNow)
  56. {
  57. m_pHashTable = new CAssoc* [nHashSize];
  58. memset(m_pHashTable, 0, sizeof(CAssoc*) * nHashSize);
  59. }
  60. m_nHashTableSize = nHashSize;
  61. }
  62. void CMapWordToPtr::RemoveAll()
  63. {
  64. ASSERT_VALID(this);
  65. if (m_pHashTable != NULL)
  66. {
  67. // free hash table
  68. delete[] m_pHashTable;
  69. m_pHashTable = NULL;
  70. }
  71. m_nCount = 0;
  72. m_pFreeList = NULL;
  73. m_pBlocks->FreeDataChain();
  74. m_pBlocks = NULL;
  75. }
  76. CMapWordToPtr::~CMapWordToPtr()
  77. {
  78. RemoveAll();
  79. ASSERT(m_nCount == 0);
  80. }
  81. /////////////////////////////////////////////////////////////////////////////
  82. // Assoc helpers
  83. // same as CList implementation except we store CAssoc's not CNode's
  84. // and CAssoc's are singly linked all the time
  85. CMapWordToPtr::CAssoc*
  86. CMapWordToPtr::NewAssoc()
  87. {
  88. if (m_pFreeList == NULL)
  89. {
  90. // add another block
  91. CPlex* newBlock = CPlex::Create(m_pBlocks, m_nBlockSize, sizeof(CMapWordToPtr::CAssoc));
  92. // chain them into free list
  93. CMapWordToPtr::CAssoc* pAssoc = (CMapWordToPtr::CAssoc*) newBlock->data();
  94. // free in reverse order to make it easier to debug
  95. pAssoc += m_nBlockSize - 1;
  96. for (int i = m_nBlockSize-1; i >= 0; i--, pAssoc--)
  97. {
  98. pAssoc->pNext = m_pFreeList;
  99. m_pFreeList = pAssoc;
  100. }
  101. }
  102. ASSERT(m_pFreeList != NULL); // we must have something
  103. CMapWordToPtr::CAssoc* pAssoc = m_pFreeList;
  104. m_pFreeList = m_pFreeList->pNext;
  105. m_nCount++;
  106. ASSERT(m_nCount > 0); // make sure we don't overflow
  107. pAssoc->key = 0;
  108. pAssoc->value = 0;
  109. return pAssoc;
  110. }
  111. void CMapWordToPtr::FreeAssoc(CMapWordToPtr::CAssoc* pAssoc)
  112. {
  113. pAssoc->pNext = m_pFreeList;
  114. m_pFreeList = pAssoc;
  115. m_nCount--;
  116. ASSERT(m_nCount >= 0); // make sure we don't underflow
  117. // if no more elements, cleanup completely
  118. if (m_nCount == 0)
  119. RemoveAll();
  120. }
  121. CMapWordToPtr::CAssoc*
  122. CMapWordToPtr::GetAssocAt(WORD key, UINT& nHash) const
  123. // find association (or return NULL)
  124. {
  125. nHash = HashKey(key) % m_nHashTableSize;
  126. if (m_pHashTable == NULL)
  127. return NULL;
  128. // see if it exists
  129. CAssoc* pAssoc;
  130. for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext)
  131. {
  132. if (pAssoc->key == key)
  133. return pAssoc;
  134. }
  135. return NULL;
  136. }
  137. /////////////////////////////////////////////////////////////////////////////
  138. BOOL CMapWordToPtr::Lookup(WORD key, void*& rValue) const
  139. {
  140. ASSERT_VALID(this);
  141. UINT nHash;
  142. CAssoc* pAssoc = GetAssocAt(key, nHash);
  143. if (pAssoc == NULL)
  144. return FALSE; // not in map
  145. rValue = pAssoc->value;
  146. return TRUE;
  147. }
  148. void*& CMapWordToPtr::operator[](WORD key)
  149. {
  150. ASSERT_VALID(this);
  151. UINT nHash;
  152. CAssoc* pAssoc;
  153. if ((pAssoc = GetAssocAt(key, nHash)) == NULL)
  154. {
  155. if (m_pHashTable == NULL)
  156. InitHashTable(m_nHashTableSize);
  157. // it doesn't exist, add a new Association
  158. pAssoc = NewAssoc();
  159. pAssoc->key = key;
  160. // 'pAssoc->value' is a constructed object, nothing more
  161. // put into hash table
  162. pAssoc->pNext = m_pHashTable[nHash];
  163. m_pHashTable[nHash] = pAssoc;
  164. }
  165. return pAssoc->value; // return new reference
  166. }
  167. BOOL CMapWordToPtr::RemoveKey(WORD key)
  168. // remove key - return TRUE if removed
  169. {
  170. ASSERT_VALID(this);
  171. if (m_pHashTable == NULL)
  172. return FALSE; // nothing in the table
  173. CAssoc** ppAssocPrev;
  174. ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize];
  175. CAssoc* pAssoc;
  176. for (pAssoc = *ppAssocPrev; pAssoc != NULL; pAssoc = pAssoc->pNext)
  177. {
  178. if (pAssoc->key == key)
  179. {
  180. // remove it
  181. *ppAssocPrev = pAssoc->pNext; // remove from list
  182. FreeAssoc(pAssoc);
  183. return TRUE;
  184. }
  185. ppAssocPrev = &pAssoc->pNext;
  186. }
  187. return FALSE; // not found
  188. }
  189. /////////////////////////////////////////////////////////////////////////////
  190. // Iterating
  191. void CMapWordToPtr::GetNextAssoc(POSITION& rNextPosition,
  192. WORD& rKey, void*& rValue) const
  193. {
  194. ASSERT_VALID(this);
  195. ASSERT(m_pHashTable != NULL); // never call on empty map
  196. CAssoc* pAssocRet = (CAssoc*)rNextPosition;
  197. ASSERT(pAssocRet != NULL);
  198. if (pAssocRet == (CAssoc*) BEFORE_START_POSITION)
  199. {
  200. // find the first association
  201. for (UINT nBucket = 0; nBucket < m_nHashTableSize; nBucket++)
  202. if ((pAssocRet = m_pHashTable[nBucket]) != NULL)
  203. break;
  204. ASSERT(pAssocRet != NULL); // must find something
  205. }
  206. // find next association
  207. ASSERT(AfxIsValidAddress(pAssocRet, sizeof(CAssoc)));
  208. CAssoc* pAssocNext;
  209. if ((pAssocNext = pAssocRet->pNext) == NULL)
  210. {
  211. // go to next bucket
  212. for (UINT nBucket = (HashKey(pAssocRet->key) % m_nHashTableSize) + 1;
  213. nBucket < m_nHashTableSize; nBucket++)
  214. if ((pAssocNext = m_pHashTable[nBucket]) != NULL)
  215. break;
  216. }
  217. rNextPosition = (POSITION) pAssocNext;
  218. // fill in return data
  219. rKey = pAssocRet->key;
  220. rValue = pAssocRet->value;
  221. }
  222. /////////////////////////////////////////////////////////////////////////////
  223. // Diagnostics
  224. #ifdef _DEBUG
  225. void CMapWordToPtr::Dump(CDumpContext& dc) const
  226. {
  227. CObject::Dump(dc);
  228. dc << "with " << m_nCount << " elements";
  229. if (dc.GetDepth() > 0)
  230. {
  231. // Dump in format "[key] -> value"
  232. WORD key;
  233. void* val;
  234. POSITION pos = GetStartPosition();
  235. while (pos != NULL)
  236. {
  237. GetNextAssoc(pos, key, val);
  238. dc << "\n\t[" << key << "] = " << val;
  239. }
  240. }
  241. dc << "\n";
  242. }
  243. void CMapWordToPtr::AssertValid() const
  244. {
  245. CObject::AssertValid();
  246. ASSERT(m_nHashTableSize > 0);
  247. ASSERT(m_nCount == 0 || m_pHashTable != NULL);
  248. // non-empty map should have hash table
  249. }
  250. #endif //_DEBUG
  251. #ifdef AFX_INIT_SEG
  252. #pragma code_seg(AFX_INIT_SEG)
  253. #endif
  254. IMPLEMENT_DYNAMIC(CMapWordToPtr, CObject)
  255. /////////////////////////////////////////////////////////////////////////////