Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1432 lines
42 KiB

  1. /*****************************************************************************
  2. * SPCollec.h *
  3. *------------*
  4. * This header file contains the SAPI5 collection class templates. These
  5. * are a modified version of the MFC template classes without the dependencies.
  6. *-----------------------------------------------------------------------------
  7. * Copyright (c) Microsoft Corporation. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef SPCollec_h
  10. #define SPCollec_h
  11. #ifndef _INC_LIMITS
  12. #include <limits.h>
  13. #endif
  14. #ifndef _INC_STRING
  15. #include <string.h>
  16. #endif
  17. #ifndef _INC_STDLIB
  18. #include <stdlib.h>
  19. #endif
  20. #ifndef _WIN32_WCE
  21. #ifndef _INC_SEARCH
  22. #include <search.h>
  23. #endif
  24. #endif
  25. /////////////////////////////////////////////////////////////////////////////
  26. #define SPASSERT_VALID( a ) // This doesn't do anything right now
  27. typedef void* SPLISTPOS;
  28. typedef DWORD SPLISTHANDLE;
  29. #define SP_BEFORE_START_POSITION ((void*)-1L)
  30. inline BOOL SPIsValidAddress(const void* lp, UINT nBytes, BOOL bReadWrite)
  31. {
  32. // simple version using Win-32 APIs for pointer validation.
  33. return (lp != NULL && !IsBadReadPtr(lp, nBytes) &&
  34. (!bReadWrite || !IsBadWritePtr((LPVOID)lp, nBytes)));
  35. }
  36. /////////////////////////////////////////////////////////////////////////////
  37. // global helpers (can be overridden)
  38. template<class TYPE>
  39. inline HRESULT SPConstructElements(TYPE* pElements, int nCount)
  40. {
  41. HRESULT hr = S_OK;
  42. SPDBG_ASSERT( nCount == 0 ||
  43. SPIsValidAddress( pElements, nCount * sizeof(TYPE), TRUE ) );
  44. // default is bit-wise zero initialization
  45. memset((void*)pElements, 0, nCount * sizeof(TYPE));
  46. return hr;
  47. }
  48. template<class TYPE>
  49. inline void SPDestructElements(TYPE* pElements, int nCount)
  50. {
  51. SPDBG_ASSERT( ( nCount == 0 ||
  52. SPIsValidAddress( pElements, nCount * sizeof(TYPE), TRUE ) ) );
  53. pElements; // not used
  54. nCount; // not used
  55. // default does nothing
  56. }
  57. template<class TYPE>
  58. inline HRESULT SPCopyElements(TYPE* pDest, const TYPE* pSrc, int nCount)
  59. {
  60. HRESULT hr = S_OK;
  61. SPDBG_ASSERT( ( nCount == 0 ||
  62. SPIsValidAddress( pDest, nCount * sizeof(TYPE), TRUE )) );
  63. SPDBG_ASSERT( ( nCount == 0 ||
  64. SPIsValidAddress( pSrc, nCount * sizeof(TYPE), FALSE )) );
  65. // default is bit-wise copy
  66. memcpy(pDest, pSrc, nCount * sizeof(TYPE));
  67. return hr;
  68. }
  69. template<class TYPE, class ARG_TYPE>
  70. BOOL SPCompareElements(const TYPE* pElement1, const ARG_TYPE* pElement2)
  71. {
  72. SPDBG_ASSERT( SPIsValidAddress( pElement1, sizeof(TYPE), FALSE ) );
  73. SPDBG_ASSERT( SPIsValidAddress( pElement2, sizeof(ARG_TYPE), FALSE ) );
  74. return *pElement1 == *pElement2;
  75. }
  76. template<class ARG_KEY>
  77. inline UINT SPHashKey(ARG_KEY key)
  78. {
  79. // default identity hash - works for most primitive values
  80. return ((UINT)(void*)(DWORD)key) >> 4;
  81. }
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CSPPlex
  84. struct CSPPlex // warning variable length structure
  85. {
  86. CSPPlex* pNext;
  87. UINT nMax;
  88. UINT nCur;
  89. /* BYTE data[maxNum*elementSize]; */
  90. void* data() { return this+1; }
  91. static CSPPlex* PASCAL Create( CSPPlex*& pHead, UINT nMax, UINT cbElement )
  92. {
  93. CSPPlex* p = (CSPPlex*) new BYTE[sizeof(CSPPlex) + nMax * cbElement];
  94. SPDBG_ASSERT(p);
  95. p->nMax = nMax;
  96. p->nCur = 0;
  97. p->pNext = pHead;
  98. pHead = p; // change head (adds in reverse order for simplicity)
  99. return p;
  100. }
  101. void FreeDataChain()
  102. {
  103. CSPPlex* p = this;
  104. while (p != NULL)
  105. {
  106. BYTE* bytes = (BYTE*) p;
  107. CSPPlex* pNext = p->pNext;
  108. delete[] bytes;
  109. p = pNext;
  110. }
  111. }
  112. };
  113. /////////////////////////////////////////////////////////////////////////////
  114. // CSPArray<TYPE, ARG_TYPE>
  115. template<class TYPE, class ARG_TYPE>
  116. class CSPArray
  117. {
  118. public:
  119. // Construction
  120. CSPArray();
  121. // Attributes
  122. int GetSize() const;
  123. int GetUpperBound() const;
  124. HRESULT SetSize(int nNewSize, int nGrowBy = -1);
  125. // Operations
  126. // Clean up
  127. void FreeExtra();
  128. void RemoveAll();
  129. // Accessing elements
  130. TYPE GetAt(int nIndex) const;
  131. void SetAt(int nIndex, ARG_TYPE newElement);
  132. TYPE& ElementAt(int nIndex);
  133. // Direct Access to the element data (may return NULL)
  134. const TYPE* GetData() const;
  135. TYPE* GetData();
  136. // Potentially growing the array
  137. HRESULT SetAtGrow(int nIndex, ARG_TYPE newElement);
  138. int Add(ARG_TYPE newElement);
  139. int Append(const CSPArray& src);
  140. HRESULT Copy(const CSPArray& src);
  141. // overloaded operator helpers
  142. TYPE operator[](int nIndex) const;
  143. TYPE& operator[](int nIndex);
  144. // Operations that move elements around
  145. HRESULT InsertAt(int nIndex, ARG_TYPE newElement, int nCount = 1);
  146. void RemoveAt(int nIndex, int nCount = 1);
  147. HRESULT InsertAt(int nStartIndex, CSPArray* pNewArray);
  148. void Sort(int (__cdecl *compare )(const void *elem1, const void *elem2 ));
  149. // Implementation
  150. protected:
  151. TYPE* m_pData; // the actual array of data
  152. int m_nSize; // # of elements (upperBound - 1)
  153. int m_nMaxSize; // max allocated
  154. int m_nGrowBy; // grow amount
  155. public:
  156. ~CSPArray();
  157. #ifdef _DEBUG
  158. // void Dump(CDumpContext&) const;
  159. void AssertValid() const;
  160. #endif
  161. };
  162. /////////////////////////////////////////////////////////////////////////////
  163. // CSPArray<TYPE, ARG_TYPE> inline functions
  164. template<class TYPE, class ARG_TYPE>
  165. inline int CSPArray<TYPE, ARG_TYPE>::GetSize() const
  166. { return m_nSize; }
  167. template<class TYPE, class ARG_TYPE>
  168. inline int CSPArray<TYPE, ARG_TYPE>::GetUpperBound() const
  169. { return m_nSize-1; }
  170. template<class TYPE, class ARG_TYPE>
  171. inline void CSPArray<TYPE, ARG_TYPE>::RemoveAll()
  172. { SetSize(0, -1); }
  173. template<class TYPE, class ARG_TYPE>
  174. inline TYPE CSPArray<TYPE, ARG_TYPE>::GetAt(int nIndex) const
  175. { SPDBG_ASSERT( (nIndex >= 0 && nIndex < m_nSize) );
  176. return m_pData[nIndex]; }
  177. template<class TYPE, class ARG_TYPE>
  178. inline void CSPArray<TYPE, ARG_TYPE>::SetAt(int nIndex, ARG_TYPE newElement)
  179. { SPDBG_ASSERT( (nIndex >= 0 && nIndex < m_nSize) );
  180. m_pData[nIndex] = newElement; }
  181. template<class TYPE, class ARG_TYPE>
  182. inline TYPE& CSPArray<TYPE, ARG_TYPE>::ElementAt(int nIndex)
  183. { SPDBG_ASSERT( (nIndex >= 0 && nIndex < m_nSize) );
  184. return m_pData[nIndex]; }
  185. template<class TYPE, class ARG_TYPE>
  186. inline const TYPE* CSPArray<TYPE, ARG_TYPE>::GetData() const
  187. { return (const TYPE*)m_pData; }
  188. template<class TYPE, class ARG_TYPE>
  189. inline TYPE* CSPArray<TYPE, ARG_TYPE>::GetData()
  190. { return (TYPE*)m_pData; }
  191. template<class TYPE, class ARG_TYPE>
  192. inline int CSPArray<TYPE, ARG_TYPE>::Add(ARG_TYPE newElement)
  193. { int nIndex = m_nSize;
  194. SetAtGrow(nIndex, newElement);
  195. return nIndex; }
  196. template<class TYPE, class ARG_TYPE>
  197. inline TYPE CSPArray<TYPE, ARG_TYPE>::operator[](int nIndex) const
  198. { return GetAt(nIndex); }
  199. template<class TYPE, class ARG_TYPE>
  200. inline TYPE& CSPArray<TYPE, ARG_TYPE>::operator[](int nIndex)
  201. { return ElementAt(nIndex); }
  202. /////////////////////////////////////////////////////////////////////////////
  203. // CSPArray<TYPE, ARG_TYPE> out-of-line functions
  204. template<class TYPE, class ARG_TYPE>
  205. CSPArray<TYPE, ARG_TYPE>::CSPArray()
  206. {
  207. m_pData = NULL;
  208. m_nSize = m_nMaxSize = m_nGrowBy = 0;
  209. }
  210. template<class TYPE, class ARG_TYPE>
  211. CSPArray<TYPE, ARG_TYPE>::~CSPArray()
  212. {
  213. SPASSERT_VALID( this );
  214. if (m_pData != NULL)
  215. {
  216. SPDestructElements(m_pData, m_nSize);
  217. delete[] (BYTE*)m_pData;
  218. }
  219. }
  220. template<class TYPE, class ARG_TYPE>
  221. HRESULT CSPArray<TYPE, ARG_TYPE>::SetSize(int nNewSize, int nGrowBy)
  222. {
  223. SPASSERT_VALID( this );
  224. SPDBG_ASSERT( nNewSize >= 0 );
  225. HRESULT hr = S_OK;
  226. if (nGrowBy != -1)
  227. m_nGrowBy = nGrowBy; // set new size
  228. if (nNewSize == 0)
  229. {
  230. // shrink to nothing
  231. if (m_pData != NULL)
  232. {
  233. SPDestructElements(m_pData, m_nSize);
  234. delete[] (BYTE*)m_pData;
  235. m_pData = NULL;
  236. }
  237. m_nSize = m_nMaxSize = 0;
  238. }
  239. else if (m_pData == NULL)
  240. {
  241. // create one with exact size
  242. #ifdef SIZE_T_MAX
  243. SPDBG_ASSERT( nNewSize <= SIZE_T_MAX/sizeof(TYPE) ); // no overflow
  244. #endif
  245. m_pData = (TYPE*) new BYTE[nNewSize * sizeof(TYPE)];
  246. if( m_pData )
  247. {
  248. hr = SPConstructElements(m_pData, nNewSize);
  249. if( SUCCEEDED( hr ) )
  250. {
  251. m_nSize = m_nMaxSize = nNewSize;
  252. }
  253. else
  254. {
  255. delete[] (BYTE*)m_pData;
  256. m_pData = NULL;
  257. }
  258. }
  259. else
  260. {
  261. hr = E_OUTOFMEMORY;
  262. }
  263. }
  264. else if (nNewSize <= m_nMaxSize)
  265. {
  266. // it fits
  267. if (nNewSize > m_nSize)
  268. {
  269. // initialize the new elements
  270. hr = SPConstructElements(&m_pData[m_nSize], nNewSize-m_nSize);
  271. }
  272. else if (m_nSize > nNewSize)
  273. {
  274. // destroy the old elements
  275. SPDestructElements(&m_pData[nNewSize], m_nSize-nNewSize);
  276. }
  277. if( SUCCEEDED( hr ) )
  278. {
  279. m_nSize = nNewSize;
  280. }
  281. }
  282. else
  283. {
  284. // otherwise, grow array
  285. int nGrowBy = m_nGrowBy;
  286. if (nGrowBy == 0)
  287. {
  288. // heuristically determe growth when nGrowBy == 0
  289. // (this avoids heap fragmentation in many situations)
  290. nGrowBy = min(1024, max(4, m_nSize / 8));
  291. }
  292. int nNewMax;
  293. if (nNewSize < m_nMaxSize + nGrowBy)
  294. nNewMax = m_nMaxSize + nGrowBy; // granularity
  295. else
  296. nNewMax = nNewSize; // no slush
  297. SPDBG_ASSERT( nNewMax >= m_nMaxSize ); // no wrap around
  298. #ifdef SIZE_T_MAX
  299. SPDBG_ASSERT( nNewMax <= SIZE_T_MAX/sizeof(TYPE) ); // no overflow
  300. #endif
  301. TYPE* pNewData = (TYPE*) new BYTE[nNewMax * sizeof(TYPE)];
  302. if( pNewData )
  303. {
  304. // copy new data from old
  305. memcpy(pNewData, m_pData, m_nSize * sizeof(TYPE));
  306. // construct remaining elements
  307. SPDBG_ASSERT( nNewSize > m_nSize );
  308. hr = SPConstructElements(&pNewData[m_nSize], nNewSize-m_nSize);
  309. // get rid of old stuff (note: no destructors called)
  310. delete[] (BYTE*)m_pData;
  311. m_pData = pNewData;
  312. m_nSize = nNewSize;
  313. m_nMaxSize = nNewMax;
  314. }
  315. else
  316. {
  317. hr = E_OUTOFMEMORY;
  318. }
  319. }
  320. return hr;
  321. }
  322. template<class TYPE, class ARG_TYPE>
  323. int CSPArray<TYPE, ARG_TYPE>::Append(const CSPArray& src)
  324. {
  325. SPASSERT_VALID( this );
  326. SPDBG_ASSERT( this != &src ); // cannot append to itself
  327. int nOldSize = m_nSize;
  328. HRESULT hr = SetSize(m_nSize + src.m_nSize);
  329. if( SUCCEEDED( hr ) )
  330. {
  331. hr = SPCopyElements(m_pData + nOldSize, src.m_pData, src.m_nSize);
  332. }
  333. return ( SUCCEEDED( hr ) )?(nOldSize):(-1);
  334. }
  335. template<class TYPE, class ARG_TYPE>
  336. HRESULT CSPArray<TYPE, ARG_TYPE>::Copy(const CSPArray& src)
  337. {
  338. SPASSERT_VALID( this );
  339. SPDBG_ASSERT( this != &src ); // cannot copy to itself
  340. HRESULT hr = SetSize(src.m_nSize);
  341. if( SUCCEEDED( hr ) )
  342. {
  343. hr = SPCopyElements(m_pData, src.m_pData, src.m_nSize);
  344. }
  345. return hr;
  346. }
  347. template<class TYPE, class ARG_TYPE>
  348. void CSPArray<TYPE, ARG_TYPE>::FreeExtra()
  349. {
  350. SPASSERT_VALID( this );
  351. if (m_nSize != m_nMaxSize)
  352. {
  353. // shrink to desired size
  354. #ifdef SIZE_T_MAX
  355. SPDBG_ASSERT( m_nSize <= SIZE_T_MAX/sizeof(TYPE)); // no overflow
  356. #endif
  357. TYPE* pNewData = NULL;
  358. if (m_nSize != 0)
  359. {
  360. pNewData = (TYPE*) new BYTE[m_nSize * sizeof(TYPE)];
  361. SPDBG_ASSERT(pNewData);
  362. // copy new data from old
  363. memcpy(pNewData, m_pData, m_nSize * sizeof(TYPE));
  364. }
  365. // get rid of old stuff (note: no destructors called)
  366. delete[] (BYTE*)m_pData;
  367. m_pData = pNewData;
  368. m_nMaxSize = m_nSize;
  369. }
  370. }
  371. template<class TYPE, class ARG_TYPE>
  372. HRESULT CSPArray<TYPE, ARG_TYPE>::SetAtGrow(int nIndex, ARG_TYPE newElement)
  373. {
  374. SPASSERT_VALID( this );
  375. SPDBG_ASSERT( nIndex >= 0 );
  376. HRESULT hr = S_OK;
  377. if (nIndex >= m_nSize)
  378. {
  379. hr = SetSize(nIndex+1, -1);
  380. }
  381. if( SUCCEEDED( hr ) )
  382. {
  383. m_pData[nIndex] = newElement;
  384. }
  385. return hr;
  386. }
  387. template<class TYPE, class ARG_TYPE>
  388. HRESULT CSPArray<TYPE, ARG_TYPE>::InsertAt(int nIndex, ARG_TYPE newElement, int nCount /*=1*/)
  389. {
  390. SPASSERT_VALID( this );
  391. SPDBG_ASSERT( nIndex >= 0 ); // will expand to meet need
  392. SPDBG_ASSERT( nCount > 0 ); // zero or negative size not allowed
  393. HRESULT hr = S_OK;
  394. if (nIndex >= m_nSize)
  395. {
  396. // adding after the end of the array
  397. hr = SetSize(nIndex + nCount, -1); // grow so nIndex is valid
  398. }
  399. else
  400. {
  401. // inserting in the middle of the array
  402. int nOldSize = m_nSize;
  403. hr = SetSize(m_nSize + nCount, -1); // grow it to new size
  404. if( SUCCEEDED( hr ) )
  405. {
  406. // shift old data up to fill gap
  407. memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],
  408. (nOldSize-nIndex) * sizeof(TYPE));
  409. // re-init slots we copied from
  410. hr = SPConstructElements(&m_pData[nIndex], nCount);
  411. }
  412. }
  413. // insert new value in the gap
  414. if( SUCCEEDED( hr ) )
  415. {
  416. SPDBG_ASSERT( nIndex + nCount <= m_nSize );
  417. while (nCount--)
  418. m_pData[nIndex++] = newElement;
  419. }
  420. return hr;
  421. }
  422. template<class TYPE, class ARG_TYPE>
  423. void CSPArray<TYPE, ARG_TYPE>::RemoveAt(int nIndex, int nCount)
  424. {
  425. SPASSERT_VALID( this );
  426. SPDBG_ASSERT( nIndex >= 0 );
  427. SPDBG_ASSERT( nCount >= 0 );
  428. SPDBG_ASSERT( nIndex + nCount <= m_nSize );
  429. // just remove a range
  430. int nMoveCount = m_nSize - (nIndex + nCount);
  431. SPDestructElements(&m_pData[nIndex], nCount);
  432. if (nMoveCount)
  433. memcpy(&m_pData[nIndex], &m_pData[nIndex + nCount],
  434. nMoveCount * sizeof(TYPE));
  435. m_nSize -= nCount;
  436. }
  437. template<class TYPE, class ARG_TYPE>
  438. HRESULT CSPArray<TYPE, ARG_TYPE>::InsertAt(int nStartIndex, CSPArray* pNewArray)
  439. {
  440. SPASSERT_VALID( this );
  441. SPASSERT_VALID( pNewArray );
  442. SPDBG_ASSERT( nStartIndex >= 0 );
  443. HRESULT hr = S_OK;
  444. if (pNewArray->GetSize() > 0)
  445. {
  446. hr = InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
  447. for (int i = 0; SUCCEEDED( hr )&& (i < pNewArray->GetSize()); i++)
  448. {
  449. SetAt(nStartIndex + i, pNewArray->GetAt(i));
  450. }
  451. }
  452. return hr;
  453. }
  454. template<class TYPE, class ARG_TYPE>
  455. void CSPArray<TYPE, ARG_TYPE>::Sort(int (__cdecl *compare )(const void *elem1, const void *elem2 ))
  456. {
  457. SPASSERT_VALID( this );
  458. SPDBG_ASSERT( m_pData != NULL );
  459. qsort( m_pData, m_nSize, sizeof(TYPE), compare );
  460. }
  461. #ifdef _DEBUG
  462. template<class TYPE, class ARG_TYPE>
  463. void CSPArray<TYPE, ARG_TYPE>::AssertValid() const
  464. {
  465. if (m_pData == NULL)
  466. {
  467. SPDBG_ASSERT( m_nSize == 0 );
  468. SPDBG_ASSERT( m_nMaxSize == 0 );
  469. }
  470. else
  471. {
  472. SPDBG_ASSERT( m_nSize >= 0 );
  473. SPDBG_ASSERT( m_nMaxSize >= 0 );
  474. SPDBG_ASSERT( m_nSize <= m_nMaxSize );
  475. SPDBG_ASSERT( SPIsValidAddress(m_pData, m_nMaxSize * sizeof(TYPE), TRUE ) );
  476. }
  477. }
  478. #endif //_DEBUG
  479. /////////////////////////////////////////////////////////////////////////////
  480. // CSPList<TYPE, ARG_TYPE>
  481. template<class TYPE, class ARG_TYPE>
  482. class CSPList
  483. {
  484. protected:
  485. struct CNode
  486. {
  487. CNode* pNext;
  488. CNode* pPrev;
  489. TYPE data;
  490. };
  491. public:
  492. // Construction
  493. CSPList(int nBlockSize = 10);
  494. // Attributes (head and tail)
  495. // count of elements
  496. int GetCount() const;
  497. BOOL IsEmpty() const;
  498. // peek at head or tail
  499. TYPE& GetHead();
  500. TYPE GetHead() const;
  501. TYPE& GetTail();
  502. TYPE GetTail() const;
  503. // Operations
  504. // get head or tail (and remove it) - don't call on empty list !
  505. TYPE RemoveHead();
  506. TYPE RemoveTail();
  507. // add before head or after tail
  508. SPLISTPOS AddHead(ARG_TYPE newElement);
  509. SPLISTPOS AddTail(ARG_TYPE newElement);
  510. // add another list of elements before head or after tail
  511. void AddHead(CSPList* pNewList);
  512. void AddTail(CSPList* pNewList);
  513. // remove all elements
  514. void RemoveAll();
  515. // iteration
  516. SPLISTPOS GetHeadPosition() const;
  517. SPLISTPOS GetTailPosition() const;
  518. TYPE& GetNext(SPLISTPOS& rPosition); // return *Position++
  519. TYPE GetNext(SPLISTPOS& rPosition) const; // return *Position++
  520. TYPE& GetPrev(SPLISTPOS& rPosition); // return *Position--
  521. TYPE GetPrev(SPLISTPOS& rPosition) const; // return *Position--
  522. // getting/modifying an element at a given position
  523. TYPE& GetAt(SPLISTPOS position);
  524. TYPE GetAt(SPLISTPOS position) const;
  525. void SetAt(SPLISTPOS pos, ARG_TYPE newElement);
  526. void RemoveAt(SPLISTPOS position);
  527. // inserting before or after a given position
  528. SPLISTPOS InsertBefore(SPLISTPOS position, ARG_TYPE newElement);
  529. SPLISTPOS InsertAfter(SPLISTPOS position, ARG_TYPE newElement);
  530. // helper functions (note: O(n) speed)
  531. SPLISTPOS Find(ARG_TYPE searchValue, SPLISTPOS startAfter = NULL) const;
  532. // defaults to starting at the HEAD, return NULL if not found
  533. SPLISTPOS FindIndex(int nIndex) const;
  534. // get the 'nIndex'th element (may return NULL)
  535. // Implementation
  536. protected:
  537. CNode* m_pNodeHead;
  538. CNode* m_pNodeTail;
  539. int m_nCount;
  540. CNode* m_pNodeFree;
  541. struct CSPPlex* m_pBlocks;
  542. int m_nBlockSize;
  543. CNode* NewNode(CNode*, CNode*);
  544. void FreeNode(CNode*);
  545. public:
  546. ~CSPList();
  547. #ifdef _DEBUG
  548. void AssertValid() const;
  549. #endif
  550. };
  551. /////////////////////////////////////////////////////////////////////////////
  552. // CSPList<TYPE, ARG_TYPE> inline functions
  553. template<class TYPE, class ARG_TYPE>
  554. inline int CSPList<TYPE, ARG_TYPE>::GetCount() const
  555. { return m_nCount; }
  556. template<class TYPE, class ARG_TYPE>
  557. inline BOOL CSPList<TYPE, ARG_TYPE>::IsEmpty() const
  558. { return m_nCount == 0; }
  559. template<class TYPE, class ARG_TYPE>
  560. inline TYPE& CSPList<TYPE, ARG_TYPE>::GetHead()
  561. { SPDBG_ASSERT( m_pNodeHead != NULL );
  562. return m_pNodeHead->data; }
  563. template<class TYPE, class ARG_TYPE>
  564. inline TYPE CSPList<TYPE, ARG_TYPE>::GetHead() const
  565. { SPDBG_ASSERT( m_pNodeHead != NULL );
  566. return m_pNodeHead->data; }
  567. template<class TYPE, class ARG_TYPE>
  568. inline TYPE& CSPList<TYPE, ARG_TYPE>::GetTail()
  569. { SPDBG_ASSERT( m_pNodeTail != NULL );
  570. return m_pNodeTail->data; }
  571. template<class TYPE, class ARG_TYPE>
  572. inline TYPE CSPList<TYPE, ARG_TYPE>::GetTail() const
  573. { SPDBG_ASSERT( m_pNodeTail != NULL );
  574. return m_pNodeTail->data; }
  575. template<class TYPE, class ARG_TYPE>
  576. inline SPLISTPOS CSPList<TYPE, ARG_TYPE>::GetHeadPosition() const
  577. { return (SPLISTPOS) m_pNodeHead; }
  578. template<class TYPE, class ARG_TYPE>
  579. inline SPLISTPOS CSPList<TYPE, ARG_TYPE>::GetTailPosition() const
  580. { return (SPLISTPOS) m_pNodeTail; }
  581. template<class TYPE, class ARG_TYPE>
  582. inline TYPE& CSPList<TYPE, ARG_TYPE>::GetNext(SPLISTPOS& rPosition) // return *Position++
  583. { CNode* pNode = (CNode*) rPosition;
  584. SPDBG_ASSERT( SPIsValidAddress(pNode, sizeof(CNode), TRUE ) );
  585. rPosition = (SPLISTPOS) pNode->pNext;
  586. return pNode->data; }
  587. template<class TYPE, class ARG_TYPE>
  588. inline TYPE CSPList<TYPE, ARG_TYPE>::GetNext(SPLISTPOS& rPosition) const // return *Position++
  589. { CNode* pNode = (CNode*) rPosition;
  590. SPDBG_ASSERT( SPIsValidAddress(pNode, sizeof(CNode), TRUE ) );
  591. rPosition = (SPLISTPOS) pNode->pNext;
  592. return pNode->data; }
  593. template<class TYPE, class ARG_TYPE>
  594. inline TYPE& CSPList<TYPE, ARG_TYPE>::GetPrev(SPLISTPOS& rPosition) // return *Position--
  595. { CNode* pNode = (CNode*) rPosition;
  596. SPDBG_ASSERT( SPIsValidAddress(pNode, sizeof(CNode), TRUE ) );
  597. rPosition = (SPLISTPOS) pNode->pPrev;
  598. return pNode->data; }
  599. template<class TYPE, class ARG_TYPE>
  600. inline TYPE CSPList<TYPE, ARG_TYPE>::GetPrev(SPLISTPOS& rPosition) const // return *Position--
  601. { CNode* pNode = (CNode*) rPosition;
  602. SPDBG_ASSERT( SPIsValidAddress(pNode, sizeof(CNode), TRUE ) );
  603. rPosition = (SPLISTPOS) pNode->pPrev;
  604. return pNode->data; }
  605. template<class TYPE, class ARG_TYPE>
  606. inline TYPE& CSPList<TYPE, ARG_TYPE>::GetAt(SPLISTPOS position)
  607. { CNode* pNode = (CNode*) position;
  608. SPDBG_ASSERT( SPIsValidAddress(pNode, sizeof(CNode), TRUE ) );
  609. return pNode->data; }
  610. template<class TYPE, class ARG_TYPE>
  611. inline TYPE CSPList<TYPE, ARG_TYPE>::GetAt(SPLISTPOS position) const
  612. { CNode* pNode = (CNode*) position;
  613. SPDBG_ASSERT( SPIsValidAddress(pNode, sizeof(CNode), TRUE ) );
  614. return pNode->data; }
  615. template<class TYPE, class ARG_TYPE>
  616. inline void CSPList<TYPE, ARG_TYPE>::SetAt(SPLISTPOS pos, ARG_TYPE newElement)
  617. { CNode* pNode = (CNode*) pos;
  618. SPDBG_ASSERT( SPIsValidAddress(pNode, sizeof(CNode), TRUE ) );
  619. pNode->data = newElement; }
  620. /////////////////////////////////////////////////////////////////////////////
  621. // CSPList<TYPE, ARG_TYPE> out-of-line functions
  622. template<class TYPE, class ARG_TYPE>
  623. CSPList<TYPE, ARG_TYPE>::CSPList( int nBlockSize )
  624. {
  625. SPDBG_ASSERT( nBlockSize > 0 );
  626. m_nCount = 0;
  627. m_pNodeHead = m_pNodeTail = m_pNodeFree = NULL;
  628. m_pBlocks = NULL;
  629. m_nBlockSize = nBlockSize;
  630. }
  631. template<class TYPE, class ARG_TYPE>
  632. void CSPList<TYPE, ARG_TYPE>::RemoveAll()
  633. {
  634. SPASSERT_VALID( this );
  635. // destroy elements
  636. CNode* pNode;
  637. for (pNode = m_pNodeHead; pNode != NULL; pNode = pNode->pNext)
  638. SPDestructElements(&pNode->data, 1);
  639. m_nCount = 0;
  640. m_pNodeHead = m_pNodeTail = m_pNodeFree = NULL;
  641. m_pBlocks->FreeDataChain();
  642. m_pBlocks = NULL;
  643. }
  644. template<class TYPE, class ARG_TYPE>
  645. CSPList<TYPE, ARG_TYPE>::~CSPList()
  646. {
  647. RemoveAll();
  648. SPDBG_ASSERT( m_nCount == 0 );
  649. }
  650. /////////////////////////////////////////////////////////////////////////////
  651. // Node helpers
  652. //
  653. // Implementation note: CNode's are stored in CSPPlex blocks and
  654. // chained together. Free blocks are maintained in a singly linked list
  655. // using the 'pNext' member of CNode with 'm_pNodeFree' as the head.
  656. // Used blocks are maintained in a doubly linked list using both 'pNext'
  657. // and 'pPrev' as links and 'm_pNodeHead' and 'm_pNodeTail'
  658. // as the head/tail.
  659. //
  660. // We never free a CSPPlex block unless the List is destroyed or RemoveAll()
  661. // is used - so the total number of CSPPlex blocks may grow large depending
  662. // on the maximum past size of the list.
  663. //
  664. template<class TYPE, class ARG_TYPE>
  665. typename CSPList<TYPE, ARG_TYPE>::CNode*
  666. CSPList<TYPE, ARG_TYPE>::NewNode(CNode* pPrev, CNode* pNext)
  667. {
  668. if (m_pNodeFree == NULL)
  669. {
  670. // add another block
  671. CSPPlex* pNewBlock = CSPPlex::Create(m_pBlocks, m_nBlockSize,sizeof(CNode));
  672. // chain them into free list
  673. CNode* pNode = (CNode*) pNewBlock->data();
  674. // free in reverse order to make it easier to debug
  675. pNode += m_nBlockSize - 1;
  676. for (int i = m_nBlockSize-1; i >= 0; i--, pNode--)
  677. {
  678. pNode->pNext = m_pNodeFree;
  679. m_pNodeFree = pNode;
  680. }
  681. }
  682. CSPList::CNode* pNode = m_pNodeFree;
  683. if( pNode )
  684. {
  685. if( SUCCEEDED( SPConstructElements(&pNode->data, 1) ) )
  686. {
  687. m_pNodeFree = m_pNodeFree->pNext;
  688. pNode->pPrev = pPrev;
  689. pNode->pNext = pNext;
  690. m_nCount++;
  691. SPDBG_ASSERT( m_nCount > 0 ); // make sure we don't overflow
  692. }
  693. }
  694. return pNode;
  695. }
  696. template<class TYPE, class ARG_TYPE>
  697. void CSPList<TYPE, ARG_TYPE>::FreeNode(CNode* pNode)
  698. {
  699. SPDestructElements(&pNode->data, 1);
  700. pNode->pNext = m_pNodeFree;
  701. m_pNodeFree = pNode;
  702. m_nCount--;
  703. SPDBG_ASSERT( m_nCount >= 0 ); // make sure we don't underflow
  704. }
  705. template<class TYPE, class ARG_TYPE>
  706. SPLISTPOS CSPList<TYPE, ARG_TYPE>::AddHead(ARG_TYPE newElement)
  707. {
  708. SPASSERT_VALID( this );
  709. CNode* pNewNode = NewNode(NULL, m_pNodeHead);
  710. if( pNewNode )
  711. {
  712. pNewNode->data = newElement;
  713. if (m_pNodeHead != NULL)
  714. m_pNodeHead->pPrev = pNewNode;
  715. else
  716. m_pNodeTail = pNewNode;
  717. m_pNodeHead = pNewNode;
  718. }
  719. return (SPLISTPOS) pNewNode;
  720. }
  721. template<class TYPE, class ARG_TYPE>
  722. SPLISTPOS CSPList<TYPE, ARG_TYPE>::AddTail(ARG_TYPE newElement)
  723. {
  724. SPASSERT_VALID( this );
  725. CNode* pNewNode = NewNode(m_pNodeTail, NULL);
  726. if( pNewNode )
  727. {
  728. pNewNode->data = newElement;
  729. if (m_pNodeTail != NULL)
  730. m_pNodeTail->pNext = pNewNode;
  731. else
  732. m_pNodeHead = pNewNode;
  733. m_pNodeTail = pNewNode;
  734. }
  735. return (SPLISTPOS) pNewNode;
  736. }
  737. template<class TYPE, class ARG_TYPE>
  738. void CSPList<TYPE, ARG_TYPE>::AddHead(CSPList* pNewList)
  739. {
  740. SPASSERT_VALID( this );
  741. SPASSERT_VALID( pNewList );
  742. // add a list of same elements to head (maintain order)
  743. SPLISTPOS pos = pNewList->GetTailPosition();
  744. while (pos != NULL)
  745. AddHead(pNewList->GetPrev(pos));
  746. }
  747. template<class TYPE, class ARG_TYPE>
  748. void CSPList<TYPE, ARG_TYPE>::AddTail(CSPList* pNewList)
  749. {
  750. SPASSERT_VALID( this );
  751. SPASSERT_VALID( pNewList );
  752. // add a list of same elements
  753. SPLISTPOS pos = pNewList->GetHeadPosition();
  754. while (pos != NULL)
  755. AddTail(pNewList->GetNext(pos));
  756. }
  757. template<class TYPE, class ARG_TYPE>
  758. TYPE CSPList<TYPE, ARG_TYPE>::RemoveHead()
  759. {
  760. SPASSERT_VALID( this );
  761. SPDBG_ASSERT( m_pNodeHead != NULL ); // don't call on empty list !!!
  762. SPDBG_ASSERT( SPIsValidAddress(m_pNodeHead, sizeof(CNode), TRUE ) );
  763. CNode* pOldNode = m_pNodeHead;
  764. TYPE returnValue = pOldNode->data;
  765. m_pNodeHead = pOldNode->pNext;
  766. if (m_pNodeHead != NULL)
  767. m_pNodeHead->pPrev = NULL;
  768. else
  769. m_pNodeTail = NULL;
  770. FreeNode(pOldNode);
  771. return returnValue;
  772. }
  773. template<class TYPE, class ARG_TYPE>
  774. TYPE CSPList<TYPE, ARG_TYPE>::RemoveTail()
  775. {
  776. SPASSERT_VALID( this );
  777. SPDBG_ASSERT( m_pNodeTail != NULL ); // don't call on empty list !!!
  778. SPDBG_ASSERT( SPIsValidAddress(m_pNodeTail, sizeof(CNode), TRUE ) );
  779. CNode* pOldNode = m_pNodeTail;
  780. TYPE returnValue = pOldNode->data;
  781. m_pNodeTail = pOldNode->pPrev;
  782. if (m_pNodeTail != NULL)
  783. m_pNodeTail->pNext = NULL;
  784. else
  785. m_pNodeHead = NULL;
  786. FreeNode(pOldNode);
  787. return returnValue;
  788. }
  789. template<class TYPE, class ARG_TYPE>
  790. SPLISTPOS CSPList<TYPE, ARG_TYPE>::InsertBefore(SPLISTPOS position, ARG_TYPE newElement)
  791. {
  792. SPASSERT_VALID( this );
  793. if (position == NULL)
  794. return AddHead(newElement); // insert before nothing -> head of the list
  795. // Insert it before position
  796. CNode* pOldNode = (CNode*) position;
  797. CNode* pNewNode = NewNode(pOldNode->pPrev, pOldNode);
  798. if( pNewNode )
  799. {
  800. pNewNode->data = newElement;
  801. if (pOldNode->pPrev != NULL)
  802. {
  803. SPDBG_ASSERT( SPIsValidAddress(pOldNode->pPrev, sizeof(CNode), TRUE ) );
  804. pOldNode->pPrev->pNext = pNewNode;
  805. }
  806. else
  807. {
  808. SPDBG_ASSERT( pOldNode == m_pNodeHead );
  809. m_pNodeHead = pNewNode;
  810. }
  811. pOldNode->pPrev = pNewNode;
  812. }
  813. return (SPLISTPOS) pNewNode;
  814. }
  815. template<class TYPE, class ARG_TYPE>
  816. SPLISTPOS CSPList<TYPE, ARG_TYPE>::InsertAfter(SPLISTPOS position, ARG_TYPE newElement)
  817. {
  818. SPASSERT_VALID( this );
  819. if (position == NULL)
  820. return AddTail(newElement); // insert after nothing -> tail of the list
  821. // Insert it before position
  822. CNode* pOldNode = (CNode*) position;
  823. SPDBG_ASSERT( SPIsValidAddress(pOldNode, sizeof(CNode), TRUE ));
  824. CNode* pNewNode = NewNode(pOldNode, pOldNode->pNext);
  825. if( pNewNode )
  826. {
  827. pNewNode->data = newElement;
  828. if (pOldNode->pNext != NULL)
  829. {
  830. SPDBG_ASSERT( SPIsValidAddress(pOldNode->pNext, sizeof(CNode), TRUE ));
  831. pOldNode->pNext->pPrev = pNewNode;
  832. }
  833. else
  834. {
  835. SPDBG_ASSERT( pOldNode == m_pNodeTail );
  836. m_pNodeTail = pNewNode;
  837. }
  838. pOldNode->pNext = pNewNode;
  839. }
  840. return (SPLISTPOS) pNewNode;
  841. }
  842. template<class TYPE, class ARG_TYPE>
  843. void CSPList<TYPE, ARG_TYPE>::RemoveAt(SPLISTPOS position)
  844. {
  845. SPASSERT_VALID( this );
  846. CNode* pOldNode = (CNode*) position;
  847. SPDBG_ASSERT( SPIsValidAddress(pOldNode, sizeof(CNode), TRUE ) );
  848. // remove pOldNode from list
  849. if (pOldNode == m_pNodeHead)
  850. {
  851. m_pNodeHead = pOldNode->pNext;
  852. }
  853. else
  854. {
  855. SPDBG_ASSERT( SPIsValidAddress(pOldNode->pPrev, sizeof(CNode), TRUE ) );
  856. pOldNode->pPrev->pNext = pOldNode->pNext;
  857. }
  858. if (pOldNode == m_pNodeTail)
  859. {
  860. m_pNodeTail = pOldNode->pPrev;
  861. }
  862. else
  863. {
  864. SPDBG_ASSERT( SPIsValidAddress(pOldNode->pNext, sizeof(CNode), TRUE ) );
  865. pOldNode->pNext->pPrev = pOldNode->pPrev;
  866. }
  867. FreeNode(pOldNode);
  868. }
  869. template<class TYPE, class ARG_TYPE>
  870. SPLISTPOS CSPList<TYPE, ARG_TYPE>::FindIndex(int nIndex) const
  871. {
  872. SPASSERT_VALID( this );
  873. SPDBG_ASSERT( nIndex >= 0 );
  874. if (nIndex >= m_nCount)
  875. return NULL; // went too far
  876. CNode* pNode = m_pNodeHead;
  877. while (nIndex--)
  878. {
  879. SPDBG_ASSERT( SPIsValidAddress(pNode, sizeof(CNode), TRUE ));
  880. pNode = pNode->pNext;
  881. }
  882. return (SPLISTPOS) pNode;
  883. }
  884. template<class TYPE, class ARG_TYPE>
  885. SPLISTPOS CSPList<TYPE, ARG_TYPE>::Find(ARG_TYPE searchValue, SPLISTPOS startAfter) const
  886. {
  887. SPASSERT_VALID( this );
  888. CNode* pNode = (CNode*) startAfter;
  889. if (pNode == NULL)
  890. {
  891. pNode = m_pNodeHead; // start at head
  892. }
  893. else
  894. {
  895. SPDBG_ASSERT( SPIsValidAddress(pNode, sizeof(CNode), TRUE ) );
  896. pNode = pNode->pNext; // start after the one specified
  897. }
  898. for (; pNode != NULL; pNode = pNode->pNext)
  899. if (SPCompareElements(&pNode->data, &searchValue))
  900. return (SPLISTPOS)pNode;
  901. return NULL;
  902. }
  903. #ifdef _DEBUG
  904. template<class TYPE, class ARG_TYPE>
  905. void CSPList<TYPE, ARG_TYPE>::AssertValid() const
  906. {
  907. if (m_nCount == 0)
  908. {
  909. // empty list
  910. SPDBG_ASSERT( m_pNodeHead == NULL );
  911. SPDBG_ASSERT( m_pNodeTail == NULL );
  912. }
  913. else
  914. {
  915. // non-empty list
  916. SPDBG_ASSERT( SPIsValidAddress(m_pNodeHead, sizeof(CNode), TRUE ));
  917. SPDBG_ASSERT( SPIsValidAddress(m_pNodeTail, sizeof(CNode), TRUE ));
  918. }
  919. }
  920. #endif //_DEBUG
  921. /////////////////////////////////////////////////////////////////////////////
  922. // CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>
  923. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  924. class CSPMap
  925. {
  926. protected:
  927. // Association
  928. struct CAssoc
  929. {
  930. CAssoc* pNext;
  931. UINT nHashValue; // needed for efficient iteration
  932. KEY key;
  933. VALUE value;
  934. };
  935. public:
  936. // Construction
  937. CSPMap( int nBlockSize = 10 );
  938. // Attributes
  939. // number of elements
  940. int GetCount() const;
  941. BOOL IsEmpty() const;
  942. // Lookup
  943. BOOL Lookup(ARG_KEY key, VALUE& rValue) const;
  944. // Operations
  945. // Lookup and add if not there
  946. VALUE& operator[](ARG_KEY key);
  947. // add a new (key, value) pair
  948. void SetAt(ARG_KEY key, ARG_VALUE newValue);
  949. // removing existing (key, ?) pair
  950. BOOL RemoveKey(ARG_KEY key);
  951. void RemoveAll();
  952. // iterating all (key, value) pairs
  953. SPLISTPOS GetStartPosition() const;
  954. void GetNextAssoc(SPLISTPOS& rNextPosition, KEY& rKey, VALUE& rValue) const;
  955. // advanced features for derived classes
  956. UINT GetHashTableSize() const;
  957. HRESULT InitHashTable(UINT hashSize, BOOL bAllocNow = TRUE);
  958. // Implementation
  959. protected:
  960. CAssoc** m_pHashTable;
  961. UINT m_nHashTableSize;
  962. int m_nCount;
  963. CAssoc* m_pFreeList;
  964. struct CSPPlex* m_pBlocks;
  965. int m_nBlockSize;
  966. CAssoc* NewAssoc();
  967. void FreeAssoc(CAssoc*);
  968. CAssoc* GetAssocAt(ARG_KEY, UINT&) const;
  969. public:
  970. ~CSPMap();
  971. #ifdef _DEBUG
  972. // void Dump(CDumpContext&) const;
  973. void AssertValid() const;
  974. #endif
  975. };
  976. /////////////////////////////////////////////////////////////////////////////
  977. // CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE> inline functions
  978. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  979. inline int CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetCount() const
  980. { return m_nCount; }
  981. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  982. inline BOOL CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::IsEmpty() const
  983. { return m_nCount == 0; }
  984. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  985. inline void CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::SetAt(ARG_KEY key, ARG_VALUE newValue)
  986. { (*this)[key] = newValue; }
  987. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  988. inline SPLISTPOS CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetStartPosition() const
  989. { return (m_nCount == 0) ? NULL : SP_BEFORE_START_POSITION; }
  990. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  991. inline UINT CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetHashTableSize() const
  992. { return m_nHashTableSize; }
  993. /////////////////////////////////////////////////////////////////////////////
  994. // CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE> out-of-line functions
  995. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  996. CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::CSPMap( int nBlockSize )
  997. {
  998. SPDBG_ASSERT( nBlockSize > 0 );
  999. m_pHashTable = NULL;
  1000. m_nHashTableSize = 17; // default size
  1001. m_nCount = 0;
  1002. m_pFreeList = NULL;
  1003. m_pBlocks = NULL;
  1004. m_nBlockSize = nBlockSize;
  1005. }
  1006. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1007. HRESULT CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::InitHashTable(
  1008. UINT nHashSize, BOOL bAllocNow)
  1009. //
  1010. // Used to force allocation of a hash table or to override the default
  1011. // hash table size of (which is fairly small)
  1012. {
  1013. SPASSERT_VALID( this );
  1014. SPDBG_ASSERT( m_nCount == 0 );
  1015. SPDBG_ASSERT( nHashSize > 0 );
  1016. HRESULT hr = S_OK;
  1017. if (m_pHashTable != NULL)
  1018. {
  1019. // free hash table
  1020. delete[] m_pHashTable;
  1021. m_pHashTable = NULL;
  1022. }
  1023. if (bAllocNow)
  1024. {
  1025. m_pHashTable = new CAssoc* [nHashSize];
  1026. if( m_pHashTable )
  1027. {
  1028. memset(m_pHashTable, 0, sizeof(CAssoc*) * nHashSize);
  1029. }
  1030. else
  1031. {
  1032. hr = E_OUTOFMEMORY;
  1033. }
  1034. }
  1035. m_nHashTableSize = ( SUCCEEDED( hr ) )?(nHashSize):(0);
  1036. return hr;
  1037. }
  1038. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1039. void CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::RemoveAll()
  1040. {
  1041. SPASSERT_VALID( this );
  1042. if (m_pHashTable != NULL)
  1043. {
  1044. // destroy elements (values and keys)
  1045. for (UINT nHash = 0; nHash < m_nHashTableSize; nHash++)
  1046. {
  1047. CAssoc* pAssoc;
  1048. for( pAssoc = m_pHashTable[nHash]; pAssoc != NULL;
  1049. pAssoc = pAssoc->pNext)
  1050. {
  1051. SPDestructElements(&pAssoc->value, 1);
  1052. SPDestructElements(&pAssoc->key, 1);
  1053. }
  1054. }
  1055. }
  1056. // free hash table
  1057. delete[] m_pHashTable;
  1058. m_pHashTable = NULL;
  1059. m_nCount = 0;
  1060. m_pFreeList = NULL;
  1061. m_pBlocks->FreeDataChain();
  1062. m_pBlocks = NULL;
  1063. }
  1064. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1065. CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::~CSPMap()
  1066. {
  1067. RemoveAll();
  1068. SPDBG_ASSERT( m_nCount == 0 );
  1069. }
  1070. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1071. typename CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::CAssoc*
  1072. CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::NewAssoc()
  1073. {
  1074. if (m_pFreeList == NULL)
  1075. {
  1076. // add another block
  1077. CSPPlex* newBlock = CSPPlex::Create(m_pBlocks, m_nBlockSize, sizeof(CSPMap::CAssoc));
  1078. if( newBlock )
  1079. {
  1080. // chain them into free list
  1081. CSPMap::CAssoc* pAssoc = (CSPMap::CAssoc*) newBlock->data();
  1082. // free in reverse order to make it easier to debug
  1083. pAssoc += m_nBlockSize - 1;
  1084. for (int i = m_nBlockSize-1; i >= 0; i--, pAssoc--)
  1085. {
  1086. pAssoc->pNext = m_pFreeList;
  1087. m_pFreeList = pAssoc;
  1088. }
  1089. }
  1090. }
  1091. CSPMap::CAssoc* pAssoc = m_pFreeList;
  1092. if( pAssoc )
  1093. {
  1094. if( SUCCEEDED( SPConstructElements(&pAssoc->key, 1 ) ) )
  1095. {
  1096. if( SUCCEEDED( SPConstructElements(&pAssoc->value, 1 ) ) )
  1097. {
  1098. m_pFreeList = m_pFreeList->pNext;
  1099. m_nCount++;
  1100. SPDBG_ASSERT( m_nCount > 0 ); // make sure we don't overflow
  1101. }
  1102. else
  1103. {
  1104. SPDestructElements( &pAssoc->key, 1 );
  1105. }
  1106. }
  1107. else
  1108. {
  1109. pAssoc = NULL;
  1110. }
  1111. }
  1112. return pAssoc;
  1113. }
  1114. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1115. void CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::FreeAssoc(CAssoc* pAssoc)
  1116. {
  1117. SPDestructElements(&pAssoc->value, 1);
  1118. SPDestructElements(&pAssoc->key, 1);
  1119. pAssoc->pNext = m_pFreeList;
  1120. m_pFreeList = pAssoc;
  1121. m_nCount--;
  1122. SPDBG_ASSERT( m_nCount >= 0 ); // make sure we don't underflow
  1123. }
  1124. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1125. typename CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::CAssoc*
  1126. CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetAssocAt(ARG_KEY key, UINT& nHash) const
  1127. // find association (or return NULL)
  1128. {
  1129. nHash = SPHashKey(key) % m_nHashTableSize;
  1130. if (m_pHashTable == NULL)
  1131. return NULL;
  1132. // see if it exists
  1133. CAssoc* pAssoc;
  1134. for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext)
  1135. {
  1136. if (SPCompareElements(&pAssoc->key, &key))
  1137. return pAssoc;
  1138. }
  1139. return NULL;
  1140. }
  1141. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1142. BOOL CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::Lookup(ARG_KEY key, VALUE& rValue) const
  1143. {
  1144. SPASSERT_VALID( this );
  1145. UINT nHash;
  1146. CAssoc* pAssoc = GetAssocAt(key, nHash);
  1147. if (pAssoc == NULL)
  1148. return FALSE; // not in map
  1149. rValue = pAssoc->value;
  1150. return TRUE;
  1151. }
  1152. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1153. VALUE& CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::operator[](ARG_KEY key)
  1154. {
  1155. SPASSERT_VALID( this );
  1156. HRESULT hr = S_OK;
  1157. static const CAssoc ErrAssoc = 0;
  1158. UINT nHash;
  1159. CAssoc* pAssoc;
  1160. if ((pAssoc = GetAssocAt(key, nHash)) == NULL)
  1161. {
  1162. if( m_pHashTable == NULL )
  1163. {
  1164. hr = InitHashTable(m_nHashTableSize);
  1165. }
  1166. if( SUCCEEDED( hr ) )
  1167. {
  1168. // it doesn't exist, add a new Association
  1169. pAssoc = NewAssoc();
  1170. if( pAssoc )
  1171. {
  1172. pAssoc->nHashValue = nHash;
  1173. pAssoc->key = key;
  1174. // 'pAssoc->value' is a constructed object, nothing more
  1175. // put into hash table
  1176. pAssoc->pNext = m_pHashTable[nHash];
  1177. m_pHashTable[nHash] = pAssoc;
  1178. }
  1179. else
  1180. {
  1181. pAssoc = &ErrAssoc;
  1182. }
  1183. }
  1184. }
  1185. return pAssoc->value; // return new reference
  1186. }
  1187. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1188. BOOL CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::RemoveKey(ARG_KEY key)
  1189. // remove key - return TRUE if removed
  1190. {
  1191. SPASSERT_VALID( this );
  1192. if (m_pHashTable == NULL)
  1193. return FALSE; // nothing in the table
  1194. CAssoc** ppAssocPrev;
  1195. ppAssocPrev = &m_pHashTable[SPHashKey(key) % m_nHashTableSize];
  1196. CAssoc* pAssoc;
  1197. for (pAssoc = *ppAssocPrev; pAssoc != NULL; pAssoc = pAssoc->pNext)
  1198. {
  1199. if (SPCompareElements(&pAssoc->key, &key))
  1200. {
  1201. // remove it
  1202. *ppAssocPrev = pAssoc->pNext; // remove from list
  1203. FreeAssoc(pAssoc);
  1204. return TRUE;
  1205. }
  1206. ppAssocPrev = &pAssoc->pNext;
  1207. }
  1208. return FALSE; // not found
  1209. }
  1210. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1211. void CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetNextAssoc(SPLISTPOS& rNextPosition,
  1212. KEY& rKey, VALUE& rValue) const
  1213. {
  1214. SPASSERT_VALID( this );
  1215. SPDBG_ASSERT( m_pHashTable != NULL ); // never call on empty map
  1216. CAssoc* pAssocRet = (CAssoc*)rNextPosition;
  1217. SPDBG_ASSERT( pAssocRet != NULL );
  1218. if (pAssocRet == (CAssoc*) SP_BEFORE_START_POSITION)
  1219. {
  1220. // find the first association
  1221. for (UINT nBucket = 0; nBucket < m_nHashTableSize; nBucket++)
  1222. if ((pAssocRet = m_pHashTable[nBucket]) != NULL)
  1223. break;
  1224. SPDBG_ASSERT( pAssocRet != NULL ); // must find something
  1225. }
  1226. // find next association
  1227. SPDBG_ASSERT( SPIsValidAddress(pAssocRet, sizeof(CAssoc), TRUE ));
  1228. CAssoc* pAssocNext;
  1229. if ((pAssocNext = pAssocRet->pNext) == NULL)
  1230. {
  1231. // go to next bucket
  1232. for (UINT nBucket = pAssocRet->nHashValue + 1;
  1233. nBucket < m_nHashTableSize; nBucket++)
  1234. if ((pAssocNext = m_pHashTable[nBucket]) != NULL)
  1235. break;
  1236. }
  1237. rNextPosition = (SPLISTPOS) pAssocNext;
  1238. // fill in return data
  1239. rKey = pAssocRet->key;
  1240. rValue = pAssocRet->value;
  1241. }
  1242. #ifdef _DEBUG
  1243. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  1244. void CSPMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::AssertValid() const
  1245. {
  1246. SPDBG_ASSERT( m_nHashTableSize > 0 );
  1247. SPDBG_ASSERT( (m_nCount == 0 || m_pHashTable != NULL) );
  1248. // non-empty map should have hash table
  1249. }
  1250. #endif //_DEBUG
  1251. #endif //--- This must be the last line in the file