how to handle very large array?

bauss jj_1337 at live.dk
Wed Feb 9 11:51:44 UTC 2022


On Wednesday, 9 February 2022 at 10:03:21 UTC, MichaelBi wrote:
> day6 of the advent of code 2021 needs to handle an array of 
> 10^12 length, or even bigger... plus change elements and append 
> elements. normal implementation such as length, appender and 
> ref element etc, seems cannot handle that big array? is there 
> any alternative data structure or algorithm can handle such 
> large array properly? thanks.

Use a memorymapped file that holds the array values, in theory it 
can be infinite then.

Since an array has a known size for each entry then you can treat 
the file as an array.

Let's say you have an array of ints.

For a memorymapped file you obviously only have bytes to work 
with, so each entry will be 4 bytes, since a 32 bit integer (int) 
is 4 bytes.

So to read something at a specific index you simply do N x I 
where N is the size of the type and I is the index you want to 
read at.

Otherwise the size of an array cannot exceed the RAM you have, if 
your system can't use diskspace as RAM of course.


More information about the Digitalmars-d-learn mailing list