-
Notifications
You must be signed in to change notification settings - Fork 16
/
LGArrayHelpers.pas
13739 lines (13058 loc) · 393 KB
/
LGArrayHelpers.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
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
{****************************************************************************
* *
* This file is part of the LGenerics package. *
* Generic helper utils for arrays. *
* *
* Copyright(c) 2018-2019 A.Koverdyaev(avk) *
* *
* This code is free software; you can redistribute it and/or modify it *
* under the terms of the Apache License, Version 2.0; *
* You may obtain a copy of the License at *
* http://www.apache.org/licenses/LICENSE-2.0. *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*****************************************************************************}
unit LGArrayHelpers;
{$mode objfpc}{$H+}
{$INLINE ON}
{$MODESWITCH ADVANCEDRECORDS}
{$MODESWITCH NESTEDPROCVARS}
interface
uses
SysUtils, math, typinfo,
LGUtils,
{%H-}LGHelpers,
LGStrConst;
type
TSortOrder = LGUtils.TSortOrder;
TSearchResult = record
FoundIndex,
InsertIndex: SizeInt;
constructor Create(aFoundIndex, aInsertIndex: SizeInt);
end;
{.$DEFINE FPC_REQUIRES_PROPER_ALIGNMENT :for test purpose only}
{ TGArrayHelpUtil }
generic TGArrayHelpUtil<T> = class
public
type
TItem = T;
PItem = ^T;
TArray = specialize TGArray<T>;
TOptional = specialize TGOptional<T>;
TEqualCompare = specialize TGEqualCompare<T>;
TOnEqualCompare = specialize TGOnEqualCompare<T>;
TNestEqualCompare = specialize TGNestEqualCompare<T>;
TTest = specialize TGTest<T>;
TOnTest = specialize TGOnTest<T>;
TNestTest = specialize TGNestTest<T>;
TFold = specialize TGFold<T, T>;
TOnFold = specialize TGOnFold<T, T>;
TNestFold = specialize TGNestFold<T, T>;
protected
const
HEAP_INSERTION_SORT_CUTOFF = 63;
QUICK_INSERTION_SORT_CUTOFF = 47;
MEDIAN_OF9_CUTOFF = 511;
DPQ_INSERTION_SORT_CUTOFF = 47;
INTROSORT_LOG_FACTOR = 2;
type
//to supress unnecessary refcounting
TFake = {$IFNDEF FPC_REQUIRES_PROPER_ALIGNMENT}array[0..Pred(SizeOf(T))] of Byte{$ELSE}T{$ENDIF};
TFakeArray = array of TFake;
TSortSplit = record
Left,
Right: SizeInt;
end;
TMergeSortBase = object
protected
const
MERGE_STACK_INIT_SIZE = 16;
MERGE_BUFFER_INIT_SIZE = 64;
MIN_MERGE_POW = 5;
MIN_MERGE_LEN = SizeInt(1) shl MIN_MERGE_POW;
type
TRun = record
Base, Count: SizeInt;
end;
TRunArray = array of TRun;
var
FData: PItem; // pointer to data array
FBuffer: TFakeArray;
FStack: TRunArray;
FStackSize: SizeInt;
procedure PushRun(aBase, aCount: SizeInt);
function EnsureBufferCapacity(aSize: SizeInt): PItem;
procedure Init(A: PItem);
class function MinRunLen(aTotalSize: SizeInt): SizeInt; static;
end;
TPDQSortBase = object
public
type
TPart = specialize TGTuple2<PItem, Boolean>;
private
const
BLOCK_SIZE = 128;
CACHE_LINE_SIZE = 64;
PARTIAL_INSERTION_SORT_LIMIT = 12;
NINTHER_THRESHOLD = 128;
var
FOffsetsLStorage, FOffsetsRStorage: array[0..Pred(BLOCK_SIZE + CACHE_LINE_SIZE)] of Byte;
class procedure SwapOffsets(aFirst, aLast: PItem; aOffsetsL, aOffsetsR: PByte;
aNum: PtrInt; aUseSwaps: Boolean); static;
end;
class procedure CopyItems(aSrc, aDst: PItem; aCount: SizeInt); static;
class procedure DoReverse(p: PItem; R: SizeInt); static;
public
class procedure Swap(var L, R: T); static; inline;
{ swaps the elements of A with the indices L and R;
raises EArgumentException if L or R is out of bounds }
class procedure SwapItems(var A: array of T; L, R: SizeInt); static;
class function CreateCopy(constref A: array of T): TArray; static;
class function CreateReverseCopy(constref A: array of T): TArray; static;
class function CreateMerge(constref L, R: array of T): TArray; static;
class function CreateRandomShuffle(constref A: array of T): TArray; static;
class function CreateAndFill(constref aValue: T; aSize: SizeInt): TArray; static;
class procedure Fill(var A: array of T; constref aValue: T); static;
{ returns resized array }
class function Resize(var A: TArray; aNewSize: SizeInt): TArray; static; inline;
class function Append(var A: TArray; constref aValue: T): SizeInt; static;
{ if aSrc <> aDst appends aSrc to aDst, sets aSrc to nil and returns count of merged elements,
otherwise returns 0}
class function Merge(var aDst, aSrc: TArray): SizeInt; static;
{ returns array of elements of A starting at aIndex(0-based) to High(A);
length of A becomes aIndex;
if A = nil or aIndex > High(A), then Result is empty }
class function Split(var A: TArray; aIndex: SizeInt): TArray; static;
{ extracts array of aCount elements of A starting at aIndex(0-based);
if aIndex > High(A) or aCount < 1, then Result is empty;
if aCount > length A then Result is truncated }
class function Extract(var A: TArray; aIndex, aCount: SizeInt): TArray; static;
class procedure Reverse(var A: array of T); static;
{ cyclic shift of array elements by aDist positions to the left;
the case if Abs(aDist) > Length(A) is ignored }
class procedure RotateLeft(var A: array of T; aDist: SizeInt); static;
{ cyclic shift of array elements by aDist positions to the right;
the case if Abs(aDist) > Length(A) is ignored }
class procedure RotateRight(var A: array of T; aDist: SizeInt); static;
class procedure RandomShuffle(var A: array of T); static;
{ returns 0-based leftmost position of aValue in array A, -1 if not found }
class function SequentSearch(constref A: array of T; constref aValue: T; c: TEqualCompare): SizeInt;
static;
class function SequentSearch(constref A: array of T; constref aValue: T; c: TOnEqualCompare): SizeInt;
static;
class function SequentSearch(constref A: array of T; constref aValue: T; c: TNestEqualCompare): SizeInt;
static;
{ returns True if both A and B are identical sequence of elements }
class function Same(constref A, B: array of T; c: TEqualCompare): Boolean; static;
class function Same(constref A, B: array of T; c: TOnEqualCompare): Boolean; static;
class function Same(constref A, B: array of T; c: TNestEqualCompare): Boolean; static;
class function Select(constref A: array of T; aTest: TTest): TArray;
class function Select(constref A: array of T; aTest: TOnTest): TArray;
class function Select(constref A: array of T; aTest: TNestTest): TArray;
{ left-associative linear fold }
class function FoldL(constref A: array of T; aFold: TFold; constref v0: T): T; static;
class function FoldL(constref A: array of T; aFold: TFold): TOptional; static;
class function FoldL(constref A: array of T; aFold: TOnFold; constref v0: T): T; static;
class function FoldL(constref A: array of T; aFold: TOnFold): TOptional; static;
class function FoldL(constref A: array of T; aFold: TNestFold; constref v0: T): T; static;
class function FoldL(constref A: array of T; aFold: TNestFold): TOptional; static;
{ right-associative linear fold }
class function FoldR(constref A: array of T; aFold: TFold; constref v0: T): T; static;
class function FoldR(constref A: array of T; aFold: TFold): TOptional; static;
class function FoldR(constref A: array of T; aFold: TOnFold; constref v0: T): T; static;
class function FoldR(constref A: array of T; aFold: TOnFold): TOptional; static;
class function FoldR(constref A: array of T; aFold: TNestFold; constref v0: T): T; static;
class function FoldR(constref A: array of T; aFold: TNestFold): TOptional; static;
end;
{ TGIndexedHelpUtil
type TIndexed must provide:
method/property Count: SizeInt - number of items contained;
property UncMutable[aIndex: SizeInt]: PItem - indexed access to items }
generic TGIndexedHelpUtil<T, TIndexed> = class
protected
type
TUtil = class(specialize TGArrayHelpUtil<T>);
public
type
TItem = TUtil.TItem;
PItem = TUtil.PItem;
TEqualCompare = TUtil.TEqualCompare;
TOnEqualCompare = TUtil.TOnEqualCompare;
TNestEqualCompare = TUtil.TNestEqualCompare;
TArray = TUtil.TArray;
protected
type
TFake = TUtil.TFake;
class procedure Swap(L, R: PItem); static; inline;
class procedure DoReverse(var e: TIndexed; L, R: SizeInt); static; inline;
public
class function CreateCopy(constref aEntity: TIndexed; aFrom, aCount: SizeInt): TArray; static;
class procedure Reverse(var aEntity: TIndexed); static;
class procedure Reverse(var aEntity: TIndexed; aFirst, aLast: SizeInt); static;
class procedure RandomShuffle(var aEntity: TIndexed); static;
class function SequentSearch(constref aEntity: TIndexed; constref aValue: T;
c: TEqualCompare): SizeInt; static;
class function SequentSearch(constref aEntity: TIndexed; constref aValue: T;
c: TOnEqualCompare): SizeInt; static;
class function SequentSearch(constref aEntity: TIndexed; constref aValue: T;
c: TNestEqualCompare): SizeInt; static;
class function Same(constref e1, e2: TIndexed; c: TEqualCompare): Boolean; static;
class function Same(constref e1, e2: TIndexed; c: TOnEqualCompare): Boolean; static;
class function Same(constref e1, e2: TIndexed; c: TNestEqualCompare): Boolean; static;
end;
{ TGBaseArrayHelper
functor TCmpRel(comparison relation) must provide:
class function Less([const[ref]] L, R: T): Boolean }
generic TGBaseArrayHelper<T, TCmpRel> = class(specialize TGArrayHelpUtil<T>)
protected
type
TMergeSort = object(TMergeSortBase)
private
procedure CollapseA;
procedure CollapseD;
procedure ForceCollapseA;
procedure ForceCollapseD;
procedure MergeAtA(aIndex: SizeInt);
procedure MergeAtD(aIndex: SizeInt);
procedure MergeLoA(From, CountLo, CountHi: SizeInt);
procedure MergeLoD(From, CountLo, CountHi: SizeInt);
procedure MergeHiA(From, CountLo, CountHi: SizeInt);
procedure MergeHiD(From, CountLo, CountHi: SizeInt);
class procedure InsertSortA(A: PItem; R, At: SizeInt); static;
class procedure InsertSortD(A: PItem; R, At: SizeInt); static;
class function CountRunAsc(A: PItem; R: SizeInt): SizeInt; static;
class function CountRunDesc(A: PItem; R: SizeInt): SizeInt; static;
public
class procedure SortAsc(A: PItem; R: SizeInt); static;
class procedure SortDesc(A: PItem; R: SizeInt); static;
end;
TPDQSort = object(TPDQSortBase)
private
class procedure Sort3(A, B, C: PItem); static; inline;
function PartitionRight(aStart, aFinish: PItem): TPart;
procedure DoSort(aStart, aFinish: PItem; aBadAllowed: SizeInt; aLeftMost: Boolean);
class function PartialInsertionSort(aStart, aFinish: PItem): Boolean; static;
class function PartitionLeft(aStart, aFinish: PItem): PItem; static;
public
class procedure Sort(aStart, aFinish: PItem); static;
end;
class function CountRun(A: PItem; R: SizeInt; o: TSortOrder): SizeInt; static;
class procedure InsertionSort(A: PItem; R: SizeInt); static;
class procedure UnguardInsertionSort(A: PItem; R: SizeInt); static;
class function BiSearchLeftA(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function BiSearchLeftD(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function BiSearchRightA(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function BiSearchRightD(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function DoBinSearch(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function DoBinSearchPos(A: PItem; R: SizeInt; constref aValue: T): TSearchResult; static;
class procedure DoHeapSort(A: PItem; R: SizeInt); static;
class function QSplitR(A: PItem; R: SizeInt): TSortSplit; static;
class procedure DoQSort(A: PItem; R: SizeInt; aLeftmost: Boolean); static;
class function MedianOf3(p1, p2, p3: PItem): PItem; static; inline;
class function QSplitMo9(A: PItem; R: SizeInt): TSortSplit; static;
class procedure DoIntroSort(A: PItem; R, Ttl: SizeInt; aLeftmost: Boolean); static;
class function DPQSplit(A: PItem; R: SizeInt): TSortSplit; static;
class procedure DoDPQSort(A: PItem; R: SizeInt; aLeftmost: Boolean); static;
{ QuickSelect with random pivot selection, does not checks indices }
class function QSelectR(A: PItem; R, N: SizeInt): T; static;
public
{ returns 0-based leftmost position of aValue in array A, -1 if not found }
class function SequentSearch(constref A: array of T; constref aValue: T): SizeInt; static;
{ returns 0-based leftmost position of aValue in SORTED array A, -1 if not found }
class function BinarySearch(constref A: array of T; constref aValue: T): SizeInt; static;
{ returns 0-based rightmost position of aValue in SORTED array A in Result.FoundIndex(-1 if not found);
returns position for insertion in Result.InsertIndex }
class function BinarySearchPos(constref A: array of T; constref aValue: T): TSearchResult; static;
{ returns 0-based position of minimal value in A, -1 if A is empty }
class function IndexOfMin(constref A: array of T): SizeInt; static;
{ returns 0-based position of maximal value in A, -1 if A is empty }
class function IndexOfMax(constref A: array of T): SizeInt; static;
{ returns smallest element of A in TOptional.Value if A is nonempty }
class function GetMin(constref A: array of T): TOptional; static;
{ returns greatest element of A in TOptional.Value if A is nonempty }
class function GetMax(constref A: array of T): TOptional; static;
{ returns True and smallest element of A in aValue if A is nonempty, False otherwise }
class function FindMin(constref A: array of T; out aValue: T): Boolean; static;
{ returns True and greatest element of A in aValue if A is nonempty, False otherwise }
class function FindMax(constref A: array of T; out aValue: T): Boolean; static;
{ returns True, smallest element of A in aMin and greatest element of A in aMax, if A is nonempty,
False otherwise }
class function FindMinMax(constref A: array of T; out aMin, aMax: T): Boolean; static;
{ returns True and A's Nth order statistic(0-based) in aValue if A is nonempty, False otherwise;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is destructive: changes the order of elements in A }
class function FindNthSmallest(var A: array of T; N: SizeInt; out aValue: T): Boolean; static;
{ returns A's Nth order statistic(0-based) in TOptional.Value if A is nonempty;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is destructive: changes the order of elements in A }
class function NthSmallest(var A: array of T; N: SizeInt): TOptional; static;
{ returns True and A's Nth order statistic(0-based) in aValue if A is nonempty, False otherwise;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is nondestructive: creates a temp copy of A }
class function FindNthSmallestND(constref A: array of T; N: SizeInt; out aValue: T): Boolean; static;
{ returns A's Nth order statistic(0-based) in TOptional.Value if A is nonempty;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is nondestructive: creates a temp copy of A }
class function NthSmallestND(constref A: array of T; N: SizeInt): TOptional; static;
{ returns True if permutation towards nondescending state of A has done, False otherwise }
class function NextPermutation2Asc(var A: array of T): Boolean; static;
{ returns True if permutation towards nonascending state of A has done, False otherwise }
class function NextPermutation2Desc(var A: array of T): Boolean; static;
{ note: an empty array or single element array is always nondescending }
class function IsNonDescending(constref A: array of T): Boolean; static;
{ note: an empty array or single element array is never strict ascending }
class function IsStrictAscending(constref A: array of T): Boolean; static;
{ note: an empty array or single element array is always nonascending }
class function IsNonAscending(constref A: array of T): Boolean; static;
{ note: an empty array or single element array is never strict descending}
class function IsStrictDescending(constref A: array of T): Boolean; static;
{ returns the number of inversions in A, sorts an array }
class function InversionCount(var A: array of T): Int64; static;
{ returns the number of inversions in A, nondestructive }
class function InversionCountND(constref A: array of T): Int64; static;
{ returns True if both A and B are identical sequence of elements }
class function Same(constref A, B: array of T): Boolean; static;
{ hybrid sorting based on quicksort with random pivot selection }
class procedure QuickSort(var A: array of T; o: TSortOrder = soAsc); static;
{ hybrid sorting based on introsort with pseudo-median-of-9 pivot selection }
class procedure IntroSort(var A: array of T; o: TSortOrder = soAsc); static;
{ hybrid sorting based on V.Yaroslavskiy' dual pivot quicksort with random pivot selection }
class procedure DualPivotQuickSort(var A: array of T; o: TSortOrder = soAsc); static;
{ Pascal translation of Orson Peters' PDQSort algorithm }
class procedure PDQSort(var A: array of T; o: TSortOrder = soAsc); static;
{ stable, adaptive mergesort inspired by Java Timsort }
class procedure MergeSort(var A: array of T; o: TSortOrder = soAsc); static;
{ default sorting, currently it is IntroSort}
class procedure Sort(var A: array of T; o: TSortOrder = soAsc); static;
class function Sorted(constref A: array of T; o: TSortOrder = soAsc): TArray; static;
{ copies only distinct values from A }
class function SelectDistinct(constref A: array of T): TArray; static;
end;
{ TGArrayHelper assumes that type T implements TCmpRel }
generic TGArrayHelper<T> = class(specialize TGBaseArrayHelper<T, T>);
{ TGBaseIndexedHelper
type TIndexed must provide:
method/property Count: SizeInt - number of items contained;
property UncMutable[aIndex: SizeInt]: PItem - indexed access to items;
functor TCmpRel(comparison relation) must provide:
class function Less([const[ref]] L, R: T): Boolean; }
generic TGBaseIndexedHelper<T, TIndexed, TCmpRel> = class(specialize TGIndexedHelpUtil<T, TIndexed>)
type
TOptional = TUtil.TOptional;
protected
type
TSortSplit = TUtil.TSortSplit;
class function BiSearchLeftA(constref e: TIndexed; L, R: SizeInt; constref aValue: T): SizeInt;
static;
class function BiSearchLeftD(constref e: TIndexed; L, R: SizeInt; constref aValue: T): SizeInt;
static;
class function BiSearchRightA(constref e: TIndexed; L, R: SizeInt; constref aValue: T): SizeInt;
static;
class function BiSearchRightD(constref e: TIndexed; L, R: SizeInt; constref aValue: T): SizeInt;
static;
class function DoBinSearch(constref e: TIndexed; L, R: SizeInt; constref aValue: T): SizeInt; static;
class function DoBinSearchPos(constref e: TIndexed; L, R: SizeInt; constref aValue: T): TSearchResult;
static;
class function CountRun(var e: TIndexed; L, R: SizeInt; o: TSortOrder): SizeInt;
class procedure InsertionSort(var e: TIndexed; L, R: SizeInt); static;
class procedure DoHeapSort(var e: TIndexed; L, R: SizeInt); static;
class function MedianOf3(p1, p2, p3: PItem): PItem; static; inline;
class function QSplitMo9(var e: TIndexed; L, R: SizeInt): TSortSplit; static;
class procedure DoIntroSort(var e: TIndexed; L, R, Ttl: SizeInt); static;
public
class function SequentSearch(constref aEntity: TIndexed; constref aValue: T): SizeInt; static;
class function BinarySearch(constref aEntity: TIndexed; constref aValue: T): SizeInt; static;
class function BinarySearchPos(constref aEntity: TIndexed; constref aValue: T): TSearchResult; static;
class function IndexOfMin(constref aEntity: TIndexed): SizeInt; static;
class function IndexOfMax(constref aEntity: TIndexed): SizeInt; static;
class function GetMin(constref aEntity: TIndexed): TOptional; static;
class function GetMax(constref aEntity: TIndexed): TOptional; static;
class function FindMin(constref aEntity: TIndexed; out aValue: T): Boolean; static;
class function FindMax(constref aEntity: TIndexed; out aValue: T): Boolean; static;
class function FindMinMax(constref aEntity: TIndexed; out aMin, aMax: T): Boolean; static;
class function FindNthSmallest(constref aEntity: TIndexed; N: SizeInt; out aValue: T): Boolean; static;
class function NthSmallest(constref aEntity: TIndexed; N: SizeInt): TOptional; static;
class function NextPermutation2Asc(var aEntity: TIndexed): Boolean; static;
class function NextPermutation2Desc(var aEntity: TIndexed): Boolean; static;
class function InversionCount(constref aEntity: TIndexed): Int64; static;
class function IsNonDescending(constref aEntity: TIndexed): Boolean; static;
class function IsStrictAscending(constref aEntity: TIndexed): Boolean; static;
class function IsNonAscending(constref aEntity: TIndexed): Boolean; static;
class function IsStrictDescending(constref aEntity: TIndexed): Boolean; static;
class function Same(constref e1, e2: TIndexed): Boolean; static;
class procedure Sort(var aEntity: TIndexed; o: TSortOrder = soAsc); static;
class procedure Sort(var aEntity: TIndexed; aFirst, aLast: SizeInt; o: TSortOrder = soAsc); static;
{ copies only distinct values from aEntity }
class function SelectDistinct(const aEntity: TIndexed): TArray; static;
end;
{ TGIndexedHelper assumes that type T implements TCmpRel }
generic TGIndexedHelper<T, TIndexed> = class(specialize TGBaseIndexedHelper<T, TIndexed, T>);
{TGComparableArrayHelper assumes that type T defines comparison operators }
generic TGComparableArrayHelper<T> = class(specialize TGArrayHelpUtil<T>)
protected
type
TMergeSort = object(TMergeSortBase)
private
procedure CollapseA;
procedure CollapseD;
procedure ForceCollapseA;
procedure ForceCollapseD;
procedure MergeAtA(aIndex: SizeInt);
procedure MergeAtD(aIndex: SizeInt);
procedure MergeLoA(From, CountLo, CountHi: SizeInt);
procedure MergeLoD(From, CountLo, CountHi: SizeInt);
procedure MergeHiA(From, CountLo, CountHi: SizeInt);
procedure MergeHiD(From, CountLo, CountHi: SizeInt);
class procedure InsertSortA(A: PItem; R, At: SizeInt); static;
class procedure InsertSortD(A: PItem; R, At: SizeInt); static;
class function CountRunAsc(A: PItem; R: SizeInt): SizeInt; static;
class function CountRunDesc(A: PItem; R: SizeInt): SizeInt; static;
public
class procedure SortAsc(A: PItem; R: SizeInt); static;
class procedure SortDesc(A: PItem; R: SizeInt); static;
end;
TPDQSort = object(TPDQSortBase)
private
class procedure Sort3(A, B, C: PItem); static; inline;
function PartitionRight(aStart, aFinish: PItem): TPart;
procedure DoSort(aStart, aFinish: PItem; aBadAllowed: SizeInt; aLeftMost: Boolean);
class function PartialInsertionSort(aStart, aFinish: PItem): Boolean; static;
class function PartitionLeft(aStart, aFinish: PItem): PItem; static;
public
class procedure Sort(aStart, aFinish: PItem); static;
end;
class function CountRun(A: PItem; R: SizeInt; o: TSortOrder): SizeInt; static;
class procedure InsertionSort(A: PItem; R: SizeInt); static;
class procedure UnguardInsertionSort(A: PItem; R: SizeInt); static;
class function BiSearchLeftA(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function BiSearchLeftD(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function BiSearchRightA(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function BiSearchRightD(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function DoBinSearch(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function DoBinSearchPos(A: PItem; R: SizeInt; constref aValue: T): TSearchResult; static;
class procedure DoHeapSort(A: PItem; R: SizeInt); static;
class function QSplitR(A: PItem; R: SizeInt): TSortSplit; static;
class procedure DoQSort(A: PItem; R: SizeInt; aLeftmost: Boolean); static;
class function MedianOf3(p1, p2, p3: PItem): PItem; static; inline;
class function QSplitMo9(A: PItem; R: SizeInt): TSortSplit; static;
class procedure DoIntroSort(A: PItem; R, Ttl: SizeInt; aLeftmost: Boolean); static;
class function DPQSplit(A: PItem; R: SizeInt): TSortSplit; static;
class procedure DoDPQSort(A: PItem; R: SizeInt; aLeftmost: Boolean); static;
{ QuickSelect with random pivot selection, does not checks indices }
class function QSelectR(A: PItem; R, N: SizeInt): T; static;
public
{ returns 0-based leftmost position of aValue in array A, -1 if not found }
class function SequentSearch(constref A: array of T; constref aValue: T): SizeInt; static;
{ returns 0-based leftmost position of aValue in SORTED array A, -1 if not found }
class function BinarySearch(constref A: array of T; constref aValue: T): SizeInt; static;
{ returns 0-based rightmost position of aValue in SORTED array A in Result.FoundIndex(-1 if not found);
returns position for insertion in Result.InsertIndex }
class function BinarySearchPos(constref A: array of T; constref aValue: T): TSearchResult; static;
{ returns 0-based position of minimal value in A, -1 if A is empty }
class function IndexOfMin(constref A: array of T): SizeInt; static;
{ returns 0-based position of maximal value in A, -1 if A is empty }
class function IndexOfMax(constref A: array of T): SizeInt; static;
{ returns smallest element of A in TOptional.Value if A is nonempty }
class function GetMin(constref A: array of T): TOptional; static;
{ returns greatest element of A in TOptional.Value if A is nonempty }
class function GetMax(constref A: array of T): TOptional; static;
{ retursn True and smallest element of A in aValue if A is nonempty, False otherwise }
class function FindMin(constref A: array of T; out aValue: T): Boolean; static;
{ returns True and greatest element of A in aValue if A is nonempty, False otherwise }
class function FindMax(constref A: array of T; out aValue: T): Boolean; static;
{ returns True, smallest element of A in aMin and greatest element of A in aMax,
if A is nonempty, False otherwise }
class function FindMinMax(constref A: array of T; out aMin, aMax: T): Boolean; static;
{ returns True and A's Nth order statistic(0-based) in aValue if A is nonempty, False otherwise;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is destructive: changes the order of elements in A }
class function FindNthSmallest(var A: array of T; N: SizeInt; out aValue: T): Boolean; static;
{ returns A's Nth order statistic(0-based) in TOptional.Value if A is nonempty;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is destructive: changes the order of elements in A }
class function NthSmallest(var A: array of T; N: SizeInt): TOptional; static;
{ returns True and A's Nth order statistic(0-based) in aValue if A is nonempty, False otherwise;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is nondestructive: creates a temp copy of A }
class function FindNthSmallestND(constref A: array of T; N: SizeInt; out aValue: T): Boolean; static;
{ returns A's Nth order statistic(0-based) in TOptional.Value if A is nonempty;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is nondestructive: creates a temp copy of A }
class function NthSmallestND(constref A: array of T; N: SizeInt): TOptional; static;
{ returns True if permutation towards nondescending state of A has done, False otherwise }
class function NextPermutation2Asc(var A: array of T): Boolean; static;
{ returns True if permutation towards nonascending state of A has done, False otherwise }
class function NextPermutation2Desc(var A: array of T): Boolean; static;
{ note: an empty array or single element array is always nondescending }
class function IsNonDescending(constref A: array of T): Boolean; static;
{ note: an empty array or single element array is never strict ascending }
class function IsStrictAscending(constref A: array of T): Boolean; static;
{ note: an empty array or single element array is always nonascending }
class function IsNonAscending(constref A: array of T): Boolean; static;
{ note: an empty array or single element array is never strict descending}
class function IsStrictDescending(constref A: array of T): Boolean; static;
{ returns the number of inversions in A, sorts an array }
class function InversionCount(var A: array of T): Int64; static;
{ returns the number of inversions in A, nondestructive }
class function InversionCountND(constref A: array of T): Int64; static;
{ returns True if both A and B are identical sequence of elements }
class function Same(constref A, B: array of T): Boolean; static;
{ hybrid sorting based on quicksort with random pivot selection }
class procedure QuickSort(var A: array of T; o: TSortOrder = soAsc); static;
{ hybrid sorting based on introsort with pseudo-median-of-9 pivot selection }
class procedure IntroSort(var A: array of T; o: TSortOrder = soAsc); static;
{ hybrid sorting based on V.Yaroslavskiy' dual pivot quicksort with random pivot selection }
class procedure DualPivotQuickSort(var A: array of T; o: TSortOrder = soAsc); static;
{ Pascal translation of Orson Peters' PDQSort algorithm }
class procedure PDQSort(var A: array of T; o: TSortOrder = soAsc); static;
{ stable, adaptive mergesort inspired by Java Timsort }
class procedure MergeSort(var A: array of T; o: TSortOrder = soAsc); static;
{ default sorting, currently it is IntroSort }
class procedure Sort(var A: array of T; o: TSortOrder = soAsc); static;
class function Sorted(constref A: array of T; o: TSortOrder = soAsc): TArray; static;
{ copies only distinct values from A }
class function SelectDistinct(constref A: array of T): TArray; static;
end;
{ TGRegularArrayHelper: with regular comparator}
generic TGRegularArrayHelper<T> = class(specialize TGArrayHelpUtil<T>)
public
type
TLess = specialize TGLessCompare<T>;
protected
type
TMergeSort = object(TMergeSortBase)
private
FLess: TLess;
procedure Init(A: PItem; c: TLess);
procedure CollapseA;
procedure CollapseD;
procedure ForceCollapseA;
procedure ForceCollapseD;
procedure MergeAtA(aIndex: SizeInt);
procedure MergeAtD(aIndex: SizeInt);
procedure MergeLoA(From, CountLo, CountHi: SizeInt);
procedure MergeLoD(From, CountLo, CountHi: SizeInt);
procedure MergeHiA(From, CountLo, CountHi: SizeInt);
procedure MergeHiD(From, CountLo, CountHi: SizeInt);
class procedure InsertSortA(A: PItem; R, At: SizeInt; c: TLess); static;
class procedure InsertSortD(A: PItem; R, At: SizeInt; c: TLess); static;
class function CountRunAsc(A: PItem; R: SizeInt; c: TLess): SizeInt; static;
class function CountRunDesc(A: PItem; R: SizeInt; c: TLess): SizeInt; static;
public
class procedure SortAsc(A: PItem; R: SizeInt; c: TLess); static;
class procedure SortDesc(A: PItem; R: SizeInt; c: TLess); static;
end;
TPDQSort = object(TPDQSortBase)
private
class procedure Sort3(A, B, D: PItem; c: TLess); static; inline;
function PartitionRight(aStart, aFinish: PItem; c: TLess): TPart;
procedure DoSort(aStart, aFinish: PItem; aBadAllowed: SizeInt; aLeftMost: Boolean; c: TLess);
class function PartialInsertionSort(aStart, aFinish: PItem; c: TLess): Boolean; static;
class function PartitionLeft(aStart, aFinish: PItem; c: TLess): PItem; static;
public
class procedure Sort(aStart, aFinish: PItem; c: TLess); static;
end;
class function CountRun(A: PItem; R: SizeInt; c: TLess; o: TSortOrder): SizeInt; static;
class procedure InsertionSort(A: PItem; R: SizeInt; c: TLess); static;
class procedure UnguardInsertionSort(A: PItem; R: SizeInt; c: TLess); static;
class function BiSearchLeftA(A: PItem; R: SizeInt; constref aValue: T; c: TLess): SizeInt; static;
class function BiSearchLeftD(A: PItem; R: SizeInt; constref aValue: T; c: TLess): SizeInt; static;
class function BiSearchRightA(A: PItem; R: SizeInt; constref aValue: T; c: TLess): SizeInt; static;
class function BiSearchRightD(A: PItem; R: SizeInt; constref aValue: T; c: TLess): SizeInt; static;
class function DoBinSearch(A: PItem; R: SizeInt; constref aValue: T; c: TLess): SizeInt; static;
class function DoBinSearchPos(A: PItem; R: SizeInt; constref aValue: T; c: TLess): TSearchResult;
static;
class procedure DoHeapSort(A: PItem; R: SizeInt; c: TLess); static;
class function QSplitR(A: PItem; R: SizeInt; c: TLess): TSortSplit; static;
class procedure DoQSort(A: PItem; R: SizeInt; c: TLess; aLeftmost: Boolean); static;
class function MedianOf3(p1, p2, p3: PItem; c: TLess): PItem; static; inline;
class function QSplitMo9(A: PItem; R: SizeInt; c: TLess): TSortSplit; static;
class procedure DoIntroSort(A: PItem; R, Ttl: SizeInt; c: TLess; aLeftmost: Boolean); static;
class function DPQSplit(A: PItem; R: SizeInt; c: TLess): TSortSplit; static;
class procedure DoDPQSort(A: PItem; R: SizeInt; c: TLess; aLeftmost: Boolean); static;
{ QuickSelect with random pivot selection, does not checks indices }
class function QSelectR(A: PItem; R, N: SizeInt; c: TLess): T; static;
public
{ returns 0-based leftmost position of aValue in array A, -1 if not found }
class function SequentSearch(constref A: array of T; constref aValue: T; c: TLess): SizeInt; static;
{ returns 0-based leftmost position of aValue in SORTED array A, -1 if not found }
class function BinarySearch(constref A: array of T; constref aValue: T; c: TLess): SizeInt; static;
{ returns 0-based rightmost position of aValue in SORTED array A in Result.FoundIndex(-1 if not found);
returns position for insertion in Result.InsertIndex }
class function BinarySearchPos(constref A: array of T; constref aValue: T; c: TLess): TSearchResult;
static;
{ returns 0-based position of minimal value in A, -1 if A is empty }
class function IndexOfMin(constref A: array of T; c: TLess): SizeInt; static;
{ returns 0-based position of maximal value in A, -1 if A is empty }
class function IndexOfMax(constref A: array of T; c: TLess): SizeInt; static;
{ returns smallest element of A in TOptional.Value if A <> nil }
class function GetMin(constref A: array of T; c: TLess): TOptional; static;
{ returns greatest element of A in TOptional.Value if A is nonempty }
class function GetMax(constref A: array of T; c: TLess): TOptional; static;
{ returns True and smallest element of A in aValue if A is nonempty, False otherwise }
class function FindMin(constref A: array of T; out aValue: T; c: TLess): Boolean; static;
{ returns True and greatest element of A in aValue if A is nonempty, False otherwise }
class function FindMax(constref A: array of T; out aValue: T; c: TLess): Boolean; static;
{ returns True, smallest element of A in aMin and greatest element of A in aMax,
if A is nonempty, False otherwise }
class function FindMinMax(constref A: array of T; out aMin, aMax: T; c: TLess): Boolean; static;
{ returns True and A's Nth order statistic(0-based) in aValue if A is nonempty, False otherwise;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is destructive: changes the order of elements in A }
class function FindNthSmallest(var A: array of T; N: SizeInt; out aValue: T; c: TLess): Boolean; static;
{ returns A's Nth order statistic(0-based) in TOptional.Value if A is nonempty;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is destructive: changes the order of elements in A }
class function NthSmallest(var A: array of T; N: SizeInt; c: TLess): TOptional; static;
{ returns True and A's Nth order statistic(0-based) in aValue if A is nonempty, False otherwise;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is nondestructive: creates a temp copy of A }
class function FindNthSmallestND(constref A: array of T; N: SizeInt; out aValue: T; c: TLess): Boolean; static;
{ returns A's Nth order statistic(0-based) in TOptional.Value if A is nonempty;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is nondestructive: creates a temp copy of A }
class function NthSmallestND(constref A: array of T; N: SizeInt; c: TLess): TOptional; static;
{ returns True if permutation towards nondescending state of A has done, False otherwise }
class function NextPermutation2Asc(var A: array of T; c: TLess): Boolean; static;
{ returns True if permutation towards nonascending state of A has done, False otherwise }
class function NextPermutation2Desc(var A: array of T; c: TLess): Boolean; static;
{ note: an empty array or single element array is always nondescending }
class function IsNonDescending(constref A: array of T; c: TLess): Boolean; static;
{ note: an empty array or single element array is never strict ascending }
class function IsStrictAscending(constref A: array of T; c: TLess): Boolean; static;
{ note: an empty array or single element array is always nonascending }
class function IsNonAscending(constref A: array of T; c: TLess): Boolean; static;
{ note: an empty array or single element array is never strict descending}
class function IsStrictDescending(constref A: array of T; c: TLess): Boolean; static;
{ returns the number of inversions in A, sorts an array }
class function InversionCount(var A: array of T; c: TLess): Int64; static;
{ returns the number of inversions in A, nondestructive }
class function InversionCountND(constref A: array of T; c: TLess): Int64; static;
{ returns True if both A and B are identical sequence of elements }
class function Same(constref A, B: array of T; c: TLess): Boolean; static;
{ hybrid sorting based on quicksort with random pivot selection }
class procedure QuickSort(var A: array of T; c: TLess; o: TSortOrder = soAsc); static;
{ hybrid sorting based on introsort with pseudo-median-of-9 pivot selection }
class procedure IntroSort(var A: array of T; c: TLess; o: TSortOrder = soAsc); static;
{ hybrid sorting based on V.Yaroslavskiy' dual pivot quicksort with random pivot selection }
class procedure DualPivotQuickSort(var A: array of T; c: TLess; o: TSortOrder = soAsc); static;
{ Pascal translation of Orson Peters' PDQSort algorithm }
class procedure PDQSort(var A: array of T; c: TLess; o: TSortOrder = soAsc); static;
{ stable, adaptive mergesort inspired by Java Timsort }
class procedure MergeSort(var A: array of T; c: TLess; o: TSortOrder = soAsc); static;
{ default sorting, currently it is IntroSort }
class procedure Sort(var A: array of T; c: TLess; o: TSortOrder = soAsc); static;
class function Sorted(constref A: array of T; c: TLess; o: TSortOrder = soAsc): TArray; static;
{ copies only distinct values from A }
class function SelectDistinct(constref A: array of T; c: TLess): TArray;
static;
end;
{ TGDelegatedArrayHelper: with delegated comparator}
generic TGDelegatedArrayHelper<T> = class(specialize TGArrayHelpUtil<T>)
public
type
TOnLess = specialize TGOnLessCompare<T>;
protected
type
TMergeSort = object(TMergeSortBase)
protected
FLess: TOnLess;
procedure Init(A: PItem; c: TOnLess);
procedure CollapseA;
procedure CollapseD;
procedure ForceCollapseA;
procedure ForceCollapseD;
procedure MergeAtA(aIndex: SizeInt);
procedure MergeAtD(aIndex: SizeInt);
procedure MergeLoA(From, CountLo, CountHi: SizeInt);
procedure MergeLoD(From, CountLo, CountHi: SizeInt);
procedure MergeHiA(From, CountLo, CountHi: SizeInt);
procedure MergeHiD(From, CountLo, CountHi: SizeInt);
class procedure InsertSortA(A: PItem; R, At: SizeInt; c: TOnLess); static;
class procedure InsertSortD(A: PItem; R, At: SizeInt; c: TOnLess); static;
class function CountRunAsc(A: PItem; R: SizeInt; c: TOnLess): SizeInt; static;
class function CountRunDesc(A: PItem; R: SizeInt; c: TOnLess): SizeInt; static;
public
class procedure SortAsc(A: PItem; R: SizeInt; c: TOnLess); static;
class procedure SortDesc(A: PItem; R: SizeInt; c: TOnLess); static;
end;
TPDQSort = object(TPDQSortBase)
private
class procedure Sort3(A, B, D: PItem; c: TOnLess); static; inline;
function PartitionRight(aStart, aFinish: PItem; c: TOnLess): TPart;
procedure DoSort(aStart, aFinish: PItem; aBadAllowed: SizeInt; aLeftMost: Boolean; c: TOnLess);
class function PartialInsertionSort(aStart, aFinish: PItem; c: TOnLess): Boolean; static;
class function PartitionLeft(aStart, aFinish: PItem; c: TOnLess): PItem; static;
public
class procedure Sort(aStart, aFinish: PItem; c: TOnLess); static;
end;
class function CountRun(A: PItem; R: SizeInt; c: TOnLess; o: TSortOrder): SizeInt; static;
class procedure InsertionSort(A: PItem; R: SizeInt; c: TOnLess); static;
class procedure UnguardInsertionSort(A: PItem; R: SizeInt; c: TOnLess); static;
class function BiSearchLeftA(A: PItem; R: SizeInt; constref aValue: T; c: TOnLess): SizeInt; static;
class function BiSearchLeftD(A: PItem; R: SizeInt; constref aValue: T; c: TOnLess): SizeInt; static;
class function BiSearchRightA(A: PItem; R: SizeInt; constref aValue: T; c: TOnLess): SizeInt; static;
class function BiSearchRightD(A: PItem; R: SizeInt; constref aValue: T; c: TOnLess): SizeInt; static;
class function DoBinSearch(A: PItem; R: SizeInt; constref aValue: T; c: TOnLess): SizeInt; static;
class function DoBinSearchPos(A: PItem; R: SizeInt; constref aValue: T; c: TOnLess): TSearchResult;
static;
class procedure DoHeapSort(A: PItem; R: SizeInt; c: TOnLess); static;
class function QSplitR(A: PItem; R: SizeInt; c: TOnLess): TSortSplit; static;
class procedure DoQSort(A: PItem; R: SizeInt; c: TOnLess; aLeftmost: Boolean); static;
class function MedianOf3(p1, p2, p3: PItem; c: TOnLess): PItem; static; inline;
class function QSplitMo9(A: PItem; R: SizeInt; c: TOnLess): TSortSplit; static;
class procedure DoIntroSort(A: PItem; R, Ttl: SizeInt; c: TOnLess; aLeftmost: Boolean); static;
class function DPQSplit(A: PItem; R: SizeInt; c: TOnLess): TSortSplit; static;
class procedure DoDPQSort(A: PItem; R: SizeInt; c: TOnLess; aLeftmost: Boolean); static;
{ QuickSelect with random pivot selection, does not checks indices }
class function QSelectR(A: PItem; R, N: SizeInt; c: TOnLess): T; static;
public
{ returns 0-based leftmost position of aValue in array A, -1 if not found }
class function SequentSearch(constref A: array of T; constref aValue: T; c: TOnLess): SizeInt; static;
{ returns 0-based leftmost position of aValue in SORTED array A, -1 if not found }
class function BinarySearch(constref A: array of T; constref aValue: T; c: TOnLess): SizeInt; static;
{ returns 0-based rightmost position of aValue in SORTED array A in Result.FoundIndex(-1 if not found);
returns position for insertion in Result.InsertIndex }
class function BinarySearchPos(constref A: array of T; constref aValue: T; c: TOnLess): TSearchResult;
static;
{ returns 0-based position of minimal value in A, -1 if A is empty }
class function IndexOfMin(constref A: array of T; c: TOnLess): SizeInt; static;
{ returns 0-based position of maximal value in A, -1 if A is empty }
class function IndexOfMax(constref A: array of T; c: TOnLess): SizeInt; static;
{ returns smallest element of A in TOptional.Value if A is nonempty }
class function GetMin(constref A: array of T; c: TOnLess): TOptional; static;
{ returns greatest element of A in TOptional.Value if A is nonempty }
class function GetMax(constref A: array of T; c: TOnLess): TOptional; static;
{ returns True and smallest element of A in aValue if A is nonempty, False otherwise }
class function FindMin(constref A: array of T; out aValue: T; c: TOnLess): Boolean; static;
{ returns True and greatest element of A in aValue if A is nonempty, False otherwise }
class function FindMax(constref A: array of T; out aValue: T; c: TOnLess): Boolean; static;
{ returns True, smallest element of A in aMin and greatest element of A in aMax,
if A is nonempty, False otherwise }
class function FindMinMax(constref A: array of T; out aMin, aMax: T; c: TOnLess): Boolean; static;
{ returns True and A's Nth order statistic(0-based) in aValue if A is nonempty, False otherwise;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is destructive: changes the order of elements in A }
class function FindNthSmallest(var A: array of T; N: SizeInt; out aValue: T; c: TOnLess): Boolean; static;
{ returns A's Nth order statistic(0-based) in TOptional.Value if A is nonempty;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is destructive: changes the order of elements in A }
class function NthSmallest(var A: array of T; N: SizeInt; c: TOnLess): TOptional; static;
{ returns True and A's Nth order statistic(0-based) in aValue if A is nonempty, False otherwise;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is nondestructive: creates a temp copy of A }
class function FindNthSmallestND(constref A: array of T;N: SizeInt; out aValue: T; c: TOnLess): Boolean; static;
{ returns A's Nth order statistic(0-based) in TOptional.Value if A is nonempty;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is nondestructive: creates a temp copy of A }
class function NthSmallestND(constref A: array of T; N: SizeInt; c: TOnLess): TOptional; static;
{ returns True if permutation towards nondescending state of A has done, False otherwise }
class function NextPermutation2Asc(var A: array of T; c: TOnLess): Boolean; static;
{ returns True if permutation towards nonascending state of A has done, False otherwise }
class function NextPermutation2Desc(var A: array of T; c: TOnLess): Boolean; static;
{ note: an empty array or single element array is always nondescending }
class function IsNonDescending(constref A: array of T; c: TOnLess): Boolean; static;
{ note: an empty array or single element array is never strict ascending }
class function IsStrictAscending(constref A: array of T; c: TOnLess): Boolean; static;
{ note: an empty array or single element array is always nonascending }
class function IsNonAscending(constref A: array of T; c: TOnLess): Boolean; static;
{ note: an empty array or single element array is never strict descending}
class function IsStrictDescending(constref A: array of T; c: TOnLess): Boolean; static;
{ returns the number of inversions in A, sorts an array }
class function InversionCount(var A: array of T; c: TOnLess): Int64; static;
{ returns the number of inversions in A, nondestructive }
class function InversionCountND(constref A: array of T; c: TOnLess): Int64; static;
{ returns True if both A and B are identical sequence of elements }
class function Same(constref A, B: array of T; c: TOnLess): Boolean; static;
{ hybrid sorting based on quicksort with random pivot selection }
class procedure QuickSort(var A: array of T; c: TOnLess; o: TSortOrder = soAsc); static;
{ hybrid sorting based on introsort with pseudo-median-of-9 pivot selection }
class procedure IntroSort(var A: array of T; c: TOnLess; o: TSortOrder = soAsc); static;
{ hybrid sorting based on V.Yaroslavskiy' dual pivot quicksort with random pivot selection }
class procedure DualPivotQuickSort(var A: array of T; c: TOnLess; o: TSortOrder = soAsc); static;
{ Pascal translation of Orson Peters' PDQSort algorithm }
class procedure PDQSort(var A: array of T; c: TOnLess; o: TSortOrder = soAsc); static;
{ stable, adaptive mergesort inspired by Java Timsort }
class procedure MergeSort(var A: array of T; c: TOnLess; o: TSortOrder = soAsc); static;
{ default sorting, currently it is IntroSort }
class procedure Sort(var A: array of T; c: TOnLess; o: TSortOrder = soAsc); static;
class function Sorted(constref A: array of T; c: TOnLess; o: TSortOrder = soAsc): TArray; static;
{ copies only distinct values from A }
class function SelectDistinct(constref A: array of T; c: TOnLess): TArray;
static;
end;
{ TGNestedArrayHelper: with nested comparator}
generic TGNestedArrayHelper<T> = class(specialize TGArrayHelpUtil<T>)
public
type
TNestLess = specialize TGNestLessCompare<T>;
protected
type
TMergeSort = object(TMergeSortBase)
protected
FLess: TNestLess;
procedure Init(A: PItem; c: TNestLess);
procedure CollapseA;
procedure CollapseD;
procedure ForceCollapseA;
procedure ForceCollapseD;
procedure MergeAtA(aIndex: SizeInt);
procedure MergeAtD(aIndex: SizeInt);
procedure MergeLoA(From, CountLo, CountHi: SizeInt);
procedure MergeLoD(From, CountLo, CountHi: SizeInt);
procedure MergeHiA(From, CountLo, CountHi: SizeInt);
procedure MergeHiD(From, CountLo, CountHi: SizeInt);
class procedure InsertSortA(A: PItem; R, At: SizeInt; c: TNestLess); static;
class procedure InsertSortD(A: PItem; R, At: SizeInt; c: TNestLess); static;
class function CountRunAsc(A: PItem; R: SizeInt; c: TNestLess): SizeInt; static;
class function CountRunDesc(A: PItem; R: SizeInt; c: TNestLess): SizeInt; static;
public
class procedure SortAsc(A: PItem; R: SizeInt; c: TNestLess); static;
class procedure SortDesc(A: PItem; R: SizeInt; c: TNestLess); static;
end;
TPDQSort = object(TPDQSortBase)
private
class procedure Sort3(A, B, D: PItem; c: TNestLess); static;{$ifndef CPU86}inline;{$endif}//todo: ???
function PartitionRight(aStart, aFinish: PItem; c: TNestLess): TPart;
procedure DoSort(aStart, aFinish: PItem; aBadAllowed: SizeInt; aLeftMost: Boolean; c: TNestLess);
class function PartialInsertionSort(aStart, aFinish: PItem; c: TNestLess): Boolean; static;
class function PartitionLeft(aStart, aFinish: PItem; c: TNestLess): PItem; static;
public
class procedure Sort(aStart, aFinish: PItem; c: TNestLess); static;
end;
class function CountRun(A: PItem; R: SizeInt; c: TNestLess; o: TSortOrder): SizeInt; static;
class procedure InsertionSort(A: PItem; R: SizeInt; c: TNestLess); static;
class procedure UnguardInsertionSort(A: PItem; R: SizeInt; c: TNestLess); static;
class function BiSearchLeftA(A: PItem; R: SizeInt; constref aValue: T; c: TNestLess): SizeInt;
static;
class function BiSearchLeftD(A: PItem; R: SizeInt; constref aValue: T; c: TNestLess): SizeInt;
static;
class function BiSearchRightA(A: PItem; R: SizeInt; constref aValue: T; c: TNestLess): SizeInt;
static;
class function BiSearchRightD(A: PItem; R: SizeInt; constref aValue: T; c: TNestLess): SizeInt;
static;
class function DoBinSearch(A: PItem; R: SizeInt; constref aValue: T; c: TNestLess): SizeInt; static;
class function DoBinSearchPos(A: PItem; R: SizeInt; constref aValue: T; c: TNestLess): TSearchResult;
static;
class procedure DoHeapSort(A: PItem; R: SizeInt; c: TNestLess); static;
class function QSplitR(A: PItem; R: SizeInt; c: TNestLess): TSortSplit; static;
class procedure DoQSort(A: PItem; R: SizeInt; c: TNestLess; aLeftmost: Boolean); static;
class function MedianOf3(p1, p2, p3: PItem; c: TNestLess): PItem; static; inline;
class function QSplitMo9(A: PItem; R: SizeInt; c: TNestLess): TSortSplit; static;
class procedure DoIntroSort(A: PItem; R, Ttl: SizeInt; c: TNestLess; aLeftmost: Boolean); static;
class function DPQSplit(A: PItem; R: SizeInt; c: TNestLess): TSortSplit; static;
class procedure DoDPQSort(A: PItem; R: SizeInt; c: TNestLess; aLeftmost: Boolean); static;
{ QuickSelect with random pivot selection, does not checks indices }
class function QSelectR(A: PItem; R, N: SizeInt; c: TNestLess): T; static;
public
{ returns 0-based leftmost position of aValue in array A, -1 if not found }
class function SequentSearch(constref A: array of T; constref aValue: T; c: TNestLess): SizeInt; static;
{ returns 0-based leftmost position of aValue in SORTED array A, -1 if not found }
class function BinarySearch(constref A: array of T; constref aValue: T; c: TNestLess): SizeInt; static;
{ returns 0-based rightmost position of aValue in SORTED array A in Result.FoundIndex(-1 if not found);
returns position for insertion in Result.InsertIndex }
class function BinarySearchPos(constref A: array of T; constref aValue: T; c: TNestLess): TSearchResult;
static;
{ returns 0-based position of minimal value in A, -1 if A is empty }
class function IndexOfMin(constref A: array of T; c: TNestLess): SizeInt; static;
{ returns 0-based position of maximal value in A, -1 if A is empty }
class function IndexOfMax(constref A: array of T; c: TNestLess): SizeInt; static;
{ returns smallest element of A in TOptional.Value if A is nonempty }
class function GetMin(constref A: array of T; c: TNestLess): TOptional; static;
{ returns greatest element of A in TOptional.Value if A is nonempty }
class function GetMax(constref A: array of T; c: TNestLess): TOptional; static;
{ returns True and smallest element of A in aValue if A is nonempty, False otherwise }
class function FindMin(constref A: array of T; out aValue: T; c: TNestLess): Boolean; static;
{ returns True and greatest element of A in aValue if A is nonempty, False otherwise }
class function FindMax(constref A: array of T; out aValue: T; c: TNestLess): Boolean; static;
{ returns True, smallest element of A in aMin and greatest element of A in aMax,
if A is nonempty, False otherwise }
class function FindMinMax(constref A: array of T; out aMin, aMax: T; c: TNestLess): Boolean; static;
{ returns True and A's Nth order statistic(0-based) in aValue if A is nonempty, False otherwise;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is destructive: changes the order of elements in A }
class function FindNthSmallest(var A: array of T; N: SizeInt; out aValue: T; c: TNestLess): Boolean; static;
{ returns A's Nth order statistic(0-based) in TOptional.Value if A is nonempty;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is destructive: changes the order of elements in A }
class function NthSmallest(var A: array of T; N: SizeInt; c: TNestLess): TOptional; static;
{ returns True and A's Nth order statistic(0-based) in aValue if A is nonempty, False otherwise;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is nondestructive: creates a temp copy of A }
class function FindNthSmallestND(constref A: array of T;N: SizeInt; out aValue: T; c: TNestLess): Boolean; static;
{ returns A's Nth order statistic(0-based) in TOptional.Value if A is nonempty;
if N < 0 then N sets to 0; if N > High(A) then N sets to High(A);
is nondestructive: creates a temp copy of A }
class function NthSmallestND(constref A: array of T; N: SizeInt; c: TNestLess): TOptional; static;
{ returns True if permutation towards nondescending state of A has done, False otherwise }
class function NextPermutation2Asc(var A: array of T; c: TNestLess): Boolean; static;
{ returns True if permutation towards nonascending state of A has done, False otherwise }
class function NextPermutation2Desc(var A: array of T; c: TNestLess): Boolean; static;
{ note: an empty array or single element array is always nondescending }
class function IsNonDescending(constref A: array of T; c: TNestLess): Boolean; static;
{ note: an empty array or single element array is never strict ascending }
class function IsStrictAscending(constref A: array of T; c: TNestLess): Boolean; static;
{ note: an empty array or single element array is always nonascending }
class function IsNonAscending(constref A: array of T; c: TNestLess): Boolean; static;
{ note: an empty array or single element array is never strict descending}
class function IsStrictDescending(constref A: array of T; c: TNestLess): Boolean; static;
{ returns the number of inversions in A, sorts array }
class function InversionCount(var A: array of T; c: TNestLess): Int64; static;
{ returns the number of inversions in A, nondestructive }
class function InversionCountND(constref A: array of T; c: TNestLess): Int64; static;
{ returns True if both A and B are identical sequence of elements }
class function Same(constref A, B: array of T; c: TNestLess): Boolean; static;
{ hybrid sorting based on quicksort with random pivot selection }
class procedure QuickSort(var A: array of T; c: TNestLess; o: TSortOrder = soAsc); static;
{ hybrid sorting based on introsort with pseudo-median-of-9 pivot selection }
class procedure IntroSort(var A: array of T; c: TNestLess; o: TSortOrder = soAsc); static;
{ hybrid sorting based on V.Yaroslavskiy' dual pivot quicksort with random pivot selection }
class procedure DualPivotQuickSort(var A: array of T; c: TNestLess; o: TSortOrder = soAsc); static;
{ Pascal translation of Orson Peters' PDQSort algorithm }
class procedure PDQSort(var A: array of T; c: TNestLess; o: TSortOrder = soAsc); static;
{ stable, adaptive mergesort inspired by Java Timsort }
class procedure MergeSort(var A: array of T; c: TNestLess; o: TSortOrder = soAsc); static;
{ default sorting, currently it is IntroSort }
class procedure Sort(var A: array of T; c: TNestLess; o: TSortOrder = soAsc); static;
class function Sorted(constref A: array of T; c: TNestLess; o: TSortOrder = soAsc): TArray; static;
{ copies only distinct values from A }
class function SelectDistinct(constref A: array of T; c: TNestLess): TArray;
static;
end;
{ TGSimpleArrayHelper: for simple types only }
generic TGSimpleArrayHelper<T> = class(specialize TGArrayHelpUtil<T>)
private
type
TPDQSort = object(TPDQSortBase)
private
class procedure Sort3(A, B, C: PItem); static; inline;
function PartitionRight(aStart, aFinish: PItem): TPart;
procedure DoSort(aStart, aFinish: PItem; aBadAllowed: SizeInt; aLeftMost: Boolean);
class function PartialInsertionSort(aStart, aFinish: PItem): Boolean; static;
class function PartitionLeft(aStart, aFinish: PItem): PItem; static;
class procedure SwapOffsets(aFirst, aLast: PItem; aOffsetsL, aOffsetsR: PByte;
aNum: PtrInt; aUseSwaps: Boolean); static;
public
class procedure Sort(aStart, aFinish: PItem); static;
end;
class function CountRun(var A: array of T; L, R: SizeInt; o: TSortOrder): SizeInt; static;
class procedure InsertionSort(var A: array of T; L, R: SizeInt); static;
class procedure UnguardInsertionSort(var A: array of T; L, R: SizeInt); static;
class function BiSearchLeftA(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function BiSearchLeftD(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function BiSearchRightA(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function BiSearchRightD(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function DoBinSearch(A: PItem; R: SizeInt; constref aValue: T): SizeInt; static;
class function DoBinSearchPos(A: PItem; R: SizeInt; constref aValue: T): TSearchResult; static;
class procedure DoHeapSort(A: PItem; R: SizeInt); static;
class function QSplitR(var A: array of T; L, R: SizeInt): TSortSplit; static;
class procedure DoQSort(var A: array of T; L, R: SizeInt); static;
class function MedianOf3(p1, p2, p3: PItem): PItem; static; inline;
class function GetMo9Pivot(constref A: array of T; L, R: SizeInt): T; static;
class function QSplitMo9(var A: array of T; L, R: SizeInt): TSortSplit; static;
class procedure DoIntroSort(var A: array of T; L, R, Ttl: SizeInt); static;
class function DPQSplit(var A: array of T; L, R: SizeInt): TSortSplit; static;
class procedure DoDPQSort(var A: array of T; L, R: SizeInt); static;
class procedure DoSwap(p: PItem; L, R: SizeInt); static; inline;
class procedure DoReverse(var A: array of T; L, R: SizeInt); static;