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

Table of contents

About

A doubly-linked list stores a collection of values. Each entry in the list contains a link to the next entry and the previous entry. It is therefore possible to iterate over entries in the list in either direction.

uses
  container.list, utils.functor;

type
  generic TList<T, BinaryCompareFunctor> = class

BinaryCompareFunctor is based on utils.functor.TBinaryFunctor interface and used to compare two array items in sort and search functions.

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 list can be created by call its constructor.

constructor Create;
Example
uses
  container.list, utils.functor;

type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;

var
  list : TIntegerList;

begin
  list := TIntegerList.Create;

  FreeAndNil(list);
end;

Insert

There are several methods to insert data to the list.

Append

Append a value to the end of a TList. Return true if the request was successful, false if it was not possible to add the new entry.

function Append (AValue : T) : Boolean;
Example
uses
  container.list, utils.functor;
 
type
  IntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger
 
var
  list : TIntegerList;
  
begin
  list := TIntegerList.Create;
  
  list.Append(15);
  list.Append(-598565101);
  
  FreeAndNil(list);
end;

Prepend

Prepend a value to the beginning of a TList. Return true if the request was successful, false if it was not possible to add the new entry.

function Prepend (AValue : T) : Boolean;
Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  
begin
  list := TIntegerList.Create;
  
  list.Prepend(12);
  list.Prepend(-54);
  
  FreeAndNil(list);
end;

Remove

The methods to remove data from the list.

Remove

Remove all occurrences of a particular value from a list. Return the number of entries removed from the list.

function Remove (AData: T) : Cardinal;
Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  
begin
  list := TIntegerList.Create;
  
  list.Remove(0);
  list.Remove(8);
  
  FreeAndNil(list);
end;

Clear

Remove all entries from a TList.

procedure Clear;
Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  
begin
  list := TIntegerList.Create;
  
  list.Clear;
  
  FreeAndNil(list);
end;

Search

The methods to find data in the list.

FindEntry

Find the entry for a particular value in a list. Return TIterator.

function FindEntry (AData : T) : TIterator;
Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  iterator : TIntegerList.TIterator;
  
begin
  list := TIntegerList.Create;
  
  iterator := list.FindEntry(5);
  if iterator.HasValue then
    writeln(iterator.Value);
  
  FreeAndNil(list);
end;

NthEntry

Retrieve the entry at a specified index in a list.

function NthEntry (AIndex : Cardinal) : TIterator;
Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  iterator : TIntegerList.TIterator;
  
begin
  list := TIntegerList.Create;
  
  iterator := list.NthEntry(5);
  if iterator.HasValue then
    writeln(iterator.Value);
  
  FreeAndNil(list);
end;

Sort

Sort the values in a TList.

procedure Sort;
Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  
begin
  list := TIntegerList.Create;
  
  list.Sort;
  
  FreeAndNil(list);
end;

Length

Return TList size.

property Length : Cardinal;

IsEmpty

Return true if container is empty.

function IsEmpty : Boolean;
Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  iterator : TIntegerList.TIterator;
  
begin
  list := TIntegerList.Create;
  if list.IsEmpty then
    ;
  
  FreeAndNil(list);
end;

Iterate

It is possible to iterate for TList values using in operator. Each value would present as TList.TIterator object.

uses
  container.list, utils.functor;
  
type
  generic TList<T, BinaryCompareFunctor> = class
  type
    TIterator = class({$IFDEF FPC}specialize{$ENDIF} TBidirectionalIterator<T, TIterator>)
  end;

TBidirectionalIterator is a abstract class which provide interface for iterable object that can moves to both sides.

For ... in ...

Use for ... in ... operator for iterates over container items.

Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  val : Integer;
  
begin
  list := TIntegerList.Create;
  
  for val in list do
    ;
  
  FreeAndNil(list);
end;

FirstEntry

Retrive the iterator for first entry in a list.

function FirstEntry : TIterator;
Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  iterator : TIntegerList.TIterator;
  
begin
  list := TIntegerList.Create;
  
  iterator := list.FirstEntry;
  
  FreeAndNil(list);
end;

LastEntry

Retrive the iterator for last entry in a list.

function LastEntry : TIterator;
Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  iterator : TIntegerList.TIterator;
  
begin
  list := TIntegerList.Create;
  
  iterator := list.LastEntry;
  
  FreeAndNil(list);
end;

TIterator

HasValue

Return true if iterator has correct value.

function HasValue : Boolean;
Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  iterator : TIntegerList.TIterator;
  
begin
  list := TIntegerList.Create;
  
  iterator := list.FirstEntry;
  while iterator.HasValue do
  begin
  
    iterator := iterator.Next;
  end;
  
  FreeAndNil(list);
end;
Prev

Retrieve the iterator for previous entry in a list.

Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  iterator : TIntegerList.TIterator;
  
begin
  list := TIntegerList.Create;
  
  iterator := list.LastEntry;
  while iterator.HasValue do
  begin
  
    iterator := iterator.Prev;
  end;
  
  FreeAndNil(list);
end;
Next

Retrieve the iterator for next entry in a list.

Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  iterator : TIntegerList.TIterator;
  
begin
  list := TIntegerList.Create;
  
  iterator := list.FirstEntry;
  while iterator.HasValue do
  begin
  
    iterator := iterator.Next;
  end;
  
  FreeAndNil(list);
end;
Value

To get/set value use Value property.

property Value : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue{$ENDIF};

If iterator not have correct value returns empty TOptionalValue or raise EValueNotExistsException.

Example
uses
  container.list, utils.functor;
 
type
  TIntegerList = {$IFDEF FPC}type specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
 
var
  list : TIntegerList;
  iterator : TIntegerList.TIterator;
  
begin
  list := TIntegerList.Create;
  
  iterator := list.FirstEntry;
  while iterator.HasValue do
  begin
  	writeln(iterator.Value);
    iterator := iterator.Next;
  end;
  
  FreeAndNil(list);
end;

Additional

TFilterEnumerator

Class provides filtering enumerator by TUnaryFunctor.

More details read on wiki page.

TAccumulate

Accumulate iterable object data using functor.

More details read on wiki page.

TMap

Map applying the given functor to each item of a given iterable object.

More details read on wiki page.

Clone this wiki locally