// Bonus! Slow and dumb implementation of Map structure
{$MODE OBJFPC}
unit dshitmap;

interface

type
generic TShitMap<V> = object
public type
  TKey = AnsiString;
  PKey = ^TKey;
  TValue = V;
  PValue = ^TValue;
private
  FElements: array of record
    Key: AnsiString;
    Value: TValue;
  end;
  FCount: LongInt;
public
  constructor Init;
  destructor Done;
  procedure Add(const Key: AnsiString; Value: TValue);
  function Get(const Key: AnsiString): PValue;
  function GetByIndex(Index: LongInt): PValue;
  property Count: LongInt read FCount;
  property Elements[Key: AnsiString]: PValue read Get; default;
end;

implementation

constructor TShitMap.Init;
begin
  FCount := 0;
  SetLength(FElements, 0);
end;

destructor TShitMap.Done;
begin
  SetLength(FElements, 0);
end;

procedure TShitMap.Add(const Key: AnsiString; Value: TValue);
begin
  Get(Key)^ := Value;
end;

function TShitMap.Get(const Key: AnsiString): PValue;
var
  I: LongInt;
begin
  for I := 0 to FCount - 1 do
    if Key = FElements[I].Key then
      Exit(@FElements[I].Value);
  Inc(FCount);
  if FCount > Length(FElements) then
    SetLength(FElements, Length(FElements) + 128);
  FElements[FCount - 1].Key := Key;
  Exit(@FElements[FCount - 1].Value);
end;

function TShitMap.GetByIndex(Index: LongInt): PValue;
begin
  Result := @FElements[Index].Value;
end;

end.
