Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.
Ivan Semenkov edited this page Feb 9, 2021 · 2 revisions

Table of contents

About

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> = class

TOptionalValue

If 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}

Create

A new queue can be created by call its constructor.

constructor Create;
Example
uses
  container.queue;
  
type
  TIntegerQueue = {$IFDEF FPC}type specialize{$ENDIF} TQueue<Integer>;
	
var
  queue : TIntegerQueue;
  
begin
  queue := TIntegerQueue.Create;
  
  FreeAndNil(queue);
end;

Insert

There are several methods to append data to the queue.

PushHead

Add a value to the head of a queue.

function PushHead (AData : T) : Boolean;
Example
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;

PushTail

Add a value to the tail of a queue.

function PushTail (AData : T) : Boolean;
Example
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;

Remove

The methods to remove data from the queue.

PopHead

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.

Example
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;

PopTail

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.

Example
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;

Values

To get value from a TQueue use PeekHead or PeekTail functions.

PeekHead

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.

Example
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;

PeekTail

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.

Example
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;

NumEntries

Retrieve the number of entries in the queue.

function NumEntries : Cardinal;
Example
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;

IsEmpty

Returns true if the queue contains no data.

function IsEmpty : Boolean;
Example
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;