unit untArrayHelper;

interface

uses
  Classes, SysUtils, untCard;

type
  TCardArrayHelper = record helper for TArray<ICard>
  public
    procedure Add(ACard: ICard);
    procedure Delete(AIndex: Integer);
    function Count(): Integer;
  end;

  TInt64ArrayHelper = record helper for TArray<Int64>
  public
    procedure Add(AInt: Int64);
    procedure Delete(AIndex: Integer);
    function Count(): Integer;
  end;

function ArrayOfCard(arr: array of ICard): TArray<ICard>;
function ArrayOfInt64(arr: array of Int64): TArray<Int64>;

implementation

function ArrayOfCard(arr: array of ICard): TArray<ICard>;
var
  size: Integer;
  i: Integer;
begin
  size := Length(arr);
  SetLength(Result, size);
  for i := 0 to size - 1 do begin
    Result[i] := arr[i];
  end;
end;

function ArrayOfInt64(arr: array of Int64): TArray<Int64>;
var
  size: Integer;
  i: Integer;
begin
  size := Length(arr);
  SetLength(Result, size);
  for i := 0 to size - 1 do begin
    Result[i] := arr[i];
  end;
end;

procedure TCardArrayHelper.Add(ACard: ICard);
var
  size: Integer;
begin
  size := Length(Self);
  SetLength(Self, size + 1);
  Self[size] := ACard;
end;

procedure TCardArrayHelper.Delete(AIndex: Integer);
var
  i: Integer;
  size: Integer;
begin
  // 删掉当前位置的引用
  size := Length(Self);
  Self[AIndex] := nil;
  // 将后面的内容依次往前移
  for i := AIndex to size - 2  do begin
    Self[i] := Self[i + 1];
  end;
  // 整体长度缩小1
  SetLength(Self, size - 1);
end;

function TCardArrayHelper.Count(): Integer;
begin
  Exit(Length(Self));
end;

procedure TInt64ArrayHelper.Add(AInt: Int64);
var
  size: Integer;
begin
  size := Length(Self);
  SetLength(Self, size + 1);
  Self[size] := AInt;
end;

procedure TInt64ArrayHelper.Delete(AIndex: Integer);
var
  i: Integer;
  size: Integer;
begin
  // 删掉当前位置的引用
  size := Length(Self);
  // 将后面的内容依次往前移
  for i := AIndex to size - 2  do begin
    Self[i] := Self[i + 1];
  end;
  // 整体长度缩小1
  SetLength(Self, size - 1);
end;

function TInt64ArrayHelper.Count(): Integer;
begin
  Exit(Length(Self));
end;

end.
