SRL Archive - OSR Help

Appending numbers to a tintegerarray?


elephant

2015-04-15 22:38:43

How can I append numbers to a TIntgerArray? I can't seem to figure it out/find anything that works for it.

Obscurity

2015-04-15 22:44:29

procedure tIntegerArray.push(funcNum:int32);
var
funcLength:uInt32;
begin
setLength(self,(funcLength:=length(self))+1);
self[funcLength]:=funcNum;
end;

var
__array:tIntegerArray;
__index:uInt32=0;

begin
for __index to 100 do
__array.push(__index);
writeLN(__array);
end

'roundabout way.

3Garrett3

2015-04-15 23:08:27

If you're using SRL there is a function TIntegerArray.Append();

If you're not:


procedure TIntegerArray.Append(value:Integer);
begin
Self := Self + value;
end;

elephant

2015-04-15 23:20:31

Ah, thank you for the replies :). I was trying to use TIntegerArray.Append() without SRl, those solutions are great.

Obscurity

2015-04-16 14:57:13

I've just tried them, and self:=self+value is about 7x slower then setting the length and then adding the value. Shame, I was excited to use that. >.<

bonsai

2015-04-16 17:20:37

We should fix them up then. That was a recent update.

Probably bogs down my fighter some I use a ton of int array funcs

Gu3st...

2015-04-16 17:22:46

There's also the generic insert(needle, haystack, index) function that works for all arrays of any type

slacky

2015-04-16 19:22:36

All:
> Insert
> SRL's Append
> Arr := Arr + Item
Is indeed much slower than setting the length ONCE. This should be obvious since we don't over-allocate. There will be lots of calls to SetLength, and so the location might have to be moved a few times while reallocating, and realloc in it self costs a bit.

"Arr+Item" is the same as doing (this is pretty much what it outputs):
> tmp := arr
> SetLength(tmp, Length(tmp)+1)
> tmp[High(tmp)] := Item
> result := tmp