Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

TArrayList

Ivan Semenkov edited this page Aug 8, 2021 · 12 revisions

Table of contents

About

TArrayList is a generic array of T which automatically increase in size. It is based on pascal's dynamic array structure. ArrayList internally stores all data in a safe manner for strings and user-defined data structures.

uses
  container.arraylist, utils.functor;

type
  generic TArrayList<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 EIndexOutOfRangeException is thrown.

type
  {$IFNDEF USE_OPTIONAL}
  EIndexOutOfRangeException = class(Exception);
  {$ENDIF}

Create

A new array can be created by call its constructor. It is also possible to reserve memory for items by the first argument.

constructor Create (ALength : Cardinal = 0);
Example
uses
  container.arraylist, utils.functor;

type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;

var
  arr : TIntegerArray;

begin
  arr := TIntegerArray.Create;

  FreeAndNil(arr);
end;

Insert

There are several methods to insert data to the array.

Append

Append a value to the end of a TArrayList. 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.arraylist, utils.functor;
 
type
  IntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger
 
var
  arr : TIntegerArray;
  
begin
  arr := TIntegerArray.Create;
  
  arr.Append(15);
  arr.Append(-598565101);
  
  FreeAndNil(arr);
end;

Prepend

Prepend a value to the beginning of a TArrayList. 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.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  
begin
  arr := TIntegerArray.Create;
  
  arr.Prepend(12);
  arr.Prepend(-54);
  
  FreeAndNil(arr);
end;

Insert

Insert a value at the specified index in a TArrayList. The index where the new value can be inserted is limited by the size of the array. Returns true if successful, else false (due to an invalid index or if it was impossible to add the more entry).

function Insert (AIndex : LongInt; AData : T) : Boolean;
Examples
uses
  container.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  
begin
  arr := TIntegerArray.Create;
  
  arr.Insert(0, 25);
  arr.Insert(1, -36);
  
  FreeAndNil(arr);
end;

Remove

The methods to remove data from the array.

Remove

Remove the entry at the specified location in a TArrayList. If the item at index doesn't exists, nothing happens and returns false.

function Remove (AIndex: Cardinal) : Boolean;
Example
uses
  container.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  
begin
  arr := TIntegerArray.Create;
  
  arr.Remove(0);
  arr.Remove(8);
  
  FreeAndNil(arr);
end;

Remove range

Remove a range of entries at the specified location in a TArrayList. If the items at indexes don't exists, nothing happens and returns false.

function RemoveRange (AIndex : LongInt; ALength : LongInt) : Boolean;
Example
uses
  container.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  
begin
  arr := TIntegerArray.Create;
  
  arr.RemoveRange(0, 5);
  
  FreeAndNil(arr);
end;

Clear

Remove all entries from a TArrayList.

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

Search

Find the index of a particular value in a TArrayList. Return the index of the value if found, or -1 if not found.

function IndexOf (AData : T) : Integer;
Example
uses
  container.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  
begin
  arr := TIntegerArray.Create;
  
  writeln(arr.IndexOf(5));
  
  FreeAndNil(arr);
end;

Sort

Sort the values in a TArrayList.

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

Value

To get/set value for a TArrayList use Value property.

property Value [AIndex : LongInt] : {$IFNDEF USE_OPTIONAL}T{$ELSE}TOptionalValue{$ENDIF};

If index not exists returns empty TOptionalValue or raise EIndexOutOfRangeException.

Example
uses
  container.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  
begin
  arr := TIntegerArray.Create;
  
  arr.Value[0] := 12;
  writeln(arr.Value[0]);
  
  FreeAndNil(arr);
end;

Length

Get TArrayList size.

property Length : LongInt;
Example
uses
  container.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  
begin
  arr := TIntegerArray.Create;
  writeln(arr.Length);
  
  FreeAndNil(arr);
end;

IsEmpty

Return true if container is empty.

function IsEmpty : Boolean;
Example
uses
  container.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  
begin
  arr := TIntegerArray.Create;
  if arr.IsEmpty then
    ;
  
  FreeAndNil(arr);
end;

Iterate

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

uses
  container.arraylist, utils.functor;
  
type
  generic TArrayList<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.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  val : Integer;
  
begin
  arr := TIntegerArray.Create;
  
  for val in arr do
    ;
  
  FreeAndNil(arr);
end;

FirstEntry

Retrive the iterator for first entry in an array list.

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

LastEntry

Retrive the iterator for last entry in an array list.

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

TIterator

TOptionalIndex

If macro {$USE_OPTIONAL} is defined, then Index method return a TOptionalIndex wrapper, otherwise LongInt.

uses
  utils.optional;

type
  TOptionalIndex = {$IFDEF FPC}specialize{$ENDIF} TOptional<LongInt>;

For non-existent index, returns a empty TOptionalIndex if defined or an EIndexOutOfRangeException is thrown.

type
  {$IFNDEF USE_OPTIONAL}
  EIndexOutOfRangeException = class(Exception);
  {$ENDIF}
HasValue

Return true if iterator has correct value.

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

Retrieve the iterator for previous entry in an array list.

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

Retrieve the iterator for next entry in an array list.

function Next : TIterator;
Example
uses
  container.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  iterator : TIntegerArray.TIterator;
  
begin
  arr := TIntegerArray.Create;
  
  iterator := arr.FirstEntry;
  while iterator.HasValue do
  begin
  
    iterator := iterator.Next;
  end;
  
  FreeAndNil(arr);
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 EIndexOutOfRangeException.

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

Get current item index.

property Index : {$IFNDEF USE_OPTIONAL}LongInt{$ELSE}TOptionalIndex;

If iterator not have correct value returns empty TOptionalIndex or raise EIndexOutOfRangeException.

uses
  container.arraylist, utils.functor;
 
type
  TIntegerArray = {$IFDEF FPC}type specialize{$ENDIF} TArrayList<Integer, TCompareFunctorInteger>;
 
var
  arr : TIntegerArray;
  iterator : TIntegerArray.TIterator;
  
begin
  arr := TIntegerArray.Create;
  
  iterator := arr.FirstEntry;
  while iterator.HasValue do
  begin
  	writeln(iterator.Index);
    iterator := iterator.Next;
  end;
  
  FreeAndNil(arr);
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