www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dynamic array + AA array

reply Brett <Brett gmail.com> writes:
The idea is to basically use a dynamic array for most of the 
items, then an array to get the rest.

T[] Base;
T[int] Rest;

Then if Base has a max size(usually it might be fixed due to some 
algorithm) the Rest AA can pick up any outside values easily.

The idea here is to be able to combine them as one "infinite" 
array of T.

Any indexing outside of Base gets carried in to Rest.

Now the question is, is there any way to wrap all this in to 
dynamic array semantics easily?

In fact, we might want to have ranges of indices that are 
relatively dense handled by several Bases and then sparse ranges 
handled by the Rest. If the structure is nice we can just nest it.

E.g.,

InfArray(InfArray(min1, max1),min2,max2)

would have two bases, one that handles [min1...max1) and then 
[min2..max2) and then has one or two Rest's for the rest of the 
values(not sure if we could probably use a single AA sense it 
might be used for something else or not, could make it optional 
to reference the same array).

This sorta gets one that best of both worlds, in fact, I wonder 
if a programming language could use such a thing(I think Lua 
probably is based on this idea with it's table's?).
Sep 17 2019
next sibling parent Paul Backus <snarwin gmail.com> writes:
On Tuesday, 17 September 2019 at 14:33:30 UTC, Brett wrote:
 The idea is to basically use a dynamic array for most of the 
 items, then an array to get the rest.

 T[] Base;
 T[int] Rest;

 Then if Base has a max size(usually it might be fixed due to 
 some algorithm) the Rest AA can pick up any outside values 
 easily.

 The idea here is to be able to combine them as one "infinite" 
 array of T.

 Any indexing outside of Base gets carried in to Rest.

 Now the question is, is there any way to wrap all this in to 
 dynamic array semantics easily?
You can create a wrapper type for this that supports indexing, slicing, etc. using D's operator overloading: https://dlang.org/spec/operatoroverloading.html
Sep 17 2019
prev sibling parent reply Andrea Fontana <nospam example.com> writes:
On Tuesday, 17 September 2019 at 14:33:30 UTC, Brett wrote:
 The idea is to basically use a dynamic array for most of the 
 items, then an array to get the rest.

 T[] Base;
 T[int] Rest;

 Then if Base has a max size(usually it might be fixed due to 
 some algorithm) the Rest AA can pick up any outside values 
 easily.

 The idea here is to be able to combine them as one "infinite" 
 array of T.

 Any indexing outside of Base gets carried in to Rest.
What if I try to read an index that was not assigned? What if I try a foreach on it?
Sep 17 2019
parent Brett <Brett gmail.com> writes:
On Tuesday, 17 September 2019 at 15:51:34 UTC, Andrea Fontana 
wrote:
 On Tuesday, 17 September 2019 at 14:33:30 UTC, Brett wrote:
 The idea is to basically use a dynamic array for most of the 
 items, then an array to get the rest.

 T[] Base;
 T[int] Rest;

 Then if Base has a max size(usually it might be fixed due to 
 some algorithm) the Rest AA can pick up any outside values 
 easily.

 The idea here is to be able to combine them as one "infinite" 
 array of T.

 Any indexing outside of Base gets carried in to Rest.
What if I try to read an index that was not assigned? What if I try a foreach on it?
It all has to function as an single array. If you try to read an index that wasn't assigned to a normal array or AA what happens? If you try a foreach on a normal array or AA what happens?
Sep 17 2019