Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2b857d6

Browse files
Merge branch 'feature/8-add-generic-min+max-methods' into develop
Fixes #8
2 parents 59c09cc + 26f1a54 commit 2b857d6

File tree

3 files changed

+215
-1
lines changed

3 files changed

+215
-1
lines changed

‎collection/623.dat

Lines changed: 66 additions & 0 deletions
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;

‎tests/Cat-Arrays/TestUArraysCatSnippets.pas

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ TestTArrayUtils = class(TTestCase)
3838
// TestReverse must come after TestEqual since the test calls TArrayUtils.Equal<T>
3939
procedure TestReverse;
4040
procedure TestSameStart;
41+
procedure TestMax;
42+
procedure TestMin;
43+
procedure TestMinMax;
4144
end;
4245

4346
TestArraysCatSnippets = class(TTestCase)
@@ -55,6 +58,8 @@ implementation
5558
var
5659
IntegerCompareFn: TEqualityComparison<Integer>;
5760
StringCompareFn: TEqualityComparison<string>;
61+
IntegerComparisonFn: TComparison<Integer>;
62+
StringComparisonFn: TComparison<string>;
5863

5964
{ TestTArrayUtils }
6065

@@ -167,6 +172,72 @@ procedure TestTArrayUtils.TestLast;
167172
CheckEquals(37, TArrayUtils.Last<Integer>(fIAN), 'Test 6');
168173
end;
169174

