1) What would be an %u201Cideal%u201D, but real life example of using these dynamic arrays features?
On the very first look it seems like a very cool feature, but then I tried to think of a possible application of it. This approach is probably not very fast as (I guess) it uses array resizing (like SetLength) at before addition of new elements and therefore there is not much sense in putting it into a loop. So far I have come up with only one example, when one needs to iteratively compute some value (an integral, etc) with a pre-set accuracy and would like to keep track of the whole trajectory. So at each iteration (of unknown number of course) it is possible to add the values of some parameters into an array to see later how the changed.
2) Isn’t this anything similar (or even based on) to the TVector type from RTL? As far as I can see it is capable of doing (almost?) exactly the same things.
Using it in a loop to add single elements is indeed not a good idea, but that is also true for manually doing this. If you however concatenate larger arrays then it will pay off, because the RTL uses Move to copy the elements (of course it also adjusts reference counts of managed types).
Using it in a loop to add single elements is indeed not a good idea, but that is also true for manually doing this. If you however concatenate larger arrays then it will pay off, because the RTL uses Move to copy the elements (of course it also adjusts reference counts of managed types).
Thanks a lot for explanations!
Do I understand it correctly, that at each addition of a new element (to the end or insertion in the middle) the array is resized by making a new N+1 array and copying all the N elements of the original array + one new into it? If so, I guess, the same applies to deletions within arrays and concatenations.
Therefore, the bigger the array the longer it takes to add/remove a new element?
Isn't that what is estimated as O(N)?
Of course deleting an element inside the array is more involved again.Yes, but it should be O(N/2)?
I tried this, but this is applicable only to array of byte. Is there any way that I can do the similar thing with array of integer?
What do you mean? It should work with any array. Or are you using FPC 3.0.4? The array features are new in 3.2.0.
In principle it is O(N), but there is an optimization the RTL does: it uses ReallocMem to allocate the new memory, so if the memory manager determines that it can reuse the space directly behind the array there won't be a completely new allocation, thus no need to move the whole array over. If you added an element at the end that that will be it. If you added an element in the middle you'll of course have another Move. Concat also does a ReallocMem and if everything fits there will only be the Move of the second (third, fourth, ...) array.Ah, so it is where that O(N/2) comes from... for an addition of one element to the end of the array I guess.
Also when shrinking the array this works out as well, especially if non-managed types are used. Of course deleting an element inside the array is more involved again.
1) What would be an %u201Cideal%u201D, but real life example of using these dynamic arrays features?
For example, when writing a game you use TInventory to store player's items.
const MaxItemID = 7; HPBig = 1; // big healing potion HPSmall = 2; // small healing potion MPBig = 3; // big mana potion MPSmall = 4; // small mana potion Sw = 5; // common sword SwSmall = 6; // small sword SwLong = 7; // long sword type TInventory = array of Byte; var Inventory = TInventory; // Give some bonus items if player finish a quest procedure BonusItems; begin Inventory := Inventory + [HPSmall, MPSmall] + [Random(MaxItemID)+1]; end;
It just quickly written example. For better coding I probably will use enum type for the items' IDs.
Sorry, I am not familiar with this kind of definitions:Does it mean one doesn't have to specify the exact type of T? Is there any specific term for this operation so I could search for it online?
procedure push<T>(var a :T;index:int32;insertion:T); begin insert(insertion,a,index); end;
I am using version 3.2.2 64 bits win.
I think you need either this or the previous version for generics
https://wiki.freepascal.org/Generics
The generic examples and methods are hard to follow (for me anyway) in the link.
If I didn't already use them in c++ I would still be trying to fathom them out.
Luckily they are almost identical to c++.
The gmax function for instance, I don't really need any class or use specialize.