wxlist.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. //------------------------------------------------------------------------------
  2. // File: WXList.cpp
  3. //
  4. // Desc: DirectShow base classes - implements a non-MFC based generic list
  5. // template class.
  6. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8. /* A generic list of pointers to objects.
  9. Objectives: avoid using MFC libraries in ndm kernel mode and
  10. provide a really useful list type.
  11. The class is thread safe in that separate threads may add and
  12. delete items in the list concurrently although the application
  13. must ensure that constructor and destructor access is suitably
  14. synchronised.
  15. The list name must not conflict with MFC classes as an
  16. application may use both
  17. The nodes form a doubly linked, NULL terminated chain with an anchor
  18. block (the list object per se) holding pointers to the first and last
  19. nodes and a count of the nodes.
  20. There is a node cache to reduce the allocation and freeing overhead.
  21. It optionally (determined at construction time) has an Event which is
  22. set whenever the list becomes non-empty and reset whenever it becomes
  23. empty.
  24. It optionally (determined at construction time) has a Critical Section
  25. which is entered during the important part of each operation. (About
  26. all you can do outside it is some parameter checking).
  27. The node cache is a repository of nodes that are NOT in the list to speed
  28. up storage allocation. Each list has its own cache to reduce locking and
  29. serialising. The list accesses are serialised anyway for a given list - a
  30. common cache would mean that we would have to separately serialise access
  31. of all lists within the cache. Because the cache only stores nodes that are
  32. not in the list, releasing the cache does not release any list nodes. This
  33. means that list nodes can be copied or rechained from one list to another
  34. without danger of creating a dangling reference if the original cache goes
  35. away.
  36. Questionable design decisions:
  37. 1. Retaining the warts for compatibility
  38. 2. Keeping an element count -i.e. counting whenever we do anything
  39. instead of only when we want the count.
  40. 3. Making the chain pointers NULL terminated. If the list object
  41. itself looks just like a node and the list is kept as a ring then
  42. it reduces the number of special cases. All inserts look the same.
  43. */
  44. #include <streams.h>
  45. /* set cursor to the position of each element of list in turn */
  46. #define INTERNALTRAVERSELIST(list, cursor) \
  47. for ( cursor = (list).GetHeadPositionI() \
  48. ; cursor!=NULL \
  49. ; cursor = (list).Next(cursor) \
  50. )
  51. /* set cursor to the position of each element of list in turn
  52. in reverse order
  53. */
  54. #define INTERNALREVERSETRAVERSELIST(list, cursor) \
  55. for ( cursor = (list).GetTailPositionI() \
  56. ; cursor!=NULL \
  57. ; cursor = (list).Prev(cursor) \
  58. )
  59. /* Constructor calls a separate initialisation function that
  60. creates a node cache, optionally creates a lock object
  61. and optionally creates a signaling object.
  62. By default we create a locking object, a DEFAULTCACHE sized
  63. cache but no event object so the list cannot be used in calls
  64. to WaitForSingleObject
  65. */
  66. CBaseList::CBaseList(__in_opt LPCTSTR pName, // Descriptive list name
  67. INT iItems) : // Node cache size
  68. #ifdef DEBUG
  69. CBaseObject(pName),
  70. #endif
  71. m_pFirst(NULL),
  72. m_pLast(NULL),
  73. m_Count(0),
  74. m_Cache(iItems)
  75. {
  76. } // constructor
  77. CBaseList::CBaseList(__in_opt LPCTSTR pName) : // Descriptive list name
  78. #ifdef DEBUG
  79. CBaseObject(pName),
  80. #endif
  81. m_pFirst(NULL),
  82. m_pLast(NULL),
  83. m_Count(0),
  84. m_Cache(DEFAULTCACHE)
  85. {
  86. } // constructor
  87. #ifdef UNICODE
  88. CBaseList::CBaseList(__in_opt LPCSTR pName, // Descriptive list name
  89. INT iItems) : // Node cache size
  90. #ifdef DEBUG
  91. CBaseObject(pName),
  92. #endif
  93. m_pFirst(NULL),
  94. m_pLast(NULL),
  95. m_Count(0),
  96. m_Cache(iItems)
  97. {
  98. } // constructor
  99. CBaseList::CBaseList(__in_opt LPCSTR pName) : // Descriptive list name
  100. #ifdef DEBUG
  101. CBaseObject(pName),
  102. #endif
  103. m_pFirst(NULL),
  104. m_pLast(NULL),
  105. m_Count(0),
  106. m_Cache(DEFAULTCACHE)
  107. {
  108. } // constructor
  109. #endif
  110. /* The destructor enumerates all the node objects in the list and
  111. in the cache deleting each in turn. We do not do any processing
  112. on the objects that the list holds (i.e. points to) so if they
  113. represent interfaces for example the creator of the list should
  114. ensure that each of them is released before deleting us
  115. */
  116. CBaseList::~CBaseList()
  117. {
  118. /* Delete all our list nodes */
  119. RemoveAll();
  120. } // destructor
  121. /* Remove all the nodes from the list but don't do anything
  122. with the objects that each node looks after (this is the
  123. responsibility of the creator).
  124. Aa a last act we reset the signalling event
  125. (if available) to indicate to clients that the list
  126. does not have any entries in it.
  127. */
  128. void CBaseList::RemoveAll()
  129. {
  130. /* Free up all the CNode objects NOTE we don't bother putting the
  131. deleted nodes into the cache as this method is only really called
  132. in serious times of change such as when we are being deleted at
  133. which point the cache will be deleted anway */
  134. CNode *pn = m_pFirst;
  135. while (pn) {
  136. CNode *op = pn;
  137. pn = pn->Next();
  138. delete op;
  139. }
  140. /* Reset the object count and the list pointers */
  141. m_Count = 0;
  142. m_pFirst = m_pLast = NULL;
  143. } // RemoveAll
  144. /* Return a position enumerator for the entire list.
  145. A position enumerator is a pointer to a node object cast to a
  146. transparent type so all we do is return the _head/_tail node
  147. pointer in the list.
  148. WARNING because the position is a pointer to a node there is
  149. an implicit assumption for users a the list class that after
  150. deleting an object from the list that any other position
  151. enumerators that you have may be invalid (since the node
  152. may be gone).
  153. */
  154. __out_opt POSITION CBaseList::GetHeadPositionI() const
  155. {
  156. return (POSITION) m_pFirst;
  157. } // GetHeadPosition
  158. __out_opt POSITION CBaseList::GetTailPositionI() const
  159. {
  160. return (POSITION) m_pLast;
  161. } // GetTailPosition
  162. /* Get the number of objects in the list,
  163. Get the lock before accessing the count.
  164. Locking may not be entirely necessary but it has the side effect
  165. of making sure that all operations are complete before we get it.
  166. So for example if a list is being added to this list then that
  167. will have completed in full before we continue rather than seeing
  168. an intermediate albeit valid state
  169. */
  170. int CBaseList::GetCountI() const
  171. {
  172. return m_Count;
  173. } // GetCount
  174. /* Return the object at rp, update rp to the next object from
  175. the list or NULL if you have moved over the last object.
  176. You may still call this function once we return NULL but
  177. we will continue to return a NULL position value
  178. */
  179. __out void *CBaseList::GetNextI(__inout POSITION& rp) const
  180. {
  181. /* have we reached the end of the list */
  182. if (rp == NULL) {
  183. return NULL;
  184. }
  185. /* Lock the object before continuing */
  186. void *pObject;
  187. /* Copy the original position then step on */
  188. CNode *pn = (CNode *) rp;
  189. ASSERT(pn != NULL);
  190. rp = (POSITION) pn->Next();
  191. /* Get the object at the original position from the list */
  192. pObject = pn->GetData();
  193. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  194. return pObject;
  195. } //GetNext
  196. /* Return the object at p.
  197. Asking for the object at NULL ASSERTs then returns NULL
  198. The object is NOT locked. The list is not being changed
  199. in any way. If another thread is busy deleting the object
  200. then locking would only result in a change from one bad
  201. behaviour to another.
  202. */
  203. __out_opt void *CBaseList::GetI(__in_opt POSITION p) const
  204. {
  205. if (p == NULL) {
  206. return NULL;
  207. }
  208. CNode * pn = (CNode *) p;
  209. void *pObject = pn->GetData();
  210. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  211. return pObject;
  212. } //Get
  213. __out void *CBaseList::GetValidI(__in POSITION p) const
  214. {
  215. CNode * pn = (CNode *) p;
  216. void *pObject = pn->GetData();
  217. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  218. return pObject;
  219. } //Get
  220. /* Return the first position in the list which holds the given pointer.
  221. Return NULL if it's not found.
  222. */
  223. __out_opt POSITION CBaseList::FindI( __in void * pObj) const
  224. {
  225. POSITION pn;
  226. INTERNALTRAVERSELIST(*this, pn){
  227. if (GetI(pn)==pObj) {
  228. return pn;
  229. }
  230. }
  231. return NULL;
  232. } // Find
  233. /* Remove the first node in the list (deletes the pointer to its object
  234. from the list, does not free the object itself).
  235. Return the pointer to its object or NULL if empty
  236. */
  237. __out_opt void *CBaseList::RemoveHeadI()
  238. {
  239. /* All we do is get the _head position and ask for that to be deleted.
  240. We could special case this since some of the code path checking
  241. in Remove() is redundant as we know there is no previous
  242. node for example but it seems to gain little over the
  243. added complexity
  244. */
  245. return RemoveI((POSITION)m_pFirst);
  246. } // RemoveHead
  247. /* Remove the last node in the list (deletes the pointer to its object
  248. from the list, does not free the object itself).
  249. Return the pointer to its object or NULL if empty
  250. */
  251. __out_opt void *CBaseList::RemoveTailI()
  252. {
  253. /* All we do is get the _tail position and ask for that to be deleted.
  254. We could special case this since some of the code path checking
  255. in Remove() is redundant as we know there is no previous
  256. node for example but it seems to gain little over the
  257. added complexity
  258. */
  259. return RemoveI((POSITION)m_pLast);
  260. } // RemoveTail
  261. /* Remove the pointer to the object in this position from the list.
  262. Deal with all the chain pointers
  263. Return a pointer to the object removed from the list.
  264. The node object that is freed as a result
  265. of this operation is added to the node cache where
  266. it can be used again.
  267. Remove(NULL) is a harmless no-op - but probably is a wart.
  268. */
  269. __out_opt void *CBaseList::RemoveI(__in_opt POSITION pos)
  270. {
  271. /* Lock the critical section before continuing */
  272. // ASSERT (pos!=NULL); // Removing NULL is to be harmless!
  273. if (pos==NULL) return NULL;
  274. CNode *pCurrent = (CNode *) pos;
  275. ASSERT(pCurrent != NULL);
  276. /* Update the previous node */
  277. CNode *pNode = pCurrent->Prev();
  278. if (pNode == NULL) {
  279. m_pFirst = pCurrent->Next();
  280. } else {
  281. pNode->SetNext(pCurrent->Next());
  282. }
  283. /* Update the following node */
  284. pNode = pCurrent->Next();
  285. if (pNode == NULL) {
  286. m_pLast = pCurrent->Prev();
  287. } else {
  288. pNode->SetPrev(pCurrent->Prev());
  289. }
  290. /* Get the object this node was looking after */
  291. void *pObject = pCurrent->GetData();
  292. // ASSERT(pObject != NULL); // NULL pointers in the list are allowed.
  293. /* Try and add the node object to the cache -
  294. a NULL return code from the cache means we ran out of room.
  295. The cache size is fixed by a constructor argument when the
  296. list is created and defaults to DEFAULTCACHE.
  297. This means that the cache will have room for this many
  298. node objects. So if you have a list of media samples
  299. and you know there will never be more than five active at
  300. any given time of them for example then override the default
  301. constructor
  302. */
  303. m_Cache.AddToCache(pCurrent);
  304. /* If the list is empty then reset the list event */
  305. --m_Count;
  306. ASSERT(m_Count >= 0);
  307. return pObject;
  308. } // Remove
  309. /* Add this object to the _tail end of our list
  310. Return the new _tail position.
  311. */
  312. __out_opt POSITION CBaseList::AddTailI(__in void *pObject)
  313. {
  314. /* Lock the critical section before continuing */
  315. CNode *pNode;
  316. // ASSERT(pObject); // NULL pointers in the list are allowed.
  317. /* If there is a node objects in the cache then use
  318. that otherwise we will have to create a new one */
  319. pNode = (CNode *) m_Cache.RemoveFromCache();
  320. if (pNode == NULL) {
  321. pNode = new CNode;
  322. }
  323. /* Check we have a valid object */
  324. if (pNode == NULL) {
  325. return NULL;
  326. }
  327. /* Initialise all the CNode object
  328. just in case it came from the cache
  329. */
  330. pNode->SetData(pObject);
  331. pNode->SetNext(NULL);
  332. pNode->SetPrev(m_pLast);
  333. if (m_pLast == NULL) {
  334. m_pFirst = pNode;
  335. } else {
  336. m_pLast->SetNext(pNode);
  337. }
  338. /* Set the new last node pointer and also increment the number
  339. of list entries, the critical section is unlocked when we
  340. exit the function
  341. */
  342. m_pLast = pNode;
  343. ++m_Count;
  344. return (POSITION) pNode;
  345. } // AddTail(object)
  346. /* Add this object to the _head end of our list
  347. Return the new _head position.
  348. */
  349. __out_opt POSITION CBaseList::AddHeadI(__in void *pObject)
  350. {
  351. CNode *pNode;
  352. // ASSERT(pObject); // NULL pointers in the list are allowed.
  353. /* If there is a node objects in the cache then use
  354. that otherwise we will have to create a new one */
  355. pNode = (CNode *) m_Cache.RemoveFromCache();
  356. if (pNode == NULL) {
  357. pNode = new CNode;
  358. }
  359. /* Check we have a valid object */
  360. if (pNode == NULL) {
  361. return NULL;
  362. }
  363. /* Initialise all the CNode object
  364. just in case it came from the cache
  365. */
  366. pNode->SetData(pObject);
  367. /* chain it in (set four pointers) */
  368. pNode->SetPrev(NULL);
  369. pNode->SetNext(m_pFirst);
  370. if (m_pFirst == NULL) {
  371. m_pLast = pNode;
  372. } else {
  373. m_pFirst->SetPrev(pNode);
  374. }
  375. m_pFirst = pNode;
  376. ++m_Count;
  377. return (POSITION) pNode;
  378. } // AddHead(object)
  379. /* Add all the elements in *pList to the _tail of this list.
  380. Return TRUE if it all worked, FALSE if it didn't.
  381. If it fails some elements may have been added.
  382. */
  383. BOOL CBaseList::AddTail(__in CBaseList *pList)
  384. {
  385. /* lock the object before starting then enumerate
  386. each entry in the source list and add them one by one to
  387. our list (while still holding the object lock)
  388. Lock the other list too.
  389. */
  390. POSITION pos = pList->GetHeadPositionI();
  391. while (pos) {
  392. if (NULL == AddTailI(pList->GetNextI(pos))) {
  393. return FALSE;
  394. }
  395. }
  396. return TRUE;
  397. } // AddTail(list)
  398. /* Add all the elements in *pList to the _head of this list.
  399. Return TRUE if it all worked, FALSE if it didn't.
  400. If it fails some elements may have been added.
  401. */
  402. BOOL CBaseList::AddHead(__in CBaseList *pList)
  403. {
  404. /* lock the object before starting then enumerate
  405. each entry in the source list and add them one by one to
  406. our list (while still holding the object lock)
  407. Lock the other list too.
  408. To avoid reversing the list, traverse it backwards.
  409. */
  410. POSITION pos;
  411. INTERNALREVERSETRAVERSELIST(*pList, pos) {
  412. if (NULL== AddHeadI(pList->GetValidI(pos))){
  413. return FALSE;
  414. }
  415. }
  416. return TRUE;
  417. } // AddHead(list)
  418. /* Add the object after position p
  419. p is still valid after the operation.
  420. AddAfter(NULL,x) adds x to the start - same as AddHead
  421. Return the position of the new object, NULL if it failed
  422. */
  423. __out_opt POSITION CBaseList::AddAfterI(__in_opt POSITION pos, __in void * pObj)
  424. {
  425. if (pos==NULL)
  426. return AddHeadI(pObj);
  427. /* As someone else might be furkling with the list -
  428. Lock the critical section before continuing
  429. */
  430. CNode *pAfter = (CNode *) pos;
  431. ASSERT(pAfter != NULL);
  432. if (pAfter==m_pLast)
  433. return AddTailI(pObj);
  434. /* set pnode to point to a new node, preferably from the cache */
  435. CNode *pNode = (CNode *) m_Cache.RemoveFromCache();
  436. if (pNode == NULL) {
  437. pNode = new CNode;
  438. }
  439. /* Check we have a valid object */
  440. if (pNode == NULL) {
  441. return NULL;
  442. }
  443. /* Initialise all the CNode object
  444. just in case it came from the cache
  445. */
  446. pNode->SetData(pObj);
  447. /* It is to be added to the middle of the list - there is a before
  448. and after node. Chain it after pAfter, before pBefore.
  449. */
  450. CNode * pBefore = pAfter->Next();
  451. ASSERT(pBefore != NULL);
  452. /* chain it in (set four pointers) */
  453. pNode->SetPrev(pAfter);
  454. pNode->SetNext(pBefore);
  455. pBefore->SetPrev(pNode);
  456. pAfter->SetNext(pNode);
  457. ++m_Count;
  458. return (POSITION) pNode;
  459. } // AddAfter(object)
  460. BOOL CBaseList::AddAfter(__in_opt POSITION p, __in CBaseList *pList)
  461. {
  462. POSITION pos;
  463. INTERNALTRAVERSELIST(*pList, pos) {
  464. /* p follows along the elements being added */
  465. p = AddAfterI(p, pList->GetValidI(pos));
  466. if (p==NULL) return FALSE;
  467. }
  468. return TRUE;
  469. } // AddAfter(list)
  470. /* Mirror images:
  471. Add the element or list after position p.
  472. p is still valid after the operation.
  473. AddBefore(NULL,x) adds x to the end - same as AddTail
  474. */
  475. __out_opt POSITION CBaseList::AddBeforeI(__in_opt POSITION pos, __in void * pObj)
  476. {
  477. if (pos==NULL)
  478. return AddTailI(pObj);
  479. /* set pnode to point to a new node, preferably from the cache */
  480. CNode *pBefore = (CNode *) pos;
  481. ASSERT(pBefore != NULL);
  482. if (pBefore==m_pFirst)
  483. return AddHeadI(pObj);
  484. CNode * pNode = (CNode *) m_Cache.RemoveFromCache();
  485. if (pNode == NULL) {
  486. pNode = new CNode;
  487. }
  488. /* Check we have a valid object */
  489. if (pNode == NULL) {
  490. return NULL;
  491. }
  492. /* Initialise all the CNode object
  493. just in case it came from the cache
  494. */
  495. pNode->SetData(pObj);
  496. /* It is to be added to the middle of the list - there is a before
  497. and after node. Chain it after pAfter, before pBefore.
  498. */
  499. CNode * pAfter = pBefore->Prev();
  500. ASSERT(pAfter != NULL);
  501. /* chain it in (set four pointers) */
  502. pNode->SetPrev(pAfter);
  503. pNode->SetNext(pBefore);
  504. pBefore->SetPrev(pNode);
  505. pAfter->SetNext(pNode);
  506. ++m_Count;
  507. return (POSITION) pNode;
  508. } // Addbefore(object)
  509. BOOL CBaseList::AddBefore(__in_opt POSITION p, __in CBaseList *pList)
  510. {
  511. POSITION pos;
  512. INTERNALREVERSETRAVERSELIST(*pList, pos) {
  513. /* p follows along the elements being added */
  514. p = AddBeforeI(p, pList->GetValidI(pos));
  515. if (p==NULL) return FALSE;
  516. }
  517. return TRUE;
  518. } // AddBefore(list)
  519. /* Split *this after position p in *this
  520. Retain as *this the _tail portion of the original *this
  521. Add the _head portion to the _tail end of *pList
  522. Return TRUE if it all worked, FALSE if it didn't.
  523. e.g.
  524. foo->MoveToTail(foo->GetHeadPosition(), bar);
  525. moves one element from the _head of foo to the _tail of bar
  526. foo->MoveToTail(NULL, bar);
  527. is a no-op
  528. foo->MoveToTail(foo->GetTailPosition, bar);
  529. concatenates foo onto the end of bar and empties foo.
  530. A better, except excessively long name might be
  531. MoveElementsFromHeadThroughPositionToOtherTail
  532. */
  533. BOOL CBaseList::MoveToTail
  534. (__in_opt POSITION pos, __in CBaseList *pList)
  535. {
  536. /* Algorithm:
  537. Note that the elements (including their order) in the concatenation
  538. of *pList to the _head of *this is invariant.
  539. 1. Count elements to be moved
  540. 2. Join *pList onto the _head of this to make one long chain
  541. 3. Set first/Last pointers in *this and *pList
  542. 4. Break the chain at the new place
  543. 5. Adjust counts
  544. 6. Set/Reset any events
  545. */
  546. if (pos==NULL) return TRUE; // no-op. Eliminates special cases later.
  547. /* Make cMove the number of nodes to move */
  548. CNode * p = (CNode *)pos;
  549. int cMove = 0; // number of nodes to move
  550. while(p!=NULL) {
  551. p = p->Prev();
  552. ++cMove;
  553. }
  554. /* Join the two chains together */
  555. if (pList->m_pLast!=NULL)
  556. pList->m_pLast->SetNext(m_pFirst);
  557. if (m_pFirst!=NULL)
  558. m_pFirst->SetPrev(pList->m_pLast);
  559. /* set first and last pointers */
  560. p = (CNode *)pos;
  561. if (pList->m_pFirst==NULL)
  562. pList->m_pFirst = m_pFirst;
  563. m_pFirst = p->Next();
  564. if (m_pFirst==NULL)
  565. m_pLast = NULL;
  566. pList->m_pLast = p;
  567. /* Break the chain after p to create the new pieces */
  568. if (m_pFirst!=NULL)
  569. m_pFirst->SetPrev(NULL);
  570. p->SetNext(NULL);
  571. /* Adjust the counts */
  572. m_Count -= cMove;
  573. pList->m_Count += cMove;
  574. return TRUE;
  575. } // MoveToTail
  576. /* Mirror image of MoveToTail:
  577. Split *this before position p in *this.
  578. Retain in *this the _head portion of the original *this
  579. Add the _tail portion to the start (i.e. _head) of *pList
  580. Return TRUE if it all worked, FALSE if it didn't.
  581. e.g.
  582. foo->MoveToHead(foo->GetTailPosition(), bar);
  583. moves one element from the _tail of foo to the _head of bar
  584. foo->MoveToHead(NULL, bar);
  585. is a no-op
  586. foo->MoveToHead(foo->GetHeadPosition, bar);
  587. concatenates foo onto the start of bar and empties foo.
  588. */
  589. BOOL CBaseList::MoveToHead
  590. (__in_opt POSITION pos, __in CBaseList *pList)
  591. {
  592. /* See the comments on the algorithm in MoveToTail */
  593. if (pos==NULL) return TRUE; // no-op. Eliminates special cases later.
  594. /* Make cMove the number of nodes to move */
  595. CNode * p = (CNode *)pos;
  596. int cMove = 0; // number of nodes to move
  597. while(p!=NULL) {
  598. p = p->Next();
  599. ++cMove;
  600. }
  601. /* Join the two chains together */
  602. if (pList->m_pFirst!=NULL)
  603. pList->m_pFirst->SetPrev(m_pLast);
  604. if (m_pLast!=NULL)
  605. m_pLast->SetNext(pList->m_pFirst);
  606. /* set first and last pointers */
  607. p = (CNode *)pos;
  608. if (pList->m_pLast==NULL)
  609. pList->m_pLast = m_pLast;
  610. m_pLast = p->Prev();
  611. if (m_pLast==NULL)
  612. m_pFirst = NULL;
  613. pList->m_pFirst = p;
  614. /* Break the chain after p to create the new pieces */
  615. if (m_pLast!=NULL)
  616. m_pLast->SetNext(NULL);
  617. p->SetPrev(NULL);
  618. /* Adjust the counts */
  619. m_Count -= cMove;
  620. pList->m_Count += cMove;
  621. return TRUE;
  622. } // MoveToHead
  623. /* Reverse the order of the [pointers to] objects in *this
  624. */
  625. void CBaseList::Reverse()
  626. {
  627. /* algorithm:
  628. The obvious booby trap is that you flip pointers around and lose
  629. addressability to the node that you are going to process next.
  630. The easy way to avoid this is do do one chain at a time.
  631. Run along the forward chain,
  632. For each node, set the reverse pointer to the one ahead of us.
  633. The reverse chain is now a copy of the old forward chain, including
  634. the NULL termination.
  635. Run along the reverse chain (i.e. old forward chain again)
  636. For each node set the forward pointer of the node ahead to point back
  637. to the one we're standing on.
  638. The first node needs special treatment,
  639. it's new forward pointer is NULL.
  640. Finally set the First/Last pointers
  641. */
  642. CNode * p;
  643. // Yes we COULD use a traverse, but it would look funny!
  644. p = m_pFirst;
  645. while (p!=NULL) {
  646. CNode * q;
  647. q = p->Next();
  648. p->SetNext(p->Prev());
  649. p->SetPrev(q);
  650. p = q;
  651. }
  652. p = m_pFirst;
  653. m_pFirst = m_pLast;
  654. m_pLast = p;
  655. #if 0 // old version
  656. if (m_pFirst==NULL) return; // empty list
  657. if (m_pFirst->Next()==NULL) return; // single node list
  658. /* run along forward chain */
  659. for ( p = m_pFirst
  660. ; p!=NULL
  661. ; p = p->Next()
  662. ){
  663. p->SetPrev(p->Next());
  664. }
  665. /* special case first element */
  666. m_pFirst->SetNext(NULL); // fix the old first element
  667. /* run along new reverse chain i.e. old forward chain again */
  668. for ( p = m_pFirst // start at the old first element
  669. ; p->Prev()!=NULL // while there's a node still to be set
  670. ; p = p->Prev() // work in the same direction as before
  671. ){
  672. p->Prev()->SetNext(p);
  673. }
  674. /* fix forward and reverse pointers
  675. - the triple XOR swap would work but all the casts look hideous */
  676. p = m_pFirst;
  677. m_pFirst = m_pLast;
  678. m_pLast = p;
  679. #endif
  680. } // Reverse