Source code of Windows XP (NT5)
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.

241 lines
4.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992-2001.
  5. //
  6. // File: LIST . H
  7. //
  8. // Contents:
  9. //
  10. // Notes: List manipulation functions.
  11. //
  12. //----------------------------------------------------------------------------
  13. #ifndef LIST_H_INCLUDED
  14. #define LIST_H_INCLUDED
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <windows.h>
  18. template<class X, class Y> class List {
  19. struct Node {
  20. X item;
  21. Y key;
  22. Node *next;
  23. };
  24. Node *m_head;
  25. DWORD m_dwNodeCount;
  26. public:
  27. List ();
  28. virtual ~List();
  29. HRESULT Insert (X item,
  30. Y key);
  31. HRESULT Remove (X *item);
  32. HRESULT RemoveThis (X item);
  33. HRESULT RemoveByKey (Y key,
  34. X *item);
  35. VOID RemoveAll(VOID);
  36. HRESULT Find (DWORD dwIndex,
  37. X *item);
  38. HRESULT FindByKey (Y key,
  39. X *item);
  40. DWORD ListCount (VOID);
  41. };
  42. template<class X, class Y> List<X, Y>::List ()
  43. {
  44. m_head = NULL;
  45. m_dwNodeCount = 0;
  46. }
  47. template<class X, class Y> List<X, Y>::~List ()
  48. {
  49. RemoveAll();
  50. }
  51. template<class X, class Y> HRESULT List<X, Y>::Insert (X item,
  52. Y key)
  53. {
  54. Node *pNewNode;
  55. pNewNode = new Node;
  56. if ( pNewNode ) {
  57. pNewNode->item = item;
  58. pNewNode->key = key;
  59. pNewNode->next = NULL;
  60. if ( m_dwNodeCount ) {
  61. pNewNode->next = m_head;
  62. }
  63. m_head = pNewNode;
  64. m_dwNodeCount++;
  65. }
  66. return ( pNewNode ) ? S_OK : HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  67. }
  68. template<class X, class Y> HRESULT List<X, Y>::Remove (X *item)
  69. {
  70. Node *temp;
  71. if ( m_dwNodeCount == 0 ) {
  72. return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  73. }
  74. *item = m_head->item;
  75. temp = m_head;
  76. m_head = m_head->next;
  77. delete temp;
  78. m_dwNodeCount--;
  79. return S_OK;
  80. }
  81. template<class X, class Y> HRESULT List<X, Y>::RemoveThis (X item)
  82. {
  83. Node *temp;
  84. Node *nodeToFind;
  85. if ( m_dwNodeCount == 0 ) {
  86. return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  87. }
  88. if ( m_head->item == item ) {
  89. nodeToFind = m_head;
  90. m_head = m_head->next;
  91. }
  92. else {
  93. for (temp = m_head; temp->next && (temp->next->item != item); ) {
  94. temp = temp->next;
  95. }
  96. if ( temp->next ) {
  97. nodeToFind = temp->next;
  98. temp->next = temp->next->next;
  99. }
  100. else
  101. nodeToFind = NULL;
  102. }
  103. if ( nodeToFind ) {
  104. delete nodeToFind;
  105. m_dwNodeCount--;
  106. return S_OK;
  107. }
  108. else
  109. return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  110. }
  111. template<class X, class Y> HRESULT List<X, Y>::RemoveByKey (Y key,
  112. X *item)
  113. {
  114. Node *temp;
  115. Node *nodeToFind;
  116. if ( m_dwNodeCount == 0 ) {
  117. return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  118. }
  119. if ( m_head->key == key ) {
  120. nodeToFind = m_head;
  121. m_head = m_head->next;
  122. }
  123. else {
  124. for (temp = m_head; temp->next && (temp->next->key != key); ) {
  125. temp = temp->next;
  126. }
  127. if ( temp->next ) {
  128. nodeToFind = temp->next;
  129. temp->next = temp->next->next;
  130. }
  131. else
  132. nodeToFind = NULL;
  133. }
  134. if ( nodeToFind ) {
  135. *item = nodeToFind->item;
  136. delete nodeToFind;
  137. m_dwNodeCount--;
  138. return S_OK;
  139. }
  140. else
  141. return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  142. }
  143. template<class X, class Y> VOID List<X, Y>::RemoveAll (VOID)
  144. {
  145. Node *temp;
  146. for (; m_dwNodeCount; --m_dwNodeCount) {
  147. temp = m_head;
  148. m_head = m_head->next;
  149. delete temp;
  150. }
  151. return;
  152. }
  153. template<class X, class Y> HRESULT List<X, Y>::Find (DWORD dwIndex,
  154. X *item)
  155. {
  156. Node *temp;
  157. DWORD i;
  158. if ( (m_dwNodeCount == 0) || (dwIndex > m_dwNodeCount) ) {
  159. return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  160. }
  161. for (i=0, temp = m_head; i < dwIndex; ++i) {
  162. temp = temp->next;
  163. }
  164. *item = temp->item;
  165. return S_OK;
  166. }
  167. template<class X, class Y> HRESULT List<X, Y>::FindByKey (Y key,
  168. X *item)
  169. {
  170. Node *temp;
  171. for (temp = m_head; temp && (temp->key != key); )
  172. temp = temp->next;
  173. if ( temp ) {
  174. *item = temp->item;
  175. return S_OK;
  176. }
  177. else {
  178. return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  179. }
  180. }
  181. template<class X, class Y> DWORD List<X, Y>::ListCount (VOID)
  182. {
  183. return m_dwNodeCount;
  184. }
  185. #endif