program Project1;
{$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
{$mode objfpc}
uses
Generics.Collections;
type
{ TCustomList }
TIntegerList = specialize TList<integer>;
TCustomList = class(TObject)
strict private
FCount: Integer;
FIndex: Integer;
FCurrent: Integer;
FList: TIntegerList;
private
procedure SetCount(const AValue: Integer);
procedure SetIndex(const AValue: Integer);
function GetCurrent: Integer;
procedure SetCurrent(const AValue: Integer);
public
constructor Create;
destructor Destroy; override;
procedure Add(const AValue: Integer);
procedure Show;
procedure Remove(const AIndex: Integer);
published
property Count: Integer read FCount write SetCount default 0;
property Index: Integer read FIndex write SetIndex default -1;
property Current: Integer read GetCurrent write SetCurrent default -1;
end;
{ begin TCustomList }
constructor TCustomList.Create;
begin
inherited Create;
FList := TIntegerList.Create;
FCount := 0;
FIndex := -1;
FCurrent := -1;
end;
destructor TCustomList.Destroy;
begin
FList.Free;
FList := nil;
inherited Destroy;
end;
procedure TCustomList.SetIndex(const AValue: Integer);
begin
if ((AValue >= 0) and (AValue < FCount)) then
FIndex := AValue
else
FIndex := -1;
end;
function TCustomList.GetCurrent: Integer;
begin
if ((FIndex >= 0) and (FIndex < FCount)) then
Result := FList.Items[FIndex]
else
Result := -1;
end;
procedure TCustomList.SetCurrent(const AValue: Integer);
begin
if ((FIndex >= 0) and (FIndex < FCount)) then
FList.Items[FIndex] := AValue;
end;
procedure TCustomList.SetCount(const AValue: Integer);
var
i: Integer;
begin
if (FCount = 0) then
Exit;
if (AValue < FCount) then
for i := Pred(FCount) downto AValue do
FList.Delete(i);
FCount := FList.Count;
FIndex := Pred(FCount);
end;
procedure TCustomList.Add(const AValue: Integer);
begin
FList.Add(AValue);
FCount := FList.Count;
FIndex := Pred(FCount);
end;
procedure TCustomList.Remove(const AIndex: Integer);
begin
if (FCount = 0) then
Exit;
if ((AIndex >= 0) and (AIndex < FCount)) then
FList.Delete(AIndex);
FCount := FList.Count;
FIndex := Pred(FCount);
end;
procedure TCustomList.Show;
var
i: Integer;
begin
if (FCount = 0) then
Exit;
for i in FList
do WriteLn(i);
end;
{ end TCustomList }
var
List: TCustomList;
i: integer;
begin
List := TCustomList.Create;
try
WriteLn('Add 10 elements');
for i := 0 to 9 do
List.Add(i);
WriteLn('Show all elements');
List.Show;
WriteLn('Remove last 5 elements');
List.Count := List.Count - 5;
WriteLn('Show all elements');
List.Show;
WriteLn('Change value of element 3 to 99');
List.Index := 2;
List.Current := 99;
WriteLn('Show all elements');
List.Show;
finally
List.Free;
end;
{$IFDEF MSWINDOWS}ReadLn; {$ENDIF}
end.