- Notifications
You must be signed in to change notification settings - Fork 10
TList
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> = classBinaryCompareFunctor is based on utils.functor.TBinaryFunctor interface and used to compare two array items in sort and search functions.
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}A new list can be created by call its constructor.
constructor Create;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;There are several methods to insert data to the list.
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;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 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;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;The methods to remove data from the list.
Remove all occurrences of a particular value from a list. Return the number of entries removed from the list.
function Remove (AData: T) : Cardinal;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;Remove all entries from a TList.
procedure Clear;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;The methods to find data in the list.
Find the entry for a particular value in a list. Return TIterator.
function FindEntry (AData : T) : TIterator;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;Retrieve the entry at a specified index in a list.
function NthEntry (AIndex : Cardinal) : TIterator;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 the values in a TList.
procedure Sort;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;Return TList size.
property Length : Cardinal;Return true if container is empty.
function IsEmpty : Boolean;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;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.
Use for ... in ... operator for iterates over container items.
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;Retrive the iterator for first entry in a list.
function FirstEntry : TIterator;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;Retrive the iterator for last entry in a list.
function LastEntry : TIterator;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;Return true if iterator has correct value.
function HasValue : Boolean;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;Retrieve the iterator for previous entry in a list.
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;Retrieve the iterator for next entry in a list.
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;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.
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;Class provides filtering enumerator by TUnaryFunctor.
More details read on wiki page.
Accumulate iterable object data using functor.
More details read on wiki page.
Map applying the given functor to each item of a given iterable object.
More details read on wiki page.