list_s.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 parameterized List
  13. //
  14. /////////////////////////////////////////////////////////////////////////////
  15. #include "stdafx.h"
  16. #ifdef AFX_COLL_SEG
  17. #pragma code_seg(AFX_COLL_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. CStringList::CStringList(int nBlockSize)
  27. {
  28. ASSERT(nBlockSize > 0);
  29. m_nCount = 0;
  30. m_pNodeHead = m_pNodeTail = m_pNodeFree = NULL;
  31. m_pBlocks = NULL;
  32. m_nBlockSize = nBlockSize;
  33. }
  34. void CStringList::RemoveAll()
  35. {
  36. ASSERT_VALID(this);
  37. // destroy elements
  38. CNode* pNode;
  39. for (pNode = m_pNodeHead; pNode != NULL; pNode = pNode->pNext)
  40. DestructElement(&pNode->data);
  41. m_nCount = 0;
  42. m_pNodeHead = m_pNodeTail = m_pNodeFree = NULL;
  43. m_pBlocks->FreeDataChain();
  44. m_pBlocks = NULL;
  45. }
  46. CStringList::~CStringList()
  47. {
  48. RemoveAll();
  49. ASSERT(m_nCount == 0);
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // Node helpers
  53. /*
  54. * Implementation note: CNode's are stored in CPlex blocks and
  55. * chained together. Free blocks are maintained in a singly linked list
  56. * using the 'pNext' member of CNode with 'm_pNodeFree' as the head.
  57. * Used blocks are maintained in a doubly linked list using both 'pNext'
  58. * and 'pPrev' as links and 'm_pNodeHead' and 'm_pNodeTail'
  59. * as the head/tail.
  60. *
  61. * We never free a CPlex block unless the List is destroyed or RemoveAll()
  62. * is used - so the total number of CPlex blocks may grow large depending
  63. * on the maximum past size of the list.
  64. */
  65. CStringList::CNode*
  66. CStringList::NewNode(CStringList::CNode* pPrev, CStringList::CNode* pNext)
  67. {
  68. if (m_pNodeFree == NULL)
  69. {
  70. // add another block
  71. CPlex* pNewBlock = CPlex::Create(m_pBlocks, m_nBlockSize,
  72. sizeof(CNode));
  73. // chain them into free list
  74. CNode* pNode = (CNode*) pNewBlock->data();
  75. // free in reverse order to make it easier to debug
  76. pNode += m_nBlockSize - 1;
  77. for (int i = m_nBlockSize-1; i >= 0; i--, pNode--)
  78. {
  79. pNode->pNext = m_pNodeFree;
  80. m_pNodeFree = pNode;
  81. }
  82. }
  83. ASSERT(m_pNodeFree != NULL); // we must have something
  84. CStringList::CNode* pNode = m_pNodeFree;
  85. m_pNodeFree = m_pNodeFree->pNext;
  86. pNode->pPrev = pPrev;
  87. pNode->pNext = pNext;
  88. m_nCount++;
  89. ASSERT(m_nCount > 0); // make sure we don't overflow
  90. ConstructElement(&pNode->data);
  91. return pNode;
  92. }
  93. void CStringList::FreeNode(CStringList::CNode* pNode)
  94. {
  95. DestructElement(&pNode->data);
  96. pNode->pNext = m_pNodeFree;
  97. m_pNodeFree = pNode;
  98. m_nCount--;
  99. ASSERT(m_nCount >= 0); // make sure we don't underflow
  100. // if no more elements, cleanup completely
  101. if (m_nCount == 0)
  102. RemoveAll();
  103. }
  104. /////////////////////////////////////////////////////////////////////////////
  105. POSITION CStringList::AddHead(LPCTSTR newElement)
  106. {
  107. return AddHead(CString(newElement));
  108. }
  109. POSITION CStringList::AddHead(const CString& newElement)
  110. {
  111. ASSERT_VALID(this);
  112. CNode* pNewNode = NewNode(NULL, m_pNodeHead);
  113. pNewNode->data = newElement;
  114. if (m_pNodeHead != NULL)
  115. m_pNodeHead->pPrev = pNewNode;
  116. else
  117. m_pNodeTail = pNewNode;
  118. m_pNodeHead = pNewNode;
  119. return (POSITION) pNewNode;
  120. }
  121. POSITION CStringList::AddTail(LPCTSTR newElement)
  122. {
  123. return AddTail(CString(newElement));
  124. }
  125. POSITION CStringList::AddTail(const CString& newElement)
  126. {
  127. ASSERT_VALID(this);
  128. CNode* pNewNode = NewNode(m_pNodeTail, NULL);
  129. pNewNode->data = newElement;
  130. if (m_pNodeTail != NULL)
  131. m_pNodeTail->pNext = pNewNode;
  132. else
  133. m_pNodeHead = pNewNode;
  134. m_pNodeTail = pNewNode;
  135. return (POSITION) pNewNode;
  136. }
  137. void CStringList::AddHead(CStringList* pNewList)
  138. {
  139. ASSERT_VALID(this);
  140. ASSERT(pNewList != NULL);
  141. ASSERT_KINDOF(CStringList, pNewList);
  142. ASSERT_VALID(pNewList);
  143. // add a list of same elements to head (maintain order)
  144. POSITION pos = pNewList->GetTailPosition();
  145. while (pos != NULL)
  146. AddHead(pNewList->GetPrev(pos));
  147. }
  148. void CStringList::AddTail(CStringList* pNewList)
  149. {
  150. ASSERT_VALID(this);
  151. ASSERT(pNewList != NULL);
  152. ASSERT_KINDOF(CStringList, pNewList);
  153. ASSERT_VALID(pNewList);
  154. // add a list of same elements
  155. POSITION pos = pNewList->GetHeadPosition();
  156. while (pos != NULL)
  157. AddTail(pNewList->GetNext(pos));
  158. }
  159. CString CStringList::RemoveHead()
  160. {
  161. ASSERT_VALID(this);
  162. ASSERT(m_pNodeHead != NULL); // don't call on empty list !!!
  163. ASSERT(AfxIsValidAddress(m_pNodeHead, sizeof(CNode)));
  164. CNode* pOldNode = m_pNodeHead;
  165. CString returnValue = pOldNode->data;
  166. m_pNodeHead = pOldNode->pNext;
  167. if (m_pNodeHead != NULL)
  168. m_pNodeHead->pPrev = NULL;
  169. else
  170. m_pNodeTail = NULL;
  171. FreeNode(pOldNode);
  172. return returnValue;
  173. }
  174. CString CStringList::RemoveTail()
  175. {
  176. ASSERT_VALID(this);
  177. ASSERT(m_pNodeTail != NULL); // don't call on empty list !!!
  178. ASSERT(AfxIsValidAddress(m_pNodeTail, sizeof(CNode)));
  179. CNode* pOldNode = m_pNodeTail;
  180. CString returnValue = pOldNode->data;
  181. m_pNodeTail = pOldNode->pPrev;
  182. if (m_pNodeTail != NULL)
  183. m_pNodeTail->pNext = NULL;
  184. else
  185. m_pNodeHead = NULL;
  186. FreeNode(pOldNode);
  187. return returnValue;
  188. }
  189. POSITION CStringList::InsertBefore(POSITION position, LPCTSTR newElement)
  190. {
  191. return InsertBefore(position, CString(newElement));
  192. }
  193. POSITION CStringList::InsertBefore(POSITION position, const CString& newElement)
  194. {
  195. ASSERT_VALID(this);
  196. if (position == NULL)
  197. return AddHead(newElement); // insert before nothing -> head of the list
  198. // Insert it before position
  199. CNode* pOldNode = (CNode*) position;
  200. CNode* pNewNode = NewNode(pOldNode->pPrev, pOldNode);
  201. pNewNode->data = newElement;
  202. if (pOldNode->pPrev != NULL)
  203. {
  204. ASSERT(AfxIsValidAddress(pOldNode->pPrev, sizeof(CNode)));
  205. pOldNode->pPrev->pNext = pNewNode;
  206. }
  207. else
  208. {
  209. ASSERT(pOldNode == m_pNodeHead);
  210. m_pNodeHead = pNewNode;
  211. }
  212. pOldNode->pPrev = pNewNode;
  213. return (POSITION) pNewNode;
  214. }
  215. POSITION CStringList::InsertAfter(POSITION position, LPCTSTR newElement)
  216. {
  217. return InsertAfter(position, CString(newElement));
  218. }
  219. POSITION CStringList::InsertAfter(POSITION position, const CString& newElement)
  220. {
  221. ASSERT_VALID(this);
  222. if (position == NULL)
  223. return AddTail(newElement); // insert after nothing -> tail of the list
  224. // Insert it before position
  225. CNode* pOldNode = (CNode*) position;
  226. ASSERT(AfxIsValidAddress(pOldNode, sizeof(CNode)));
  227. CNode* pNewNode = NewNode(pOldNode, pOldNode->pNext);
  228. pNewNode->data = newElement;
  229. if (pOldNode->pNext != NULL)
  230. {
  231. ASSERT(AfxIsValidAddress(pOldNode->pNext, sizeof(CNode)));
  232. pOldNode->pNext->pPrev = pNewNode;
  233. }
  234. else
  235. {
  236. ASSERT(pOldNode == m_pNodeTail);
  237. m_pNodeTail = pNewNode;
  238. }
  239. pOldNode->pNext = pNewNode;
  240. return (POSITION) pNewNode;
  241. }
  242. void CStringList::RemoveAt(POSITION position)
  243. {
  244. ASSERT_VALID(this);
  245. CNode* pOldNode = (CNode*) position;
  246. ASSERT(AfxIsValidAddress(pOldNode, sizeof(CNode)));
  247. // remove pOldNode from list
  248. if (pOldNode == m_pNodeHead)
  249. {
  250. m_pNodeHead = pOldNode->pNext;
  251. }
  252. else
  253. {
  254. ASSERT(AfxIsValidAddress(pOldNode->pPrev, sizeof(CNode)));
  255. pOldNode->pPrev->pNext = pOldNode->pNext;
  256. }
  257. if (pOldNode == m_pNodeTail)
  258. {
  259. m_pNodeTail = pOldNode->pPrev;
  260. }
  261. else
  262. {
  263. ASSERT(AfxIsValidAddress(pOldNode->pNext, sizeof(CNode)));
  264. pOldNode->pNext->pPrev = pOldNode->pPrev;
  265. }
  266. FreeNode(pOldNode);
  267. }
  268. /////////////////////////////////////////////////////////////////////////////
  269. // slow operations
  270. POSITION CStringList::FindIndex(int nIndex) const
  271. {
  272. ASSERT_VALID(this);
  273. if (nIndex >= m_nCount || nIndex < 0)
  274. return NULL; // went too far
  275. CNode* pNode = m_pNodeHead;
  276. while (nIndex--)
  277. {
  278. ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
  279. pNode = pNode->pNext;
  280. }
  281. return (POSITION) pNode;
  282. }
  283. POSITION CStringList::Find(LPCTSTR searchValue, POSITION startAfter) const
  284. {
  285. ASSERT_VALID(this);
  286. CNode* pNode = (CNode*) startAfter;
  287. if (pNode == NULL)
  288. {
  289. pNode = m_pNodeHead; // start at head
  290. }
  291. else
  292. {
  293. ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
  294. pNode = pNode->pNext; // start after the one specified
  295. }
  296. for (; pNode != NULL; pNode = pNode->pNext)
  297. if (pNode->data == searchValue)
  298. return (POSITION) pNode;
  299. return NULL;
  300. }
  301. /////////////////////////////////////////////////////////////////////////////
  302. // Serialization
  303. void CStringList::Serialize(CArchive& ar)
  304. {
  305. ASSERT_VALID(this);
  306. CObject::Serialize(ar);
  307. if (ar.IsStoring())
  308. {
  309. ar.WriteCount(m_nCount);
  310. for (CNode* pNode = m_pNodeHead; pNode != NULL; pNode = pNode->pNext)
  311. {
  312. ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
  313. ar << pNode->data;
  314. }
  315. }
  316. else
  317. {
  318. DWORD nNewCount = ar.ReadCount();
  319. CString newData;
  320. while (nNewCount--)
  321. {
  322. ar >> newData;
  323. AddTail(newData);
  324. }
  325. }
  326. }
  327. /////////////////////////////////////////////////////////////////////////////
  328. // Diagnostics
  329. #ifdef _DEBUG
  330. void CStringList::Dump(CDumpContext& dc) const
  331. {
  332. CObject::Dump(dc);
  333. dc << "with " << m_nCount << " elements";
  334. if (dc.GetDepth() > 0)
  335. {
  336. POSITION pos = GetHeadPosition();
  337. while (pos != NULL)
  338. dc << "\n\t" << GetNext(pos);
  339. }
  340. dc << "\n";
  341. }
  342. void CStringList::AssertValid() const
  343. {
  344. CObject::AssertValid();
  345. if (m_nCount == 0)
  346. {
  347. // empty list
  348. ASSERT(m_pNodeHead == NULL);
  349. ASSERT(m_pNodeTail == NULL);
  350. }
  351. else
  352. {
  353. // non-empty list
  354. ASSERT(AfxIsValidAddress(m_pNodeHead, sizeof(CNode)));
  355. ASSERT(AfxIsValidAddress(m_pNodeTail, sizeof(CNode)));
  356. }
  357. }
  358. #endif //_DEBUG
  359. #ifdef AFX_INIT_SEG
  360. #pragma code_seg(AFX_INIT_SEG)
  361. #endif
  362. IMPLEMENT_SERIAL(CStringList, CObject, 0)
  363. /////////////////////////////////////////////////////////////////////////////