175+
procedure TestTArrayUtils.TestMax;
176+
begin
177+
CheckEquals(42, TArrayUtils.Max<Integer>(fIA1, IntegerComparisonFn), 'Test 1');
178+
CheckEquals(56, TArrayUtils.Max<Integer>(fIA2, IntegerComparisonFn), 'Test 2');
179+
CheckEquals(102, TArrayUtils.Max<Integer>(fIA3, IntegerComparisonFn), 'Test 3');
180+
CheckEquals(37, TArrayUtils.Max<Integer>(fIAY, IntegerComparisonFn), 'Test 4');
181+
CheckEquals(37, TArrayUtils.Max<Integer>(fIAR, IntegerComparisonFn), 'Test 5');
182+
CheckEquals('foo', TArrayUtils.Max<string>(fSA1, StringComparisonFn), 'Test 6');
183+
CheckEquals('foo', TArrayUtils.Max<string>(fSA2, StringComparisonFn), 'Test 7');
184+
CheckEquals('foo', TArrayUtils.Max<string>(fSA2R, StringComparisonFn), 'Test 8');
185+
CheckEquals('time', TArrayUtils.Max<string>(fSAM, StringComparisonFn), 'Test 9');
186+
CheckEquals('time', TArrayUtils.Max<string>(fSAR, StringComparisonFn), 'Test 10');
187+
end;
188+
189+
procedure TestTArrayUtils.TestMin;
190+
begin
191+
CheckEquals(42, TArrayUtils.Min<Integer>(fIA1, IntegerComparisonFn), 'Test 1');
192+
CheckEquals(42, TArrayUtils.Min<Integer>(fIA2, IntegerComparisonFn), 'Test 2');
193+
CheckEquals(42, TArrayUtils.Min<Integer>(fIA3, IntegerComparisonFn), 'Test 3');
194+
CheckEquals(0, TArrayUtils.Min<Integer>(fIAY, IntegerComparisonFn), 'Test 4');
195+
CheckEquals(1, TArrayUtils.Min<Integer>(fIAR, IntegerComparisonFn), 'Test 5');
196+
CheckEquals('foo', TArrayUtils.Min<string>(fSA1, StringComparisonFn), 'Test 6');
197+
CheckEquals('bar', TArrayUtils.Min<string>(fSA2, StringComparisonFn), 'Test 7');
198+
CheckEquals('bar', TArrayUtils.Min<string>(fSA2R, StringComparisonFn), 'Test 8');
199+
CheckEquals('a', TArrayUtils.Min<string>(fSAM, StringComparisonFn), 'Test 9');
200+
CheckEquals('a', TArrayUtils.Min<string>(fSAR, StringComparisonFn), 'Test 10');
201+
202+
end;
203+
204+
procedure TestTArrayUtils.TestMinMax;
205+
var
206+
MinInt, MaxInt: Integer;
207+
MinStr, MaxStr: string;
208+
begin
209+
TArrayUtils.MinMax<Integer>(fIA1, IntegerComparisonFn, MinInt, MaxInt);
210+
CheckEquals(42, MinInt, 'Test 1 min');
211+
CheckEquals(42, MaxInt, 'Test 1 max');
212+
TArrayUtils.MinMax<Integer>(fIA2, IntegerComparisonFn, MinInt, MaxInt);
213+
CheckEquals(42, MinInt, 'Test 2 min');
214+
CheckEquals(56, MaxInt, 'Test 2 max');
215+
TArrayUtils.MinMax<Integer>(fIA3, IntegerComparisonFn, MinInt, MaxInt);
216+
CheckEquals(42, MinInt, 'Test 3 min');
217+
CheckEquals(102, MaxInt, 'Test 3 max');
218+
TArrayUtils.MinMax<Integer>(fIAY, IntegerComparisonFn, MinInt, MaxInt);
219+
CheckEquals(0, MinInt, 'Test 4 min');
220+
CheckEquals(37, MaxInt, 'Test 4 max');
221+
TArrayUtils.MinMax<Integer>(fIAR, IntegerComparisonFn, MinInt, MaxInt);
222+
CheckEquals(1, MinInt, 'Test 5 min');
223+
CheckEquals(37, MaxInt, 'Test 5 max');
224+
TArrayUtils.MinMax<string>(fSA1, StringComparisonFn, MinStr, MaxStr);
225+
CheckEquals('foo', MinStr, 'Test 6 min');
226+
CheckEquals('foo', MaxStr, 'Test 6 max');
227+
TArrayUtils.MinMax<string>(fSA2, StringComparisonFn, MinStr, MaxStr);
228+
CheckEquals('bar', MinStr, 'Test 7 min');
229+
CheckEquals('foo', MaxStr, 'Test 7 max');
230+
TArrayUtils.MinMax<string>(fSA2R, StringComparisonFn, MinStr, MaxStr);
231+
CheckEquals('bar', MinStr, 'Test 8 min');
232+
CheckEquals('foo', MaxStr, 'Test 8 max');
233+
TArrayUtils.MinMax<string>(fSAM, StringComparisonFn, MinStr, MaxStr);
234+
CheckEquals('a', MinStr, 'Test 9 min');
235+
CheckEquals('time', MaxStr, 'Test 9 max');
236+
TArrayUtils.MinMax<string>(fSAR, StringComparisonFn, MinStr, MaxStr);
237+
CheckEquals('a', MinStr, 'Test 10 min');
238+
CheckEquals('time', MaxStr, 'Test 10 max');
239+
end;
240+
170241
procedure TestTArrayUtils.TestReverse;
171242
var
172243
RS: TArray<string>;
@@ -304,6 +375,17 @@ initialization
304375
Result := SameStr(Left, Right);
305376
end;
306377

378+
IntegerComparisonFn := function (const Left, Right: Integer): Integer
379+
begin
380+
Result := Left - Right;
381+
end;
382+
383+
StringComparisonFn := function (const Left, Right: string): Integer
384+
begin
385+
Result := CompareStr(Left, Right);
386+
end;
387+
388+
307389
// Register any test cases with the test runner
308390
RegisterTest(TestTArrayUtils.Suite);
309391
RegisterTest(TestArraysCatSnippets.Suite);

