unit uIntegerListUtils;

interface

uses
  Classes, FactuGES_Intf;

type
  TIntegerList = class(TList)
  private
    function GetInteger(Index: integer): integer;
    procedure SetInteger(Index: integer; Value: integer);
  public
    procedure Add(Value: integer); reintroduce;
    function Find(Value: integer; var Index: Integer): Boolean;
    property Integers[index: integer]: integer read GetInteger write SetInteger; default;
    function ToIntegerArray : TIntegerArray;
    function ToString : String;
  end;

implementation

uses
  SysUtils;

function TIntegerList.GetInteger(Index: integer): integer;
begin
  Result := Integer(Items[index]);
end;

procedure TIntegerList.SetInteger(Index: integer; Value: integer);
begin
  Items[index] := Pointer(Value);
end;

function TIntegerList.ToIntegerArray: TIntegerArray;
var
  i : Integer;
begin
  Result := TIntegerArray.Create;
  for i := 0 to Count - 1 do
    Result.Add(Integers[i]);
end;

function TIntegerList.ToString: String;
var
  i : Integer;
  AList : TStringList;
begin
  AList := TStringList.Create;
  try
    for i := 0 to Count - 1 do
      AList.Add(IntToStr(Integers[i]));
    Result := AList.CommaText;
  finally
    FreeANDNIL(AList);
  end;
end;

procedure TIntegerList.Add(Value: integer);
var
  Index: integer;
begin
  if not Find(Value, Index) then
    Insert(Index, Pointer(Value));
end;

function TIntegerList.Find(Value: integer; var Index: Integer): Boolean;
var
  L, H, I, C: Integer;
begin
  Result := False;
  L := 0;
  H := Count - 1;
  while L <= H do
  begin
    I := (L + H) shr 1;
    C := Integers[I] - Value;
    if C < 0 then
      L := I + 1
    else begin
      H := I - 1;
      if C = 0 then
        Result := True;
    end;
  end;
  Index := L;
end;

end.
