(* Collection of code snippets by Arne Vajhøj *)
(* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) *)
program queue;

{$APPTYPE CONSOLE}

uses
  ShareMem, Contnrs, SysUtils, Windows;

const
  SIZE = 100;

type
  Data = array[0..SIZE] of Byte;

type
  ITest = interface(IInterface)
            procedure Add(v : Byte);
            function Get() : Data;
            function GetName() : String;
            property Name : String read GetName;
          end;
  Mover = class(TInterfacedObject, ITest)
            private
              buf : Data;
              ix : Integer;
            public
              constructor Create;
              procedure Add(v : Byte);
              function Get() : Data;
              function GetName() : String;
          end;
  Wrapper = class(TObject)
              private
                val : Byte;
              public
                constructor Create(v : Byte);
                property Value : Byte read val;
            end;
  Container1 = class(TInterfacedObject, ITest)
                 private
                   q : TObjectQueue;
                 public
                   constructor Create;
                   procedure Add(v : Byte);
                   function Get() : Data;
                   destructor Destroy; override;
                   function GetName() : String;
               end;
  Container2 = class(TInterfacedObject, ITest)
                 private
                   q : TQueue;
                 public
                   constructor Create;
                   procedure Add(v : Byte);
                   function Get() : Data;
                   destructor Destroy; override;
                   function GetName() : String;
               end;
  Circular = class(TInterfacedObject, ITest)
               private
                 buf : Data;
                 ix : Integer;
               public
                 constructor Create;
                 procedure Add(v : Byte);
                 function Get() : Data;
                 function GetName() : String;
             end;
  PElement = ^Element;
  Element = record
              val : Byte;
              next : PElement;
              prev : PElement;
            end;
  DoubleLinkedList = class(TInterfacedObject, ITest)
                       private
                         n : Integer;
                         head : PElement;
                         tail : PElement;
                       public
                         constructor Create;
                         procedure Add(v : Byte);
                         function Get() : Data;
                         function GetName() : String;
                     end;

constructor Mover.Create();

begin
  ix := 0;
end;

procedure Mover.Add(v: Byte);

begin
  if ix < SIZE then begin
    buf[ix] := v;
    Inc(ix);
  end else begin
    Move(buf[1], buf[0], SIZE);
    buf[SIZE] := v;
  end;
end;

function Mover.Get() : Data;

begin
  Get := buf;
end;

function Mover.GetName() : String;

begin
  GetName := 'Moving for each Add';
end;

constructor Wrapper.Create(v : Byte);

begin
  val := v;
end;

constructor Container1.Create();

begin
  q := TObjectQueue.Create;
end;

procedure Container1.Add(v: Byte);

var
  tmp : Wrapper;

begin
  if q.Count > SIZE then begin
    tmp := Wrapper(q.Pop);
    tmp.Destroy;
  end;
  q.Push(Wrapper.Create(v));
end;

function Container1.Get() : Data;

var
  res : Data;
  i : Integer;
  tmp : Wrapper;

begin
  for i := 0 to SIZE do begin
    tmp := Wrapper(q.Pop);
    res[i] := tmp.Value;
    tmp.Destroy;
  end;
  Get := res;
end;

destructor Container1.Destroy;

begin
  q.Destroy;
  inherited;
end;

function Container1.GetName() : String;

begin
  GetName := 'Using builtin TObjectQueue';
end;

constructor Container2.Create();

begin
  q := TQueue.Create;
end;

procedure Container2.Add(v: Byte);

begin
  if q.Count > SIZE then begin
    q.Pop;
  end;
  q.Push(Pointer(v));
end;

function Container2.Get() : Data;

var
  res : Data;
  i : Integer;

begin
  for i := 0 to SIZE do begin
    res[i] := Byte(q.Pop);
  end;
  Get := res;
end;

destructor Container2.Destroy;

begin
  q.Destroy;
  inherited;
end;

function Container2.GetName() : String;

begin
  GetName := 'Using builtin TQueue';
end;

constructor Circular.Create();

begin
  ix := 0;
end;

procedure Circular.Add(v: Byte);

begin
  buf[ix] := v;
  ix := (ix + 1) mod (SIZE + 1);
end;

function Circular.Get() : Data;

var
  res : Data;
  i : Integer;

begin
  for i := 0 to SIZE + 1 do begin
    res[i] := buf[(ix + i) mod (SIZE + 1)];
  end;
  Get := res;
end;

function Circular.GetName() : String;

begin
  GetName := 'Just circling around';
end;

constructor DoubleLinkedList.Create;

begin
  n := 0;
end;

procedure DoubleLinkedList.Add(v : Byte);

var
  tmp : PElement;

begin
  new(tmp);
  tmp^.val := v;
  if n = 0 then begin
    tmp^.next := nil;
    tmp^.prev := nil;
    head := tmp;
    tail := tmp;
    Inc(n);
  end else if n <= SIZE then begin
    tmp^.next := nil;
    tmp^.prev := tail;
    tail^.next := tmp;
    tail := tmp;
    Inc(n);
  end else begin
    tmp^.next := nil;
    tmp^.prev := tail;
    tail^.next := tmp;
    tail := tmp;
    tmp := head^.next;
    dispose(head);
    head := tmp;
    head^.prev := nil;
  end;
end;

function DoubleLinkedList.Get() : Data;

var
  res : Data;
  ix : Integer;
  curr, tmp : PElement;

begin
  curr := head;
  ix := 0;
  while curr <> nil do begin
    res[ix] := curr^.val;
    Inc(ix);
    tmp := curr;
    curr := curr^.next;
    dispose(tmp);
  end;
  Get := res;
end;

function DoubleLinkedList.GetName() : String;
begin
  GetName := 'My own double linked list';
end;

procedure Test(impl : ITest);

var
  i : Integer;
  tmp : Data;
  t1, t2 : Integer;

begin
  t1 := GetTickCount;
  for i := 0 to 1234567 do begin
    impl.Add(i);
  end;
  t2 := GetTickCount;
  tmp := impl.Get;
  writeln(impl.Name);
  write('result: ');
  for i := 0 to SIZE do begin
    write(' ', tmp[i]);
  end;
  writeln;
  writeln('time: ', t2 - t1);
end;

var
  impl_1 : Mover;
  impl_2 : Container1;
  impl_3 : Container2;
  impl_4 : Circular;
  impl_5 : DoubleLinkedList;

begin
  impl_1 := Mover.Create;
  Test(impl_1);
  impl_1.Destroy;
  impl_2 := Container1.Create;
  Test(impl_2);
  impl_2.Destroy;
  impl_3 := Container2.Create;
  Test(impl_3);
  impl_3.Destroy;
  impl_4 := Circular.Create;
  Test(impl_4);
  impl_4.Destroy;
  impl_5 := DoubleLinkedList.Create;
  Test(impl_5);
  impl_5.Destroy;
end.
