-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathDelphiUtils.Lists.pas
336 lines (283 loc) · 8.04 KB
/
DelphiUtils.Lists.pas
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
unit DelphiUtils.Lists;
{
This module provides a generic double linked list.
}
interface
uses
Ntapi.WinNt, DelphiUtils.AutoObjects;
type
TDoubleListDirection = (
ldForward,
ldBackward
);
// A list entry that stores an arbitrary managed type as data.
IDoubleListEntry<T> = interface
['{A7F33808-1B21-43F5-8455-D94B524CD6E3}']
function GetLinks: PListEntry;
function GetContent: T;
procedure SetContent(const Data: T);
property Links: PListEntry read GetLinks;
property Content: T read GetContent write SetContent;
end;
// A double linked list collection for arbitrarily typed data entries.
IDoubleList<T> = interface
['{F1DCB6B4-4761-42AA-9FE9-4047579ADF0B}']
function GetIsEmpty: Boolean;
function GetLinks: PListEntry;
function GetCount: Integer;
property IsEmpty: Boolean read GetIsEmpty;
property Links: PListEntry read GetLinks;
property Count: Integer read GetCount;
function InsertTail(const Content: T): IDoubleListEntry<T>;
function InsertHead(const Content: T): IDoubleListEntry<T>;
function InsterAfter(const Entry: IDoubleListEntry<T>; const Content: T): IDoubleListEntry<T>;
function InsterBefore(const Entry: IDoubleListEntry<T>; const Content: T): IDoubleListEntry<T>;
function RemoveTail: IDoubleListEntry<T>;
function RemoveHead: IDoubleListEntry<T>;
procedure Remove(Entry: IDoubleListEntry<T>);
procedure RemoveAll;
function Iterate(Direction: TDoubleListDirection = ldForward): IEnumerable<IDoubleListEntry<T>>;
function ToEntryArray(Direction: TDoubleListDirection = ldForward): TArray<IDoubleListEntry<T>>;
function ToContentArray(Direction: TDoubleListDirection = ldForward): TArray<T>;
end;
DoubleList = class abstract
// Create an empty double-linked list
class function Create<T>: IDoubleList<T>; static;
end;
{ Internal-use }
TDoubleListEntry<T> = class (TInterfacedObject, IDoubleListEntry<T>)
protected
FLinks: TListEntry;
FContent: T;
function GetLinks: PListEntry;
function GetContent: T;
procedure SetContent(const Value: T);
class function LinksToObject(Links: PListEntry): IDoubleListEntry<T>; static;
constructor Create(const Content: T);
end;
TDoubleList<T> = class (TInterfacedObject, IDoubleList<T>)
protected
FLinks: TListEntry;
function GetIsEmpty: Boolean;
function GetLinks: PListEntry;
function GetCount: Integer;
function InsertTail(const Content: T): IDoubleListEntry<T>;
function InsertHead(const Content: T): IDoubleListEntry<T>;
function InsterAfter(const Entry: IDoubleListEntry<T>; const Content: T): IDoubleListEntry<T>;
function InsterBefore(const Entry: IDoubleListEntry<T>; const Content: T): IDoubleListEntry<T>;
function RemoveTail: IDoubleListEntry<T>;
function RemoveHead: IDoubleListEntry<T>;
procedure Remove(Entry: IDoubleListEntry<T>);
procedure RemoveAll;
function Iterate(Direction: TDoubleListDirection): IEnumerable<IDoubleListEntry<T>>;
function ToEntryArray(Direction: TDoubleListDirection): TArray<IDoubleListEntry<T>>;
function ToContentArray(Direction: TDoubleListDirection): TArray<T>;
constructor Create;
public
destructor Destroy; override;
end;
TDoubleListEnumerator<T> = class (TInterfacedObject,
IEnumerable<IDoubleListEntry<T>>, IEnumerator<IDoubleListEntry<T>>)
protected
FListHead: IDoubleList<T>;
FCurrent: PListEntry;
FCurrentRef: IDoubleListEntry<T>;
FDirection: TDoubleListDirection;
procedure Reset;
function MoveNext: Boolean;
function GetCurrent: TObject;
function GetCurrentT: IDoubleListEntry<T>;
function IEnumerator<IDoubleListEntry<T>>.GetCurrent = GetCurrentT;
function GetEnumerator: IEnumerator; // legacy
function GetEnumeratorT: IEnumerator<IDoubleListEntry<T>>;
function IEnumerable<IDoubleListEntry<T>>.GetEnumerator = GetEnumeratorT;
constructor Create(const List: IDoubleList<T>; Direction: TDoubleListDirection);
end;
implementation
uses
Ntapi.ntrtl;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ TDoubleListEntry }
constructor TDoubleListEntry<T>.Create;
begin
inherited Create;
FContent := Content;
end;
function TDoubleListEntry<T>.GetContent;
begin
Result := FContent;
end;
function TDoubleListEntry<T>.GetLinks;
begin
Result := @FLinks;
end;
class function TDoubleListEntry<T>.LinksToObject;
var
Obj: TDoubleListEntry<T>;
begin
Obj := Pointer(UIntPtr(Links) - UIntPtr(@TDoubleListEntry<T>(nil).FLinks));
Result := IDoubleListEntry<T>(Obj);
end;
procedure TDoubleListEntry<T>.SetContent;
begin
FContent := Value;
end;
{ TDoubleList }
constructor TDoubleList<T>.Create;
begin
inherited Create;
InitializeListHead(@FLinks);
end;
destructor TDoubleList<T>.Destroy;
begin
RemoveAll;
inherited;
end;
function TDoubleList<T>.GetCount;
var
FCurrent: PListEntry;
begin
Result := 0;
FCurrent := FLinks.Flink;
while FCurrent <> @FLinks do
begin
FCurrent := FCurrent.Flink;
Inc(Result);
end;
end;
function TDoubleList<T>.GetIsEmpty;
begin
Result := IsListEmpty(@FLinks);
end;
function TDoubleList<T>.GetLinks;
begin
Result := @FLinks;
end;
function TDoubleList<T>.InsertHead;
begin
Result := TDoubleListEntry<T>.Create(Content);
InsertHeadList(@FLinks, Result.Links);
Result._AddRef;
end;
function TDoubleList<T>.InsertTail;
begin
Result := TDoubleListEntry<T>.Create(Content);
InsertTailList(@FLinks, Result.Links);
Result._AddRef;
end;
function TDoubleList<T>.InsterAfter;
begin
Result := TDoubleListEntry<T>.Create(Content);
InsertHeadList(Entry.Links, Result.Links);
Result._AddRef;
end;
function TDoubleList<T>.InsterBefore;
begin
Result := TDoubleListEntry<T>.Create(Content);
InsertTailList(Entry.Links, Result.Links);
Result._AddRef;
end;
function TDoubleList<T>.Iterate;
begin
Result := TDoubleListEnumerator<T>.Create(Self, Direction);
end;
procedure TDoubleList<T>.Remove;
begin
RemoveEntryList(Entry.GetLinks);
Entry.Links^ := Default(TListEntry);
Entry._Release;
end;
procedure TDoubleList<T>.RemoveAll;
begin
while not GetIsEmpty do
RemoveTail;
end;
function TDoubleList<T>.RemoveHead;
begin
Result := TDoubleListEntry<T>.LinksToObject(RemoveHeadList(@FLinks));
Result.Links^ := Default(TListEntry);
Result._Release;
end;
function TDoubleList<T>.RemoveTail;
begin
Result := TDoubleListEntry<T>.LinksToObject(RemoveTailList(@FLinks));
Result.Links^ := Default(TListEntry);
Result._Release;
end;
function TDoubleList<T>.ToContentArray;
var
i: Integer;
Entry: IDoubleListEntry<T>;
begin
SetLength(Result, GetCount);
i := 0;
for Entry in Iterate(Direction) do
begin
Result[i] := Entry.Content;
Inc(i);
end;
end;
function TDoubleList<T>.ToEntryArray;
var
i: Integer;
Entry: IDoubleListEntry<T>;
begin
SetLength(Result, GetCount);
i := 0;
for Entry in Iterate(Direction) do
begin
Result[i] := Entry;
Inc(i);
end;
end;
{ TDoubleListEnumerator }
constructor TDoubleListEnumerator<T>.Create;
begin
inherited Create;
FListHead := List;
FDirection := Direction;
Reset;
end;
function TDoubleListEnumerator<T>.GetCurrent;
begin
Assert(False, 'Legacy (untyped) IEnumerator.GetCurrent not supported');
Result := nil;
end;
function TDoubleListEnumerator<T>.GetCurrentT;
begin
Result := FCurrentRef;
end;
function TDoubleListEnumerator<T>.GetEnumerator;
begin
Assert(False, 'Legacy (untyped) IEnumerable.GetEnumerator not supported');
Result := nil;
end;
function TDoubleListEnumerator<T>.GetEnumeratorT;
begin
Result := Self;
end;
function TDoubleListEnumerator<T>.MoveNext;
begin
if FDirection = ldForward then
FCurrent := FCurrent.Flink
else
FCurrent := FCurrent.Blink;
Result := Assigned(FCurrent) and (FCurrent <> FListHead.Links);
if Result then
FCurrentRef := TDoubleListEntry<T>.LinksToObject(FCurrent)
else
FCurrentRef := nil;
end;
procedure TDoubleListEnumerator<T>.Reset;
begin
FCurrent := FListHead.Links;
FCurrentRef := nil;
end;
{ DoubleList }
class function DoubleList.Create<T>;
begin
Result := TDoubleList<T>.Create;
end;
end.