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.

228 lines
3.5 KiB

  1. // PRECOMP
  2. #include "precomp.h"
  3. VOID* CWBOBLIST::GetTail()
  4. {
  5. if(m_pTail)
  6. {
  7. return m_pTail->pItem;
  8. }
  9. else
  10. {
  11. return NULL;
  12. }
  13. }
  14. VOID* CWBOBLIST::GetNext(WBPOSITION& rPos)
  15. {
  16. ASSERT(rPos);
  17. VOID* pReturn = rPos->pItem;
  18. rPos = rPos->pNext;
  19. return pReturn;
  20. }
  21. VOID* CWBOBLIST::GetPrevious(WBPOSITION& rPos)
  22. {
  23. ASSERT(rPos);
  24. VOID* pReturn = rPos->pItem;
  25. rPos = rPos->pPrev;
  26. return pReturn;
  27. }
  28. WBPOSITION CWBOBLIST::AddAt(VOID* pItem, WBPOSITION Pos)
  29. {
  30. ASSERT(Pos);
  31. WBPOSITION posRet = NULL;
  32. DBG_SAVE_FILE_LINE
  33. if (posRet = new COBNODED)
  34. {
  35. posRet->pItem = pItem;
  36. posRet->pNext = Pos->pNext;
  37. posRet->pPrev = Pos;
  38. Pos->pNext = posRet;
  39. if(posRet->pNext)
  40. {
  41. posRet->pNext->pPrev = posRet;
  42. }
  43. else
  44. {
  45. m_pTail = posRet;
  46. }
  47. }
  48. return posRet;
  49. }
  50. VOID* CWBOBLIST::RemoveAt(WBPOSITION Pos)
  51. {
  52. VOID* pReturn = NULL;
  53. if (m_pHead)
  54. {
  55. if (m_pHead == Pos)
  56. {
  57. // Removing the first element in the list
  58. m_pHead = Pos->pNext;
  59. pReturn = Pos->pItem;
  60. delete Pos;
  61. if(m_pHead != NULL)
  62. {
  63. m_pHead->pPrev = NULL;
  64. }
  65. else
  66. {
  67. // Removing the only element!
  68. m_pTail = NULL;
  69. }
  70. }
  71. else
  72. {
  73. WBPOSITION pCur = m_pHead;
  74. while (pCur && pCur->pNext)
  75. {
  76. if (pCur->pNext == Pos)
  77. {
  78. // Removing
  79. pCur->pNext = Pos->pNext;
  80. if(pCur->pNext)
  81. {
  82. pCur->pNext->pPrev = pCur;
  83. }
  84. if (m_pTail == Pos)
  85. {
  86. m_pTail = pCur;
  87. }
  88. pReturn = Pos->pItem;
  89. delete Pos;
  90. }
  91. pCur = pCur->pNext;
  92. }
  93. }
  94. }
  95. return pReturn;
  96. }
  97. WBPOSITION CWBOBLIST::AddTail(VOID* pItem)
  98. {
  99. WBPOSITION posRet = NULL;
  100. if (m_pTail)
  101. {
  102. DBG_SAVE_FILE_LINE
  103. if (m_pTail->pNext = new COBNODED)
  104. {
  105. m_pTail->pNext->pPrev = m_pTail;
  106. m_pTail = m_pTail->pNext;
  107. m_pTail->pItem = pItem;
  108. m_pTail->pNext = NULL;
  109. }
  110. }
  111. else
  112. {
  113. ASSERT(!m_pHead);
  114. DBG_SAVE_FILE_LINE
  115. if (m_pHead = new COBNODED)
  116. {
  117. m_pTail = m_pHead;
  118. m_pTail->pItem = pItem;
  119. m_pTail->pNext = NULL;
  120. m_pTail->pPrev = NULL;
  121. }
  122. }
  123. return m_pTail;
  124. }
  125. void CWBOBLIST::EmptyList()
  126. {
  127. while (!IsEmpty()) {
  128. RemoveAt(GetHeadPosition());
  129. }
  130. }
  131. CWBOBLIST::~CWBOBLIST()
  132. {
  133. ASSERT(IsEmpty());
  134. }
  135. WBPOSITION CWBOBLIST::GetPosition(void* _pItem)
  136. {
  137. // For potential efficiency of lookup (if we switched to
  138. // a doubly linked list), users should really store the WBPOSITION
  139. // of an item. For those that don't, this method is provided.
  140. WBPOSITION pos = m_pHead;
  141. while (pos) {
  142. if (pos->pItem == _pItem) {
  143. break;
  144. }
  145. GetNext(pos);
  146. }
  147. return pos;
  148. }
  149. WBPOSITION CWBOBLIST::Lookup(void* pComparator)
  150. {
  151. WBPOSITION pos = m_pHead;
  152. while (pos) {
  153. if (Compare(pos->pItem, pComparator)) {
  154. break;
  155. }
  156. GetNext(pos);
  157. }
  158. return pos;
  159. }
  160. WBPOSITION CWBOBLIST::AddHead(VOID* pItem)
  161. {
  162. WBPOSITION posRet = NULL;
  163. if (m_pHead)
  164. {
  165. DBG_SAVE_FILE_LINE
  166. if (posRet = new COBNODED)
  167. {
  168. posRet->pNext = m_pHead;
  169. m_pHead->pPrev = posRet;
  170. posRet->pItem = pItem;
  171. m_pHead = posRet;
  172. m_pHead->pPrev = NULL;
  173. }
  174. }
  175. else
  176. {
  177. ASSERT(!m_pTail);
  178. DBG_SAVE_FILE_LINE
  179. if (m_pHead = new COBNODED)
  180. {
  181. m_pTail = m_pHead;
  182. m_pHead->pItem = pItem;
  183. m_pHead->pNext = NULL;
  184. m_pHead->pPrev = NULL;
  185. }
  186. }
  187. return m_pHead;
  188. }