Skip to content

Commit 0ea66ab

Browse files
committed
Add new methods to TArrayUtils in Arrays category
Added new Max<T>, Min<T> and MinMax<T> methods to TArrayUtils. Updated source code file accordingly. No change made to metadata.
1 parent 59c09cc commit 0ea66ab

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

collection/623.dat

+66
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,51 @@
33
public
44
// Returns first element of given array, which must not be empty.
55
class function First<T>(const A: array of T): T; static;
6+
67
// Returns last element of given array, which must not be empty.
78
class function Last<T>(const A: array of T): T; static;
9+
810
// Returns index of given item in given array or -1 if element no in array.
911
// Given equality comparer is used to compare array elements with Elem.
1012
class function IndexOf<T>(const Item: T; const A: array of T;
1113
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>):
1214
Integer; static;
15+
1316
// Checks if two given arrays have the same contents, in same order. Given
1417
// equality comparer is used to compare array elements.
1518
class function Equal<T>(const Left, Right: array of T;
1619
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>):
1720
Boolean; static;
21+
1822
// Checks if the first Count elements of the given arrays are the same.
1923
// Given equality comparer is used to compare array elements.
2024
class function SameStart<T>(const Left, Right: array of T;
2125
const Count: Integer;
2226
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>):
2327
Boolean; static;
28+
2429
// Creates and returns a new array that is the reverse of the given array.
2530
class function Reverse<T>(const A: array of T): TArray<T>; static;
31+
32+
// Returns the maximum value of array A, which must not be be empty. The
33+
// given comparer must return -ve if its 1st argument is less than the 2nd
34+
// argument, +ve if the reverse holds and zero if both arguments are equal.
35+
class function Max<T>(const A: array of T; const Comparer: TComparison<T>):
36+
T; static;
37+
38+
// Returns the minimum value of array A, which must not be be empty. The
39+
// given comparer must return -ve if its 1st argument is less than the 2nd
40+
// argument, +ve if the reverse holds and zero if both arguments are equal.
41+
class function Min<T>(const A: array of T; const Comparer: TComparison<T>):
42+
T; static;
43+
44+
// Finds the minimum and maximum value of array A, which must not be empty.
45+
// The minimum and maximum are returned via the MinValue and MaxValue
46+
// parameters respectively. The given comparer must return -ve if its 1st
47+
// argument is less than the 2nd argument, +ve if the reverse holds and zero
48+
// if both arguments are equal.
49+
class procedure MinMax<T>(const A: array of T;
50+
const Comparer: TComparison<T>; out MinValue, MaxValue: T); static;
2651
end;
2752

2853
class function TArrayUtils.Equal<T>(const Left, Right: array of T;
@@ -62,6 +87,47 @@ begin
6287
Result := A[Pred(Length(A))];
6388
end;
6489

90+
class function TArrayUtils.Max<T>(const A: array of T;
91+
const Comparer: TComparison<T>): T;
92+
var
93+
Idx: Integer;
94+
begin
95+
Assert(System.Length(A) > 0);
96+
Result := A[0];
97+
for Idx := 1 to Pred(System.Length(A)) do
98+
if Comparer(A[Idx], Result) > 0 then
99+
Result := A[Idx];
100+
end;
101+
102+
class function TArrayUtils.Min<T>(const A: array of T;
103+
const Comparer: TComparison<T>): T;
104+
var
105+
Idx: Integer;
106+
begin
107+
Assert(System.Length(A) > 0);
108+
Result := A[0];
109+
for Idx := 1 to Pred(System.Length(A)) do
110+
if Comparer(A[Idx], Result) < 0 then
111+
Result := A[Idx];
112+
end;
113+
114+
class procedure TArrayUtils.MinMax<T>(const A: array of T;
115+
const Comparer: TComparison<T>; out MinValue, MaxValue: T);
116+
var
117+
Idx: Integer;
118+
begin
119+
Assert(System.Length(A) > 0);
120+
MinValue := A[0];
121+
MaxValue := A[0];
122+
for Idx := 1 to Pred(System.Length(A)) do
123+
begin
124+
if Comparer(A[Idx], MinValue) < 0 then
125+
MinValue := A[Idx]
126+
else if Comparer(A[Idx], MaxValue) > 0 then
127+
MaxValue := A[Idx];
128+
end;
129+
end;
130+
65131
class function TArrayUtils.Reverse<T>(const A: array of T): TArray<T>;
66132
var
67133
I: Integer;

0 commit comments

Comments
 (0)