Generic Builder for Dynamic Arrays in Delphi
Code for blogpost: https://schellingerhout.github.io/utilities/array-builder-delphi/
Sample use:
function PrimesBelow(MaxValue: integer): TArray<Integer>;
var
Prime: integer;
PrimeBuilder: TArrayBuilder<Integer>;
begin
Prime := 2;
while Prime < MaxValue do
begin
PrimeBuilder.Add(Prime)
Prime := NextPrime(Prime); // implementation not shown
end;
result := PrimeBuilder.GetArray;
end;
The growth function that grows linearly can be set also. For instance to grow linearly in blocks of 100
...
PrimeBuilder.SetGrowthFunction(
function (Size: integer): integer
begin
result := Size + 100;
end);
...