unit IntMemory;

interface

uses
  Classes, SysUtils, Int;

type

  { TIntMemory }

  TIntMemory = class
  private
    FData: array of TInt;
    FPosition: TInt;
    function GetSize: TInt;
    procedure SetSize(AValue: TInt);
  public
    procedure Write(Address, Data: TInt);
    function Read(Address: TInt): TInt;
    procedure WritePos(Data: TInt);
    function ReadPos: TInt;
    procedure CopyFrom(Source: TIntMemory; Dst, Src, Count: TInt);
    procedure WriteStringPos(Value: string);
    property Size: TInt read GetSize write SetSize;
    property Position: TInt read FPosition write FPosition;
    property Items[Index: TInt]: TInt read Read write Write; default;
  end;


implementation

{ TIntMemory }

function TIntMemory.GetSize: TInt;
begin
  Result := Length(FData);
end;

procedure TIntMemory.SetSize(AValue: TInt);
begin
  SetLength(FData, AValue);
end;

procedure TIntMemory.Write(Address, Data: TInt);
begin
  FData[Address] := Data;
end;

function TIntMemory.Read(Address: TInt): TInt;
begin
  Result := FData[Address];
end;

procedure TIntMemory.WritePos(Data: TInt);
begin
  Write(FPosition, Data);
  Inc(FPosition);
end;

function TIntMemory.ReadPos: TInt;
begin
  Result := Read(FPosition);
  Inc(FPosition);
end;

procedure TIntMemory.CopyFrom(Source: TIntMemory; Dst, Src, Count: TInt);
var
  I: Integer;
begin
  for I := 0 to Count - 1 do begin
    Write(Dst, Source.Read(Src));
    Inc(Dst);
    Inc(Src);
  end;
end;

procedure TIntMemory.WriteStringPos(Value: string);
var
  I: Integer;
begin
  if Length(Value) > 0 then begin
    if Position + Length(Value) > Size then Size := Position + Length(Value);
    for I := 0 to Length(Value) - 1 do
      Items[Position + I] := Ord(Value[I + 1]);
    Inc(FPosition, Length(Value));
  end;
end;

end.

