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.

288 lines
5.9 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. dlist.inl
  6. Abstract:
  7. Double linked list template class.
  8. Author:
  9. Weihai Chen (WeihaiC) 06/29/98
  10. Revision History:
  11. Weihai Chen (WeihaiC) 03/08/00 Rename to T*
  12. --*/
  13. template <class T, class KEYTYPE>
  14. TDoubleList<T, KEYTYPE>::TDoubleList<T, KEYTYPE>(void):
  15. m_pHead (NULL),
  16. m_pTail (NULL),
  17. m_dwNumNode (0),
  18. m_bValid (TRUE)
  19. {
  20. }
  21. template <class T, class KEYTYPE>
  22. TDoubleList<T, KEYTYPE>::~TDoubleList<T, KEYTYPE>(void)
  23. {
  24. TDoubleNode<T, KEYTYPE> *pNode = m_pHead;
  25. TDoubleNode<T, KEYTYPE> *pNext;
  26. while (pNode) {
  27. pNext = pNode->GetNext();
  28. delete pNode;
  29. pNode = pNext;
  30. }
  31. }
  32. template <class T, class KEYTYPE>
  33. BOOL TDoubleList<T, KEYTYPE>::InsertItem (T item)
  34. {
  35. BOOL bRet = FALSE;
  36. TDoubleNode<T, KEYTYPE> *pNewNode = new TDoubleNode<T, KEYTYPE> (item);
  37. if (pNewNode) {
  38. bRet = InsertNode (pNewNode);
  39. }
  40. return bRet;
  41. }
  42. template <class T, class KEYTYPE>
  43. BOOL TDoubleList<T, KEYTYPE>::AppendItem (T item)
  44. {
  45. BOOL bRet = FALSE;
  46. TDoubleNode<T, KEYTYPE> *pNewNode = new TDoubleNode<T, KEYTYPE> (item);
  47. if (pNewNode) {
  48. bRet = AppendNode (pNewNode);
  49. }
  50. return bRet;
  51. }
  52. template <class T, class KEYTYPE>
  53. BOOL TDoubleList<T, KEYTYPE>::DeleteItem (T item)
  54. {
  55. BOOL bRet = FALSE;
  56. TDoubleNode<T, KEYTYPE> *pNode = m_pHead;
  57. while (pNode) {
  58. if (pNode->IsSameItem (item)) {
  59. // Delete it
  60. DeleteNode (pNode);
  61. delete (item);
  62. bRet = TRUE;
  63. break;
  64. }
  65. pNode = pNode->GetNext();
  66. }
  67. return bRet;
  68. }
  69. template <class T, class KEYTYPE>
  70. T TDoubleList<T, KEYTYPE>::GetItemFromIndex (DWORD dwIndex)
  71. {
  72. TDoubleNode<T, KEYTYPE> *pNode = GetNodeFromIndex (dwIndex);
  73. if (pNode) {
  74. return pNode->GetData();
  75. }
  76. return NULL;
  77. }
  78. template <class T, class KEYTYPE>
  79. TDoubleNode<T, KEYTYPE> *
  80. TDoubleList<T, KEYTYPE>::GetNodeFromIndex (DWORD dwIndex)
  81. {
  82. TDoubleNode<T, KEYTYPE> *pNode = m_pHead;
  83. DWORD i = 0;
  84. while (pNode && i < dwIndex) {
  85. i++;
  86. pNode = pNode->GetNext();
  87. }
  88. return pNode;
  89. }
  90. template <class T, class KEYTYPE>
  91. BOOL TDoubleList<T, KEYTYPE>::InsertNode (TDoubleNode<T, KEYTYPE> *pNewNode)
  92. {
  93. BOOL bRet = FALSE;
  94. if (pNewNode) {
  95. if (m_pHead) {
  96. pNewNode->SetNext (m_pHead);
  97. pNewNode->SetPrev (NULL);
  98. m_pHead->SetPrev (pNewNode);
  99. }
  100. else {
  101. // This is the first node
  102. pNewNode->SetNext (NULL);
  103. pNewNode->SetPrev (NULL);
  104. m_pTail = pNewNode;
  105. }
  106. m_pHead = pNewNode;
  107. m_dwNumNode ++;
  108. bRet = TRUE;
  109. }
  110. return bRet;
  111. }
  112. template <class T, class KEYTYPE>
  113. BOOL TDoubleList<T, KEYTYPE>::AppendNode (TDoubleNode<T, KEYTYPE> *pNewNode)
  114. {
  115. BOOL bRet = FALSE;
  116. if (pNewNode) {
  117. if (m_pTail) {
  118. pNewNode->SetNext (NULL);
  119. pNewNode->SetPrev (m_pTail);
  120. m_pTail->SetNext (pNewNode);
  121. }
  122. else {
  123. // This is the first node
  124. pNewNode->SetNext (NULL);
  125. pNewNode->SetPrev (NULL);
  126. m_pHead = pNewNode;
  127. }
  128. m_pTail = pNewNode;
  129. m_dwNumNode ++;
  130. bRet = TRUE;
  131. }
  132. return bRet;
  133. }
  134. template <class T, class KEYTYPE>
  135. BOOL TDoubleList<T, KEYTYPE>::DeleteNode (TDoubleNode<T, KEYTYPE> *pNode)
  136. {
  137. TDoubleNode<T, KEYTYPE> *pNextNode;
  138. TDoubleNode<T, KEYTYPE> *pPrevNode;
  139. BOOL bRet = FALSE;
  140. if (pNode) {
  141. // It is not the first one, i.e. not the only one
  142. pNextNode = pNode->GetNext ();
  143. pPrevNode = pNode->GetPrev ();
  144. if (pNextNode) {
  145. pNextNode->SetPrev (pPrevNode);
  146. }
  147. else {
  148. m_pTail = pPrevNode;
  149. }
  150. if (pPrevNode) {
  151. pPrevNode->SetNext (pNextNode);
  152. }
  153. else {
  154. // It is the first one
  155. m_pHead = pNextNode;
  156. }
  157. m_dwNumNode --;
  158. bRet = TRUE;
  159. }
  160. return bRet;
  161. }
  162. template <class T, class KEYTYPE>
  163. TDoubleNode<T, KEYTYPE> *
  164. TDoubleList<T, KEYTYPE>::GetHead (void)
  165. {
  166. TDoubleNode<T, KEYTYPE> * pHead = NULL;
  167. pHead = m_pHead;
  168. return pHead;
  169. }
  170. template <class T, class KEYTYPE>
  171. BOOL
  172. TDoubleList<T, KEYTYPE>::GetTotalNode (PDWORD pdwCount)
  173. {
  174. BOOL bRet = FALSE;
  175. if (m_bValid) {
  176. *pdwCount = m_dwNumNode;
  177. bRet = TRUE;
  178. }
  179. return bRet;
  180. }
  181. template <class T, class KEYTYPE>
  182. T TSrchDoubleList<T, KEYTYPE>::FindItemFromItem (T item)
  183. {
  184. TDoubleNode<T, KEYTYPE> *pNode = FindNodeFromItem (item);
  185. if (pNode) {
  186. return pNode->GetData();
  187. }
  188. return NULL;
  189. }
  190. template <class T, class KEYTYPE>
  191. T TSrchDoubleList<T, KEYTYPE>::FindItemFromKey (KEYTYPE key)
  192. {
  193. TDoubleNode<T, KEYTYPE> *pNode = FindNodeFromKey (key);
  194. if (pNode) {
  195. return pNode->GetData();
  196. }
  197. return NULL;
  198. }
  199. template <class T, class KEYTYPE>
  200. TDoubleNode<T, KEYTYPE> *
  201. TSrchDoubleList<T, KEYTYPE>::FindNodeFromItem (T item)
  202. {
  203. TDoubleNode<T, KEYTYPE> *pNode = m_pHead;
  204. while (pNode) {
  205. if (pNode->IsSameItem (item)) {
  206. return pNode;
  207. }
  208. pNode = pNode->GetNext();
  209. }
  210. return NULL;
  211. }
  212. template <class T, class KEYTYPE>
  213. TDoubleNode<T, KEYTYPE> *
  214. TSrchDoubleList<T, KEYTYPE>::FindNodeFromKey (KEYTYPE key)
  215. {
  216. TDoubleNode<T, KEYTYPE> *pNode = m_pHead;
  217. while (pNode) {
  218. if (pNode->IsSameKey (key)) {
  219. return pNode;
  220. }
  221. pNode = pNode->GetNext();
  222. }
  223. return NULL;
  224. }