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.

122 lines
2.3 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation
  2. //
  3. // alist.cpp
  4. //
  5. #include "alist.h"
  6. LONG AListItem::GetCount(void) const
  7. {
  8. LONG l;
  9. const AListItem *li;
  10. for(l=0,li=this; li!=NULL ; li=li->m_pNext,++l);
  11. return l;
  12. }
  13. AListItem* AListItem::Cat(AListItem *pItem)
  14. {
  15. AListItem *li;
  16. if(this==NULL)
  17. return pItem;
  18. for(li=this ; li->m_pNext!=NULL ; li=li->m_pNext);
  19. li->m_pNext=pItem;
  20. return this;
  21. }
  22. AListItem* AListItem::Remove(AListItem *pItem)
  23. {
  24. AListItem *li,*prev;
  25. //treat remove(NULL) same as item not found in list
  26. if (pItem==NULL)
  27. return this;
  28. if(pItem==this)
  29. {
  30. li = m_pNext;
  31. m_pNext = NULL;
  32. return li;
  33. }
  34. prev=NULL;
  35. for(li=this; li!=NULL && li!=pItem ; li=li->m_pNext)
  36. prev=li;
  37. if(li==NULL) // item not found in list
  38. return this;
  39. // here it is guaranteed that prev is non-NULL since we checked for
  40. // that condition at the very beginning
  41. prev->SetNext(li->m_pNext);
  42. li->SetNext(NULL);
  43. // SetNext on pItem to NULL
  44. pItem->SetNext(NULL);
  45. return this;
  46. }
  47. AListItem* AListItem::GetPrev(AListItem *pItem) const
  48. {
  49. const AListItem *li,*prev;
  50. prev=NULL;
  51. for(li=this ; li!=NULL && li!=pItem ; li=li->m_pNext)
  52. prev=li;
  53. return (AListItem*)prev;
  54. }
  55. AListItem * AListItem::GetItem(LONG index)
  56. {
  57. AListItem *scan;
  58. for (scan = this; scan!=NULL && index; scan = scan->m_pNext)
  59. {
  60. index--;
  61. }
  62. return (scan);
  63. }
  64. void AList::InsertBefore(AListItem *pItem,AListItem *pInsert)
  65. {
  66. AListItem *prev = GetPrev(pItem);
  67. pInsert->SetNext(pItem);
  68. if (prev) prev->SetNext(pInsert);
  69. else m_pHead = pInsert;
  70. }
  71. void AList::AddTail(AListItem *pItem)
  72. {
  73. if (m_pHead == NULL)
  74. {
  75. AddHead(pItem);
  76. }
  77. else
  78. {
  79. m_pHead = m_pHead->AddTail(pItem);
  80. }
  81. }
  82. void AList::Reverse()
  83. {
  84. AList Temp;
  85. AListItem *pItem;
  86. while (pItem = RemoveHead())
  87. {
  88. Temp.AddHead(pItem);
  89. }
  90. m_pHead = Temp.GetHead();
  91. }
  92. BOOL AList::IsMember(AListItem *pItem)
  93. {
  94. AListItem *pScan = GetHead();
  95. for (;pScan;pScan = pScan->GetNext())
  96. {
  97. if (pItem == pScan) return TRUE;
  98. }
  99. return FALSE;
  100. }