Forum > Beginners

Dynamic arrays concatenation, insertion and deletion of elements

(1/6) > >>

Kwadrum:
Hi,
Reading Marco Cantu’s book on Object Pascal, I got to the description of dynamic arrays concatenation and adding unitary elements into them. It is probably nothing new for the most of people here, but I realized this also works in FPC 3.2:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program dyn_array_tricks; {$mode objfpc} {$H+} {$modeSwitch arrayOperators+} usesSysUtils; varda: array of byte;i, k: byte; BEGIN    da := [1, 2, 3];    da := da + da;                  //da := Concat(da,da); does the same as above    da := da + [4, 5];      k := 99;    da := da + [k];      for i in da do            Writeln(i.ToString);           //Writeln(IntToStr(i)); does the same as aboveEND.
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.

So I have two questions on this set of features:
1) What would be an “ideal”, but real life example of using these dynamic arrays features?
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.

Thank you.

Handoko:

--- Quote from: Kwadrum on September 28, 2020, 03:23:26 am ---1) What would be an %u201Cideal%u201D, but real life example of using these dynamic arrays features?

--- End quote ---

For example, when writing a game you use TInventory to store player's items.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 questprocedure 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.

Thaddy:
The feature is new in 3.2.0. See the release notes:
https://wiki.freepascal.org/FPC_New_Features_3.2.0#Dynamic_Arrays_have_built_in_.2B_operator_support


A side note regarding your code: write/writeln/writestr already supports integer - and many more -  formatting capabilities by default so:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---      for i in da do            Writeln(i);// no string conversion necessaryThis is Delphi compatible(apart from writestr).   
see the manual for the supported types and formatting options: https://www.freepascal.org/docs-html/rtl/system/write.html

PascalDragon:

--- Quote from: Kwadrum on September 28, 2020, 03:23:26 am ---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.
--- End quote ---

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).


--- Quote from: Kwadrum on September 28, 2020, 03:23:26 am ---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.
--- End quote ---

Dynamic arrays are more low level than any class type and in addition to that they are managed automatically. More often than not using an array is simpler than using a class based container.

Kwadrum:

--- Quote from: PascalDragon on September 28, 2020, 09:22:34 am ---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).
--- End quote ---

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)?

Navigation

[0] Message Index

[#] Next page

Go to full version