sllist.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. {
  2. This file is part of the Free Pascal Run Time Library (rtl)
  3. Copyright (c) 2007 by Michael Van Canneyt,
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. Type
  12. TLinkedListItem = Class
  13. Public
  14. Next : TLinkedListItem;
  15. end;
  16. TLinkedListItemClass = Class of TLinkedListItem;
  17. { TLinkedListVisitor }
  18. TLinkedListVisitor = Class
  19. Function Visit(Item : TLinkedListItem) : Boolean; virtual; abstract;
  20. end;
  21. { TLinkedList }
  22. TLinkedList = Class
  23. private
  24. FItemClass: TLinkedListItemClass;
  25. FRoot: TLinkedListItem;
  26. function GetCount: Integer;
  27. Public
  28. Constructor Create(AnItemClass : TLinkedListItemClass); virtual;
  29. Destructor Destroy; override;
  30. Procedure Clear;
  31. Function Add : TLinkedListItem;
  32. Procedure ForEach(Visitor: TLinkedListVisitor);
  33. Procedure RemoveItem(Item : TLinkedListItem; FreeItem : Boolean = False);
  34. Property Root : TLinkedListItem Read FRoot;
  35. Property ItemClass : TLinkedListItemClass Read FItemClass;
  36. Property Count : Integer Read GetCount;
  37. end;
  38. { TLinkedList }
  39. function TLinkedList.GetCount: Integer;
  40. Var
  41. I : TLinkedListItem;
  42. begin
  43. I:=FRoot;
  44. Result:=0;
  45. While I<>Nil do
  46. begin
  47. I:=I.Next;
  48. Inc(Result);
  49. end;
  50. end;
  51. constructor TLinkedList.Create(AnItemClass: TLinkedListItemClass);
  52. begin
  53. FItemClass:=AnItemClass;
  54. end;
  55. destructor TLinkedList.Destroy;
  56. begin
  57. Clear;
  58. inherited Destroy;
  59. end;
  60. procedure TLinkedList.Clear;
  61. Var
  62. I : TLinkedListItem;
  63. begin
  64. // Can't use visitor, because it'd kill the next pointer...
  65. I:=FRoot;
  66. While I<>Nil do
  67. begin
  68. FRoot:=I;
  69. I:=I.Next;
  70. FRoot.Next:=Nil;
  71. FreeAndNil(FRoot);
  72. end;
  73. end;
  74. function TLinkedList.Add: TLinkedListItem;
  75. begin
  76. Result:=FItemClass.Create;
  77. Result.Next:=FRoot;
  78. FRoot:=Result;
  79. end;
  80. procedure TLinkedList.ForEach(Visitor : TLinkedListVisitor);
  81. Var
  82. I : TLinkedListItem;
  83. begin
  84. I:=FRoot;
  85. While (I<>Nil) and Visitor.Visit(I) do
  86. I:=I.Next;
  87. end;
  88. procedure TLinkedList.RemoveItem(Item: TLinkedListItem; FreeItem : Boolean = False);
  89. Var
  90. I : TLinkedListItem;
  91. begin
  92. If (Item<>Nil) and (FRoot<>Nil) then
  93. begin
  94. If (Item=FRoot) then
  95. FRoot:=Item.Next
  96. else
  97. begin
  98. I:=FRoot;
  99. While (I.Next<>Nil) and (I.Next<>Item) do
  100. I:=I.Next;
  101. If (I.Next=Item) then
  102. I.Next:=Item.Next;
  103. end;
  104. If FreeItem Then
  105. Item.Free;
  106. end;
  107. end;