sllist.inc 2.7 KB

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