Dynamic Array reserve

Vino vino.bheeman at hotmail.com
Sat Dec 16 22:48:13 UTC 2017


On Saturday, 16 December 2017 at 16:46:49 UTC, Jacob Carlborg 
wrote:
> On 2017-12-16 15:11, Vino wrote:
>> Hi All,
>> 
>>   Request your help on reserve an dynamic array when the 
>> capacity is reached to a point(eg: 80%) so the array to extend 
>> the reserve by next 20%
>> 
>> Example:
>> Array!string Test;
>> Test. reserve(100) - Initall
>> Test =(.........) - The number of entries are dynamic
>> if (array.capacity > 80%) { array.reserve(100+20%)
>
> There's a "capacity" property which you can use. Compare that 
> to the length of the array to know when it has reach 80%.

Hi Jacob,

   Thank you , yes we can use the length property and calculate 
the capacity, but the question is how to implement it 
dynamically, so let me explain a bit further.

1> Let assume that an array named Size is defined
string ConfigFile = "Test.txt"  //The size of the file is not 
known.
Array!string Size

2> Reserve the size of the array Test.
Size.reserve(100) //Initial allocation

3> Appending data
Size = File(ConfigFile).byLineCopy();

4> Let us assume the file Test.txt   contains 50,000 lines.

5> As per step 2 we have reserved the array to 100 which means 
that the array can hold 100 lines.

6> Check for every 1 mins if the length of the array(Size) is 
above 80(lines), then increase the reservation of the array by 
20, Size.reserve = (Size.capacity + 20).

7> Step 3 and Step 6 should be running parallel

8> Once step 3 is completed Step 6 should also be ended.

From,
Vino.B






More information about the Digitalmars-d-learn mailing list