-
Notifications
You must be signed in to change notification settings - Fork 10
TMultiArray
TMultiArray is a generic array of array of T which automatically increase in size. It is based on pascal's dynamic array structures. TMultiArray internally stores all data in a safe manner for strings and user-defined data structures.
uses
container.multiarray, utils.functor;
type
generic TMultiArray<T, BinaryCompareFunctor> = class
BinaryCompareFunctor is based on utils.functor.TBinaryFunctor interface and used to compare two array items in sort and search functions.
MultiValue uses as internal array item value.
type
TMultiValue = {$IFDEF FPC}specialize{$ENDIF} TArrayList<T, BinaryCompareFunctor>;
If macro {$USE_OPTIONAL}
is defined, then all methods return a TOptionalMultiValue wrapper, otherwise TMultiValue.
uses
utils.optional;
type
TOptionalMultiValue = {$IFDEF FPC}specialize{$ENDIF} TOptional<TMultiValue>;
For non-existent values, returns a empty TOptionalMultiValue if defined or an EIndexOutOfRangeException is thrown.
type
{$IFNDEF USE_OPTIONAL}
EIndexOutOfRangeException = class(Exception);
{$ENDIF}
A new multi 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);
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
begin
arr := TIntegerMultiArray.Create;
FreeAndNil(arr);
end;
There are several methods to insert array to the multi array.
Append an array to the end of a TMultiArray. Return true if the request was successful, false if it was not possible to add the new entry.
function Append (ALength : Cardinal = 0) : Boolean;
uses
container.multiarray, utils.functor;
type
IntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
begin
arr := TIntegerMultiArray.Create;
arr.Append;
FreeAndNil(arr);
end;
Prepend an array to the beginning of a TMultiArray. Return true if the request was successful, false if it was not possible to add the new entry.
function Prepend (ALength : Cardinal = 0) : Boolean;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
begin
arr := TIntegerMultiArray.Create;
arr.Prepend;
FreeAndNil(arr);
end;
Insert an array at the specified index in a TMultiArray. 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; ALength : Cardinal = 0) : Boolean;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
begin
arr := TIntegerMultiArray.Create;
arr.Insert(0);
FreeAndNil(arr);
end;
The methods to remove array from the multi array.
Remove the array at the specified location in a TMultiArray. If the array at index doesn't exists, nothing happens and returns false.
function Remove (AIndex: Cardinal) : Boolean;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
begin
arr := TIntegerMultiArray.Create;
arr.Remove(0);
FreeAndNil(arr);
end;
Remove a range of arrays at the specified location in a TMultiArray. If the arrays at indexes don't exists, nothing happens and returns false.
function RemoveRange (AIndex : LongInt; ALength : LongInt) : Boolean;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
begin
arr := TIntegerMultiArray.Create;
arr.RemoveRange(0, 5);
FreeAndNil(arr);
end;
Remove all arrays from a TMultiArray.
procedure Clear;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
begin
arr := TIntegerMultiArray.Create;
arr.Clear;
FreeAndNil(arr);
end;
To get value from a TMultiArray use Value property.
property Value [AIndex : LongInt] : {$IFNDEF USE_OPTIONAL}TMultiValue{$ELSE}TOptionalMultiValue{$ENDIF};
If index not exists returns empty TOptionalMultiValue or raise EIndexOutOfRangeException.
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
begin
arr := TIntegerMultiArray.Create;
writeln(arr.Value[0].Value[0]);
FreeAndNil(arr);
end;
Get TMultiArray size.
property Length : LongInt;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
begin
arr := TIntegerMultiArray.Create;
writeln(arr.Length);
FreeAndNil(arr);
end;
Return true if container is empty.
function IsEmpty : Boolean;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
begin
arr := TIntegerMultiArray.Create;
if arr.IsEmpty then
;
FreeAndNil(arr);
end;
It is possible to iterate for TMultiArray arrays using in
operator. Each value would present as TMultiArrayList.TIterator object.
uses
container.multiarray, utils.functor;
type
generic TMultiArray<T, BinaryCompareFunctor> = class
type
TIterator = class({$IFDEF FPC}specialize{$ENDIF} TBidirectionalIterator<T, TMultiValue>)
end;
TBidirectionalIterator is a abstract class which provide interface for iterable object that can moves to both sides.
Use for ... in ... operator for iterates over container arrays.
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
val : Integer;
begin
arr := TIntegerMultiArray.Create;
for val in arr do
;
FreeAndNil(arr);
end;
Retrive the iterator for first arrays in a multi array.
function FirstEntry : TIterator;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
iterator : TIntegerMultiArray.TIterator;
begin
arr := TIntegerMultiArray.Create;
iterator := arr.FirstEntry;
FreeAndNil(arr);
end;
Retrive the iterator for last array in a multi array.
function LastEntry : TIterator;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
iterator : TIntegerMultiArray.TIterator;
begin
arr := TIntegerMultiArray.Create;
iterator := arr.LastEntry;
FreeAndNil(arr);
end;
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}
Return true if iterator has correct value.
function HasValue : Boolean;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
iterator : TIntegerMultiArray.TIterator;
begin
arr := TIntegerMultiArray.Create;
iterator := arr.FirstEntry;
while iterator.HasValue do
begin
iterator := iterator.Next;
end;
FreeAndNil(arr);
end;
Retrieve the iterator for previous array in a multi array.
function Prev : TIterator;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
iterator : TIntegerMultiArray.TIterator;
begin
arr := TIntegerMultiArray.Create;
iterator := arr.LastEntry;
while iterator.HasValue do
begin
iterator := iterator.Prev;
end;
FreeAndNil(arr);
end;
Retrieve the iterator for next array in a multi array.
function Next : TIterator;
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
iterator : TIntegerMultiArray.TIterator;
begin
arr := TIntegerMultiArray.Create;
iterator := arr.FirstEntry;
while iterator.HasValue do
begin
iterator := iterator.Next;
end;
FreeAndNil(arr);
end;
To get value use Value property.
property Value : {$IFNDEF USE_OPTIONAL}TMultiValue{$ELSE}TOptionalMultiValue{$ENDIF};
If iterator not have correct value returns empty TOptionalMultiValue or raise EIndexOutOfRangeException.
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
iterator : TIntegerMultiArray.TIterator;
begin
arr := TIntegerMultiArray.Create;
iterator := arr.FirstEntry;
while iterator.HasValue do
begin
writeln(iterator.Value.Value[0]);
iterator := iterator.Next;
end;
FreeAndNil(arr);
end;
Get current array index.
property Index : {$IFNDEF USE_OPTIONAL}LongInt{$ELSE}TOptionalIndex;
If iterator not have correct value returns empty TOptionalIndex or raise EIndexOutOfRangeException.
uses
container.multiarray, utils.functor;
type
TIntegerMultiArray = {$IFDEF FPC}type specialize{$ENDIF} TMultiArray<Integer, TCompareFunctorInteger>;
var
arr : TIntegerMultiArray;
iterator : TIntegerMultiArray.TIterator;
begin
arr := TIntegerMultiArray.Create;
iterator := arr.FirstEntry;
while iterator.HasValue do
begin
writeln(iterator.Index);
iterator := iterator.Next;
end;
FreeAndNil(arr);
end;