-
Notifications
You must be signed in to change notification settings - Fork 2
/
lists.pas
747 lines (709 loc) · 21.5 KB
/
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit lists;
interface
uses
storable;
type
TTraversalDirection = (tdForward, tdReverse);
TStorableListFlags = set of (slOwner, { if set, frees the list contents and writes objects to the stream; otherwise, doesn't free and writes references }
slDropDuplicates); { if set, duplicates are checked for and ignored when adding; makes insertion O(N) }
PListNode = ^TListNode;
TListNode = record
Next: PListNode;
Previous: PListNode;
Value: TStorable; // XXX should be a nested item, and of type TItem, but that doesn't work
end;
// This is a linked list, O(1) when appending and removing, O(N) to search (e.g. using Contains()).
// If you enable slDropDuplicates, appending becomes O(N) (since it has to search for duplicates)
generic TStorableList<TItem> = class(TStorable) // XXX can't use constraint <TItem: TStorable> because we instantiate it with forwarded types
public
type
TEnumerator = class
protected
FCurrentListNode: PListNode;
FList: TStorableList;
FDirection: TTraversalDirection;
FAdvanced: Boolean;
strict private
function InternalGetCurrent(): TItem;
public
constructor Create(List: TStorableList; StartListNode: PListNode; Direction: TTraversalDirection);
destructor Destroy(); override;
function HasMore(): Boolean;
procedure Remove();
procedure RemoveRemainder();
{ Enumerator support }
function MoveNext(): Boolean;
property Current: TItem read InternalGetCurrent;
end;
strict private
FFlags: TStorableListFlags;
FFirstNode, FLastNode: PListNode;
FLength, FActiveEnumerators: Cardinal;
procedure InternalRemoveItem(Node: PListNode); { has to be visible by enumerator }
procedure InternalRemoveFromItem(Node: PListNode; Direction: TTraversalDirection); { has to be visible by enumerator }
procedure InternalMergeList(List: TStorableList);
procedure InternalAppendList(List: TStorableList);
function InternalGetFirst(): TItem;
{$IFOPT C+} procedure CheckLength(); {$ENDIF}
public
constructor Create(Flags: TStorableListFlags = []);
constructor Clone(Template: TStorableList; Flags: TStorableListFlags = []);
constructor Read(Stream: TReadStream); override;
procedure Write(Stream: TWriteStream); override;
destructor Destroy(); override;
procedure Empty(); { without freeing }
procedure FreeItems(); { and empty }
procedure Deduplicate();
procedure AppendItem(Item: TItem);
procedure RemoveItem(Item: TItem);
procedure AdoptItem(Enumerator: TEnumerator);
procedure AdoptList(List: TStorableList);
function GetEnumerator(const Direction: TTraversalDirection = tdForward): TEnumerator;
function Contains(Item: TItem): Boolean;
property Length: Cardinal read FLength;
property First: TItem read InternalGetFirst;
end;
implementation
uses
sysutils;
constructor TStorableList.TEnumerator.Create(List: TStorableList; StartListNode: PListNode; Direction: TTraversalDirection);
begin
inherited Create();
FList := List;
FCurrentListNode := StartListNode;
FDirection := Direction;
FAdvanced := True;
Inc(FList.FActiveEnumerators);
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
end;
destructor TStorableList.TEnumerator.Destroy();
begin
Dec(FList.FActiveEnumerators);
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
inherited;
end;
function TStorableList.TEnumerator.MoveNext(): Boolean;
begin
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
if (not FAdvanced) then
begin
Assert(Assigned(FCurrentListNode));
case FDirection of
tdForward: FCurrentListNode := FCurrentListNode^.Next;
tdReverse: FCurrentListNode := FCurrentListNode^.Previous;
end;
end
else
begin
FAdvanced := False;
end;
Result := Assigned(FCurrentListNode);
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
end;
function TStorableList.TEnumerator.HasMore(): Boolean;
begin
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
if (FAdvanced) then
begin
Result := Assigned(FCurrentListNode);
end
else
begin
Assert(Assigned(FCurrentListNode));
case FDirection of
tdForward: Result := Assigned(FCurrentListNode^.Next);
tdReverse: Result := Assigned(FCurrentListNode^.Previous);
end;
end;
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
end;
function TStorableList.TEnumerator.InternalGetCurrent(): TItem;
begin
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
Assert(not FAdvanced);
Assert(Assigned(FCurrentListNode));
Result := TItem(FCurrentListNode^.Value);
end;
procedure TStorableList.TEnumerator.Remove();
var
Node: PListNode;
{$IFOPT C+} BeforeLength: Cardinal; {$ENDIF}
begin
Assert(not FAdvanced);
Assert(Assigned(FCurrentListNode));
Assert(Assigned(FList));
Assert(FList.FActiveEnumerators = 1);
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
{$IFOPT C+} BeforeLength := FList.Length; {$ENDIF}
Node := FCurrentListNode;
case FDirection of
tdForward: FCurrentListNode := FCurrentListNode^.Next;
tdReverse: FCurrentListNode := FCurrentListNode^.Previous;
end;
FAdvanced := True;
FList.InternalRemoveItem(Node);
{$IFOPT C+} Assert(FList.Length = BeforeLength-1); {$ENDIF}
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
end;
procedure TStorableList.TEnumerator.RemoveRemainder();
begin
Assert(not FAdvanced);
Assert(Assigned(FCurrentListNode));
Assert(Assigned(FList));
Assert(FList.FActiveEnumerators = 1);
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
FList.InternalRemoveFromItem(FCurrentListNode, FDirection);
FCurrentListNode := nil;
FAdvanced := True;
{$IFOPT C+} FList.CheckLength(); {$ENDIF}
end;
constructor TStorableList.Create(Flags: TStorableListFlags = []);
begin
inherited Create();
Assert(not ((slOwner in Flags) and (slDropDuplicates in Flags)));
FFlags := Flags;
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
destructor TStorableList.Destroy();
begin
Assert(FActiveEnumerators = 0);
{$IFOPT C+} CheckLength(); {$ENDIF}
if (slOwner in FFlags) then
FreeItems()
else
Empty();
{$IFOPT C+} CheckLength(); {$ENDIF}
inherited;
end;
procedure TStorableList.FreeItems();
var
CurrentNode: PListNode;
begin
Assert(slOwner in FFlags);
{$IFOPT C+} CheckLength(); {$ENDIF}
CurrentNode := FFirstNode;
while (Assigned(CurrentNode)) do
begin
FFirstNode := CurrentNode^.Next;
CurrentNode^.Value.Free();
Dispose(CurrentNode);
CurrentNode := FFirstNode;
end;
Assert(not Assigned(FFirstNode));
FLastNode := nil;
FLength := 0;
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
procedure TStorableList.Empty();
var
CurrentNode: PListNode;
begin
{$IFOPT C+} CheckLength(); {$ENDIF}
CurrentNode := FFirstNode;
while (Assigned(CurrentNode)) do
begin
FFirstNode := CurrentNode^.Next;
Dispose(CurrentNode);
CurrentNode := FFirstNode;
end;
Assert(not Assigned(FFirstNode));
FLastNode := nil;
FLength := 0;
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
procedure TStorableList.Deduplicate();
var
Scan, Search: PListNode;
begin
Assert(not (slDropDuplicates in FFlags)); { because why would you call this otherwise? }
Assert(not (slOwner in FFlags)); { because slOwner is always slDropDuplicates }
{$IFOPT C+} CheckLength(); {$ENDIF}
if (not Assigned(FFirstNode)) then
Exit;
Scan := FFirstNode^.Next;
while (Assigned(Scan)) do
begin
Search := FFirstNode;
while (Search^.Value <> Scan^.Value) do
Search := Search^.Next;
if (Search <> Scan) then
begin
Scan^.Previous^.Next := Scan^.Next;
if (Assigned(Scan^.Next)) then
begin
Scan^.Next^.Previous := Scan^.Previous;
end
else
begin
Assert(Scan = FLastNode);
FLastNode := Scan^.Previous;
end;
Search := Scan;
Scan := Scan^.Next;
Dispose(Search);
Dec(FLength);
end
else
begin
Scan := Scan^.Next;
end;
end;
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
procedure TStorableList.Write(Stream: TWriteStream);
var
CurrentNode: PListNode;
{$IFOPT C+} WriteLength: Cardinal; {$ENDIF}
begin
inherited;
Assert(SizeOf(FFlags) <= SizeOf(Cardinal));
{$IFOPT C+} CheckLength(); {$ENDIF}
Stream.WriteCardinal(Cardinal(FFlags));
Stream.WriteCardinal(FLength);
{$IFOPT C+} WriteLength := 0; {$ENDIF}
CurrentNode := FFirstNode;
while (Assigned(CurrentNode)) do
begin
Assert(Assigned(CurrentNode^.Value));
if (slOwner in FFlags) then
Stream.WriteObject(TStorable(CurrentNode^.Value))
else
Stream.WriteReference(CurrentNode^.Value);
CurrentNode := CurrentNode^.Next;
{$IFOPT C+} Inc(WriteLength); {$ENDIF}
end;
Stream.WriteSentinel();
{$IFOPT C+} Assert(WriteLength = FLength); {$ENDIF}
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
procedure TStorableList.InternalRemoveItem(Node: PListNode);
begin
Assert(Assigned(FFirstNode));
Assert(Assigned(FLastNode));
Assert(Assigned(Node));
{$IFOPT C+} CheckLength(); {$ENDIF}
if (Assigned(Node^.Previous)) then
Node^.Previous^.Next := Node^.Next
else
FFirstNode := Node^.Next;
if (Assigned(Node^.Next)) then
Node^.Next^.Previous := Node^.Previous
else
FLastNode := Node^.Previous;
Dispose(Node);
Dec(FLength);
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
procedure TStorableList.InternalRemoveFromItem(Node: PListNode; Direction: TTraversalDirection);
var
Garbage: PListNode;
begin
Assert(Assigned(FFirstNode));
Assert(Assigned(FLastNode));
Assert(Assigned(Node));
{$IFOPT C+} CheckLength(); {$ENDIF}
case Direction of
tdForward:
begin
if (Assigned(Node^.Previous)) then
begin
Node^.Previous^.Next := nil;
FLastNode := Node^.Previous;
while (Assigned(Node)) do
begin
Garbage := Node;
Node := Node^.Next;
Dispose(Garbage);
Dec(FLength);
end;
end
else
Empty();
end;
tdReverse:
begin
if (Assigned(Node^.Next)) then
begin
Node^.Next^.Previous := nil;
FFirstNode := Node^.Next;
while (Assigned(Node)) do
begin
Garbage := Node;
Node := Node^.Previous;
Dispose(Garbage);
Dec(FLength);
end;
end
else
Empty();
end;
end;
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
{$IFOPT C+}
procedure TStorableList.CheckLength();
var
Node: PListNode;
TestLength: Cardinal;
begin
Node := FFirstNode;
TestLength := 0;
while (Assigned(Node)) do
begin
Inc(TestLength);
Node := Node^.Next;
end;
Assert(TestLength = FLength);
Node := FLastNode;
TestLength := 0;
while (Assigned(Node)) do
begin
Inc(TestLength);
Node := Node^.Previous;
end;
Assert(TestLength = FLength);
end;
{$ENDIF}
constructor TStorableList.Clone(Template: TStorableList; Flags: TStorableListFlags = []);
var
TheirNode, OurNode: PListNode;
begin
inherited;
Assert(Assigned(Template));
Assert(Template is Self.ClassType);
Assert((not (slOwner in Flags)) or (not (slOwner in Template.FFlags)));
{$IFOPT C+} CheckLength(); {$ENDIF}
FFlags := Flags;
TheirNode := Template.FFirstNode;
while (Assigned(TheirNode)) do
begin
New(OurNode);
OurNode^.Value := TheirNode^.Value;
OurNode^.Previous := FLastNode;
OurNode^.Next := nil;
if (Assigned(FLastNode)) then
begin
Assert(Assigned(FFirstNode));
FLastNode^.Next := OurNode;
end
else
begin
FFirstNode := OurNode;
end;
FLastNode := OurNode;
TheirNode := TheirNode^.Next;
{$IFOPT C+} Inc(FLength); {$ENDIF}
end;
Assert(FLength = Template.Length);
FLength := Template.Length;
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
constructor TStorableList.Read(Stream: TReadStream);
var
ReadLength, Index: Cardinal;
begin
inherited;
{$IFOPT C+} CheckLength(); {$ENDIF}
FFlags := TStorableListFlags(Stream.ReadCardinal());
ReadLength := Stream.ReadCardinal();
if (ReadLength > 0) then
begin
if (slOwner in FFlags) then
begin
for Index := 0 to ReadLength-1 do // $R-
AppendItem(TItem(Stream.ReadObject()));
end
else
begin
for Index := 0 to ReadLength-1 do // $R-
begin
AppendItem(nil);
Assert(Assigned(FFirstNode));
Assert(Assigned(FLastNode));
Stream.ReadReference(@Pointer(FLastNode^.Value));
end;
end;
end;
Stream.VerifySentinel();
Assert(FLength = ReadLength);
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
procedure TStorableList.AppendItem(Item: TItem);
var
NewNode: PListNode;
begin
Assert((not Assigned(Pointer(Item))) or (Item is TItem.ClassType));
Assert(FActiveEnumerators = 0);
{$IFOPT C+}
if (slOwner in FFlags) then
Assert(not Contains(Item));
{$ENDIF}
{$IFOPT C+} CheckLength(); {$ENDIF}
if (slDropDuplicates in FFlags) then
begin
if (Contains(Item)) then
Exit;
end;
New(NewNode);
NewNode^.Value := Item;
NewNode^.Previous := FLastNode;
if (Assigned(FLastNode)) then
begin
Assert(Assigned(FFirstNode));
FLastNode^.Next := NewNode;
end
else
begin
Assert(not Assigned(FFirstNode));
FFirstNode := NewNode;
end;
NewNode^.Next := nil;
FLastNode := NewNode;
Inc(FLength);
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
procedure TStorableList.RemoveItem(Item: TItem);
var
Node: PListNode;
begin
Assert(Assigned(Pointer(Item)), 'TStorableList.RemoveItem called with nil item.');
Assert(Item is TItem.ClassType, 'TStorableList.RemoveItem called with item that is of the wrong class for this list.');
Assert(Assigned(FFirstNode), 'TStorableList.RemoveItem called when there is no first node.');
Assert(Assigned(FLastNode), 'TStorableList.RemoveItem called when there is no last node.');
Assert(FActiveEnumerators = 0, 'TStorableList.RemoveItem called while there is an active enumerator.');
{$IFOPT C+} CheckLength(); {$ENDIF}
Node := FFirstNode;
while ({$IFOPT C+} Assigned(Node) and {$ENDIF} (Node^.Value <> Item)) do
Node := Node^.Next;
Assert(Assigned(Node));
InternalRemoveItem(Node);
{$IFOPT C+}
if ((slOwner in FFlags) or (slDropDuplicates in FFlags)) then
Assert(not Contains(Item));
{$ENDIF}
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
procedure TStorableList.AdoptItem(Enumerator: TEnumerator);
var
Node: PListNode;
begin
Assert(Assigned(Pointer(Enumerator)));
Assert(not Enumerator.FAdvanced);
Assert(Assigned(Enumerator.FCurrentListNode));
Assert(Enumerator.FCurrentListNode^.Value is TItem.ClassType);
Assert(Assigned(Enumerator.FList));
Assert(Enumerator.FList is Self.ClassType, 'Enumerator.FList has class ' + Enumerator.FList.ClassName + ' rather than expected type ' + Self.ClassName);
Assert(Enumerator.FList <> Self);
Assert(Enumerator.FList.FActiveEnumerators = 1);
Assert(Enumerator.FList.FFlags = FFlags);
Assert(FActiveEnumerators = 0);
Assert(not (slDropDuplicates in FFlags), 'AdoptItem() doesn''t yet check for duplicates');
{$IFOPT C+} CheckLength(); {$ENDIF}
Node := Enumerator.FCurrentListNode;
{$IFOPT C+}
if (slOwner in FFlags) then
Assert(not Contains(TItem(Node^.Value)));
{$ENDIF}
{ First update iterator }
case TTraversalDirection(Enumerator.FDirection) of
tdForward: Enumerator.FCurrentListNode := Enumerator.FCurrentListNode^.Next;
tdReverse: Enumerator.FCurrentListNode := Enumerator.FCurrentListNode^.Previous;
end;
Enumerator.FAdvanced := True;
{ Then update source list }
if (not Assigned(Node^.Previous)) then
begin
{ item is at start of list }
if (not Assigned(Node^.Next)) then
begin
{ item is alone }
Assert(Enumerator.FList.FLength = 1);
Enumerator.FList.FFirstNode := nil;
Enumerator.FList.FLastNode := nil;
end
else
begin
Assert(Enumerator.FList.FLength > 1);
Enumerator.FList.FFirstNode := Node^.Next;
Node^.Next^.Previous := nil;
end;
end
else
if (not Assigned(Node^.Next)) then
begin
{ item is at end of list }
Assert(Enumerator.FList.FLength > 1);
Assert(Assigned(Node^.Previous));
Enumerator.FList.FLastNode := Node^.Previous;
Node^.Previous^.Next := nil;
end
else
begin
{ item is in middle }
Assert(Enumerator.FList.FLength >= 3);
Assert(Assigned(Node^.Next));
Assert(Assigned(Node^.Previous));
Node^.Previous^.Next := Node^.Next;
Node^.Next^.Previous := Node^.Previous;
end;
Dec(Enumerator.FList.FLength);
{ Finally, update us }
if (Assigned(FLastNode)) then
begin
Assert(Assigned(FFirstNode));
FLastNode^.Next := Node;
end
else
if (not Assigned(FFirstNode)) then
begin
Assert(not Assigned(FLastNode));
FFirstNode := Node;
end;
Node^.Previous := FLastNode;
Node^.Next := nil;
FLastNode := Node;
Inc(FLength);
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
procedure TStorableList.AdoptList(List: TStorableList);
begin
Assert(List is Self.ClassType);
Assert(List.FActiveEnumerators = 0);
{$IFOPT C+} CheckLength(); {$ENDIF}
if (slDropDuplicates in FFlags) then
InternalMergeList(List)
else
InternalAppendList(List);
Assert(List.FLength = 0);
Assert(not Assigned(List.FFirstNode));
Assert(not Assigned(List.FLastNode));
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
{ if this becomes a bottleneck, maybe a hashtable would help }
procedure TStorableList.InternalMergeList(List: TStorableList);
var
LastOriginal, CurrentOriginal, CurrentCandidate, NextCandidate: PListNode;
begin
Assert(List is Self.ClassType);
Assert(List.FFlags = FFlags);
{$IFOPT C+} CheckLength(); {$ENDIF}
if (Assigned(List.FFirstNode)) then
begin
Assert(Assigned(List.FLastNode));
Assert(List.FLength > 0);
if (Assigned(FLastNode)) then
begin
Assert(Assigned(FFirstNode));
LastOriginal := FLastNode;
CurrentCandidate := List.FFirstNode;
while (Assigned(CurrentCandidate)) do
begin
NextCandidate := CurrentCandidate^.Next;
CurrentOriginal := LastOriginal;
while ((Assigned(CurrentOriginal)) and (CurrentOriginal^.Value <> CurrentCandidate^.Value)) do
CurrentOriginal := CurrentOriginal^.Previous;
if (Assigned(CurrentOriginal)) then
begin
Assert(CurrentOriginal^.Value = CurrentCandidate^.Value);
Dispose(CurrentCandidate);
end
else
begin
FLastNode^.Next := CurrentCandidate;
CurrentCandidate^.Previous := FLastNode;
CurrentCandidate^.Next := nil;
FLastNode := CurrentCandidate;
end;
CurrentCandidate := NextCandidate;
end;
end
else
begin
Assert(not Assigned(FFirstNode));
FFirstNode := List.FFirstNode;
FLastNode := List.FLastNode;
Inc(FLength, List.FLength);
end;
List.FFirstNode := nil;
List.FLastNode := nil;
List.FLength := 0;
end
else
begin
Assert(not Assigned(List.FLastNode));
Assert(List.FLength = 0);
end;
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
procedure TStorableList.InternalAppendList(List: TStorableList);
{$IFOPT C+}
var
Item: PListNode;
{$ENDIF}
begin
{$IFOPT C+} CheckLength(); {$ENDIF}
if (Assigned(List.FFirstNode)) then
begin
{$IFOPT C+}
if (slOwner in FFlags) then
begin
Item := List.FFirstNode;
while (Assigned(Item)) do
begin
Assert(not Contains(TItem(Item^.Value)));
Item := Item^.Next;
end;
end;
{$ENDIF}
Assert(Assigned(List.FLastNode));
Assert(List.FLength > 0);
if (Assigned(FLastNode)) then
begin
FLastNode^.Next := List.FFirstNode;
List.FFirstNode^.Previous := FLastNode;
end
else
begin
FFirstNode := List.FFirstNode;
end;
FLastNode := List.FLastNode;
Inc(FLength, List.FLength);
List.FFirstNode := nil;
List.FLastNode := nil;
List.FLength := 0;
end
else
begin
Assert(not Assigned(List.FLastNode));
Assert(List.FLength = 0);
end;
{$IFOPT C+} CheckLength(); {$ENDIF}
end;
function TStorableList.Contains(Item: TItem): Boolean;
var
Candidate: PListNode;
begin
Assert(Item is TItem.ClassType);
{$IFOPT C+} CheckLength(); {$ENDIF}
Candidate := FFirstNode;
while (Assigned(Candidate) and (Candidate^.Value <> Item)) do
Candidate := Candidate^.Next;
Result := Assigned(Candidate);
end;
function TStorableList.GetEnumerator(const Direction: TTraversalDirection = tdForward): TEnumerator;
begin
{$IFOPT C+} CheckLength(); {$ENDIF}
case Direction of
tdForward: Result := TEnumerator.Create(Self, FFirstNode, Direction);
tdReverse: Result := TEnumerator.Create(Self, FLastNode, Direction);
end;
end;
function TStorableList.InternalGetFirst(): TItem;
begin
Assert(Assigned(FFirstNode));
{$IFOPT C+} CheckLength(); {$ENDIF}
Result := TItem(FFirstNode^.Value);
end;
end.