‎tests/Cat-Arrays/UArraysCatSnippets.pas

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* The unit is copyright © 2005-2024 by Peter Johnson & Contributors and is
77
* licensed under the MIT License (https://opensource.org/licenses/MIT).
88
*
9-
* Generated on : Mon, 06 Jan 2025 17:38:54 GMT.
9+
* Generated on : Wed, 15 Jan 2025 16:44:27 GMT.
1010
* Generated by : DelphiDabbler CodeSnip Release 4.24.0.
1111
*
1212
* The latest version of CodeSnip is available from the CodeSnip GitHub project
@@ -41,26 +41,51 @@ TArrayUtils = record
4141
public
4242
// Returns first element of given array, which must not be empty.
4343
class function First<T>(const A: array of T): T; static;
44+
4445
// Returns last element of given array, which must not be empty.
4546
class function Last<T>(const A: array of T): T; static;
47+
4648
// Returns index of given item in given array or -1 if element no in array.
4749
// Given equality comparer is used to compare array elements with Elem.
4850
class function IndexOf<T>(const Item: T; const A: array of T;
4951
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>):
5052
Integer; static;
53+
5154
// Checks if two given arrays have the same contents, in same order. Given
5255
// equality comparer is used to compare array elements.
5356
class function Equal<T>(const Left, Right: array of T;
5457
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>):
5558
Boolean; static;
59+
5660
// Checks if the first Count elements of the given arrays are the same.
5761
// Given equality comparer is used to compare array elements.
5862
class function SameStart<T>(const Left, Right: array of T;
5963
const Count: Integer;
6064
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>):
6165
Boolean; static;
66+
6267
// Creates and returns a new array that is the reverse of the given array.
6368
class function Reverse<T>(const A: array of T): TArray<T>; static;
69+
70+
// Returns the maximum value of array A, which must not be be empty. The
71+
// given comparer must return -ve if its 1st argument is less than the 2nd
72+
// argument, +ve if the reverse holds and zero if both arguments are equal.
73+
class function Max<T>(const A: array of T; const Comparer: TComparison<T>):
74+
T; static;
75+
76+
// Returns the minimum value of array A, which must not be be empty. The
77+
// given comparer must return -ve if its 1st argument is less than the 2nd
78+
// argument, +ve if the reverse holds and zero if both arguments are equal.
79+
class function Min<T>(const A: array of T; const Comparer: TComparison<T>):
80+
T; static;
81+
82+
// Finds the minimum and maximum value of array A, which must not be empty.
83+
// The minimum and maximum are returned via the MinValue and MaxValue
84+
// parameters respectively. The given comparer must return -ve if its 1st
85+
// argument is less than the 2nd argument, +ve if the reverse holds and zero
86+
// if both arguments are equal.
87+
class procedure MinMax<T>(const A: array of T;
88+
const Comparer: TComparison<T>; out MinValue, MaxValue: T); static;
6489
end;
6590

6691
{
@@ -457,6 +482,47 @@ class function TArrayUtils.Last<T>(const A: array of T): T;
457482
Result := A[Pred(Length(A))];
458483
end;
459484

485+
class function TArrayUtils.Max<T>(const A: array of T;
486+
const Comparer: TComparison<T>): T;
487+
var
488+
Idx: Integer;
489+
begin
490+
Assert(System.Length(A) > 0);
491+
Result := A[0];
492+
for Idx := 1 to Pred(System.Length(A)) do
493+
if Comparer(A[Idx], Result) > 0 then
494+
Result := A[Idx];
495+
end;
496+
497+
class function TArrayUtils.Min<T>(const A: array of T;
498+
const Comparer: TComparison<T>): T;
499+
var
500+
Idx: Integer;
501+
begin
502+
Assert(System.Length(A) > 0);
503+
Result := A[0];
504+
for Idx := 1 to Pred(System.Length(A)) do
505+
if Comparer(A[Idx], Result) < 0 then
506+
Result := A[Idx];
507+
end;
508+
509+
class procedure TArrayUtils.MinMax<T>(const A: array of T;
510+
const Comparer: TComparison<T>; out MinValue, MaxValue: T);
511+
var
512+
Idx: Integer;
513+
begin
514+
Assert(System.Length(A) > 0);
515+
MinValue := A[0];
516+
MaxValue := A[0];
517+
for Idx := 1 to Pred(System.Length(A)) do
518+
begin
519+
if Comparer(A[Idx], MinValue) < 0 then
520+
MinValue := A[Idx]
521+
else if Comparer(A[Idx], MaxValue) > 0 then
522+
MaxValue := A[Idx];
523+
end;
524+
end;
525+
460526
class function TArrayUtils.Reverse<T>(const A: array of T): TArray<T>;
461527
var
462528
I: Integer;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /