forked from mcvanderkooij/rtfdocument
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUSimpleLinkedList.pas
More file actions
218 lines (189 loc) · 5.73 KB
/
USimpleLinkedList.pas
File metadata and controls
218 lines (189 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
unit USimpleLinkedList;
{ copied from https://delphihaven.wordpress.com/2011/07/31/generic-linked-lists-redux/
}
interface
type
TSimpleLinkedList<T> = class
public type
TNode = class
strict private
FPrevious, FNext: TNode;
FList: TSimpleLinkedList<T>;
FValue: T;
protected
constructor Create(AList: TSimpleLinkedList<T>; APrevious, ANext: TNode;
const AValue: T); overload;
public
constructor Create; overload; //disallow outside construction by raising an exception
destructor Destroy; override;
procedure MoveBefore(ANode: TNode);
procedure MoveAfter(ANode: TNode);
property List: TSimpleLinkedList<T> read FList;
property Next: TNode read FNext write FNext;
property Previous: TNode read FPrevious write FPrevious;
property Value: T read FValue write FValue;
end;
TValueEnumerator = record
strict private
FDoneFirst: Boolean;
FNode: TNode;
function GetCurrent: T;
public
constructor Create(AHead: TNode);
function MoveNext: Boolean;
property Current: T read GetCurrent;
end;
private
FCount: Integer;
FFirst, FLast: TNode;
procedure ValidateNode(ANode: TNode);
public
destructor Destroy; override;
function GetEnumerator: TValueEnumerator;
procedure Clear;
function Add(const AValue: T): TNode; inline;
function AddAfter(ANode: TNode; const AValue: T): TNode;
function AddFirst(const AValue: T): TNode;
function AddLast(const AValue: T): TNode;
function Insert(Index: Integer; const AValue: T): TNode;
function InsertBefore(ANode: TNode; const AValue: T): TNode;
property Count: Integer read FCount;
property First: TNode read FFirst;
property Last: TNode read FLast;
end;
implementation
uses SysUtils, RTLConsts; //for the standard exception types and error messages
{ TNode }
constructor TSimpleLinkedList<T>.TNode.Create;
begin
raise ENoConstructException.CreateResFmt(@SNoConstruct, [ClassName]);
end;
constructor TSimpleLinkedList<T>.TNode.Create(AList: TSimpleLinkedList<T>;
APrevious, ANext: TNode; const AValue: T);
begin
Assert(AList <> nil);
inherited Create;
FPrevious := APrevious;
FNext := ANext;
if APrevious <> nil then APrevious.Next := Self;
if ANext <> nil then ANext.Previous := Self;
if APrevious = AList.Last then AList.FLast := Self;
if ANext = AList.First then AList.FFirst := Self;
FList := AList;
FValue := AValue;
Inc(AList.FCount);
end;
destructor TSimpleLinkedList<T>.TNode.Destroy;
begin
Dec(List.FCount);
if Self = List.First then List.FFirst := Next;
if Self = List.Last then List.FLast := Previous;
if Previous <> nil then Previous.FNext := Next;
if Next <> nil then Next.FPrevious := Previous;
inherited Destroy;
end;
procedure TSimpleLinkedList<T>.TNode.MoveBefore(ANode: TNode);
begin
List.ValidateNode(ANode);
if ANode = Next then Exit;
//unlink ourselves from our current neighbours
if Previous <> nil then Previous.Next := Next;
if Next <> nil then Next.Previous := Previous;
//link ourselves to our new neighbours
Previous := ANode.Previous;
if Previous <> nil then Previous.Next := Self;
ANode.Previous := Self;
Next := ANode;
end;
procedure TSimpleLinkedList<T>.TNode.MoveAfter(ANode: TNode);
begin
List.ValidateNode(ANode);
if ANode = Previous then Exit;
//unlink ourselves from our current neighbours
if Previous <> nil then Previous.Next := Next;
if Next <> nil then Next.Previous := Previous;
//link ourselves to our new neighbours
Next := ANode.Next;
if Next <> nil then Next.Previous := Self;
ANode.Next := Self;
Previous := ANode;
end;
{ TValueEnumerator }
constructor TSimpleLinkedList<T>.TValueEnumerator.Create(AHead: TNode);
begin
FDoneFirst := False;
FNode := AHead;
end;
function TSimpleLinkedList<T>.TValueEnumerator.MoveNext: Boolean;
begin
if not FDoneFirst then
FDoneFirst := True
else
FNode := FNode.Next;
Result := (FNode <> nil);
end;
function TSimpleLinkedList<T>.TValueEnumerator.GetCurrent: T;
begin
Result := FNode.Value;
end;
{ TSimpleLinkedList }
destructor TSimpleLinkedList<T>.Destroy;
begin
Clear;
inherited Destroy;
end;
function TSimpleLinkedList<T>.GetEnumerator: TValueEnumerator;
begin
Result := TValueEnumerator.Create(First);
end;
function TSimpleLinkedList<T>.Add(const AValue: T): TNode;
begin
Result := TNode.Create(Self, Last, nil, AValue);
end;
function TSimpleLinkedList<T>.AddAfter(ANode: TNode; const AValue: T): TNode;
begin
ValidateNode(ANode);
Result := TNode.Create(Self, ANode, ANode.Next, AValue);
end;
function TSimpleLinkedList<T>.AddFirst(const AValue: T): TNode;
begin
Result := TNode.Create(Self, nil, First, AValue);
end;
function TSimpleLinkedList<T>.AddLast(const AValue: T): TNode;
begin
Result := TNode.Create(Self, Last, nil, AValue);
end;
procedure TSimpleLinkedList<T>.Clear;
begin
while Count > 0 do First.Free;
end;
function TSimpleLinkedList<T>.Insert(Index: Integer; const AValue: T): TNode;
var
I: Integer;
Node: TNode;
begin
if (Index < 0) or (Index > Count) then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
if Index = Count then
Result := Add(AValue)
else
begin
Node := First;
for I := 1 to Index do
Node := Node.Next;
Result := TNode.Create(Self, Node.Previous, Node, AValue);
end;
end;
function TSimpleLinkedList<T>.InsertBefore(ANode: TNode; const AValue: T): TNode;
begin
ValidateNode(ANode);
Result := TNode.Create(Self, ANode.Previous, ANode, AValue);
end;
procedure TSimpleLinkedList<T>.ValidateNode(ANode: TNode);
begin
if ANode = nil then
raise EArgumentNilException.CreateRes(@SArgumentNil);
if ANode.List <> Self then
raise EArgumentOutOfRangeException.CreateRes(@SGenericItemNotFound);
end;
end.