map_ss.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. CMapStringToString::CMapStringToString(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 CMapStringToString::HashKey(LPCTSTR key) const
  37. {
  38. UINT nHash = 0;
  39. while (*key)
  40. nHash = (nHash<<5) + nHash + *key++;
  41. return nHash;
  42. }
  43. void CMapStringToString::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 CMapStringToString::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. DestructElement(&pAssoc->value);
  79. }
  80. }
  81. // free hash table
  82. delete [] m_pHashTable;
  83. m_pHashTable = NULL;
  84. }
  85. m_nCount = 0;
  86. m_pFreeList = NULL;
  87. m_pBlocks->FreeDataChain();
  88. m_pBlocks = NULL;
  89. }
  90. CMapStringToString::~CMapStringToString()
  91. {
  92. RemoveAll();
  93. ASSERT(m_nCount == 0);
  94. }
  95. /////////////////////////////////////////////////////////////////////////////
  96. // Assoc helpers
  97. // same as CList implementation except we store CAssoc's not CNode's
  98. // and CAssoc's are singly linked all the time
  99. CMapStringToString::CAssoc*
  100. CMapStringToString::NewAssoc()
  101. {
  102. if (m_pFreeList == NULL)
  103. {
  104. // add another block
  105. CPlex* newBlock = CPlex::Create(m_pBlocks, m_nBlockSize,
  106. sizeof(CMapStringToString::CAssoc));
  107. // chain them into free list
  108. CMapStringToString::CAssoc* pAssoc =
  109. (CMapStringToString::CAssoc*) newBlock->data();
  110. // free in reverse order to make it easier to debug
  111. pAssoc += m_nBlockSize - 1;
  112. for (int i = m_nBlockSize-1; i >= 0; i--, pAssoc--)
  113. {
  114. pAssoc->pNext = m_pFreeList;
  115. m_pFreeList = pAssoc;
  116. }
  117. }
  118. ASSERT(m_pFreeList != NULL); // we must have something
  119. CMapStringToString::CAssoc* pAssoc = m_pFreeList;
  120. m_pFreeList = m_pFreeList->pNext;
  121. m_nCount++;
  122. ASSERT(m_nCount > 0); // make sure we don't overflow
  123. memcpy(&pAssoc->key, &afxEmptyString, sizeof(CString));
  124. ConstructElement(&pAssoc->value);
  125. return pAssoc;
  126. }
  127. void CMapStringToString::FreeAssoc(CMapStringToString::CAssoc* pAssoc)
  128. {
  129. DestructElement(&pAssoc->key); // free up string data
  130. DestructElement(&pAssoc->value);
  131. pAssoc->pNext = m_pFreeList;
  132. m_pFreeList = pAssoc;
  133. m_nCount--;
  134. ASSERT(m_nCount >= 0); // make sure we don't underflow
  135. // if no more elements, cleanup completely
  136. if (m_nCount == 0)
  137. RemoveAll();
  138. }
  139. CMapStringToString::CAssoc*
  140. CMapStringToString::GetAssocAt(LPCTSTR key, UINT& nHash) const
  141. // find association (or return NULL)
  142. {
  143. nHash = HashKey(key) % m_nHashTableSize;
  144. if (m_pHashTable == NULL)
  145. return NULL;
  146. // see if it exists
  147. CAssoc* pAssoc;
  148. for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext)
  149. {
  150. if (pAssoc->key == key)
  151. return pAssoc;
  152. }
  153. return NULL;
  154. }
  155. /////////////////////////////////////////////////////////////////////////////
  156. BOOL CMapStringToString::Lookup(LPCTSTR key, CString& rValue) const
  157. {
  158. ASSERT_VALID(this);
  159. UINT nHash;
  160. CAssoc* pAssoc = GetAssocAt(key, nHash);
  161. if (pAssoc == NULL)
  162. return FALSE; // not in map
  163. rValue = pAssoc->value;
  164. return TRUE;
  165. }
  166. BOOL CMapStringToString::LookupKey(LPCTSTR key, LPCTSTR& rKey) const
  167. {
  168. ASSERT_VALID(this);
  169. UINT nHash;
  170. CAssoc* pAssoc = GetAssocAt(key, nHash);
  171. if (pAssoc == NULL)
  172. return FALSE; // not in map
  173. rKey = pAssoc->key;
  174. return TRUE;
  175. }
  176. CString& CMapStringToString::operator[](LPCTSTR key)
  177. {
  178. ASSERT_VALID(this);
  179. UINT nHash;
  180. CAssoc* pAssoc;
  181. if ((pAssoc = GetAssocAt(key, nHash)) == NULL)
  182. {
  183. if (m_pHashTable == NULL)
  184. InitHashTable(m_nHashTableSize);
  185. // it doesn't exist, add a new Association
  186. pAssoc = NewAssoc();
  187. pAssoc->nHashValue = nHash;
  188. pAssoc->key = key;
  189. // 'pAssoc->value' is a constructed object, nothing more
  190. // put into hash table
  191. pAssoc->pNext = m_pHashTable[nHash];
  192. m_pHashTable[nHash] = pAssoc;
  193. }
  194. return pAssoc->value; // return new reference
  195. }
  196. BOOL CMapStringToString::RemoveKey(LPCTSTR key)
  197. // remove key - return TRUE if removed
  198. {
  199. ASSERT_VALID(this);
  200. if (m_pHashTable == NULL)
  201. return FALSE; // nothing in the table
  202. CAssoc** ppAssocPrev;
  203. ppAssocPrev = &m_pHashTable[HashKey(key) % m_nHashTableSize];
  204. CAssoc* pAssoc;
  205. for (pAssoc = *ppAssocPrev; pAssoc != NULL; pAssoc = pAssoc->pNext)
  206. {
  207. if (pAssoc->key == key)
  208. {
  209. // remove it
  210. *ppAssocPrev = pAssoc->pNext; // remove from list
  211. FreeAssoc(pAssoc);
  212. return TRUE;
  213. }
  214. ppAssocPrev = &pAssoc->pNext;
  215. }
  216. return FALSE; // not found
  217. }
  218. /////////////////////////////////////////////////////////////////////////////
  219. // Iterating
  220. void CMapStringToString::GetNextAssoc(POSITION& rNextPosition,
  221. CString& rKey, CString& rValue) const
  222. {
  223. ASSERT_VALID(this);
  224. ASSERT(m_pHashTable != NULL); // never call on empty map
  225. CAssoc* pAssocRet = (CAssoc*)rNextPosition;
  226. ASSERT(pAssocRet != NULL);
  227. if (pAssocRet == (CAssoc*) BEFORE_START_POSITION)
  228. {
  229. // find the first association
  230. for (UINT nBucket = 0; nBucket < m_nHashTableSize; nBucket++)
  231. if ((pAssocRet = m_pHashTable[nBucket]) != NULL)
  232. break;
  233. ASSERT(pAssocRet != NULL); // must find something
  234. }
  235. // find next association
  236. ASSERT(AfxIsValidAddress(pAssocRet, sizeof(CAssoc)));
  237. CAssoc* pAssocNext;
  238. if ((pAssocNext = pAssocRet->pNext) == NULL)
  239. {
  240. // go to next bucket
  241. for (UINT nBucket = pAssocRet->nHashValue + 1;
  242. nBucket < m_nHashTableSize; nBucket++)
  243. if ((pAssocNext = m_pHashTable[nBucket]) != NULL)
  244. break;
  245. }
  246. rNextPosition = (POSITION) pAssocNext;
  247. // fill in return data
  248. rKey = pAssocRet->key;
  249. rValue = pAssocRet->value;
  250. }
  251. /////////////////////////////////////////////////////////////////////////////
  252. // Serialization
  253. void CMapStringToString::Serialize(CArchive& ar)
  254. {
  255. ASSERT_VALID(this);
  256. CObject::Serialize(ar);
  257. if (ar.IsStoring())
  258. {
  259. ar.WriteCount(m_nCount);
  260. if (m_nCount == 0)
  261. return; // nothing more to do
  262. ASSERT(m_pHashTable != NULL);
  263. for (UINT nHash = 0; nHash < m_nHashTableSize; nHash++)
  264. {
  265. CAssoc* pAssoc;
  266. for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL;
  267. pAssoc = pAssoc->pNext)
  268. {
  269. ar << pAssoc->key;
  270. ar << pAssoc->value;
  271. }
  272. }
  273. }
  274. else
  275. {
  276. DWORD nNewCount = ar.ReadCount();
  277. CString newKey;
  278. CString newValue;
  279. while (nNewCount--)
  280. {
  281. ar >> newKey;
  282. ar >> newValue;
  283. SetAt(newKey, newValue);
  284. }
  285. }
  286. }
  287. /////////////////////////////////////////////////////////////////////////////
  288. // Diagnostics
  289. #ifdef _DEBUG
  290. void CMapStringToString::Dump(CDumpContext& dc) const
  291. {
  292. CObject::Dump(dc);
  293. dc << "with " << m_nCount << " elements";
  294. if (dc.GetDepth() > 0)
  295. {
  296. // Dump in format "[key] -> value"
  297. CString key;
  298. CString val;
  299. POSITION pos = GetStartPosition();
  300. while (pos != NULL)
  301. {
  302. GetNextAssoc(pos, key, val);
  303. dc << "\n\t[" << key << "] = " << val;
  304. }
  305. }
  306. dc << "\n";
  307. }
  308. void CMapStringToString::AssertValid() const
  309. {
  310. CObject::AssertValid();
  311. ASSERT(m_nHashTableSize > 0);
  312. ASSERT(m_nCount == 0 || m_pHashTable != NULL);
  313. // non-empty map should have hash table
  314. }
  315. #endif //_DEBUG
  316. #ifdef AFX_INIT_SEG
  317. #pragma code_seg(AFX_INIT_SEG)
  318. #endif
  319. IMPLEMENT_SERIAL(CMapStringToString, CObject, 0)
  320. /////////////////////////////////////////////////////////////////////////////