-
Notifications
You must be signed in to change notification settings - Fork 1
/
adtalgs_impl.i
1962 lines (1738 loc) · 50.9 KB
/
adtalgs_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
adtalgs_impl.i::prefix=&_mcp_prefix&::item_type=&ItemType&
}
&include adtalgs.defs
&include adtalgs_impl.mcp
&define TForwardIteratorPair T&_mcp_prefix&ForwardIteratorPair
&define Identity &_mcp_prefix&Identity
{ ---------------------- helper functions --------------------------- }
procedure InsertionSortAux(start : TRandomAccessIterator; si, fi : IndexType;
comparer : IBinaryComparer); overload;
var
i, j : IndexType;
begin
i := si + 1;
while i < fi do
begin
j := i;
while (j > si) and (_mcp_gt(start[j - 1], start[j], comparer)) do
begin
start.ExchangeItemsAt(j - 1, j);
Dec(j);
end;
Inc(i);
end;
end;
function PartitionAux(start : TRandomAccessIterator; si, fi : IndexType;
pred : IUnaryPredicate) : IndexType; overload;
begin
Dec(fi);
while si <> fi do
begin
while (si <> fi) and pred.Test(start[si]) do
Inc(si);
while (si <> fi) and (not pred.Test(start[fi])) do
Dec(fi);
if (si <> fi) then
start.ExchangeItemsAt(si, fi);
end;
if pred.Test(start[fi]) then
Inc(fi);
Result := fi;
end;
{ =========================== iterators ================================ }
{ ----------------------- TInserterBase --------------------------- }
function TInserterBase.GetItem : ItemType;
begin
Result := DefaultItem; { for the compiler not to complain }
raise EAssertionFailed.Create(msgInvalidIterator);
end;
procedure TInserterBase.SetItem(aitem : ItemType);
begin
raise EAssertionFailed.Create(msgInvalidIterator);
end;
procedure TInserterBase.ExchangeItem(iter : TIterator);
begin
raise EAssertionFailed.Create(msgInvalidIterator);
end;
{ ------------------------- TInserter ----------------------------------- }
constructor TInserter.Create(apos : TForwardIterator);
begin
inherited Create(apos.Owner);
FPos := apos;
FOwner := FPos.Owner;
end;
function TInserter.CopySelf : TIterator;
begin
Result := TInserter.Create(FPos);
end;
function TInserter.Equal(const Pos : TIterator) : Boolean;
begin
if pos is TInserter then
Result := TInserter(pos).FPos.Equal(FPos)
else
Result:= FPos.Equal(pos);
end;
procedure TInserter.Write(aitem : ItemType);
begin
FPos.Insert(aitem);
FPos.Advance;
end;
function TInserter.GetItem : ItemType;
begin
Result := FPos.GetItem;
end;
procedure TInserter.SetItem(aitem : ItemType);
begin
FPos.SetItem(aitem);
end;
procedure TInserter.ExchangeItem(iter : TIterator);
begin
if iter is TInserter then
FPos.ExchangeItem(TInserter(iter).FPos)
else
FPos.ExchangeItem(iter);
end;
function TInserter.Owner : TContainerAdt;
begin
{ Note: We cannot simply return FPos.Owner because this method may
be called from the destructor to obtain the grabage collector.
This means that, if the destructor was called from the grabage
collector, FPos might have been destroyed earlier! We therefore
have to store the owner separately in a field. }
Result := FOwner;
end;
{ ------------------------ TBasicInserter -------------------------------- }
constructor TBasicInserter.Create(cont : TContainerAdt);
begin
inherited Create(cont);
FCont := cont;
end;
function TBasicInserter.CopySelf : TIterator;
begin
Result := TBasicInserter.Create(FCont);
end;
function TBasicInserter.Equal(const Pos : TIterator) : Boolean;
begin
Result := (pos is TBasicInserter) and (TBasicInserter(pos).FCont = FCont);
end;
procedure TBasicInserter.Write(aitem : ItemType);
begin
if not FCont.InsertItem(aitem) then
raise EPascalAdt.Create('TBasicInserter: Cannot insert');
end;
function TBasicInserter.Owner : TContainerAdt;
begin
Result := FCont;
end;
{ ----------------------- TBackInserter ---------------------------- }
constructor TBackInserter.Create(cont : TQueueAdt);
begin
inherited Create(cont);
FCont := cont;
end;
function TBackInserter.CopySelf : TIterator;
begin
Result := TBackInserter.Create(FCont);
end;
function TBackInserter.Equal(const Pos : TIterator) : Boolean;
begin
Result := (pos is TBackInserter) and (TBackInserter(pos).FCont = FCont);
end;
procedure TBackInserter.Write(aitem : ItemType);
begin
FCont.PushBack(aitem);
end;
function TBackInserter.Owner : TContainerAdt;
begin
Result := FCont;
end;
{ ---------------------- TFrontInserter ------------------------------ }
constructor TFrontInserter.Create(cont : TDequeAdt);
begin
inherited Create(cont);
FCont := cont;
end;
function TFrontInserter.CopySelf : TIterator;
begin
Result := TFrontInserter.Create(FCont);
end;
function TFrontInserter.Equal(const Pos : TIterator) : Boolean;
begin
Result := (pos is TFrontInserter) and (TFrontInserter(pos).FCont = FCont);
end;
procedure TFrontInserter.Write(aitem : ItemType);
begin
FCont.PushFront(aitem);
end;
function TFrontInserter.Owner : TContainerAdt;
begin
Result := FCont;
end;
{ ======================= non-modifying algorithms ============================ }
{ ------------------------- searching ------------------------------------ }
function Find(const start, finish : TForwardIterator;
aitem : ItemType; const comparer : IBinaryComparer) : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
Result := CopyOf(start);
while (not Result.Equal(finish)) and
(not _mcp_equal(Result.Item, aitem, comparer)) do
begin
Result.Advance;
end;
end;
function Find(const start, finish : TForwardIterator;
const pred : IUnaryPredicate) : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
Result := CopyOf(start);
while not (Result.Equal(finish) or pred.Test(Result.Item)) do
begin
Result.Advance;
end;
end;
{ ----------------------- other ------------------------------------- }
function Count(const start, finish : TForwardIterator;
const pred : IUnaryPredicate) : SizeType;
var
iter : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
Result := 0;
iter := CopyOf(start);
while not iter.Equal(finish) do
begin
if pred.Test(iter.Item) then
Inc(Result);
iter.Advance;
end;
iter.Destroy;
end;
function Minimal(const start, finish : TForwardIterator;
const comparer : IBinaryComparer) : TForwardIterator;
var
iter : TForwardIterator;
minptr, aitem : ItemType;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
if start.Equal(finish) then
begin
Result := CopyOf(finish);
Exit;
end;
Result := CopyOf(start);
minptr := Result.Item;
iter := Next(start);
while not iter.Equal(finish) do
begin
aitem := iter.Item;
if _mcp_lt(aitem, minptr, comparer) then
begin
minptr := aitem;
Result.Destroy;
Result := CopyOf(iter);
end;
iter.Advance;
end;
end;
function Maximal(const start, finish : TForwardIterator;
const comparer : IBinaryComparer) : TForwardIterator;
var
iter : TForwardIterator;
maxptr, aitem : ItemType;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
if start.Equal(finish) then
begin
Result := CopyOf(finish);
Exit;
end;
Result := CopyOf(start);
maxptr := Result.Item;
iter := Next(start);
while not iter.Equal(finish) do
begin
aitem := iter.Item;
if _mcp_gt(aitem, maxptr, comparer) then
begin
maxptr := aitem;
Result.Destroy;
Result := CopyOf(iter);
end;
iter.Advance;
end;
end;
function Equal(const start1, finish1, start2 : TForwardIterator;
const pred : IBinaryPredicate) : Boolean;
var
iter1, iter2 : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start1, finish1);
{$endif }
Result := false;
iter1 := CopyOf(start1);
iter2 := CopyOf(start2);
while not iter1.Equal(finish1) do
begin
if not pred.Test(iter1.Item, iter2.Item) then
begin
iter1.Destroy;
iter2.Destroy;
Exit;
end;
iter1.Advance;
iter2.Advance;
end;
iter1.Destroy;
iter2.Destroy;
Result := true;
end;
function Mismatch(const start1, finish1, start2 : TForwardIterator;
const pred : IBinaryPredicate) : TForwardIteratorPair;
var
iter1, iter2 : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start1, finish1);
{$endif }
iter1 := CopyOf(start1);
iter2 := CopyOf(start2);
while not iter1.Equal(finish1) do
begin
if not pred.Test(iter1.Item, iter2.Item) then
break;
iter1.Advance;
iter2.Advance;
end;
Result := TForwardIteratorPair.Create(iter1, iter2);
end;
function LexicographicalCompare(const start1, finish1,
start2, finish2 : TForwardIterator;
const comparer : IBinaryComparer) : Integer;
var
iter1, iter2 : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start1, finish1);
CheckIteratorRange(start2, finish2);
{$endif }
Result := 0;
iter1 := CopyOf(start1);
iter2 := CopyOf(start2);
while (not iter1.Equal(finish1)) and (not iter2.Equal(finish2)) and
(Result = 0) do
begin
_mcp_compare_assign(iter1.Item, iter2.Item, Result, comparer);
iter1.Advance;
iter2.Advance;
end;
if Result = 0 then
begin
if iter1.Equal(finish1) then
begin
if not iter2.Equal(finish2) then
Result := -1;
end else
begin
Result := +1;
end;
end;
iter1.Destroy;
iter2.Destroy;
end;
{ ======================= modifying algorithms ============================ }
procedure ForEach(start, finish : TForwardIterator;
const funct : IUnaryFunctor);
var
owner : TContainerAdt;
owns : Boolean;
iter : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
iter := CopyOf(start);
owner := start.Owner;
owns := owner.OwnsItems;
owner.OwnsItems := false;
try
while not iter.Equal(finish) do
begin
iter.SetItem(funct.Perform(iter.GetItem));
iter.Advance;
end;
finally
owner.OwnsItems := owns;
start.Destroy;
end;
end;
procedure Generate(start, finish : TForwardIterator;
const funct : IUnaryFunctor);
var
iter : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
iter := CopyOf(start);
while not iter.Equal(finish) do
begin
iter.SetItem(funct.Perform(iter.GetItem));
iter.Advance;
end;
end;
procedure Copy(const start1, finish1 : TForwardIterator;
start2 : TOutputIterator;
const itemCopier : IUnaryFunctor);
var
iter : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start1, finish1);
{$endif }
iter := CopyOf(start1);
while not iter.Equal(finish1) do
begin
start2.Write(itemCopier.Perform(iter.Item));
iter.Advance;
end;
iter.Destroy;
end;
procedure Move(start1, finish1, start2 : TForwardIterator);
var
owns : Boolean;
owner : TContainerAdt;
iter, fstart2 : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start1, finish1);
{$endif }
if start1.Owner <> start2.Owner then
begin
iter := CopyOf(start1);
while not iter.Equal(finish1) do
begin
start2.Insert(iter.Item);
start2.Advance;
iter.Advance;
end;
iter.Destroy;
owner := start1.Owner;
owns := owner.OwnsItems;
owner.OwnsItems := false;
try
start1.Delete(finish1);
finally
owner.OwnsItems := owns;
end;
end else { not start1.Owner <> start2.Owner }
begin
Assert(start2 is TForwardIterator);
fstart2 := TForwardIterator(start2);
if Less(start1, fstart2) then
begin
if finish1.Equal(fstart2) then
Exit;
Assert(Less(finish1, fstart2), msgMovingBadRange);
if start1 is TBidirectionalIterator then
begin
Assert((fstart2 is TBidirectionalIterator) and
(finish1 is TBidirectionalIterator));
iter := CopyOf(fstart2);
Retreat(TBidirectionalIterator(iter), Distance(start1, finish1));
end else
begin
iter := CopyOf(start1);
Advance(iter, Distance(finish1, fstart2));
end;
Rotate(start1, iter, fstart2);
end else { not Less(start1, fstart2) }
begin
if start1.Equal(fstart2) then
Exit;
if start1 is TBidirectionalIterator then
begin
Assert((fstart2 is TBidirectionalIterator) and
(finish1 is TBidirectionalIterator));
iter := CopyOf(finish1);
Retreat(TBidirectionalIterator(iter), Distance(fstart2, start1));
end else
begin
iter := CopyOf(fstart2);
Advance(iter, Distance(start1, finish1));
end;
Rotate(fstart2, iter, finish1);
end;
end; { end not start1.Owner <> start2.Owner }
end;
procedure Combine(const start1, finish1, start2 : TForwardIterator;
start3 : TOutputIterator; const itemJoiner : IBinaryFunctor);
var
iter1, iter2 : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start1, finish1);
{$endif }
iter1 := CopyOf(start1);
iter2 := CopyOf(start2);
while not iter1.Equal(finish1) do
begin
start3.Write(itemJoiner.Perform(iter1.Item, iter2.Item));
iter1.Advance;
iter2.Advance;
end;
iter1.Destroy;
iter2.Destroy;
end;
{ ===================== mutating algorithms ======================== }
{ ---------------------- sorting -------------------------- }
procedure Sort(start, finish : TRandomAccessIterator;
const comparer : IBinaryComparer);
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
if finish.Index - start.Index <= qsMinItems then
InsertionSort(start, finish, comparer)
else
QuickSort(start, finish, comparer);
end;
procedure StableSort(start, finish : TRandomAccessIterator;
const comparer : IBinaryComparer);
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
if finish.Index - start.Index <= msMinItems then
InsertionSort(start, finish, comparer)
else
MergeSort(start, finish, comparer);
end;
procedure QuickSort(start, finish : TRandomAccessIterator;
const comparer : IBinaryComparer);
var
pi, si, fi, starti, finishi : IndexType;
stack : TDynamicArray;
predi : IUnaryPredicate;
pred : TLessBinder;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
starti := start.Index;
finishi := finish.Index;
si := 0;
fi := finishi - starti;
ArrayAllocate(stack, CeilLog2(fi - si), 0); { may raise }
try
pred := nil;
pred := TLessBinder.Create(comparer, DefaultItem); { may raise }
predi := pred;
ArrayPushBack(stack, ItemType(si)); { may raise }
ArrayPushBack(stack, ItemType(fi));
while stack^.Size <> 0 do
begin
fi := IndexType(ArrayPopBack(stack));
si := IndexType(ArrayPopBack(stack));
while fi - si >= qsMinItems do
begin
pi := Random(fi - si) + si;
pred.Item := start[pi];
{ save the pivot }
start.ExchangeItemsAt(si, pi);
pi := PartitionAux(start, si + 1, fi, predi);
{ move the saved pivot to its proper position }
start.ExchangeItemsAt(si, pi - 1);
{ this if-statement is essential to have an O(log(n))
memory complexity }
if pi - 1 - si < fi - pi then
begin
ArrayPushBack(stack, ItemType(pi)); { may raise }
ArrayPushBack(stack, ItemType(fi));
{ we should not consider the position pi - 1 any more
since the item at that position is at its proper
position }
fi := pi - 1;
end else
begin
ArrayPushBack(stack, ItemType(si));
ArrayPushBack(stack, ItemType(pi - 1));
si := pi;
end;
end; { end while fi - si }
{ insertion-sort is performed at the very end for the whole
range }
end; { end while stack }
finally
ArrayDeallocate(stack);
end;
{ now use InsertionSort to sort small groups that have remained
unsorted; InsertionSort will work very fast because it has only
groups with maximally qsMinItems to sort; }
InsertionSort(start, finish, comparer);
end;
procedure MergeSort(start, finish : TRandomAccessIterator;
const comparer : IBinaryComparer);
var
buffer : array of ItemType;
fi, i, j, k, m : IndexType;
n : SizeType;
owner : TContainerAdt;
invalidContainer, owns : boolean;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
owner := start.Owner;
owns := owner.OwnsItems;
owner.OwnsItems := false;
invalidContainer := false;
fi := finish.Index - start.Index;
try
SetLength(buffer, fi);
n := 1; { the length of each of the sequences already sorted }
while n < fi do
begin
i := 0;
while i + n < fi do
begin
j := i;
k := i + n;
m := i;
while (j < i + n) and (k < fi) and (k < i + 2*n) do
begin
if _mcp_lte(start[j], start[k], comparer) then
begin
buffer[m] := start[j];
Inc(j);
end else
begin
buffer[m] := start[k];
Inc(k);
end;
Inc(m);
end; { end while end of sequence not reached }
while j < i + n do
begin
buffer[m] := start[j];
Inc(j);
Inc(m);
end;
while (k < i + 2*n) and (k < fi) do
begin
buffer[m] := start[k];
Inc(k);
Inc(m);
end;
i := i + 2*n;
end; { end while i + n < fi }
{ copy the tail }
while i < fi do
begin
buffer[i] := start[i];
Inc(i);
end;
n := 2*n;
invalidContainer := true; { exception handling }
if n < fi then
begin
{ now merge the sequences in the buffer and put them into
the proper array }
i := 0;
while i + n < fi do
begin
j := i;
k := i + n;
m := i;
while (j < i + n) and (k < fi) and (k < i + 2*n) do
begin
if _mcp_lte(buffer[j], buffer[k], comparer) then
begin
start[m] := buffer[j];
Inc(j);
end else
begin
start[m] := buffer[k];
Inc(k);
end;
Inc(m);
end; { end while end of sequence not reached }
while j < i + n do
begin
start[m] := buffer[j];
Inc(j);
Inc(m);
end;
while (k < i + 2*n) and (k < fi) do
begin
start[m] := buffer[k];
Inc(k);
Inc(m);
end;
i := i + 2*n;
end; { end while i + n < fi }
{ copy the tail }
while i < fi do
begin
start[i] := buffer[i];
Inc(i);
end;
n := 2*n;
end else
begin
{ copy items from the buffer to the container }
for i := 0 to fi - 1 do
start[i] := buffer[i];
end; { end not if n < fi }
invalidContainer := false;
end; { end while n < fi }
finally
if invalidContainer then
begin
{ the exception was raised in the second part of the loop
when items are copied from the buffer to the container;
just copy all of them back into the container not to leak
anything }
for i := 0 to fi - 1 do
start[i] := buffer[i];
end;
owner.OwnsItems := owns;
end;
end;
procedure ShellSort(start, finish : TRandomAccessIterator;
const comparer : IBinaryComparer);
var
n, k, i, j : IndexType;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
n := finish.Index - start.Index;
k := n div 2;
while k <> 0 do
begin
for i := k to n - 1 do
begin
j := i;
while j - k >= 0 do
begin
if _mcp_gt(start[j - k], start[j], comparer) then
begin
start.ExchangeItemsAt(j - k, j);
j := j - k;
end else
break;
end;
end;
k := k div 2;
end;
end;
procedure InsertionSort(start, finish : TBidirectionalIterator;
const comparer : IBinaryComparer);
var
iter, iter2, iter3 : TBidirectionalIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
if start is TRandomAccessIterator then
begin
Assert(finish is TRandomAccessIterator);
InsertionSort(TRandomAccessIterator(start), TRandomAccessIterator(finish),
comparer);
end;
iter := CopyOf(start);
iter2 := CopyOf(iter);
iter.Advance;
iter3 := CopyOf(iter);
while not iter.Equal(finish) do
begin
while _mcp_gt(iter2.Item, iter3.Item, comparer) do
begin
iter2.ExchangeItem(iter3);
iter3.Retreat;
if iter3.Equal(start) then
break;
iter2.Retreat;
end;
iter2.Destroy;
iter3.Destroy;
iter2 := CopyOf(iter);
iter.Advance;
iter3 := CopyOf(iter);
end;
iter.Destroy;
end;
procedure InsertionSort(start, finish : TRandomAccessIterator;
const comparer : IBinaryComparer);
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
{$endif }
InsertionSortAux(start, 0, finish.Index - start.Index, comparer);
end;
{ ------------------- other mutating algorithms -------------------------- }
procedure Rotate(start, newstart, finish : TForwardIterator);
var
src, dest, finsrc, findest : TForwardIterator;
begin
{$ifdef DEBUG_PASCAL_ADT }
CheckIteratorRange(start, finish);
CheckIteratorRange(start, newstart);
CheckIteratorRange(newstart, finish);
{$endif }
if start.Equal(newstart) or newstart.Equal(finish) then
Exit;
src := nil; { will be set in the loop }
dest := CopyOf(newstart);
// newstart := CopyOf(newstart);
// finish := CopyOf(finish);
findest := CopyOf(finish);
finsrc := CopyOf(newstart);
start := CopyOf(start); { start of source }
repeat
{ First, we swap the source as many times as possible with
subsequent destination blocks. We shall call a block any
physically continuous group of items. For clarity, we assume
that the number of the situation is like in the picture below,
but the algorithm may be easily generalised. }
{ +--------+ }
{(5)| size m | <- remaining dest block (size m < n) }
{ |--------| ] }
{(4)| size n | ] }
{ |--------| ] }
{(3)| size n | ]-> destinations blocks (all size n) }
{ |--------| ] }
{(2)| size n | ] }
{ |--------| <- newstart }
{(1)|*size*n*| <- the first source block (size n) }
{ +--------+ }
{ We first swap (1) with (2). Then, (2) is where (1) used to be,
and we swap it with (3), and so on. Having swapped block (4),
the remaining dest block contains less items than the source
block (m < n), and the blocks (1) to (3) are now at their
proper positions (i.e. one block up) and (4) is at the very
bottom of the table. So, we swap m items from (4) with the
block (5). Now, only the items in [start, newstart) are in
wrong order. We have the following: }
{ | ... | }
{ | ordered | }
{ +---------+ <- old newstart; logical array end }
{ | | ] }
{ | size n-m| ]-> source }
{ |---------| <- start }
{ (5)| size m | -> dest }
{ +---------+ }
{ We simply apply the algorithm recursively by setting the
variables as shown above. The only peculiarity is that we have
to make the array wrap around, since the destination is
logically above the source. Logically, the range we operate on
should be treated as circular, but we set several additional
variables to avoid the necessity of checking the end of the
physical range and make iterators go 'wrapping around'. }
repeat
src.Free;
src := CopyOf(start);