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.

111 lines
3.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) 1998-1998 Microsoft Corporation
  6. //
  7. // File: tlist.h
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. // tlist.h --- template version of AList
  12. //
  13. #ifndef __TLIST_H__
  14. #define __TLIST_H__
  15. //#include "stdafx.h"
  16. //template <class T>
  17. //typedef BOOL (* TRelation) (T, T);
  18. // TListItem<> contains four more members than AListItem: one additional constructor,
  19. // a destructor, one member function, and one data member.
  20. template <class T>
  21. class TListItem
  22. {
  23. public:
  24. TListItem() { m_pNext=NULL; };
  25. ~TListItem(); // new destructor
  26. static void Delete(TListItem<T>* pFirst); // new deletion helper
  27. TListItem(const T& item) { m_Tinfo = item; m_pNext=NULL; }; // additional constructor.
  28. TListItem<T> *GetNext() const {return m_pNext;};
  29. void SetNext(TListItem<T> *pNext) {m_pNext=pNext;};
  30. LONG GetCount() const;
  31. TListItem<T>* Cat(TListItem<T>* pItem);
  32. TListItem<T>* AddTail(TListItem<T>* pItem) {return Cat(pItem);};
  33. TListItem<T>* Remove(TListItem<T>* pItem);
  34. TListItem<T>* GetPrev(TListItem<T> *pItem) const;
  35. TListItem<T>* GetItem(LONG index);
  36. T& GetItemValue() { return m_Tinfo; } // additional member function
  37. TListItem<T>* MergeSort(BOOL (* fcnCompare) (T&, T&)); // Destructively mergeSorts the list items
  38. private:
  39. void Divide(TListItem<T>* &pHalf1, TListItem<T>* &pHalf2);
  40. TListItem<T>* Merge(TListItem<T>* pOtherList, BOOL (* fcnCompare) (T&, T&));
  41. T m_Tinfo; // additional data member, but memory is the same since in AListItem
  42. // you put the extra data member in the derived class
  43. TListItem<T> *m_pNext;
  44. };
  45. // TList<> adds a destructor to AList.
  46. template <class T>
  47. class TList
  48. {
  49. public:
  50. TList() {m_pHead=NULL;}
  51. ~TList()
  52. {
  53. //if (m_pHead != NULL) delete m_pHead;
  54. TListItem<T>::Delete(m_pHead);
  55. } // new destructor
  56. TListItem<T> *GetHead() const { return m_pHead;};
  57. void RemoveAll() { m_pHead=NULL;};
  58. void CleanUp()
  59. {
  60. //if (m_pHead) delete m_pHead;
  61. if (m_pHead) TListItem<T>::Delete(m_pHead);
  62. m_pHead=NULL;
  63. }
  64. LONG GetCount() const {return m_pHead->GetCount();};
  65. TListItem<T> *GetItem(LONG index) { return m_pHead->GetItem(index);};
  66. void InsertBefore(TListItem<T> *pItem,TListItem<T> *pInsert);
  67. void Cat(TListItem<T> *pItem) {m_pHead=m_pHead->Cat(pItem);};
  68. void Cat(TList<T> *pList)
  69. {
  70. // assert(pList!=NULL);
  71. m_pHead=m_pHead->Cat(pList->GetHead());
  72. };
  73. void AddHead(TListItem<T> *pItem)
  74. {
  75. // assert(pItem!=NULL);
  76. pItem->SetNext(m_pHead);
  77. m_pHead=pItem;
  78. }
  79. void AddTail(TListItem<T> *pItem);// {m_pHead=m_pHead->AddTail(pItem);};
  80. void Remove(TListItem<T> *pItem) {m_pHead=m_pHead->Remove(pItem);};
  81. TListItem<T> *GetPrev(TListItem<T> *pItem) const {return m_pHead->GetPrev(pItem);};
  82. TListItem<T> *GetTail() const {return GetPrev(NULL);};
  83. BOOL IsEmpty(void) const {return (m_pHead==NULL);};
  84. TListItem<T> *RemoveHead(void)
  85. {
  86. TListItem<T> *li;
  87. li=m_pHead;
  88. if(m_pHead)
  89. {
  90. m_pHead=m_pHead->GetNext();
  91. li->SetNext(NULL);
  92. }
  93. return li;
  94. }
  95. void MergeSort(BOOL (* fcnCompare) (T&, T&)); // Destructively mergeSorts the list
  96. void Reverse(void); // Reverses the entire list
  97. HRESULT Copy(TList<T>& rList); // Copies one list to another
  98. protected:
  99. TListItem<T> *m_pHead;
  100. };
  101. #include "tlist.cpp"
  102. #endif // __TLIST_H__