root / ULinkedList.pas
History | View | Annotate | Download (3.9 kB)
| 1 | (*
|
|---|---|
| 2 | * CDDL HEADER START |
| 3 | * |
| 4 | * The contents of this file are subject to the terms of the |
| 5 | * Common Development and Distribution License, Version 1.0 only |
| 6 | * (the "License"). You may not use this file except in compliance |
| 7 | * with the License. |
| 8 | * |
| 9 | * You can obtain a copy of the license at |
| 10 | * http://www.opensource.org/licenses/cddl1.php. |
| 11 | * See the License for the specific language governing permissions |
| 12 | * and limitations under the License. |
| 13 | * |
| 14 | * When distributing Covered Code, include this CDDL HEADER in each |
| 15 | * file and include the License file at |
| 16 | * http://www.opensource.org/licenses/cddl1.php. If applicable, |
| 17 | * add the following below this CDDL HEADER, with the fields enclosed |
| 18 | * by brackets "[]" replaced with your own identifying * information: |
| 19 | * Portions Copyright [yyyy] [name of copyright owner] |
| 20 | * |
| 21 | * CDDL HEADER END |
| 22 | * |
| 23 | * |
| 24 | * Portions Copyright 2007 Andreas Schneider |
| 25 | *) |
| 26 | unit ULinkedList;
|
| 27 | |
| 28 | interface
|
| 29 | |
| 30 | uses
|
| 31 | SysUtils; |
| 32 | |
| 33 | type
|
| 34 | PLinkedItem = ^TLinkedItem; |
| 35 | TLinkedItem = record
|
| 36 | ID: Integer; |
| 37 | Data: Pointer; |
| 38 | Next: PLinkedItem; |
| 39 | end;
|
| 40 | TLinkedList = class(TObject)
|
| 41 | constructor Create; virtual; |
| 42 | destructor Destroy; override; |
| 43 | protected
|
| 44 | FFirst: PLinkedItem; |
| 45 | FLast: PLinkedItem; |
| 46 | public
|
| 47 | procedure Clear; virtual; |
| 48 | function Iterate(var ALinkedItem: PLinkedItem): Boolean; virtual; |
| 49 | function Add(AID: Integer; AData: Pointer): PLinkedItem; virtual; |
| 50 | procedure Delete(AData: Pointer); overload; virtual; |
| 51 | procedure Delete(AID: Integer); overload; virtual; |
| 52 | function Get(AID: Integer): Pointer; virtual; |
| 53 | property Last: PLinkedItem read FLast; |
| 54 | end;
|
| 55 | |
| 56 | implementation
|
| 57 | |
| 58 | { TBlockList }
|
| 59 | |
| 60 | function TLinkedList.Add(AID: Integer; AData: Pointer): PLinkedItem;
|
| 61 | var
|
| 62 | current: PLinkedItem; |
| 63 | begin
|
| 64 | New(current); |
| 65 | current^.ID := AID; |
| 66 | current^.Data := AData; |
| 67 | current^.Next := nil;
|
| 68 | if FFirst = nil then FFirst := current; |
| 69 | if FLast <> nil then FLast^.Next := current; |
| 70 | FLast := current; |
| 71 | Result := current; |
| 72 | end;
|
| 73 | |
| 74 | procedure TLinkedList.Clear;
|
| 75 | var
|
| 76 | current, next: PLinkedItem; |
| 77 | begin
|
| 78 | current := FFirst; |
| 79 | while current <> nil do |
| 80 | begin
|
| 81 | next := current^.Next; |
| 82 | Dispose(current); |
| 83 | current := next; |
| 84 | end;
|
| 85 | FFirst := nil;
|
| 86 | FLast := nil;
|
| 87 | end;
|
| 88 | |
| 89 | constructor TLinkedList.Create;
|
| 90 | begin
|
| 91 | inherited Create;
|
| 92 | FFirst := nil;
|
| 93 | FLast := nil;
|
| 94 | end;
|
| 95 | |
| 96 | procedure TLinkedList.Delete(AData: Pointer);
|
| 97 | var
|
| 98 | currentItem, lastItem, nextItem: PLinkedItem; |
| 99 | begin
|
| 100 | lastItem := nil;
|
| 101 | currentItem := FFirst; |
| 102 | while currentItem <> nil do |
| 103 | begin
|
| 104 | if currentItem^.Data = AData then |
| 105 | begin
|
| 106 | if FFirst = currentItem then FFirst := currentItem^.Next; |
| 107 | if FLast = currentItem then FLast := lastItem; |
| 108 | if lastItem <> nil then lastItem^.Next := currentItem^.Next; |
| 109 | Dispose(currentItem); |
| 110 | nextItem := nil;
|
| 111 | end else |
| 112 | nextItem := currentItem^.Next; |
| 113 | lastItem := currentItem; |
| 114 | currentItem := nextItem; |
| 115 | end;
|
| 116 | end;
|
| 117 | |
| 118 | procedure TLinkedList.Delete(AID: Integer);
|
| 119 | var
|
| 120 | currentItem, lastItem, nextItem: PLinkedItem; |
| 121 | begin
|
| 122 | lastItem := nil;
|
| 123 | currentItem := FFirst; |
| 124 | while currentItem <> nil do |
| 125 | begin
|
| 126 | if currentItem^.ID = AID then |
| 127 | begin
|
| 128 | if FFirst = currentItem then FFirst := currentItem^.Next; |
| 129 | if FLast = currentItem then FLast := lastItem; |
| 130 | if lastItem <> nil then lastItem^.Next := currentItem^.Next; |
| 131 | Dispose(currentItem); |
| 132 | nextItem := nil;
|
| 133 | end else |
| 134 | nextItem := currentItem^.Next; |
| 135 | lastItem := currentItem; |
| 136 | currentItem := nextItem; |
| 137 | end;
|
| 138 | end;
|
| 139 | |
| 140 | destructor TLinkedList.Destroy;
|
| 141 | begin
|
| 142 | Clear; |
| 143 | inherited Destroy;
|
| 144 | end;
|
| 145 | |
| 146 | function TLinkedList.Get(AID: Integer): Pointer;
|
| 147 | var
|
| 148 | item: PLinkedItem; |
| 149 | begin
|
| 150 | Result := nil;
|
| 151 | item := nil;
|
| 152 | while Iterate(item) and (Result = nil) do |
| 153 | if item^.ID = AID then |
| 154 | Result := item^.Data; |
| 155 | end;
|
| 156 | |
| 157 | function TLinkedList.Iterate(var ALinkedItem: PLinkedItem): Boolean; |
| 158 | begin
|
| 159 | if ALinkedItem = nil then |
| 160 | ALinkedItem := FFirst |
| 161 | else
|
| 162 | ALinkedItem := ALinkedItem^.Next; |
| 163 | Result := ALinkedItem <> nil;
|
| 164 | end;
|
| 165 | |
| 166 | end.
|
| 167 |