-
Notifications
You must be signed in to change notification settings - Fork 35
/
DelphiUtils.Arrays.pas
1193 lines (985 loc) · 29 KB
/
DelphiUtils.Arrays.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
unit DelphiUtils.Arrays;
{
A collection of helper functions for working with arrays.
Includes operations such as filtration, conversion, search, and grouping.
}
interface
type
// Search and filtration
TFilterAction = (ftKeep, ftExclude);
TCondition<T> = reference to function (const Entry: T): Boolean;
TVarCondition<T> = reference to function (var Entry: T): Boolean;
TConditionEx<T> = reference to function (
const Index: Integer;
const Entry: T
): Boolean;
TEqualityCheck<T> = reference to function (const A, B: T): Boolean;
TComparer<T> = reference to function (const A, B: T): Integer;
TBinaryCondition<T> = reference to function (const Entry: T): Integer;
TDuplicateHandling = (dhInsert, dhOverwrite, dhSkip);
// Conversion
TItemCallback<T> = reference to procedure (var Item: T);
TMapRoutine<T1, T2> = reference to function (const Entry: T1): T2;
TMapRoutineEx<T1, T2> = reference to function (
const Index: Integer;
const Entry: T1
): T2;
TConvertRoutine<T1, T2> = reference to function (
const Entry: T1;
out ConvertedEntry: T2
): Boolean;
TConvertRoutineEx<T1, T2> = reference to function (
const Index: Integer;
const Entry: T1;
out ConvertedEntry: T2
): Boolean;
// Grouping
TArrayGroup<TKey, TValue> = record
Key: TKey;
Values: TArray<TValue>;
end;
// Other
TGenerator<T> = reference to function (const Index: Integer): T;
TAggregator<T> = reference to function (const A, B: T): T;
TConflictChecker<TData, TChanges> = reference to function (
const Data: TData;
const Changes: TChanges
): Boolean;
TConflictResolver<TData, TChanges> = reference to function (
const Existing: TData;
const New: TChanges
): TData;
TTreeNode<T> = record
Entry: T;
Index: Integer;
Parent: ^TTreeNode<T>;
Children: TArray<^TTreeNode<T>>;
end;
TParentChecker<T> = reference to function (const Parent, Child: T): Boolean;
TArray = class abstract
{ ------------------------ Conditional operations ------------------------ }
// Filter an array on by-element basis
class function Filter<T>(
const Entries: TArray<T>;
Condition: TCondition<T>;
Action: TFilterAction = ftKeep
): TArray<T>; static;
// Filter an array on by-element basis
class function FilterEx<T>(
const Entries: TArray<T>;
Condition: TConditionEx<T>;
Action: TFilterAction = ftKeep
): TArray<T>; static;
// Filter an array on by-element basis modifying the array
class procedure FilterInline<T>(
var Entries: TArray<T>;
Condition: TCondition<T>;
Action: TFilterAction = ftKeep
); static;
// Filter an array on by-element basis modifying the array and its elements
class procedure FilterInlineVar<T>(
var Entries: TArray<T>;
Condition: TVarCondition<T>;
Action: TFilterAction = ftKeep
); static;
// Filter an array on by-element basis modifying the array
class procedure FilterInlineEx<T>(
var Entries: TArray<T>;
Condition: TConditionEx<T>;
Action: TFilterAction = ftKeep
); static;
// Sort an array using the Quick Sort algorithm
class function Sort<T>(
const Entries: TArray<T>;
Comparer: TComparer<T> = nil
): TArray<T>; static;
// Sort an array using Quick Sort
class procedure SortInline<T>(
var Entries: TArray<T>;
Comparer: TComparer<T> = nil
); static;
// Fast search for an element in a sorted array.
// - A non-negative result indicates an index.
// - A negative result indicates a location where to insert the new
// element by calling System.Insert(Value, Entries, -(Result + 1));
// NOTE: do not use the default comparer with signed integer types
class function BinarySearch<T>(
const Entries: TArray<T>;
const Element: T;
Comparer: TComparer<T> = nil;
ReversedOrder: Boolean = False
): Integer; static;
// Insert an element into a sorted array preserving sorting. The return
// value has the same semantic as the binary search.
class function InsertSorted<T>(
var Entries: TArray<T>;
const Element: T;
DuplicateHandling: TDuplicateHandling;
Comparer: TComparer<T> = nil;
ReversedOrder: Boolean = False
): Integer; static;
// Fast search for an element in a sorted array.
class function BinarySearchEx<T>(
const Entries: TArray<T>;
BinarySearcher: TBinaryCondition<T>
): Integer; static;
// Check if the array contains a specific element
class function Contains<T>(
const Entries: TArray<T>;
const Element: T;
EqualityCheck: TEqualityCheck<T> = nil
): Boolean; static;
// Check if any elements match
class function ContainsMatch<T>(
const Entries: TArray<T>;
Condition: TCondition<T>
): Boolean; static;
// Check if any elements match
class function ContainsMatchEx<T>(
const Entries: TArray<T>;
Condition: TConditionEx<T>
): Boolean; static;
// Count the number of elements that are equal to the specified
class function Count<T>(
var Entries: TArray<T>;
const Element: T;
EqualityCheck: TEqualityCheck<T> = nil
): Integer; static;
// Count the number of elements that match a condition
class function CountMatches<T>(
var Entries: TArray<T>;
Condition: TCondition<T>
): Integer; static;
// Find the position of the first occurrence of an element
class function IndexOf<T>(
const Entries: TArray<T>;
const Element: T;
EqualityCheck: TEqualityCheck<T> = nil
): Integer; static;
// Find the position of the first occurrence of an element that matches
// a condition
class function IndexOfMatch<T>(
const Entries: TArray<T>;
Condition: TCondition<T>
): Integer; static;
// Find the first matching element or return a Default(T)
class function FindFirst<T>(
const Entries: TArray<T>;
Condition: TCondition<T>
): T; static;
// Find the first matching element or return the specified default
class function FindFirstOrDefault<T>(
const Entries: TArray<T>;
Condition: TCondition<T>;
const Default: T
): T; static;
// Try to find the first matching element
class function TryFindFirst<T>(
const Entries: TArray<T>;
Condition: TCondition<T>;
out Element: T
): Boolean; static;
// Search within an array and remove the second and subsequent duplicates
class function RemoveDuplicates<T>(
const Entries: TArray<T>;
EqualityCheck: TEqualityCheck<T> = nil
): TArray<T>; static;
{ ------------------------ Conversional operations ----------------------- }
// Convert each array element into a different type
class function Map<T1, T2>(
const Entries: TArray<T1>;
Converter: TMapRoutine<T1, T2>
): TArray<T2>; static;
// Convert each array element into a different type
class function MapEx<T1, T2>(
const Entries: TArray<T1>;
ConverterEx: TMapRoutineEx<T1, T2>
): TArray<T2>; static;
// Try to convert each array element
class function Convert<T1, T2>(
const Entries: TArray<T1>;
Converter: TConvertRoutine<T1, T2>
): TArray<T2>; static;
// Try to convert each array element
class function ConvertEx<T1, T2>(
const Entries: TArray<T1>;
ConverterEx: TConvertRoutineEx<T1, T2>
): TArray<T2>; static;
// Convert the first convertible entry or return Default(T2)
class function ConvertFirst<T1, T2>(
const Entries: TArray<T1>;
Converter: TConvertRoutine<T1, T2>
): T2; static;
// Convert the first convertible entry or return the specified default
class function ConvertFirstOrDefault<T1, T2>(
const Entries: TArray<T1>;
Converter: TConvertRoutine<T1, T2>;
const Default: T2
): T2; static;
// Concatenate an array of arrays into a single array
class function Flatten<T>(
const Arrays: TArray<TArray<T>>
): TArray<T>; static;
// Expand each element into an array and then concatenate them
class function FlattenEx<T1, T2>(
const Entries: TArray<T1>;
Converter: TMapRoutine<T1, TArray<T2>>
): TArray<T2>; static;
{ --------------------------- Other operations --------------------------- }
// Create an array from an iterator
class function Collect<T>(
const Iterator: IEnumerable<T>
): TArray<T>; static;
// Reverse the order of the elements in an array
class function Reverse<T>(
const Entries: TArray<T>
): TArray<T>; static;
// Execute a function for each element, potentially altering it
class procedure ForAll<T>(
var Entries: TArray<T>;
Callback: TItemCallback<T>
); static;
// Group array elements by different keys
class function GroupBy<TElement, TKey>(
const Entries: TArray<TElement>;
LookupKey: TMapRoutine<TElement, TKey>;
CompareKeys: TEqualityCheck<TKey> = nil
): TArray<TArrayGroup<TKey, TElement>>; static;
// Construct an new array element-by-element
class function Generate<T>(
const Count: Integer;
Generator: TGenerator<T>
): TArray<T>; static;
// Combine pairs of elements until only one element is left.
// Returns Default(T) for empty input.
class function Aggregate<T>(
const Entries: TArray<T>;
Aggregator: TAggregator<T>
): T; static;
// Combine pairs of elements until only one element is left.
// Returns the specified default for empty input.
class function AggregateOrDefault<T>(
const Entries: TArray<T>;
Aggregator: TAggregator<T>;
const Default: T
): T; static;
// Update existing items or add new ones into an ordered set by merging
// changes and resolving conflicts.
class function Merge<TData, TChanges>(
const Data: TArray<TData>;
const Changes: TArray<TChanges>;
CheckForConflicts: TConflictChecker<TData, TChanges>;
ResolveConflict: TConflictResolver<TData, TChanges>;
ConvertChange: TConvertRoutine<TChanges, TData>
): TArray<TData>; static;
// Find all parent-child relationships in an array
class function BuildTree<T>(
const Entries: TArray<T>;
ParentChecker: TParentChecker<T>
): TArray<TTreeNode<T>>; static;
{ --------------------------- Helper functions --------------------------- }
// A default function for checking equality of array elements
class function DefaultEqualityCheck<T>(const A, B: T): Boolean; static;
// A default function for ordering array elements.
// NOTE: the functions treats integers as unsigned
class function DefaultComparer<T>(const A, B: T): Integer; static;
end;
{ Internal Use }
type
// An anonymous callback for sorting. Internal use.
TQsortContext = reference to function (
KeyIndex: Integer;
ElementIndex: Integer
): Integer;
// Index comparer for the legacy qsort. Internal use only.
threadvar
SmartContextLegacy: TQsortContext;
// A CRT-compatible callback for sorting indexes on Win 7. Internal use.
function SortCallbackLegacy(
key: Pointer;
element: Pointer
): Integer; cdecl;
// A CRT-compatible callback for sorting indexes on Win 8+. Internal use.
function SortCallback(
context: Pointer;
key: Pointer;
element: Pointer
): Integer; cdecl;
implementation
uses
Ntapi.crt, Ntapi.ntdef, NtUtils, NtUtils.Ldr;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ TArray }
class function TArray.Aggregate<T>;
begin
Result := AggregateOrDefault<T>(Entries, Aggregator, Default(T));
end;
class function TArray.AggregateOrDefault<T>;
var
i: Integer;
begin
if Length(Entries) <= 0 then
Exit(Default);
Result := Entries[0];
for i := 1 to High(Entries) do
Result := Aggregator(Result, Entries[i]);
end;
class function TArray.BinarySearch<T>;
begin
if not Assigned(Comparer) then
Comparer := DefaultComparer<T>;
try
Result := BinarySearchEx<T>(Entries,
function (const Entry: T): Integer
begin
Result := Comparer(Entry, Element);
if ReversedOrder then
Result := -Result;
end
);
finally
// For some reason, the anonymous function from above doesn't want to
// capture ownership over the default comparer. Explicitly release the
// variable here as a workaround.
Comparer := nil;
end;
end;
class function TArray.BinarySearchEx<T>;
var
Start, Finish, Middle: Integer;
AtStart, AtFinish: Integer;
begin
if Length(Entries) = 0 then
Exit(-1);
// Start with full range
Start := Low(Entries);
Finish := High(Entries);
while Start <> Finish do
begin
Middle := (Start + Finish) shr 1;
// Prevent infinite loops
if Middle = Start then
Break;
// Move one boundary into the middle on each iteration
if BinarySearcher(Entries[Middle]) < 0 then
Start := Middle
else
Finish := Middle;
end;
// Compare to the start
AtStart := BinarySearcher(Entries[Start]);
// Found at start
if AtStart = 0 then
Exit(Start);
// Compare to the finish
AtFinish := BinarySearcher(Entries[Finish]);
// Found at finish
if AtFinish = 0 then
Exit(Finish);
// Insert between start and finish
if (AtStart < 0) xor (AtFinish < 0) then
Exit(-Start - 2);
// Insert after finish
if AtFinish < 0 then
Exit(-Finish - 2);
// Insert before start
Exit(-1);
end;
class function TArray.BuildTree<T>;
var
i, j, k, Count: Integer;
begin
SetLength(Result, Length(Entries));
// Copy entries
for i := 0 to High(Entries) do
begin
Result[i].Entry := Entries[i];
Result[i].Index := i;
end;
// Fill parents as references to array elements
for i := 0 to High(Entries) do
for j := 0 to High(Entries) do
if (i <> j) and ParentChecker(Entries[j], Entries[i]) then
begin
Result[i].Parent := @Result[j];
Break;
end;
// Fill children, also as references
for i := 0 to High(Entries) do
begin
Count := 0;
for j := 0 to High(Entries) do
if Result[j].Parent = @Result[i] then
Inc(Count);
SetLength(Result[i].Children, Count);
k := 0;
for j := 0 to High(Entries) do
if Result[j].Parent = @Result[i] then
begin
Result[i].Children[k] := @Result[j];
Inc(k);
end;
end;
end;
class function TArray.Collect<T>;
var
Element: T;
begin
Result := nil;
for Element in Iterator do
begin
SetLength(Result, Succ(Length(Result)));
Result[High(Result)] := Element;
end;
end;
class function TArray.Contains<T>;
var
i: Integer;
begin
if not Assigned(EqualityCheck) then
EqualityCheck := DefaultEqualityCheck<T>;
for i := 0 to High(Entries) do
if EqualityCheck(Entries[i], Element) then
Exit(True);
Result := False;
end;
class function TArray.ContainsMatch<T>;
var
i: Integer;
begin
for i := 0 to High(Entries) do
if Condition(Entries[i]) then
Exit(True);
Result := False;
end;
class function TArray.ContainsMatchEx<T>;
var
i: Integer;
begin
for i := 0 to High(Entries) do
if Condition(i, Entries[i]) then
Exit(True);
Result := False;
end;
class function TArray.Convert<T1, T2>;
var
i, j: Integer;
begin
SetLength(Result, Length(Entries));
j := 0;
for i := 0 to High(Entries) do
if Converter(Entries[i], Result[j]) then
Inc(j);
// Trim failed conversions
if Length(Result) <> j then
SetLength(Result, j);
end;
class function TArray.ConvertEx<T1, T2>;
var
i, j: Integer;
begin
SetLength(Result, Length(Entries));
j := 0;
for i := 0 to High(Entries) do
if ConverterEx(i, Entries[i], Result[j]) then
Inc(j);
// Trim failed conversions
if Length(Result) <> j then
SetLength(Result, j);
end;
class function TArray.ConvertFirst<T1, T2>;
begin
Result := ConvertFirstOrDefault<T1, T2>(Entries, Converter, Default(T2));
end;
class function TArray.ConvertFirstOrDefault<T1, T2>;
var
i: Integer;
begin
for i := 0 to High(Entries) do
if Converter(Entries[i], Result) then
Exit;
Result := Default;
end;
class function TArray.Count<T>;
var
i: Integer;
begin
if not Assigned(EqualityCheck) then
EqualityCheck := DefaultEqualityCheck<T>;
Result := 0;
for i := 0 to High(Entries) do
if EqualityCheck(Entries[i], Element) then
Inc(Result);
end;
class function TArray.CountMatches<T>;
var
i: Integer;
begin
Result := 0;
for i := 0 to High(Entries) do
if Condition(Entries[i]) then
Inc(Result);
end;
class function TArray.DefaultComparer<T>;
var
StringA: String absolute A;
StringB: String absolute B;
AnsiStringA: AnsiString absolute A;
AnsiStringB: AnsiString absolute B;
begin
if TypeInfo(T) = TypeInfo(String) then
Result := wcscmp(PWideChar(StringA), PWideChar(StringB))
else if TypeInfo(T) = TypeInfo(AnsiString) then
Result := strcmp(PAnsiChar(AnsiStringA), PAnsiChar(AnsiStringB))
else
Result := memcmp(@A, @B, SizeOf(T));
end;
class function TArray.DefaultEqualityCheck<T>;
var
StringA: String absolute A;
StringB: String absolute B;
AnsiStringA: AnsiString absolute A;
AnsiStringB: AnsiString absolute B;
begin
if TypeInfo(T) = TypeInfo(String) then
Result := StringA = StringB
else if TypeInfo(T) = TypeInfo(AnsiString) then
Result := AnsiStringA = AnsiStringB
else
Result := memcmp(@A, @B, SizeOf(T)) = 0;
end;
class function TArray.Filter<T>;
var
i, Count: Integer;
begin
SetLength(Result, Length(Entries));
Count := 0;
for i := 0 to High(Entries) do
if Condition(Entries[i]) xor (Action = ftExclude) then
begin
Result[Count] := Entries[i];
Inc(Count);
end;
// Trim unused slots
if Length(Result) <> Count then
SetLength(Result, Count);
end;
class function TArray.FilterEx<T>;
var
i, Count: Integer;
begin
SetLength(Result, Length(Entries));
Count := 0;
for i := 0 to High(Entries) do
if Condition(i, Entries[i]) xor (Action = ftExclude) then
begin
Result[Count] := Entries[i];
Inc(Count);
end;
// Trim unused slots
if Length(Result) <> Count then
SetLength(Result, Count);
end;
class procedure TArray.FilterInline<T>;
var
i, j: Integer;
begin
j := 0;
for i := 0 to High(Entries) do
if Condition(Entries[i]) xor (Action = ftExclude) then
begin
// j grows slower then i; move elements backwards overwriting ones that
// don't match
if i <> j then
Entries[j] := Entries[i];
Inc(j);
end;
// Trim released slots
if Length(Entries) <> j then
SetLength(Entries, j);
end;
class procedure TArray.FilterInlineEx<T>;
var
i, j: Integer;
begin
j := 0;
for i := 0 to High(Entries) do
if Condition(i, Entries[i]) xor (Action = ftExclude) then
begin
// j grows slower then i; move elements backwards overwriting ones that
// don't match
if i <> j then
Entries[j] := Entries[i];
Inc(j);
end;
// Trim released slots
if Length(Entries) <> j then
SetLength(Entries, j);
end;
class procedure TArray.FilterInlineVar<T>;
var
i, j: Integer;
begin
j := 0;
for i := 0 to High(Entries) do
if Condition(Entries[i]) xor (Action = ftExclude) then
begin
// j grows slower then i; move elements backwards overwriting ones that
// don't match
if i <> j then
Entries[j] := Entries[i];
Inc(j);
end;
// Trim released slots
if Length(Entries) <> j then
SetLength(Entries, j);
end;
class function TArray.FindFirst<T>;
begin
Result := FindFirstOrDefault<T>(Entries, Condition, Default(T));
end;
class function TArray.FindFirstOrDefault<T>;
var
i: Integer;
begin
for i := 0 to High(Entries) do
if Condition(Entries[i]) then
Exit(Entries[i]);
Result := Default;
end;
class function TArray.Flatten<T>;
var
Count, i, j: Integer;
begin
// No need to copy when provided with a single array
if Length(Arrays) = 1 then
Exit(Arrays[0]);
// Compute the total number of elements
Count := 0;
for i := 0 to High(Arrays) do
Inc(Count, Length(Arrays[i]));
SetLength(Result, Count);
// Fill them preserving order
Count := 0;
for i := 0 to High(Arrays) do
for j := 0 to High(Arrays[i]) do
begin
Result[Count] := Arrays[i][j];
Inc(Count);
end;
end;
class function TArray.FlattenEx<T1, T2>;
var
Expanded: TArray<TArray<T2>>;
i, j, Count: Integer;
begin
// Convert each element into an array
Expanded := TArray.Map<T1, TArray<T2>>(Entries, Converter);
// Count total elements
Count := 0;
for i := 0 to High(Expanded) do
Inc(Count, Length(Expanded[i]));
SetLength(Result, Count);
// Flatten them into one array
Count := 0;
for i := 0 to High(Expanded) do
for j := 0 to High(Expanded[i]) do
begin
Result[Count] := Expanded[i, j];
Inc(Count);
end;
end;
class procedure TArray.ForAll<T>;
var
i: Integer;
begin
for i := 0 to High(Entries) do
Callback(Entries[i]);
end;
class function TArray.Generate<T>;
var
i: Integer;
begin
SetLength(Result, Count);
for i := 0 to High(Result) do
Result[i] := Generator(i);
end;
class function TArray.GroupBy<TElement, TKey>;
var
i, j, Count: Integer;
KeyIndexes: TArray<Integer>;
Found: Boolean;
Key: TKey;
begin
if not Assigned(CompareKeys) then
CompareKeys := DefaultEqualityCheck<TKey>;
SetLength(KeyIndexes, Length(Entries));
SetLength(Result, Length(Entries));
Count := 0;
for i := 0 to High(Entries) do
begin
Key := LookupKey(Entries[i]);
Found := False;
// Check if we already encountered this key
for j := Pred(Count) downto 0 do
if CompareKeys(Key, Result[j].Key) then
begin
// Attach the entry to this bucket
KeyIndexes[i] := j;
Found := True;
Break;
end;
if not Found then
begin
// Create a new bucket for this key
Result[Count].Key := Key;
KeyIndexes[i] := Count;
Inc(Count);
end;
end;
// Trim unused buckets
if Length(Result) <> Count then
SetLength(Result, Count);
for j := 0 to High(Result) do
begin
Count := 0;
// Count the number of elements that belong to this key
for i := 0 to High(KeyIndexes) do
if KeyIndexes[i] = j then
Inc(Count);
SetLength(Result[j].Values, Count);
Count := 0;
// Copy entries for the key
for i := 0 to High(KeyIndexes) do
if KeyIndexes[i] = j then
begin
Result[j].Values[Count] := Entries[i];
Inc(Count);
end;
end;
end;
class function TArray.IndexOf<T>;
var
i: Integer;
begin
if not Assigned(EqualityCheck) then
EqualityCheck := DefaultEqualityCheck<T>;
for i := 0 to High(Entries) do
if EqualityCheck(Entries[i], Element) then
Exit(i);
Result := -1;
end;
class function TArray.IndexOfMatch<T>;
var
i: Integer;
begin
for i := 0 to High(Entries) do
if Condition(Entries[i]) then
Exit(i);
Result := -1;
end;
class function TArray.InsertSorted<T>;
begin
Result := BinarySearch<T>(Entries, Element, Comparer, ReversedOrder);
if Result < 0 then
System.Insert(Element, Entries, -(Result + 1)) // No collisions; insert
else if DuplicateHandling = dhInsert then
System.Insert(Element, Entries, Result) // Collision; insert anyway
else if DuplicateHandling = dhOverwrite then
Entries[Result] := Element // Collision; overwrite
else if DuplicateHandling = dhSkip then
; // Collision; skip
end;
class function TArray.Map<T1, T2>;
var
i: Integer;
begin
SetLength(Result, Length(Entries));
for i := 0 to High(Entries) do
Result[i] := Converter(Entries[i]);
end;
class function TArray.MapEx<T1, T2>;
var
i: Integer;
begin
SetLength(Result, Length(Entries));
for i := 0 to High(Entries) do
Result[i] := ConverterEx(i, Entries[i]);
end;
class function TArray.Merge<TData, TChanges>;
var
ConflictIndexes: TArray<Integer>;
NewEntries: TArray<TData>;
i, j: Integer;
begin
SetLength(ConflictIndexes, Length(Changes));
// Find indexes of data entries with which we have conflicts
for i := 0 to High(Changes) do
begin
ConflictIndexes[i] := -1;
for j := 0 to High(Data) do