map_sp.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 from CString to value
  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. #include "elements.h" // used for special creation
  24. #define new DEBUG_NEW
  25. /////////////////////////////////////////////////////////////////////////////
  26. CMapStringToPtr::CMapStringToPtr(int nBlockSize)
  27. {
  28. ASSERT(nBlockSize > 0);
  29. m_pHashTable = NULL;
  30. m_nHashTableSize = 17; // default size
  31. m_nCount = 0;
  32. m_pFreeList = NULL;
  33. m_pBlocks = NULL;
  34. m_nBlockSize = nBlockSize;
  35. }
  36. inline UINT CMapStringToPtr::HashKey(LPCTSTR key) const
  37. {
  38. UINT nHash = 0;
  39. while (*key)
  40. nHash = (nHash<<5) + nHash + *key++;
  41. return nHash;
  42. }
  43. void CMapStringToPtr::InitHashTable(
  44. UINT nHashSize, BOOL bAllocNow)
  45. //
  46. // Used to force allocation of a hash table or to override the default
  47. // hash table size of (which is fairly small)
  48. {
  49. ASSERT_VALID(this);
  50. ASSERT(m_nCount == 0);
  51. ASSERT(nHashSize > 0);
  52. if (m_pHashTable != NULL)
  53. {
  54. // free hash table
  55. delete[] m_pHashTable;
  56. m_pHashTable = NULL;
  57. }
  58. if (bAllocNow)
  59. {
  60. m_pHashTable = new CAssoc* [nHashSize];
  61. memset(m_pHashTable, 0, sizeof(CAssoc*) * nHashSize);
  62. }
  63. m_nHashTableSize = nHashSize;
  64. }
  65. void CMapStringToPtr::RemoveAll()
  66. {
  67. ASSERT_VALID(this);
  68. if (m_pHashTable != NULL)
  69. {
  70. // destroy elements
  71. for (UINT nHash = 0; nHash < m_nHashTableSize; nHash++)
  72. {
  73. CAssoc* pAssoc;
  74. for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL;
  75. pAssoc = pAssoc->pNext)
  76. {
  77. DestructElement(&pAssoc->key); // free up string data
  78. }
  79. }
  80. // free hash table
  81. delete [] m_pHashTable;
  82. m_pHashTable = NULL;
  83. }
  84. m_nCount = 0;
  85. m_pFreeList = NULL;
  86. m_pBlocks->FreeDataChain();
  87. m_pBlocks = NULL;
  88. }
  89. CMapStringToPtr::~CMapStringToPtr()
  90. {
  91. RemoveAll();
  92. ASSERT(m_nCount == 0);
  93. }
  94. /////////////////////////////////////////////////////////////////////////////
  95. // Assoc helpers
  96. // same as CList implementation except we store CAssoc's not CNode's
  97. // and CAssoc's are singly linked all the time
  98. CMapStringToPtr::CAssoc*
  99. CMapStringToPtr::NewAssoc()
  100. {
  101. if (m_pFreeList == NULL)
  102. {
  103. // add another block
  104. CPlex* newBlock = CPlex::Create(m_pBlocks, m_nBlockSize,
  105. sizeof(CMapStringToPtr::CAssoc));
  106. // chain them into free list
  107. CMapStringToPtr::CAssoc* pAssoc =
  108. (CMapStringToPtr::CAssoc*) newBlock->data();
  109. // free in reverse order to make it easier to debug
  110. pAssoc += m_nBlockSize - 1;
  111. for (int i = m_nBlockSize-1; i >= 0; i--, pAssoc--)
  112. {
  113. pAssoc->pNext = m_pFreeList;
  114. m_pFreeList = pAssoc;
  115. }
  116. }
  117. ASSERT(m_pFreeList != NULL); // we must have something
  118. CMapStringToPtr::CAssoc* pAssoc = m_pFreeList;
  119. m_pFreeList = m_pFreeList->pNext;
  120. m_nCount++;
  121. ASSERT(m_nCount > 0); // make sure we don't overflow
  122. memcpy(&pAssoc->key, &afxEmptyString, sizeof(CString));
  123. pAssoc->value = 0;
  124. return pAssoc;
  125. }
  126. void CMapStringToPtr::FreeAssoc(CMapStringToPtr::CAssoc* pAssoc)
  127. {
  128. DestructElement(&pAssoc->key); // free up string data
  129. pAssoc->pNext = m_pFreeList;
  130. m_pFreeList = pAssoc;
  131. m_nCount--;
  132. ASSERT(m_nCount >= 0); // make sure we don't underflow
  133. // if no more elements, cleanup completely
  134. if (m_nCount == 0)
  135. RemoveAll();
  136. }
  137. CMapStringToPtr::CAssoc*
  138. CMapStringToPtr::GetAssocAt(LPCTSTR key, UINT& nHash) const
  139. // find association (or return NULL)
  140. {
  141. nHash = HashKey(key) % m_nHashTableSize;
  142. if (m_pHashTable == NULL)
  143. return NULL;
  144. // see if it exists
  145. CAssoc* pAssoc;
  146. for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext)
  147. {
  148. if (pAssoc->key == key)
  149. return pAssoc;
  150. }
  151. return NULL;
  152. }
  153. /////////////////////////////////////////////////////////////////////////////
  154. BOOL CMapStringToPtr::Lookup(LPCTSTR key, void*& rValue) const
  155. {
  156. ASSERT_VALID(this);
  157. UINT nHash;
  158. CAssoc* pAssoc = GetAssocAt(key, nHash);
  159. if (pAssoc == NULL)
  160. return FALSE; // not in map
  161. rValue = pAssoc->value;
  162. return TRUE;
  163. }
  164. BOOL CMapStringToPtr::LookupKey(LPCTSTR key, LPCTSTR& rKey) const
  165. {
  166. ASSERT_VALID(this);
  167. UINT nHash;
  168. CAssoc* pAssoc = GetAssocAt(key, nHash);
  169. if (pAssoc == NULL)
  170. return FALSE; // not in map
  171. rKey = pAssoc->key;
  172. return TRUE;
  173. }
  174. void*& CMapStringToPtr::operator[](LPCTSTR key)
  175. {
  176. ASSERT_VALID(this);
  177. UINT nHash;
  178. CAssoc* pAssoc;
  179. if ((pAssoc = GetAssocAt(key, nHash)) == NULL)
  180. {
  181. if (m_pHashTable == NULL)
  182. InitHashTable(m_nHashTableSize);
  183. // it doesn't exist, add a new Association
  184. pAssoc = NewAssoc();
  185. pAssoc->nHashValue = nHash;
  186. pAssoc->key = key;
  187. // 'pAssoc->value' is a constructed object, nothing more
  188. // put into hash table
  189. pAssoc->pNext = m_pHashTable[nHash];
  190. m_pHashTable[nHash] = pAssoc;
  191. }
  192. return pAssoc->value; // return new reference
  193. }
  194. BOOL CMapStringToPtr::RemoveKey(LPCTSTR key)
  195. // remove key - return TRUE if removed
  196. {
  197. ASSERT_VALID(this);
  198. if (m_pHashTable == NULL)
  199. return FALSE; // nothing in the table
  200. CAssoc** ppAssocPrev;
  201. ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize];
  202. CAssoc* pAssoc;
  203. for (pAssoc = *ppAssocPrev; pAssoc != NULL; pAssoc = pAssoc->pNext)
  204. {
  205. if (pAssoc->key == key)
  206. {
  207. // remove it
  208. *ppAssocPrev = pAssoc->pNext; // remove from list
  209. FreeAssoc(pAssoc);
  210. return TRUE;
  211. }
  212. ppAssocPrev = &pAssoc->pNext;
  213. }
  214. return FALSE; // not found
  215. }
  216. /////////////////////////////////////////////////////////////////////////////
  217. // Iterating
  218. void CMapStringToPtr::GetNextAssoc(POSITION& rNextPosition,
  219. CString& rKey, void*& rValue) const
  220. {
  221. ASSERT_VALID(this);
  222. ASSERT(m_pHashTable != NULL); // never call on empty map
  223. CAssoc* pAssocRet = (CAssoc*)rNextPosition;
  224. ASSERT(pAssocRet != NULL);
  225. if (pAssocRet == (CAssoc*) BEFORE_START_POSITION)
  226. {
  227. // find the first association
  228. for (UINT nBucket = 0; nBucket < m_nHashTableSize; nBucket++)
  229. if ((pAssocRet = m_pHashTable[nBucket]) != NULL)
  230. break;
  231. ASSERT(pAssocRet != NULL); // must find something
  232. }
  233. // find next association
  234. ASSERT(AfxIsValidAddress(pAssocRet, sizeof(CAssoc)));
  235. CAssoc* pAssocNext;
  236. if ((pAssocNext = pAssocRet->pNext) == NULL)
  237. {
  238. // go to next bucket
  239. for (UINT nBucket = pAssocRet->nHashValue + 1;
  240. nBucket < m_nHashTableSize; nBucket++)
  241. if ((pAssocNext = m_pHashTable[nBucket]) != NULL)
  242. break;
  243. }
  244. rNextPosition = (POSITION) pAssocNext;
  245. // fill in return data
  246. rKey = pAssocRet->key;
  247. rValue = pAssocRet->value;
  248. }
  249. /////////////////////////////////////////////////////////////////////////////
  250. // Diagnostics
  251. #ifdef _DEBUG
  252. void CMapStringToPtr::Dump(CDumpContext& dc) const
  253. {
  254. CObject::Dump(dc);
  255. dc << "with " << m_nCount << " elements";
  256. if (dc.GetDepth() > 0)
  257. {
  258. // Dump in format "[key] -> value"
  259. CString key;
  260. void* val;
  261. POSITION pos = GetStartPosition();
  262. while (pos != NULL)
  263. {
  264. GetNextAssoc(pos, key, val);
  265. dc << "\n\t[" << key << "] = " << val;
  266. }
  267. }
  268. dc << "\n";
  269. }
  270. void CMapStringToPtr::AssertValid() const
  271. {
  272. CObject::AssertValid();
  273. ASSERT(m_nHashTableSize > 0);
  274. ASSERT(m_nCount == 0 || m_pHashTable != NULL);
  275. // non-empty map should have hash table
  276. }
  277. #endif //_DEBUG
  278. #ifdef AFX_INIT_SEG
  279. #pragma code_seg(AFX_INIT_SEG)
  280. #endif
  281. IMPLEMENT_DYNAMIC(CMapStringToPtr, CObject)
  282. /////////////////////////////////////////////////////////////////////////////