-
Notifications
You must be signed in to change notification settings - Fork 1
/
adtlist_impl.i
1772 lines (1526 loc) · 48.4 KB
/
adtlist_impl.i
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{@discard
This file is a part of the PascalAdt library, which provides
commonly used algorithms and data structures for the FPC and Delphi
compilers.
Copyright (C) 2004, 2005 by Lukasz Czajka
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA }
{@discard
adtlist_impl.inc::prefix:&_mcp_prefix&::item_type:&ItemType&
}
&include adtlist.defs
&include adtlist_impl.mcp
(* Notes on the parts of implementation common to all lists:
* Every list in this module has, in addition to fields needed to manage the
* representation itself, FSize and FValidSize fields. These are responsible
* of keeping track of the number of Items stored in the list. The basic idea
* is that FSize keeps the exact number of Items, so it is incremented when
* new item is added and decremented when Items are removed. However,
* it is not always possible to know how many Items are added. Such situation
* occurs when moving a range of Items from one list to another (or the same).
* There is no way of knowing how many Items are in a range, excepty for iterating
* through all its Items, which is too expensive. In such situation FValidSize
* fields of both lists are set to false to indicate that FSize fields do not
* show a proper number of Items. Next time, when Size method is called and
* FValidSize is false it recalculates the number of Items in the list by
* iterating through all Items, updates FSize field and sets FValidSize back
* to true. This takes O(n) time, but happens rarely, so we can say that the amortized
* complexity of Size operation is O(1).
*)
{ **************************************************************************** }
{ Singly linked list }
{ **************************************************************************** }
(* Notes on implementation of TSingleList:
* TSingleList is implemented using a header (FStartNode), which itself does not
* contain any data (FStartNode^.Item = DefaultItem). The position in the list is
* represented by the pointer to the node _before_ the desired one, e.g.
* data for the first node is stored in FStartNode^.Next^.Item. The 'one beyond
* last' node is also stored (as FFinishNode) and its Next field is nil.
* TSingleList graphically:
* node 1: node 2: node n: node n+1: node F: (the one
* +-----+ +-----+ +-----+ +-----+ +-----+ beyond
* | nil | -> | 1 | -> ... -> | n-1 | -> | n | -> ... -> | F-1 | the last
* +-----+ +-----+ +-----+ +-----+ +-----+ node)
*
* The number inside each node represents item field; it specifies which node's
* data is stored there.
* F - one beyond last node (FFinishNode)
* Node 1 is the first node (FStartNode).
* Arrows show which node points to which (via its Next field).
*)
{----------------------------- TSingleList members -------------------------------}
constructor TSingleList.Create;
begin
inherited Create;
InitFields;
end;
constructor TSingleList.CreateCopy(const cont : TSingleList;
const itemCopier : IUnaryFunctor);
var
pnode : PSingleListNode;
begin
inherited CreateCopy(cont);
InitFields;
if itemCopier <> nil then
begin
pnode := cont.FStartNode;
FSize := 0;
while pnode <> cont.FFinishNode do
begin
NewNode(FFinishNode^.Next);
FFinishNode := FFinishNode^.Next;
{ in case of an exception... }
FFinishNode^.Next := nil;
FFinishNode^.Item := DefaultItem;
FFinishNode^.Item := ItemCopier.Perform(pnode^.Next^.Item);
Inc(FSize);
pnode := pnode^.Next;
end;
cont.FValidSize := true;
cont.FSize := FSize;
end;
end;
destructor TSingleList.Destroy;
begin
{ if FStartNode is not nil then the object was fully constructed
(nothing can raise an exception except for the allocation of
FStartNode) }
if FStartNode <> nil then
begin
Clear;
DisposeNode(FStartNode);
end;
inherited;
end;
procedure TSingleList.InitFields;
begin
NewNode(FStartNode);
FStartNode^.Item := DefaultItem;
FStartNode^.Next := nil;
FFinishNode := FStartNode;
FValidSize := true;
FSize := 0;
end;
procedure TSingleList.DisposeNodeAndItem(node : PSingleListNode);
begin
DisposeItem(node^.Item); { may throw, but it's harmless }
DisposeNode(node);
end;
procedure TSingleList.DoSetItem(pos : PSingleListNode; aitem : ItemType);
begin
with pos^.Next^ do
begin
DisposeItem(Item);
Item := aitem;
end;
end;
{ does not maintain FSize field ! }
procedure TSingleList.DoMove(dest, source1, source2 : PSingleListNode;
list2 : TSingleList);
var
temp : PSingleListNode;
begin
Assert((dest <> nil) and (source1 <> nil) and (source2 <> nil),
msgInvalidIterator);
Assert(source1^.Next <> nil, msgInvalidIteratorRange);
if (dest = source1) or (dest = source2) then
Exit;
temp := dest^.Next;
dest^.Next := source1^.Next;
source1^.Next := source2^.Next;
source2^.Next := temp;
if (source1^.Next = nil) then
list2.FFinishNode := source1;
if (source2^.Next = nil) then
FFinishNode := source2;
end;
function TSingleList.CopySelf(const ItemCopier : IUnaryFunctor) : TContainerAdt;
begin
Result := TSingleList.CreateCopy(self, ItemCopier);
end;
procedure TSingleList.Swap(cont : TContainerAdt);
var
ls2 : TSingleList;
begin
if cont is TSingleList then
begin
BasicSwap(cont);
ls2 := TSingleList(cont);
ExchangePtr(FStartNode, ls2.FStartNode);
ExchangePtr(FFinishNode, ls2.FFinishNode);
ExchangeData(FSize, ls2.FSize, SizeOf(SizeType));
ExchangeData(FValidSize, ls2.FValidSize, SizeOf(Boolean));
end else
inherited;
end;
function TSingleList.ForwardStart : TForwardIterator;
begin
Result := TSingleListIterator.Create(FStartNode, self);
end;
function TSingleList.ForwardFinish : TForwardIterator;
begin
Result := TSingleListIterator.Create(FFinishNode, self);
end;
function TSingleList.Start : TSingleListIterator;
begin
Result := TSingleListIterator.Create(FStartNode, self);
end;
function TSingleList.Finish : TSingleListIterator;
begin
Result := TSingleListIterator.Create(FFinishNode, self);
end;
procedure TSingleList.Insert(pos : TForwardIterator; aitem : ItemType);
begin
Assert((pos is TSingleListIterator) and (pos.Owner = self), msgInvalidIterator);
InsertNode(TSingleListIterator(pos).Node, aitem);
end;
procedure TSingleList.Delete(pos : TForwardIterator);
var
aitem : ItemType;
begin
Assert(pos is TSingleListIterator, msgInvalidIterator);
Assert(pos.Owner = self, msgWrongOwner);
aitem := ExtractNode(TSingleListIterator(pos).Node);
DisposeItem(aitem);
pos.Destroy;
end;
function TSingleList.Delete(astart, afinish : TForwardIterator) : SizeType;
var
pos, fin, npos : PSingleListNode;
begin
Assert((astart is TSingleListIterator) and (afinish is TSingleListIterator),
msgInvalidIterator);
Assert((TSingleListIterator(astart).Node <> nil) and
(TSingleListIterator(afinish).Node <> nil), msgInvalidIterator);
Result := FSize;
pos := TSingleListIterator(astart).Node;
fin := TSingleListIterator(afinish).Node^.Next;
if fin = nil then
FFinishNode := pos;
while (pos^.Next <> fin) do
begin
DisposeItem(pos^.Next^.Item);
Dec(FSize);
npos := pos^.Next^.Next;
DisposeNode(pos^.Next);
pos^.Next := npos;
end;
Result := Result - FSize;
end;
function TSingleList.Extract(pos : TForwardIterator) : ItemType;
begin
Assert(pos is TSingleListIterator, msgInvalidIterator);
Assert(pos.Owner = self, msgWrongOwner);
Result := ExtractNode(TSingleListIterator(pos).Node);
pos.Destroy;
end;
procedure TSingleList.Move(Source, Dest : TForwardIterator);
var
temp : PSingleListNode;
list2 : TSingleList;
begin
Assert(dest is TSingleListIterator, msgInvalidIterator);
Assert(Source is TSingleListIterator, msgInvalidIterator);
Assert(dest.Owner = self, msgWrongOwner);
list2 := TSingleListIterator(Source).FList;
temp := TSingleListIterator(source).Node;
DoMove(TSingleListIterator(dest).Node, temp, temp^.Next, list2);
Inc(FSize);
Dec(list2.FSize);
end;
procedure TSingleList.Move(SourceStart, SourceFinish, Dest : TForwardIterator);
var
list2 : TSingleList;
source1, source2 : PSingleListNode;
begin
Assert(Dest is TSingleListIterator, msgInvalidIterator);
Assert(SourceStart is TSingleListIterator, msgInvalidIterator);
Assert(SourceFinish is TSingleListIterator, msgInvalidIterator);
Assert(dest.Owner = self, msgWrongOwner);
Assert(SourceStart.Owner = SourceFinish.Owner, msgWrongRangeOwner);
Assert((SourceStart.Owner <> Dest.Owner) or
not (Less(SourceStart, Dest) and Less(Dest, SourceFinish)),
msgMovingBadRange);
list2 := TSingleListIterator(SourceFinish).FList;
source1 := TSingleListIterator(SourceStart).Node;
source2 := TSingleListIterator(SourceFinish).Node;
if source1 = source2 then { special case - empty range }
Exit;
DoMove(TSingleListIterator(Dest).Node, source1, source2, list2);
if SourceStart.Owner <> self then
begin
FValidSize := false;
list2.FValidSize := false;
end;
end;
function TSingleList.Front : ItemType;
begin
Assert(FStartNode^.Next <> nil, msgReadEmpty);
Result := FStartNode^.Next^.Item;
end;
function TSingleList.Back : ItemType;
begin
Assert(FStartNode^.Next <> nil, msgReadEmpty);
Result := FFinishNode^.Item;
end;
procedure TSingleList.PushBack(aitem : ItemType);
begin
NewNode(FFinishNode^.Next);
FFinishNode := FFinishNode^.Next;
FFinishNode^.Item := aitem;
FFinishNode^.Next := nil;
Inc(FSize);
end;
procedure TSingleList.PushFront(aitem : ItemType);
var
temp : PSingleListNode;
begin
NewNode(temp);
temp^.Next := FStartNode;
FStartNode^.Item := aitem;
FStartNode := temp;
Inc(FSize);
end;
procedure TSingleList.PopBack;
var
prev : PSingleListNode;
begin
Assert(FStartNode^.Next <> nil, msgPopEmpty);
prev := FStartNode;
while prev^.Next <> FFinishNode do
begin
prev := prev^.Next
end;
DisposeNodeAndItem(FFinishNode);
prev^.Next := nil;
FFinishNode := prev;
Dec(FSize);
end;
procedure TSingleList.PopFront;
var
temp : PSingleListNode;
begin
Assert(FStartNode^.Next <> nil, msgPopEmpty);
temp := FStartNode^.Next^.Next;
DisposeNodeAndItem(FStartNode^.Next);
FStartNode^.Next := temp;
if FStartNode^.Next = nil then
FFinishNode := FStartNode;
Dec(FSize);
end;
procedure TSingleList.Clear;
var
temp, dnode : PSingleListNode;
begin
dnode := FStartNode^.Next;
while dnode <> nil do
begin
temp := dnode;
dnode := dnode^.Next;
DisposeNodeAndItem(temp);
end;
FStartNode^.Next := nil;
FFinishNode := FStartNode;
FSize := 0;
FValidSize := true;
{ destroy all iterators into container, as they are not valid anyway }
GrabageCollector.FreeObjects;
end;
function TSingleList.Empty : Boolean;
begin
Result := (FStartNode^.Next = nil);
end;
function TSingleList.Size : SizeType;
var
temp : PSingleListNode;
begin
{$ifdef DEBUG_PASCAL_ADT }
Assert(SizeCanRecalc or FValidSize, msgMoveNotUpdate);
SizeCanRecalc := true;
{$endif DEBUG_PASCAL_ADT }
if not FValidSize then
begin
FSize := 0;
temp := FStartNode;
while temp^.Next <> nil do
begin
Inc(FSize);
temp := temp^.Next;
end;
FValidSize := true;
end;
Result := FSize;
end;
function TSingleList.IsDefinedOrder : Boolean;
begin
Result := false;
end;
function TSingleList.InsertNode(pos : PSingleListNode;
aitem : ItemType) : PSingleListNode;
begin
Assert(pos <> nil, msgInvalidIterator);
{$warnings off }
NewNode(Result);
{$warnings on }
Result^.Next := pos^.Next;
Result^.Item := aitem;
pos^.Next := Result;
if (Result^.Next = nil) then
FFinishNode := Result;
Inc(FSize);
end;
function TSingleList.ExtractNode(pos : PSingleListNode) : ItemType;
var
todispose : PSingleListNode;
begin
Assert((pos <> nil) and (pos^.Next <> nil), msgInvalidIterator);
todispose := pos^.Next;
Result := pos^.Next^.Item;
pos^.Next := pos^.Next^.Next;
if (pos^.Next = nil) then
FFinishNode := pos;
DisposeNode(todispose);
Dec(FSize);
end;
procedure TSingleList.NewNode(var node : PSingleListNode);
begin
New(node);
end;
procedure TSingleList.DisposeNode(node : PSingleListNode);
begin
Dispose(node);
end;
{ ----------------------- TSingleListIterator members ----------------------- }
constructor TSingleListIterator.Create(xnode : PSingleListNode;
list : TSingleList);
begin
inherited Create(list);
Node := xnode;
FList := list;
end;
function TSingleListIterator.CopySelf : TIterator;
begin
Result := TSingleListIterator.Create(Node, FList);
end;
procedure TSingleListIterator.Advance;
begin
Assert((Node <> nil) and (Node^.Next <> nil), msgAdvancingFinishIterator);
Node := Node^.Next;
end;
function TSingleListIterator.GetItem : ItemType;
begin
Assert((Node <> nil) and (Node^.Next <> nil), msgDereferencingInvalidIterator);
Result := Node^.Next^.Item;
end;
procedure TSingleListIterator.SetItem(aitem : ItemType);
begin
Assert((Node <> nil) and (Node^.Next <> nil), msgDereferencingInvalidIterator);
FList.DoSetItem(Node, aitem);
end;
procedure TSingleListIterator.ExchangeItem(iter : TIterator);
var
aitem : ItemType;
xnode : PSingleListNode;
begin
Assert((Node <> nil) and (Node^.Next <> nil), msgInvalidIterator);
if iter is TSingleListIterator then
begin
xnode := TSingleListIterator(iter).Node;
Assert((xnode <> nil) and (xnode^.Next <> nil), msgInvalidIterator);
aitem := xnode^.Next^.Item;
xnode^.Next^.Item := Node^.Next^.Item;
Node^.Next^.Item := aitem;
end else
DoExchangeItem(iter);
end;
function TSingleListIterator.Extract : ItemType;
begin
Result := FList.ExtractNode(Node);
end;
function TSingleListIterator.Delete(afinish : TForwardIterator) : SizeType;
begin
Result := FList.Delete(self, afinish);
{ Node is made to point at finish in this routine }
end;
procedure TSingleListIterator.Insert(aitem : ItemType);
begin
FList.InsertNode(Node, aitem);
end;
function TSingleListIterator.Equal(const pos : TIterator) : Boolean;
begin
Assert((Node <> nil) and (pos is TSingleListIterator), msgInvalidIterator);
Assert(TSingleListIterator(pos).Node <> nil, msgInvalidIterator);
Result := (Node = TSingleListIterator(pos).Node);
end;
function TSingleListIterator.Owner : TContainerAdt;
begin
Result := FList;
end;
function TSingleListIterator.IsStart : Boolean;
begin
Result := Node = FList.FStartNode;
end;
function TSingleListIterator.IsFinish : Boolean;
begin
Result := (Node = FList.FFinishNode);
end;
{ **************************************************************************** }
{ Doubly linked list }
{ **************************************************************************** }
(* Notes on implementation of TDoubleList:
* The list is represented by a header - FStartNode and implemented as a circular list.
* The one beyond last node's Next field points to FStartNode and the first node's Prev
* field points to the one beyond last node. Unlike TSingleList, the header node (FStartNode)
* contains an item, because a position is represented by a pointer to the desired
* node (not the one before). However, the 'one beyond last' node (FStartNode^.Prev)
* does not contain an item; it is only used to somehow indicate the end of a
* sequence. An empty list is represented by a single node, whose Prev and Next
* fields point to themselves.
* TDoubleList graphically:
* node 1: node 2: node n: node F: (the one beyond last one)
* +-------+ +-------+ +-------+ +-------+
* <- | L | 2 | -> | 1 | 3 | -> ... -> |n-1|n+1| -> ... -> |L-1| 1 | -> (this node does not
* -> +-------+ <- +-------+ <- <- +-------+ <- <- +-------+ <- cointain an item,
* P N P N P N P N its aitem field is nil)
* P - Prev field; N - Next field; F - 'one beyond last' node;
* Node 1 is the first node (FStartNode).
* Each node stores its own item, except for node F, which does not have an
* item and contains DefaultItem in the aitem field.
*)
{ ----------------------------- TDoubleList members ----------------------------- }
constructor TDoubleList.Create;
begin
inherited Create;
InitFields;
end;
constructor TDoubleList.CreateCopy(const cont : TDoubleList;
const itemCopier : IUnaryFunctor);
var
pnode, pnode2 : PDoubleListNode;
begin
inherited CreateCopy(cont);
InitFields;
if itemCopier <> nil then
begin
pnode := cont.FStartNode;
pnode2 := FStartNode;
try
while pnode^.Next <> cont.FStartNode do
begin
pnode2^.Next := nil; { in case of an exception }
NewNode(pnode2^.Next); { may raise }
pnode2^.Next^.Prev := pnode2;
pnode2^.Item := ItemCopier.Perform(pnode^.Item); { may raise }
pnode2 := pnode2^.Next;
pnode := pnode^.Next;
Inc(FSize);
end;
pnode2^.Next := nil;
finally
if pnode2^.Next <> nil then
DisposeNode(pnode2^.Next);
pnode2^.Next := FStartNode;
FStartNode^.Prev := pnode2;
pnode2^.Item := DefaultItem;
end;
cont.FSize := FSize;
cont.FValidSize := true;
end;
end;
destructor TDoubleList.Destroy;
begin
if FStartNode <> nil then
begin
Clear;
DisposeNode(FStartNode);
end;
inherited;
end;
function TDoubleList.GetFinishNode : PDoubleListNode;
begin
Result := FStartNode^.Prev;
end;
procedure TDoubleList.InitFields;
begin
NewNode(FStartNode);
FStartNode^.Item := DefaultItem;
FStartNode^.Next := FStartNode;
FStartNode^.Prev := FStartNode;
FValidSize := true;
end;
procedure TDoubleList.DisposeNodeAndItem(node : PDoubleListNode);
begin
DisposeItem(node^.Item);
DisposeNode(node);
end;
procedure TDoubleList.DoSetItem(pos : PDoubleListNode; aitem : ItemType);
begin
DisposeItem(pos^.Item);
pos^.Item := aitem;
end;
procedure TDoubleList.DoMove(dest, source1, source2 : PDoubleListNode;
list2 : TDoubleList);
var
temp : PDoubleListNode;
begin
Assert((source1 <> nil) and (dest <> nil) and (source2 <> nil),
msgInvalidIterator);
Assert(source1 <> source2);
if (dest = source1) or (dest = source2) then
Exit;
temp := dest^.Prev;
dest^.Prev := source2^.Prev;
source1^.Prev^.Next := source2;
source2^.Prev := source1^.Prev;
source1^.Prev := temp;
temp^.Next := source1;
dest^.Prev^.Next := dest;
if (list2.FStartNode = source1) then
list2.FStartNode := source2;
if FStartNode = dest then
FStartNode := source1;
end;
function TDoubleList.CopySelf(const ItemCopier : IUnaryFunctor) : TContainerAdt;
begin
Result := TDoubleList.CreateCopy(self, ItemCopier);
end;
procedure TDoubleList.Swap(cont : TContainerAdt);
var
ls2 : TDoubleList;
begin
if cont is TDoubleList then
begin
BasicSwap(cont);
ls2 := TDoubleList(cont);
ExchangePtr(FStartNode, ls2.FStartNode);
ExchangeData(FSize, ls2.FSize, SizeOf(SizeType));
ExchangeData(FValidSize, ls2.FValidSize, SizeOf(Boolean));
end else
inherited;
end;
function TDoubleList.ForwardStart : TForwardIterator;
begin
Result := Start;
end;
function TDoubleList.ForwardFinish : TForwardIterator;
begin
Result := Finish;
end;
function TDoubleList.Start : TDoubleListIterator;
begin
Result := TDoubleListIterator.Create(FStartNode, self);
end;
function TDoubleList.Finish : TDoubleListIterator;
begin
Result := TDoubleListIterator.Create(FStartNode^.Prev, self);
end;
procedure TDoubleList.Insert(pos : TForwardIterator; aitem : ItemType);
begin
Assert(pos is TDoubleListIterator, msgInvalidIterator);
Assert(pos.Owner = self, msgWrongOwner);
InsertNode(TDoubleListIterator(pos).Node, aitem);
end;
procedure TDoubleList.Delete(pos : TForwardIterator);
var
aitem : ItemType;
begin
Assert(pos is TDoubleListIterator, msgInvalidIterator);
Assert(pos.Owner = self, msgWrongOwner);
aitem := ExtractNode(TDoubleListIterator(pos).Node);
DisposeItem(aitem);
pos.Destroy; { pos is invalidated so it cannot be used anyway }
end;
function TDoubleList.Delete(astart, afinish : TForwardIterator) : SizeType;
var
pos, fin, npos : PDoubleListNode;
begin
Assert((astart is TDoubleListIterator) and (afinish is TDoubleListIterator),
msgInvalidIterator);
Assert((TDoubleListIterator(astart).Node <> nil) and
(TDoubleListIterator(afinish).Node <> nil), msgInvalidIterator);
Result := FSize;
pos := TDoubleListIterator(astart).Node;
fin := TDoubleListIterator(afinish).Node;
pos^.Prev^.Next := fin;
fin^.Prev := pos^.Prev;
if FStartNode = pos then
FStartNode := fin;
while (pos <> fin) do
begin
npos := pos^.Next;
DisposeItem(pos^.Item);
Dec(FSize);
DisposeNode(pos);
pos := npos;
end;
Result := Result - FSize;
end;
function TDoubleList.Extract(pos : TForwardIterator) : ItemType;
begin
Assert(pos is TDoubleListIterator, msgInvalidIterator);
Assert(pos.Owner = self, msgWrongOwner);
Result := ExtractNode(TDoubleListIterator(pos).Node);
pos.Destroy;
end;
procedure TDoubleList.Move(Source, Dest : TForwardIterator);
var
list2 : TDoubleList;
snode : PDoubleListNode;
begin
Assert(Dest is TDoubleListIterator, msgInvalidIterator);
Assert(Source is TDoubleListIterator, msgInvalidIterator);
Assert(Dest.Owner = self, msgWrongOwner);
Assert(TDoubleListIterator(Source).Node^.Next <>
TDoubleList(Source.Owner).FStartNode, msgMovingInvalidIterator);
list2 := TDoubleListIterator(Source).FList;
snode := TDoubleListIterator(Source).Node;
DoMove(TDoubleListIterator(Dest).Node, snode, snode^.Next, list2);
Inc(FSize);
Dec(list2.FSize);
end;
procedure TDoubleList.Move(SourceStart, SourceFinish, Dest : TForwardIterator);
var
source1, source2 : PDoubleListNode;
list2 : TDoubleList;
begin
Assert(Dest is TDoubleListIterator, msgInvalidIterator);
Assert(SourceStart is TDoubleListIterator, msgInvalidIterator);
Assert(SourceFinish is TDoubleListIterator, msgInvalidIterator);
Assert(SourceStart.Owner = SourceFinish.Owner, msgWrongRangeOwner);
Assert(Dest.Owner = self, msgWrongOwner);
Assert(TDoubleListIterator(SourceStart).Node^.Next <>
TDoubleList(SourceStart.Owner).FStartNode,
msgMovingInvalidIterator);
Assert((SourceStart.Owner <> Dest.Owner) or
not (Less(SourceStart, Dest) and Less(Dest, SourceFinish)),
msgMovingBadRange);
source1 := TDoubleListIterator(SourceStart).Node;
source2 := TDoubleListIterator(SourceFinish).Node;
if source1 = source2 then { special case - empty range }
Exit;
list2 := TDoubleListIterator(SourceStart).FList;
DoMove(TDoubleListIterator(Dest).Node, source1, source2, list2);
if SourceStart.Owner <> self then
begin
FValidSize := false;
list2.FValidSize := false;
end;
end;
function TDoubleList.Front : ItemType;
begin
Assert(FStartNode^.Next <> FStartNode, msgReadEmpty);
Result := FStartNode^.Item;
end;
function TDoubleList.Back : ItemType;
begin
Assert(FStartNode^.Next <> FStartNode, msgReadEmpty);
Result := FStartNode^.Prev^.Prev^.Item;
end;
procedure TDoubleList.PushBack(aitem : ItemType);
var
temp : PDoubleListNode;
begin
NewNode(temp);
temp^.Item := aitem;
temp^.Next := FStartNode^.Prev;
temp^.Prev := FStartNode^.Prev^.Prev;
FStartNode^.Prev^.Prev^.Next := temp;
FStartNode^.Prev^.Prev := temp;
if FStartNode^.Prev = temp then { temp is the first inserted Item }
FStartNode := temp;
Inc(FSize);
end;
procedure TDoubleList.PopBack;
var
lastnode, newback : PDoubleListNode;
begin
Assert(FStartNode^.Next <> FStartNode, msgPopEmpty);
lastnode := FStartNode^.Prev;
newback := lastnode^.Prev^.Prev;
DisposeNodeAndItem(newback^.Next); { may throw }
newback^.Next := lastnode;
lastnode^.Prev := newback;
if newback^.Next = newback then
FStartNode := newback;
Dec(FSize);
end;
procedure TDoubleList.PushFront(aitem : ItemType);
var
pnewnode : PDoubleListNode;
begin
NewNode(pnewnode);
pnewnode^.Item := aitem;
pnewnode^.Next := FStartNode;
pnewnode^.Prev := FStartNode^.Prev;
FStartNode^.Prev^.Next := pnewnode;
FStartNode^.Prev := pnewnode;
FStartNode := pnewnode;
Inc(FSize);
end;
procedure TDoubleList.PopFront;
var
newfirst, lastnode : PDoubleListNode;
begin
Assert(FStartNode^.Next <> FStartNode, msgPopEmpty);
lastnode := FStartNode^.Prev;
newfirst := FStartNode^.Next;
DisposeNodeAndItem(FStartNode); { may throw exception }
FStartNode := newfirst;
FStartNode^.Prev := lastnode;
lastnode^.Next := FStartNode;
Dec(FSize);
end;
procedure TDoubleList.Clear;
var
lastnode : PDoubleListNode;
begin
FValidSize := false; { in case of an exception }
lastnode := FStartNode^.Prev;
while FStartNode <> lastnode do
begin
FStartNode := FStartNode^.Next;
DisposeNodeAndItem(FStartNode^.Prev); { exception possible here }
end;
FStartNode^.Next := FStartNode;
FStartNode^.Prev := FStartNode;
FSize := 0;
FValidSize := true;
{ destroy all iterators into container, as they are not valid anyway }
GrabageCollector.FreeObjects;
end;
function TDoubleList.Empty : Boolean;
begin
Result := (FStartNode^.Next = FStartNode);
end;
function TDoubleList.Size : SizeType;
var
temp : PDoubleListNode;
begin
{$ifdef DEBUG_PASCAL_ADT }
Assert(SizeCanRecalc or FValidSize, msgMoveNotUpdate);
SizeCanRecalc := true;
{$endif DEBUG_PASCAL_ADT }
if not FValidSize then
begin
FSize := 0;
temp := FStartNode;
while temp^.Next <> FStartNode do
begin
Inc(FSize);
temp := temp^.Next;
end;
FValidSize := true;
end;
Result := FSize;
end;
function TDoubleList.IsDefinedOrder : Boolean;
begin
Result := false;
end;
function TDoubleList.InsertNode(pos : PDoubleListNode;
aitem : ItemType) : PDoubleListNode;
begin
Assert(pos <> nil);
{$warnings off }
NewNode(Result);
{$warnings on }
Result^.Item := aitem;
Result^.Next := pos;
Result^.Prev := pos^.Prev;
pos^.Prev^.Next := Result;
pos^.Prev := Result;
if pos = FStartNode then
FStartNode := Result;
Inc(FSize);
end;
function TDoubleList.ExtractNode(pos : PDoubleListNode) : ItemType;
begin
Assert((pos <> nil) and (pos^.Next <> FStartNode), msgDeletingInvalidIterator);
pos^.Prev^.Next := pos^.Next;
pos^.Next^.Prev := pos^.Prev;
if pos = FStartNode then
FStartNode := pos^.Next;
Result := pos^.Item;
DisposeNode(pos);
Dec(FSize);
end;
procedure TDoubleList.NewNode(var node : PDoubleListNode);