# Algorithms

## Apply

### Prototypes

`TIntfApplyFunction = function (AObject: IInterface): IInterface;
TStrApplyFunction = function (const AObject: string): string;
TApplyFunction = function (AObject: TObject): TObject;
procedure Apply(First: IIntfIterator; Count: Integer; F: TIntfApplyFunction); overload;
procedure Apply(First: IStrIterator; Count: Integer; F: TStrApplyFunction); overload;
procedure Apply(First: IIterator; Count: Integer; F: TApplyFunction); overload;
`

### Description

Apply a function F to Count objects from First.

F is a callback function that implements any modification of AObject. The modified object must be returned by the function.

## Find

### Prototypes

`TIntfCompare = function (Obj1, Obj2: IInterface): Integer;
TStrCompare = function (const Obj, Obj2: string): Integer;
TCompare = function (Obj1, Obj2: TObject): Integer;
function Find(First: IIntfIterator; Count: Integer; AObject: IInterface; AComparator: TIntfCompare): IIntfIterator; overload;
function Find(First: IStrIterator; Count: Integer; const AObject: string; AComparator: TStrCompare): IStrIterator; overload;
function Find(First: IIterator; Count: Integer; AObject: TObject; AComparator: TCompare): IIterator; overload;
`

### Description

Find the first object that the function AComparator return 0 with AObject.

The algorithm treats Count objects from First.

The algorithm return an iterator pointing to the found object else nil.

The callback function AComparator must be return 0 when the 2 objects are equal.

## CountObject

### Prototypes

`TIntfCompare = function (Obj1, Obj2: IInterface): Integer;
TStrCompare = function (const Obj, Obj2: string): Integer;
TCompare = function (Obj1, Obj2: TObject): Integer;
function CountObject(First: IIntfIterator; Count: Integer; AObject: IInterface; AComparator: TIntfCompare): Integer; overload;
function CountObject(First: IStrIterator; Count: Integer; const AObject: string; AComparator: TStrCompare): Integer; overload;
function CountObject(First: IIterator; Count: Integer; AObject: TObject; AComparator: TCompare): Integer; overload;
`

### Description

Find and count all objects that the function AComparator return 0 with AObject.

The algorithm treats Count objects from First.

The algorithm return the number of objects found.

The callback function AComparator must be return 0 when the 2 objects are equal.

## Copy

### Prototypes

`procedure Copy(First: IIntfIterator; Count: Integer; Output: IIntfIterator); overload;
procedure Copy(First: IStrIterator; Count: Integer; Output: IStrIterator); overload;
procedure Copy(First: IIterator; Count: Integer; Output: IIterator); overload;
`

### Description

Copy Count objects from First to Output.

The elements in the Output must be exist. The algorithm does not create them.

## Generate

### Prototypes

`procedure Generate(List: IIntfList; Count: Integer; AObject: IInterface); overload;
procedure Generate(List: IStrList; Count: Integer; const AObject: string); overload;
procedure Generate(List: IList; Count: Integer; AObject: TObject); overload;
`

### Description

Add the value AObject in the List Count times.

The algorithm create new element for each value added.

## Fill

### Prototypes

`procedure Fill(First: IIntfIterator; Count: Integer; AObject: IInterface); overload;
procedure Fill(First: IStrIterator; Count: Integer; const AObject: string); overload;
procedure Fill(First: IIterator; Count: Integer; AObject: TObject); overload;
`

### Description

Fill with the value AObject Count objects from First.

The elements in the First must be exist. The algorithm does not create them.

## Reverse

### Prototypes

`procedure Reverse(First, Last: IIntfIterator); overload;
procedure Reverse(First, Last: IStrIterator); overload;
procedure Reverse(First, Last: IIterator); overload;
`

### Description

Reverse all objects between First and Last.

Iterators must be bidirectionnal.

## Sort

### Prototypes

`TIntfSortProc = procedure (AList: IIntfList; L, R: Integer; AComparator: TIntfCompare);
TStrSortProc = procedure (AList: IStrList; L, R: Integer; AComparator: TStrCompare);
TSortProc = procedure (AList: IList; L, R: Integer; AComparator: TCompare);
TIntfCompare = function (Obj1, Obj2: IInterface): Integer;
TStrCompare = function (const Obj, Obj2: string): Integer;
TCompare = function (Obj1, Obj2: TObject): Integer;
procedure Sort(AList: IIntfList; First, Last: Integer; AComparator: TIntfCompare); overload;
procedure Sort(AList: IStrList; First, Last: Integer; AComparator: TStrCompare); overload;
procedure Sort(AList: IList; First, Last: Integer; AComparator: TCompare); overload;
`

### Description

Sort objects in AList from First to Last using AComparator function.

The callback function AComparator must be return 0 when the 2 objects are equal, a number > 0 if Obj1 > Obj2 and a number < 0 if Obj1 < Obj2.

The default algorithm used is QuickSort. You can change it by assigning global variables IntfSortProc, StrSortProc and SortProc