Repository navigation
Expand file tree
/
Copy pathArrayEx.pas
More file actions
681 lines (591 loc) · 16.7 KB
/
Copy pathArrayEx.pas
File metadata and controls
681 lines (591 loc) · 16.7 KB
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
unit ArrayEx;
{ ******************************************************************************
泛型动态数组的扩展.
来源:网络。
sample:
var
a, b, c: TArrayEx<Integer>;
f: TArrayEx<Single>;
s : TArrayEx<string>;
//
i: Integer;
z: array of Integer;
args:TArrayEx<string>;
begin
// assign
a := [2, 2, 3, 4];
s := ['haha', 'hello'];
// clone
b := a.Clone;
// 给元素赋值
b[0] := 5;
// operator +
c := a + b;
c := 88 + c + 99;
a.Append([10, 20, 30]);
a.Insert(2, 1000);
a.Insert(2, [1000, 45]);
a[0] := 7;
// Unique
c.Unique;
// Delete
c.Delete(0, 3);
// compare
if c = z then
// for in loop
for i in c do
if i = 0 then
begin
//
end;
//
f := [1, 2, 3.1415926];
f.Size := 200;
if f[0] = 1.0 then
begin
end;
args := ['38inch','45inch','XL','XL2','X','38inch','45inch'];
args.Unique;
//sort
args.Sort;
//search
if args.BinarySearch('XL',i) then
ShowMessageFmt('foud index:%d',[i]);
end;
****************************************************************************** }
interface
uses System.Generics.Defaults, System.SysUtils;
type
TArrayEx<T> = record
strict private
type
TEnumerator = class
private
FValue: TArray<T>;
FIndex: NativeInt;
function GetCurrent: T;
public
constructor Create(const AValue: TArray<T>);
function MoveNext: Boolean;
property Current: T read GetCurrent;
end;
public
function GetEnumerator(): TEnumerator;
strict private
FData: TArray<T>;
function GetRawData: TArray<T>;
function GetElements(Index: Integer): T;
procedure SetElements(Index: Integer; const Value: T);
private
class function EqualArray(A, B: TArray<T>): Boolean; static;
class function CompareT(const A, B: T): Boolean; static;
class procedure CopyArray(var FromArray, ToArray: TArray<T>;
FromIndex: NativeInt = 0; ToIndex: NativeInt = 0;
Count: NativeInt = -1); static;
class procedure MoveArray(var AArray: array of T;
FromIndex, ToIndex, Count: Integer); static;
class function DynArrayToTArray(const Value: array of T): TArray<T>; static;
class function Min(A, B: NativeInt): NativeInt; static;
procedure QuickSort(const Comparer: IComparer<T>; L, R: Integer);
public // operators
class operator Implicit(Value: TArray<T>): TArrayEx<T>; overload;
class operator Implicit(Value: array of T): TArrayEx<T>; overload;
(*
这个无解,Delphi不允许array of T作为返回值.也就是这个转换是被废了.只好用AssignTo
class operator Implicit(Value: TArrayEx<T>):array of T; overload;
*)
class operator Implicit(Value: TArrayEx<T>): TArray<T>; overload;
class operator Explicit(Value: TArrayEx<T>): TArray<T>; overload;
class operator Explicit(Value: array of T): TArrayEx<T>; overload;
class operator Add(A, B: TArrayEx<T>): TArrayEx<T>; overload;
class operator Add(A: TArrayEx<T>; const B: T): TArrayEx<T>; overload;
class operator Add(const A: T; B: TArrayEx<T>): TArrayEx<T>; overload;
class operator Add(A: TArrayEx<T>; B: array of T): TArrayEx<T>; overload;
class operator Add(A: array of T; B: TArrayEx<T>): TArrayEx<T>; overload;
class operator In (A: T; B: TArrayEx<T>): Boolean; overload;
//
class operator Equal(A, B: TArrayEx<T>): Boolean; overload;
class operator Equal(A: TArrayEx<T>; B: TArray<T>): Boolean; overload;
class operator Equal(A: TArray<T>; B: TArrayEx<T>): Boolean; overload;
class operator Equal(A: array of T; B: TArrayEx<T>): Boolean; overload;
class operator Equal(A: TArrayEx<T>; B: array of T): Boolean; overload;
public
procedure SetLen(Value: NativeInt); inline;
function GetLen: NativeInt; inline;
function ByteLen: NativeInt; inline;
class function Create(Value: array of T): TArrayEx<T>; overload; static;
class function Create(Value: TArrayEx<T>): TArrayEx<T>; overload; static;
class function Create(const Value: T): TArrayEx<T>; overload; static;
function Clone(): TArrayEx<T>;
procedure SetValue(Value: array of T);
function ToArray(): TArray<T>;
function SubArray(AFrom, ACount: NativeInt): TArrayEx<T>;
procedure Delete(AFrom, ACount: NativeInt); overload;
procedure Delete(AIndex: NativeInt); overload;
procedure Append(Values: TArrayEx<T>); overload;
procedure Append(const Value: T); overload;
procedure Append(Values: array of T); overload;
procedure Append(Values: TArray<T>); overload;
function Insert(AIndex: NativeInt; const Value: T): NativeInt; overload;
function Insert(AIndex: NativeInt; const Values: array of T)
: NativeInt; overload;
function Insert(AIndex: NativeInt; const Values: TArray<T>)
: NativeInt; overload;
function Insert(AIndex: NativeInt; const Values: TArrayEx<T>)
: NativeInt; overload;
procedure Unique();
// 排序
procedure Sort(); overload;
procedure Sort(const Comparer: IComparer<T>); overload;
procedure Sort(const Comparer: IComparer<T>;
Index, Count: Integer); overload;
// 搜索
function BinarySearch(const Item: T; out FoundIndex: Integer;
const Comparer: IComparer<T>; Index, Count: Integer): Boolean; overload;
function BinarySearch(const Item: T; out FoundIndex: Integer;
const Comparer: IComparer<T>): Boolean; overload;
function BinarySearch(const Item: T; out FoundIndex: Integer)
: Boolean; overload;
property Size: NativeInt read GetLen write SetLen;
property Len: NativeInt read GetLen write SetLen;
property RawData: TArray<T> read GetRawData;
property Elements[Index: Integer]: T read GetElements
write SetElements; default;
end;
implementation
uses System.RTLConsts;
class operator TArrayEx<T>.Add(A, B: TArrayEx<T>): TArrayEx<T>;
begin
Result := A.Clone;
Result.Append(B);
end;
class operator TArrayEx<T>.Add(A: TArrayEx<T>; const B: T): TArrayEx<T>;
begin
Result := A.Clone;
Result.Append(B);
end;
class operator TArrayEx<T>.Add(const A: T; B: TArrayEx<T>): TArrayEx<T>;
begin
Result.SetValue([A]);
Result.Append(B);
end;
class operator TArrayEx<T>.Add(A: TArrayEx<T>; B: array of T): TArrayEx<T>;
begin
Result := A.Clone;
Result.Append(B);
end;
class operator TArrayEx<T>.Add(A: array of T; B: TArrayEx<T>): TArrayEx<T>;
begin
Result.FData := DynArrayToTArray(A);
Result.Append(B);
end;
class operator TArrayEx<T>.In(A: T; B: TArrayEx<T>): Boolean;
var
Tmp: T;
begin
Result := False;
for Tmp in B.FData do
if CompareT(A, Tmp) then
begin
Result := True;
Break;
end;
end;
class operator TArrayEx<T>.Equal(A, B: TArrayEx<T>): Boolean;
begin
Result := EqualArray(A.FData, B.FData);
end;
class operator TArrayEx<T>.Equal(A: TArrayEx<T>; B: TArray<T>): Boolean;
begin
Result := EqualArray(A.FData, B);
end;
class operator TArrayEx<T>.Equal(A: TArray<T>; B: TArrayEx<T>): Boolean;
begin
Result := EqualArray(A, B.FData);
end;
class operator TArrayEx<T>.Equal(A: array of T; B: TArrayEx<T>): Boolean;
begin
Result := EqualArray(DynArrayToTArray(A), B.FData);
end;
class operator TArrayEx<T>.Equal(A: TArrayEx<T>; B: array of T): Boolean;
begin
Result := EqualArray(A.FData, DynArrayToTArray(B));
end;
function TArrayEx<T>.BinarySearch(const Item: T; out FoundIndex: Integer;
const Comparer: IComparer<T>; Index, Count: Integer): Boolean;
var
L, H: Integer;
mid, cmp: Integer;
begin
if (Index < Low(FData)) or ((Index > High(FData)) and (Count > 0)) or
(Index + Count - 1 > High(FData)) or (Count < 0) or (Index + Count < 0) then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
if Count = 0 then
begin
FoundIndex := Index;
Exit(False);
end;
Result := False;
L := Index;
H := Index + Count - 1;
while L <= H do
begin
mid := L + (H - L) shr 1;
cmp := Comparer.Compare(FData[mid], Item);
if cmp < 0 then
L := mid + 1
else
begin
H := mid - 1;
if cmp = 0 then
Result := True;
end;
end;
FoundIndex := L;
end;
function TArrayEx<T>.BinarySearch(const Item: T; out FoundIndex: Integer;
const Comparer: IComparer<T>): Boolean;
begin
Result := BinarySearch(Item, FoundIndex, Comparer, Low(FData), Length(FData));
end;
function TArrayEx<T>.BinarySearch(const Item: T;
out FoundIndex: Integer): Boolean;
begin
Result := BinarySearch(Item, FoundIndex, TComparer<T>.Default, Low(FData),
Length(FData));
end;
function TArrayEx<T>.ByteLen: NativeInt;
begin
Result := Length(FData) * Sizeof(T);
end;
class function TArrayEx<T>.Min(A, B: NativeInt): NativeInt;
begin
Result := A;
if Result > B then
Result := B;
end;
class procedure TArrayEx<T>.CopyArray(var FromArray, ToArray: TArray<T>;
FromIndex, ToIndex, Count: NativeInt);
var
i: Integer;
begin
if Count = 0 then
Exit;
if Count < 0 then
Count := Min(Length(FromArray), Length(ToArray));
if Length(FromArray) < (FromIndex + Count) then
Count := Length(FromArray) - FromIndex;
if Length(ToArray) < (ToIndex + Count) then
Count := Length(ToArray) - ToIndex;
if Count > 0 then
for i := 0 to Count - 1 do
ToArray[ToIndex + i] := FromArray[FromIndex + i];
end;
class procedure TArrayEx<T>.MoveArray(var AArray: array of T;
FromIndex, ToIndex, Count: Integer);
var
i: Integer;
begin
if Count > 0 then
begin
if FromIndex < ToIndex then
for i := Count - 1 downto 0 do
AArray[ToIndex + i] := AArray[FromIndex + i]
else if FromIndex > ToIndex then
for i := 0 to Count - 1 do
AArray[ToIndex + i] := AArray[FromIndex + i];
end;
end;
procedure TArrayEx<T>.QuickSort(const Comparer: IComparer<T>; L, R: Integer);
var
i, J: Integer;
pivot, temp: T;
begin
if (Length(FData) = 0) or ((R - L) <= 0) then
Exit;
repeat
i := L;
J := R;
pivot := FData[L + (R - L) shr 1];
repeat
while Comparer.Compare(FData[i], pivot) < 0 do
Inc(i);
while Comparer.Compare(FData[J], pivot) > 0 do
Dec(J);
if i <= J then
begin
if i <> J then
begin
temp := FData[i];
FData[i] := FData[J];
FData[J] := temp;
end;
Inc(i);
Dec(J);
end;
until i > J;
if L < J then
QuickSort(Comparer, L, J);
L := i;
until i >= R;
end;
class function TArrayEx<T>.DynArrayToTArray(const Value: array of T): TArray<T>;
var
i: Integer;
begin
SetLength(Result, Length(Value));
for i := Low(Value) to High(Value) do
Result[i] := Value[i];
end;
class function TArrayEx<T>.EqualArray(A, B: TArray<T>): Boolean;
var
i: Integer;
begin
Result := True;
if A = B then
Exit;
if Length(A) <> Length(B) then
begin
Result := False;
end
else
begin
for i := Low(A) to High(A) do
if not CompareT(A[i], B[i]) then
begin
Result := False;
Break;
end;
end;
end;
class function TArrayEx<T>.CompareT(const A, B: T): Boolean;
var
Compare: IComparer<T>;
begin
Compare := TComparer<T>.Default;
Result := Compare.Compare(A, B) = 0;
end;
// class function TArrayEx<T>.CompareT(const A, B: T): Boolean;
// var
// p1, p2: PByte;
// i: Integer;
// begin
// Result := True;
// p1 := PByte(@A);
// p2 := PByte(@B);
// for i := 0 to Sizeof(T) - 1 do
// begin
// //
// if p1^ <> p2^ then
// begin
// Result := False;
// Exit;
// end;
// Inc(p1);
// Inc(p2);
// end;
// end;
function TArrayEx<T>.GetElements(Index: Integer): T;
begin
Result := FData[Index];
end;
function TArrayEx<T>.GetEnumerator: TEnumerator;
begin
Result := TEnumerator.Create(FData);
end;
function TArrayEx<T>.GetLen: NativeInt;
begin
Result := Length(FData);
end;
function TArrayEx<T>.GetRawData: TArray<T>;
begin
Result := FData;
end;
class operator TArrayEx<T>.Implicit(Value: TArrayEx<T>): TArray<T>;
begin
SetLength(Result, Length(Value.FData));
CopyArray(Value.FData, Result, 0, 0, Length(Value.FData));
end;
class operator TArrayEx<T>.Explicit(Value: array of T): TArrayEx<T>;
begin
Result.SetValue(Value);
end;
class operator TArrayEx<T>.Implicit(Value: array of T): TArrayEx<T>;
begin
Result.SetValue(Value);
end;
class operator TArrayEx<T>.Implicit(Value: TArray<T>): TArrayEx<T>;
begin
SetLength(Result.FData, Length(Value));
CopyArray(Value, Result.FData, 0, 0, Length(Value));
end;
class operator TArrayEx<T>.Explicit(Value: TArrayEx<T>): TArray<T>;
begin
SetLength(Result, Length(Value.FData));
CopyArray(Value.FData, Result, 0, 0, Length(Value.FData));
end;
procedure TArrayEx<T>.SetElements(Index: Integer; const Value: T);
begin
FData[Index] := Value;
end;
procedure TArrayEx<T>.SetLen(Value: NativeInt);
begin
SetLength(FData, Value);
end;
procedure TArrayEx<T>.SetValue(Value: array of T);
begin
FData := DynArrayToTArray(Value);
end;
procedure TArrayEx<T>.Sort;
begin
QuickSort(TComparer<T>.Default, Low(FData), High(FData));
end;
procedure TArrayEx<T>.Sort(const Comparer: IComparer<T>; Index, Count: Integer);
begin
if (Index < Low(FData)) or ((Index > High(FData)) and (Count > 0)) or
(Index + Count - 1 > High(FData)) or (Count < 0) or (Index + Count < 0) then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
if Count <= 1 then
Exit;
QuickSort(Comparer, Index, Index + Count - 1);
end;
procedure TArrayEx<T>.Sort(const Comparer: IComparer<T>);
begin
QuickSort(Comparer, Low(FData), High(FData));
end;
function TArrayEx<T>.ToArray(): TArray<T>;
begin
SetLength(Result, Length(FData));
CopyArray(FData, Result, 0, 0, Length(FData));
end;
class function TArrayEx<T>.Create(Value: array of T): TArrayEx<T>;
begin
Result.SetValue(Value);
end;
class function TArrayEx<T>.Create(Value: TArrayEx<T>): TArrayEx<T>;
begin
Result := Value.Clone;
end;
class function TArrayEx<T>.Create(const Value: T): TArrayEx<T>;
begin
Result.SetValue([Value]);
end;
function TArrayEx<T>.Clone(): TArrayEx<T>;
begin
Result := SubArray(0, Length(FData));
end;
function TArrayEx<T>.SubArray(AFrom, ACount: NativeInt): TArrayEx<T>;
begin
SetLength(Result.FData, ACount);
CopyArray(FData, Result.FData, AFrom, 0, ACount);
end;
procedure TArrayEx<T>.Delete(AFrom, ACount: NativeInt);
begin
if AFrom >= Length(FData) then
Exit;
if (AFrom + ACount) > Length(FData) then
ACount := Length(FData) - AFrom;
MoveArray(FData, AFrom + ACount, AFrom, Length(FData) - (AFrom + ACount));
SetLength(FData, Length(FData) - ACount);
end;
procedure TArrayEx<T>.Delete(AIndex: NativeInt);
begin
Delete(AIndex, 1);
end;
procedure TArrayEx<T>.Append(Values: TArrayEx<T>);
begin
Insert(Length(FData), Values);
end;
procedure TArrayEx<T>.Append(Values: TArray<T>);
begin
Insert(Length(FData), Values);
end;
procedure TArrayEx<T>.Append(const Value: T);
begin
SetLength(FData, Length(FData) + 1);
FData[High(FData)] := Value;
end;
procedure TArrayEx<T>.Append(Values: array of T);
begin
Insert(Length(FData), Values);
end;
function TArrayEx<T>.Insert(AIndex: NativeInt; const Value: T): NativeInt;
var
i: Integer;
begin
Result := -1;
if (AIndex > Length(FData)) or (AIndex < 0) then
Exit;
SetLength(FData, Length(FData) + 1);
MoveArray(FData, AIndex, AIndex + 1, Length(FData) - AIndex);
FData[AIndex] := Value;
Result := AIndex;
end;
function TArrayEx<T>.Insert(AIndex: NativeInt; const Values: array of T)
: NativeInt;
var
i: Integer;
begin
SetLength(FData, Length(Values));
MoveArray(FData, AIndex, AIndex + Length(Values), Length(FData) - AIndex);
for i := 0 to Length(Values) - 1 do
FData[AIndex + i] := Values[i];
Result := AIndex;
end;
function TArrayEx<T>.Insert(AIndex: NativeInt; const Values: TArray<T>)
: NativeInt;
var
i: Integer;
begin
SetLength(FData, Length(FData) + Length(Values));
MoveArray(FData, AIndex, AIndex + Length(Values), Length(FData) - AIndex);
for i := 0 to Length(Values) - 1 do
FData[AIndex + i] := Values[i];
Result := AIndex;
end;
function TArrayEx<T>.Insert(AIndex: NativeInt; const Values: TArrayEx<T>)
: NativeInt;
begin
Result := Insert(AIndex, Values.ToArray);
end;
procedure TArrayEx<T>.Unique();
var
i, J: Integer;
Tmp: TArrayEx<T>;
Flag: Boolean;
begin
for i := High(FData) downto Low(FData) do
begin
Flag := False;
for J := High(Tmp.FData) downto Low(Tmp.FData) do
begin
if CompareT(FData[i], Tmp[J]) then
begin
Flag := True;
Break;
end;
end;
if not Flag then
Tmp.Append(FData[i]);
end;
FData := Tmp.FData;
end;
{ TArrayEx<T>.TEnumerator }
constructor TArrayEx<T>.TEnumerator.Create(const AValue: TArray<T>);
begin
FValue := AValue;
FIndex := -1;
end;
function TArrayEx<T>.TEnumerator.GetCurrent: T;
begin
Result := FValue[FIndex];
end;
function TArrayEx<T>.TEnumerator.MoveNext: Boolean;
begin
Result := False;
if (FIndex >= Length(FValue)) then
Exit;
Inc(FIndex);
Result := FIndex < Length(FValue);
end;
end.