Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the maximum Value in a generic TList<Integer>?

What is the easiest way to get the maximum value in a TList<Integer>?

function GetMaximum(AList: TList<Integer>): Integer;
begin
  Assert(AList.Count > 0);
  Result := ?;
end;

I read that C# has a AList.Max, is there something like that in Delphi?

like image 467
Jens Mühlenhoff Avatar asked Nov 29 '22 01:11

Jens Mühlenhoff


2 Answers

Here's a fun example with a MaxValue implementation on a generic container:

{$APPTYPE CONSOLE}

uses
  System.SysUtils, System.Generics.Defaults, System.Generics.Collections;

type
  TMyList<T> = class(TList<T>)
  public
    function MaxValue: T;
  end;

{ TMyList<T> }

function TMyList<T>.MaxValue: T;
var
  i: Integer;
  Comparer: IComparer<T>;
begin
  if Count=0 then
    raise Exception.Create('Cannot call TMyList<T>.MaxValue on an empty list');
  Comparer := TComparer<T>.Default;
  Result := Self[0];
  for i := 1 to Count-1 do
    if Comparer.Compare(Self[i], Result)>0 then
      Result := Self[i];
end;

var
  IntList: TMyList<Integer>;
  DoubleList: TMyList<Double>;
  StringList: TMyList<string>;

begin
  IntList := TMyList<Integer>.Create;
  IntList.AddRange([10, 5, 12, -49]);
  Writeln(IntList.MaxValue);

  DoubleList := TMyList<Double>.Create;
  DoubleList.AddRange([10.0, 5.0, 12.0, -49.0]);
  Writeln(DoubleList.MaxValue);

  StringList := TMyList<string>.Create;
  StringList.AddRange(['David Heffernan', 'Uwe Raabe', 'Warren P', 'Jens Mühlenhoff']);
  Writeln(StringList.MaxValue);

  Readln;
end.

Because we cannot come up with a generic equivalent to low(Integer) I raise an exception when the method is called on an empty list.

The output is:

12
 1.20000000000000E+0001
Warren P
like image 124
David Heffernan Avatar answered Dec 18 '22 06:12

David Heffernan


Here's an alternative answer: Use the Spring.Collections.pas unit from the Spring4D framework: (found here: http://code.google.com/p/delphi-spring-framework/)

program ListEnumerableDemo;

{$APPTYPE CONSOLE}

uses 
    System.SysUtils 
  , Spring.Collections;

var 
  List: IList<Integer>; 
  Enumerable: IEnumerable<Integer>;

begin 
  try 
    List := TCollections.CreateList<Integer>; 
    List.AddRange([1,6,2,9,54,3,2,7,9,1]);

    Enumerable := List; 
    WriteLn(Enumerable.Max); 
    ReadLn; 
  except 
    on E: Exception do 
      Writeln(E.ClassName, ': ', E.Message); 
  end; 
end. 
like image 32
Nick Hodges Avatar answered Dec 18 '22 06:12

Nick Hodges