-
Notifications
You must be signed in to change notification settings - Fork 10
TQueue
TQueue is a double ended queue stores a list of values in order. New values can be added and removed from either end of the queue.
uses
container.queue;
type
generic TQueue<T> = classIf macro {$USE_OPTIONAL} is defined, then all methods return a TOptionalValue wrapper, otherwise T.
uses
utils.optional;
type
TOptionalValue = {$IFDEF FPC}specialize{$ENDIF} TOptional<T>;For non-existent values, returns a empty TOptionalValue if defined or an EValueNotExistsException is thrown.
type
{$IFNDEF USE_OPTIONAL}
EValueNotExistsException = class(Exception);
{$ENDIF}A new queue can be created by call its constructor.
constructor Create;uses
container.queue;
type
TIntegerQueue = {$IFDEF FPC}type specialize{$ENDIF} TQueue<Integer>;
var
queue : TIntegerQueue;
begin
queue := TIntegerQueue.Create;
FreeAndNil(queue);
end;There are several methods to append data to the queue.
Add a value to the head of a queue.
function PushHead (AData : T) : Boolean;uses
container.queue;
type
TIntegerQueue = {$IFDEF FPC}type specialize{$ENDIF} TQueue<Integer>;
var
queue : TIntegerQueue;
begin
queue := TIntegerQueue.Create;
queue.PushHead(12);
FreeAndNil(queue);
end;Add a value to the tail of a queue.
function PushTail (AData : T) : Boolean;uses
container.queue;
type
TIntegerQueue = {$IFDEF FPC}type specialize{$ENDIF} TQueue<Integer>;
var
queue : TIntegerQueue;
begin
queue := TIntegerQueue.Create;
queue.PushTail(12);
FreeAndNil(queue);
end;The methods to remove data from the queue.
Remove a value from the head of a queue.
function PopHead : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue{$ENDIF};If value not exists returns empty TOptionalValue or raise EValueNotExistsException.
uses
container.queue;
type
TIntegerQueue = {$IFDEF FPC}type specialize{$ENDIF} TQueue<Integer>;
var
queue : TIntegerQueue;
begin
queue := TIntegerQueue.Create;
writeln(queue.PopHead);
FreeAndNil(queue);
end;Remove a value from the tail of a queue.
function PopTail : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue{$ENDIF};If value not exists returns empty TOptionalValue or raise EValueNotExistsException.
uses
container.queue;
type
TIntegerQueue = {$IFDEF FPC}type specialize{$ENDIF} TQueue<Integer>;
var
queue : TIntegerQueue;
begin
queue := TIntegerQueue.Create;
writeln(queue.PopTail);
FreeAndNil(queue);
end;To get value from a TQueue use PeekHead or PeekTail functions.
Read value from the head of a queue, without removing it from the queue.
function PeekHead : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue{$ENDIF};If value not exists returns empty TOptionalValue or raise EValueNotExistsException.
uses
container.queue;
type
TIntegerQueue = {$IFDEF FPC}type specialize{$ENDIF} TQueue<Integer>;
var
queue : TIntegerQueue;
begin
queue := TIntegerQueue.Create;
writeln(queue.PeekHead);
FreeAndNil(queue);
end;Read a value from the tail of a queue, without removing it from the queue.
function PeekTail : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue{$ENDIF};If value not exists returns empty TOptionalValue or raise EValueNotExistsException.
uses
container.queue;
type
TIntegerQueue = {$IFDEF FPC}type specialize{$ENDIF} TQueue<Integer>;
var
queue : TIntegerQueue;
begin
queue := TIntegerQueue.Create;
writeln(queue.PeekTail);
FreeAndNil(queue);
end;Retrieve the number of entries in the queue.
function NumEntries : Cardinal;uses
container.queue;
type
TIntegerQueue = {$IFDEF FPC}type specialize{$ENDIF} TQueue<Integer>;
var
queue : TIntegerQueue;
begin
queue := TIntegerQueue.Create;
writeln(queue.NumEnties);
FreeAndNil(queue);
end;Returns true if the queue contains no data.
function IsEmpty : Boolean;uses
container.queue;
type
TIntegerQueue = {$IFDEF FPC}type specialize{$ENDIF} TQueue<Integer>;
var
queue : TIntegerQueue;
begin
queue := TIntegerQueue.Create;
if not queue.IsEmpty then
;
FreeAndNil(queue);
end;