-
Notifications
You must be signed in to change notification settings - Fork 2
/
ArrayEx.pas
1262 lines (1074 loc) · 30.2 KB
/
ArrayEx.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 ArrayEx;
interface
{$IFNDEF DEBUG}
{$D-$L-}
{$ENDIF}
// (c) Roman Lyakh [email protected]
// Freeware
{$IFDEF FPC}
{$MODE DELPHIUNICODE}
{$DEFINE MODERNCOMPILER}
{$WARN 3057 off : An inherited method is hidden by "$1"}
{$WARN 5079 off : Unit "$1" is experimental}
{$ELSE}
{$IF CompilerVersion>27}
{$DEFINE MODERNCOMPILER}
{$IFNDEF DEBUG}
{$DEFINE Inline}
{$ENDIF}
{$ENDIF}
{$IF CompilerVersion>=34}
{$DEFINE MANAGEDRECORDS}
{$ENDIF}
{$ENDIF}
uses
Classes, Generics.Defaults, Generics.Collections, SyncObjs, SysUtils, Math;
const
HashTableDefExpectedCount = 64;
type
// -------------------------------------------------------------------------
// ------------------------- Support Types ---------------------------------
// -------------------------------------------------------------------------
{$REGION 'Support Types'}
// Integer Array
TIntegerArray = TArray<integer>;
// String Array
TStringArray = TArray<string>;
// Comparator types
TCompareResult = (crLess,crEqual,crMore);
TCompareMode = (cmAscending, cmDescending);
// User comparator types
{$IFDEF FPC}
TCompareValue<T> = function(const Item1,Item2: T): TCompareResult;
TAction<T> = procedure(const Item: T);
{$ELSE}
TCompareValue<T> = reference to function(const Item1,Item2: T): TCompareResult;
TAction<T> = reference to procedure(const Item: T);
{$ENDIF}
TPredicate<T> = function(const Item: T): Boolean of object;
EIndexDuplicateKeyException = class(Exception);
TArrayExCompareUnchangedResult = record
Old: integer;
New: integer;
end;
TArrayExCompareResults = record
Added: TArray<integer>;
Removed: TArray<integer>;
Unchanged: TArray<TArrayExCompareUnchangedResult>;
end;
{$ENDREGION}
// -------------------------------------------------------------------------
// --------------------- TArrayEx (Extended array) -------------------------
// -------------------------------------------------------------------------
{$REGION 'TArrayEx'}
{ TArrayEx }
TArrayEx<T> = record
public
// Direct item access (unsafe)
Items : array of T;
// User Tag
Tag : integer;
// Need to free elements on destroy
DoFreeData : Boolean;
private
{$IFDEF MODERNCOMPILER}
type
TCollection = class;
TItemEnumerator = class(TEnumerator<T>)
private
FParent : TCollection;
FIndex : Integer;
function GetCurrent: T;
protected
function DoGetCurrent: T; override;
function DoMoveNext: Boolean; override;
public
constructor Create(Parent: TCollection);
property Current: T read GetCurrent;
function MoveNext: Boolean;
end;
{ TCollection }
TCollection = class(TEnumerable<T>)
private
FParent : pointer;
function GetCount: Integer;
protected
function DoGetEnumerator: TEnumerator<T>; override;
{$IFDEF FPC}
function GetPtrEnumerator: TEnumerator<PT>; overload;
{$ENDIF}
public
constructor Create(const ArrayEx: TArrayEx<T>);
function GetEnumerator: TEnumerator<T>; reintroduce;
property Count: Integer read GetCount;
end;
{$ENDIF}
var
{$IFNDEF MANAGEDRECORDS}
FInitCapacity : string; // Workaround for older delphi
{$ENDIF}
FIndexArray : array of array of integer; // Hash structure
FComparer : IEqualityComparer<T>; // Comparer
FCapacity : integer; // Amount of allocated memory for items
FArrayCount : PNativeInt;
FOptimisation : Boolean; // SetLength optimisations
{$IFDEF MODERNCOMPILER}
FEnumInit : string; // Iterator initialisation flag
FCollection : TCollection; // Enumerator Collection
{$ENDIF}
function GetElements(Index: integer): T; {$IFDEF Inline} inline; {$ENDIF}
procedure SetElements(Index: integer; const Value: T); {$IFDEF Inline} inline; {$ENDIF}
procedure QuickSortA(const Comparer: IComparer<T>; L, R: Integer); {$IFDEF Inline} inline; {$ENDIF}
procedure QuickSortB(L, R: Integer; CompareEvt: TCompareValue<T>; Less, More: TCompareResult); {$IFDEF Inline} inline; {$ENDIF}
procedure HashClear(NewIndexMod: integer); {$IFDEF Inline} inline; {$ENDIF}
procedure HashAdd(const Value: T; Index: integer); {$IFDEF Inline} inline; {$ENDIF}
function GetHash(const Value: T): integer; {$IFDEF Inline} inline; {$ENDIF}
procedure SetIndex(Index: Integer; const Value: T); {$IFDEF Inline} inline; {$ENDIF}
procedure SetCount(const Value: integer); {$IFDEF Inline} inline; {$ENDIF}
function GetCount: integer; {$IFDEF Inline} inline; {$ENDIF}
function GetHigh: integer; {$IFDEF Inline} inline; {$ENDIF}
procedure SetHigh(const Value: integer); {$IFDEF Inline} inline; {$ENDIF}
function GetLow: integer; {$IFDEF Inline} inline; {$ENDIF}
procedure FreeElement(Num: integer); {$IFDEF Inline} inline; {$ENDIF}
procedure SetLengthFast(NewValue: integer);
procedure SetOptimisation(const Value: Boolean);
function GetFirst: T;
function GetLast: T;
procedure SetFirst(const Value: T);
procedure SetLast(const Value: T);
{$IFDEF FPC}
function InternalAdd(Value: T): integer;
{$ENDIF}
public
// Constructor
constructor Create(DoFreeData: boolean);
// Access to elements by default
property Elements[Index: integer]: T read GetElements write SetElements; default;
// Clear array
procedure Clear; overload;
// Add item to the end of the array
function Add(Value: T): integer; overload; {$IFDEF Inline} inline; {$ENDIF}
function Add(const Values: array of T): integer; overload;
function Add(const Values: TArrayEx<T>): integer; overload;
function AddUnique(Value: T): integer; {$IFDEF Inline} inline; {$ENDIF}
// Insert element by index
procedure Insert(Index: integer; Value: T); overload; {$IFDEF Inline} inline; {$ENDIF}
procedure Insert(Index: integer; const Values: array of T); overload;
procedure Insert(Index: integer; const Values: TArrayEx<T>); overload;
// Delete element(s) by index
procedure Delete(Index: integer); {$IFDEF Inline} inline; {$ENDIF}
procedure DeleteRange(Index, Count: integer); {$IFDEF Inline} inline; {$ENDIF}
procedure DeleteFirst; {$IFDEF Inline} inline; {$ENDIF}
procedure DeleteLast; {$IFDEF Inline} inline; {$ENDIF}
function Copy(Index, Count: integer): TArrayEx<T>;
// Assign data from Source
procedure Assign(Source: TArrayEx<T>);
// Create Hash Table
procedure CreateIndex(IndexMod: integer = -1);
// Clear Hash Table
procedure DropIndex;
procedure ClearIndex;
// Array items count
property Count: integer read GetCount write SetCount;
// First element index
property Low: integer read GetLow;
// Last element index
property High: integer read GetHigh write SetHigh;
// if Count=0 ?
function IsEmpty: boolean;
// First and last elements of array
property First: T read GetFirst write SetFirst;
property Last: T read GetLast write SetLast;
// Memory management optimization
property Optimisation: Boolean read FOptimisation write SetOptimisation;
// Search of element in array
function Exists(Value: T): boolean; overload; {$IFDEF Inline} inline; {$ENDIF}
function Exists(Values: array of T; NeedAllValues: boolean = False): boolean; overload;
function Exists(Values: TArrayEx<T>; NeedAllValues: boolean = False): boolean; overload;
// Get first index in array for a value
function IndexOf(Value: T): integer; {$IFDEF Inline} inline; {$ENDIF}
{$IFNDEF FPC}
// Get all indexes in array for a value
function IndexesOf(Value: T): TArrayEx<integer>; {$IFDEF Inline} inline; {$ENDIF}
{$ELSE}
// Get all indexes in array for a value
function IndexesOf(Value: T): TArray<integer>; {$IFDEF Inline} inline; {$ENDIF}
{$ENDIF}
function IndexIsValid(Index: integer): boolean;
// Sort array
procedure Sort(Comparer: IComparer<T> = nil); overload;
procedure Sort(CompareEvt: TCompareValue<T>; Mode: TCompareMode = cmAscending); overload;
property Comparer: IEqualityComparer<T> read FComparer write FComparer;
// Serialisation to string
{$IFNDEF FPC}
function ToString: string; overload;
function ToString(Delimeter : string; Quotes: string = ''; Prefix: string = ''): string; overload;
{$ENDIF}
{$IFDEF MODERNCOMPILER}
function GetEnumerator: TItemEnumerator;
function Collection: TCollection;
{$ENDIF}
function CompareValuesWith(NewData: TArrayEx<T>): TArrayExCompareResults;
{$IFDEF MANAGEDRECORDS}
// Init array
class operator Initialize(out Dest: TArrayEx<T>);
{$ENDIF}
// Class operators
class operator Add(const A,B: TArrayEx<T>): TArrayEx<T>; overload;
class operator Add(const A: TArrayEx<T>; const B: array of T): TArrayEx<T>; overload;
class operator Add(const A: array of T; const B: TArrayEx<T>): TArrayEx<T>; overload;
class operator Implicit(const A: TArrayEx<T>): TArray<T>; overload;
class operator Implicit(const A: TArray<T>): TArrayEx<T>; overload;
class operator Implicit(const A: array of T): TArrayEx<T>; overload;
{$IFDEF MODERNCOMPILER}
class operator In(const A,B: TArrayEx<T>): Boolean; overload;
class operator In(const A: array of T; B: TArrayEx<T>): Boolean; overload;
{$ENDIF}
class operator Equal(const A,B: TArrayEx<T>): Boolean;
class operator NotEqual(const A,B: TArrayEx<T>): Boolean;
end;
{$ENDREGION}
var
TArrayExUseOptimisation : boolean = True;
implementation
uses TypInfo, RTTI;
type
{$IFNDEF FPC}
RTLString = string;
{$ELSE}
RTLString = AnsiString;
{$ENDIF}
function CompareIntegerLocal(const Value1, Value2: Integer): TCompareResult;
begin
if Value1<Value2 then begin
Result:=crLess;
end else begin
if Value1>Value2 then begin
Result:=crMore;
end else begin
Result:=crEqual;
end;
end;
end;
function CompareInt64Local(const Value1, Value2: Int64): TCompareResult;
begin
if Value1<Value2 then begin
Result:=crLess;
end else begin
if Value1>Value2 then begin
Result:=crMore;
end else begin
Result:=crEqual;
end;
end;
end;
function CompareStringLocal(const Value1, Value2: String): TCompareResult;
begin
if Value1<Value2 then begin
Result:=crLess;
end else begin
if Value1>Value2 then begin
Result:=crMore;
end else begin
Result:=crEqual;
end;
end;
end;
function CompareVariantLocal(const Value1, Value2: Variant): TCompareResult;
begin
if Value1<Value2 then begin
Result:=crLess;
end else begin
if Value1>Value2 then begin
Result:=crMore;
end else begin
Result:=crEqual;
end;
end;
end;
{$REGION 'TArrayEx Implementation'}
{ TArrayEx }
procedure TArrayEx<T>.SetCount(const Value: integer);
begin
SetLengthFast(Value);
if Length(FIndexArray)>0 then begin
CreateIndex(Value);
end;
end;
procedure TArrayEx<T>.SetHigh(const Value: integer);
begin
SetCount(Value+1);
end;
procedure TArrayEx<T>.SetIndex(Index: Integer; const Value: T);
begin
if Length(FIndexArray)=0 then Exit;
if Index>=Length(FIndexArray) then begin
CreateIndex(Index*2);
end else begin
HashAdd(Value,Index);
end;
end;
//1. Benchmark TArrayEx<Integer>
//Add 10.000.000 integers. 328 msec. 78 msec (Alt Add). 32 msec (optimised).
//Add 10.000 integers in 10.000.000 iterations. 812 msec.
//Locate 10.000 integers in 10.000.000 iterations. 813 msec.
function TArrayEx<T>.Add(Value: T): integer;
begin
Result:=Length(Items);
SetLengthFast(Result+1);
Items[Result]:=Value;
SetIndex(Result,Value);
end;
function TArrayEx<T>.Add(const Values: array of T): integer;
var
i: Integer;
begin
Result:=Length(Items);
SetLengthFast(Result+Length(Values));
for i:=0 to System.High(Values) do begin
Items[Result]:=Values[i];
SetIndex(Result,Values[i]);
end;
end;
function TArrayEx<T>.AddUnique(Value: T): integer;
begin
Result:=IndexOf(Value);
if Result<0 then begin
{$IFDEF FPC}
Result:=InternalAdd(Value);
{$ELSE}
Result:=Add(Value);
{$ENDIF}
end;
end;
procedure TArrayEx<T>.Assign(Source: TArrayEx<T>);
var
i: integer;
begin
Clear;
Count:=Source.Count;
for i:=0 to Source.High do begin
Items[i]:=Source.Items[i];
end;
end;
procedure TArrayEx<T>.Delete(Index: integer);
var
i: Integer;
begin
if (Index<0) or (Index>High) then Exit;
if DoFreeData and (PTypeInfo(TypeInfo(TValue)).Kind=tkClass) then begin
FreeElement(Index);
end;
for i:=Index+1 to High do begin
Items[i-1]:=Items[i];
end;
SetLengthFast(Length(Items)-1);
CreateIndex(Length(FIndexArray));
end;
procedure TArrayEx<T>.DeleteFirst;
begin
Delete(0);
end;
procedure TArrayEx<T>.DeleteLast;
begin
Delete(High);
end;
procedure TArrayEx<T>.DeleteRange(Index, Count: integer);
var
i: Integer;
begin
if (Index<0) or (Index>High) then Exit;
if (Count<1) then Exit;
if Index+Count>Self.Count then Count:=Self.Count-Index;
if DoFreeData and (PTypeInfo(TypeInfo(TValue)).Kind=tkClass) then begin
for i:=Index to Index+Count-1 do begin
FreeElement(i);
end;
end;
for i:=Index+Count to High do begin
Items[i-Count]:=Items[i];
end;
SetLengthFast(Length(Items)-Count);
CreateIndex(Length(FIndexArray));
end;
procedure TArrayEx<T>.DropIndex;
begin
HashClear(0);
end;
{$IFNDEF FPC}
function TArrayEx<T>.IndexesOf(Value: T): TArrayEx<integer>;
var
i,m : integer;
begin
if Count<=50 then begin
if not Assigned(FComparer) then begin
FComparer:=TEqualityComparer<T>.Default;
end;
for i:=0 to High do begin
if FComparer.Equals(Items[i],Value) then begin
Result.Add(i);
end;
end;
Exit;
end;
m:=Length(FIndexArray);
if m=0 then begin
CreateIndex;
m:=Length(FIndexArray);
end;
m:=Abs(GetHash(Value) mod m);
Result.Clear;
for i:=0 to System.High(FIndexArray[m]) do begin
if FComparer.Equals(Items[FIndexArray[m,i]],Value) then begin
Result.Add(FIndexArray[m,i]);
end;
end;
Result.Sort;
end;
function TArrayEx<T>.IndexIsValid(Index: integer): boolean;
begin
Result:=(Index>=0) and (Index<=High);
end;
{$ELSE}
function TArrayEx<T>.IndexesOf(Value: T): TArray<integer>;
var
i,m,n : integer;
begin
if Count<=50 then begin
if not Assigned(FComparer) then begin
FComparer:=TEqualityComparer<T>.Default;
end;
for i:=0 to High do begin
if FComparer.Equals(Items[i]],Value) then begin
Exit(i);
end;
end;
Exit(-1);
end;
m:=Length(FIndexArray);
if m=0 then begin
CreateIndex;
m:=Length(FIndexArray);
end;
m:=Abs(GetHash(Value) mod m);
SetLength(Result,0);
n:=-1;
for i:=0 to System.High(FIndexArray[m]) do begin
if FComparer.Equals(Items[FIndexArray[m,i]],Value) then begin
inc(n);
SetLength(Result,n+1);
Result[n]:=FIndexArray[m,i];
end;
end;
end;
{$ENDIF}
function TArrayEx<T>.IndexOf(Value: T): integer;
var
i,m,Hash : integer;
begin
if Count<=50 then begin
if not Assigned(FComparer) then begin
FComparer:=TEqualityComparer<T>.Default;
end;
for i:=0 to High do begin
if FComparer.Equals(Items[i],Value) then begin
Exit(i);
end;
end;
Exit(-1);
end;
if Length(FIndexArray)=0 then begin
CreateIndex;
end;
Hash:=GetHash(Value);
m:=Abs(Hash mod Length(FIndexArray));
for i:=0 to System.High(FIndexArray[m]) do begin
if FComparer.Equals(Items[FIndexArray[m,i]],Value) then begin
Exit(FIndexArray[m,i]);
end;
end;
Result:=-1;
end;
procedure TArrayEx<T>.Insert(Index: integer; const Values: TArrayEx<T>);
begin
Insert(Index,Values.Items);
end;
procedure TArrayEx<T>.Insert(Index: integer; const Values: array of T);
var
i: Integer;
begin
if (Index<0) or (Index>Count) then Exit;
SetLengthFast(Length(Items)+length(Values));
for i:=High downto Index+length(Values) do begin
Items[i]:=Items[i-length(Values)];
end;
for i:=Index to System.High(Values) do begin
Items[Index+i]:=Values[i];
end;
CreateIndex(Length(FIndexArray));
end;
function TArrayEx<T>.IsEmpty: boolean;
begin
Result:=Count=0;
end;
function TArrayEx<T>.GetElements(Index: integer): T;
begin
Result:=Items[Index];
end;
function TArrayEx<T>.GetEnumerator: TItemEnumerator;
begin
Result := TItemEnumerator.Create(Self.Collection);
end;
function TArrayEx<T>.GetFirst: T;
begin
Result:=Items[0];
end;
function TArrayEx<T>.GetLast: T;
begin
Result:=Items[High];
end;
function TArrayEx<T>.GetLow: integer;
begin
Result:=0;
end;
procedure TArrayEx<T>.HashClear(NewIndexMod: integer);
begin
System.SetLength(FIndexArray,0);
SetLength(FIndexArray,NewIndexMod);
end;
function TArrayEx<T>.GetCount: integer;
begin
Result:=Length(Items);
end;
function TArrayEx<T>.GetHash(const Value: T): integer;
begin
Result:=TEqualityComparer<T>.Default.GetHashCode(Value);
end;
function TArrayEx<T>.GetHigh: integer;
begin
Result:=System.High(Items);
end;
procedure TArrayEx<T>.HashAdd(const Value: T; Index: integer);
var
n,m : integer;
begin
m:=Abs(GetHash(Value) mod Length(FIndexArray));
n:=length(FIndexArray[m]);
SetLength(FIndexArray[m],n+1);
FIndexArray[m,n]:=Index;
end;
procedure TArrayEx<T>.FreeElement(Num: integer);
begin
try
PObject(@Items[num])^.Free;
Items[num]:=Default(T);
except
end;
end;
procedure TArrayEx<T>.Clear;
var
i: Integer;
begin
if DoFreeData and (PTypeInfo(TypeInfo(T)).Kind=tkClass) then begin
for i:=0 to High do begin
FreeElement(i);
end;
end;
{$IFNDEF MANAGEDRECORDS}
FInitCapacity:='';
{$ENDIF}
FArrayCount:=nil;
SetLengthFast(0);
HashClear(Length(FIndexArray));
end;
procedure TArrayEx<T>.ClearIndex;
begin
HashClear(0);
end;
function TArrayEx<T>.CompareValuesWith(NewData: TArrayEx<T>): TArrayExCompareResults;
var
i,Cnt: integer;
begin
Cnt:=0;
for i:=0 to NewData.High do begin
if Exists(NewData[i]) then Continue;
inc(Cnt);
if Length(Result.Added)<Cnt then begin
SetLength(Result.Added,Cnt+1024);
end;
Result.Added[Cnt-1]:=i;
end;
SetLength(Result.Added,Cnt);
Cnt:=0;
for i:=0 to High do begin
if NewData.Exists(Items[i]) then Continue;
inc(Cnt);
if Length(Result.Removed)<Cnt then begin
SetLength(Result.Removed,Cnt+1024);
end;
Result.Removed[Cnt-1]:=i;
end;
SetLength(Result.Removed,Cnt);
Cnt:=0;
for i:=0 to High do begin
var n:=NewData.IndexOf(Items[i]);
if n<0 then Continue;
inc(Cnt);
if Length(Result.Unchanged)<Cnt then begin
SetLength(Result.Unchanged,Cnt+1024);
end;
Result.Unchanged[Cnt-1].Old:=i;
Result.Unchanged[Cnt-1].New:=n;
end;
SetLength(Result.Unchanged,Cnt);
end;
function TArrayEx<T>.Copy(Index, Count: integer): TArrayEx<T>;
var
i : integer;
begin
Result.Clear;
Result.Count:=Count;
for i:=0 to Count-1 do begin
Result.Items[i]:=Items[Index+i];
end;
end;
constructor TArrayEx<T>.Create(DoFreeData: boolean);
begin
Tag:=0;
Clear;
Self.DoFreeData:=DoFreeData;
end;
procedure TArrayEx<T>.CreateIndex(IndexMod: integer = -1);
var
i : integer;
begin
FComparer:=TEqualityComparer<T>.Default;
if IndexMod=-1 then begin
if Count=0 then begin
IndexMod:=HashTableDefExpectedCount;
end else begin
IndexMod:=Count;
end;
end;
HashClear(IndexMod);
if IndexMod>0 then begin
for i:=Low to High do begin
HashAdd(Items[i],i);
end;
end;
end;
procedure TArrayEx<T>.Insert(Index: integer; Value: T);
var
i: Integer;
begin
if (Index<0) or (Index>Count) then Exit;
SetLengthFast(Length(Items)+1);
for i:=High downto Index+1 do begin
Items[i]:=Items[i-1];
end;
Items[Index]:=Value;
CreateIndex(Length(FIndexArray));
end;
procedure TArrayEx<T>.SetElements(Index: integer; const Value: T);
begin
if (Index<0) or (Index>High) then Exit;
Items[Index]:=Value;
SetIndex(Index,Value);
end;
procedure TArrayEx<T>.SetFirst(const Value: T);
begin
Items[0]:=Value;
end;
procedure TArrayEx<T>.SetLast(const Value: T);
begin
Items[High]:=Value;
end;
procedure TArrayEx<T>.SetLengthFast(NewValue: integer);
{$IFNDEF FPC}
const
GrowLimit = 64;
{$ENDIF}
begin
{$IFNDEF MANAGEDRECORDS}
if FInitCapacity='' then begin
FInitCapacity:='Y';
FCapacity:=0;
end;
{$ENDIF}
if NewValue<=0 then begin
FCapacity:=0;
SetLength(Items,0);
end else begin
{$IFNDEF FPC}
if FOptimisation then begin
if (NewValue>FCapacity) or (NewValue shl 1<FCapacity) then begin
FCapacity:=Min(NewValue shl 1,NewValue+GrowLimit);
SetLength(Items,FCapacity);
FArrayCount:=PNativeInt(NativeInt(@Items[0])-SizeOf(NativeInt));
end;
FArrayCount^:=NewValue;
end else begin
FCapacity:=NewValue;
SetLength(Items,NewValue);
end;
{$ELSE}
FCapacity:=NewValue;
SetLength(Items,NewValue);
{$ENDIF}
end;
end;
procedure TArrayEx<T>.SetOptimisation(const Value: Boolean);
begin
FOptimisation:=Value;
if not Value then begin
SetLength(Items,Count);
end;
end;
{$IFDEF FPC}
function TArrayEx<T>.InternalAdd(Value: T): integer;
begin
Result:=Length(Items);
SetLengthFast(Result+1);
Items[Result]:=Value;
SetIndex(Result,Value);
end;
{$ENDIF}
procedure TArrayEx<T>.QuickSortA(const Comparer: IComparer<T>; L, R: Integer);
var
I, J: Integer;
pivot, temp: T;
begin
if (Length(Items) = 0) or ((R - L) <= 0) then
Exit;
repeat
I := L;
J := R;
pivot := Items[L + (R - L) shr 1];
repeat
while Comparer.Compare(Items[I], pivot) < 0 do
Inc(I);
while Comparer.Compare(Items[J], pivot) > 0 do
Dec(J);
if I <= J then
begin
if I <> J then
begin
temp := Items[I];
Items[I] := Items[J];
Items[J] := temp;
end;
Inc(I);
Dec(J);
end;
until I > J;
if L < J then
QuickSortA(Comparer, L, J);
L := I;
until I >= R;
end;
procedure TArrayEx<T>.Sort(Comparer: IComparer<T> = nil);
var
OIndexMod: integer;
begin
OIndexMod:=Length(FIndexArray);
if OIndexMod>0 then DropIndex;
if Comparer=nil then Comparer:=TComparer<T>.Default;
QuickSortA(Comparer, 0, High);
if OIndexMod>0 then CreateIndex(OIndexMod);
end;
procedure TArrayEx<T>.QuickSortB(L, R: Integer; CompareEvt: TCompareValue<T>; Less, More: TCompareResult);
var
I, J: Integer;
pivot, temp: T;
begin
if (Length(Items) = 0) or ((R - L) <= 0) then
Exit;
repeat
I := L;
J := R;
pivot := Items[L + (R - L) shr 1];
repeat
while CompareEvt(Items[I], pivot) = crLess do
Inc(I);
while CompareEvt(Items[J], pivot) = crMore do
Dec(J);
if I <= J then
begin
if I <> J then
begin
temp := Items[I];
Items[I] := Items[J];
Items[J] := temp;
end;
Inc(I);
Dec(J);
end;
until I > J;
if L < J then
QuickSortB(L, J, CompareEvt, Less, More);
L := I;
until I >= R;
end;
procedure TArrayEx<T>.Sort(CompareEvt: TCompareValue<T>; Mode: TCompareMode = cmAscending);
var
OIndexMod: integer;
Less,More: TCompareResult;
begin
OIndexMod:=Length(FIndexArray);
if OIndexMod>0 then DropIndex;
if not Assigned(CompareEvt) then Exit;
if Mode=cmAscending then begin
Less:=crLess;
More:=crMore;
end else begin
Less:=crMore;
More:=crLess;
end;
QuickSortB(Low,High,CompareEvt,Less,More);
if OIndexMod>0 then CreateIndex(OIndexMod);
end;
{$IFNDEF FPC}
function TArrayEx<T>.ToString(Delimeter: string; Quotes: string = ''; Prefix: string = ''): string;
var
i : Integer;
s : string;
begin
Result:='';
for i:=Low to High do begin
if (Result<>'') then begin
s:=Delimeter+Quotes+Prefix+TValue.From<T>(Items[i]).ToString+Quotes;
end else begin
s:=Quotes+Prefix+TValue.From<T>(Items[i]).ToString+Quotes;
end;
Result:=Result+s;
end;
end;
function TArrayEx<T>.ToString: string;
begin
Result:=ToString(';');