Skip to content

Latest commit

 

History

History
89 lines (61 loc) · 2.13 KB

File metadata and controls

89 lines (61 loc) · 2.13 KB

Max<T> class method

Project: Array Utilities Unit

Unit: DelphiDabbler.Lib.ArrayUtils

Record: TArrayUtils

Applies to: ~>0.1

class function Max<T>(const A: array of T;
  const AComparer: TComparison<T>): T;
  overload; static;

class function Max<T>(const A: array of T;
  const AComparer: IComparer<T> ): T;
  overload; static;

class function Max<T>(const A: array of T): T;
  overload; static;

Description

Finds the maximum value of a non empty array.

Parameters:

  • A - The array to be examined.

  • AComparer - An optional function or object that can be used to compare two values. Used to compare the elements of the arrays to each other.

    If AComparer is provided it must be one of:

    If this parameter is omitted then the default comparer defined by the TComparer<T>.Default method from the Delphi's System.Generics.Defaults unit is used.

Returns:

  • The element whose value is the largest in the array.

Precondition:

  • A must not be empty. An EAssertionFailed exception is raised otherwise.

Examples

Example #1

Using an equality comparer function:

procedure Max_Eg1;
var
  A: TArray<Integer>;
  ComparerFn: TComparison<Integer>;
begin
  A := TArray<Integer>.Create(1, 2, 3, 4, 2, 3, 2);
  ComparerFn := function(const Left, Right: Integer): Integer
    begin
      Result := Left - Right;
    end;
  Assert(TArrayUtils.Max<Integer>(A, ComparerFn) = 4);
end;

Example #2

Using an equality comparer object:

procedure Max_Eg2;
var
  A: TArray<string>;
  ComparerObj: IComparer<string>;
begin
  A := TArray<string>.Create('a', 'b', 'c', 'd', 'c', 'a');
  ComparerObj := TDelegatedComparer<string>.Create(CompareStr);
  Assert(TArrayUtils.Max<string>(A, ComparerObj) = 'd');
end;

See Also