Fixed size multidimensional array at runtime

Roman D. Boiko rb at d-coding.com
Sat Jun 30 13:24:21 PDT 2012


On Saturday, 30 June 2012 at 20:06:58 UTC, Vidar Wahlberg wrote:
> On Saturday, 30 June 2012 at 19:35:33 UTC, Denis Shelomovskij 
> wrote:
>> You could be interested in my answer on this thread:
>> http://forum.dlang.org/thread/mailman.1578.1339962782.24740.digitalmars-d-learn@puremagic.com
>
> Thanks for the tip, that is interesting (I'm surprised I didn't 
> come across this post when searching for this issue earlier). 
> Although it seems to me that you still end up with "matrix[x, 
> y, z]" instead of "matrix[x][y][z]", so it will only solve one 
> half of the problem :)
> For this particular case I'll just do the conversion from 
> two-dimensional to one-dimensional array programmatically and 
> use a "get(x, y)"-method, it's neither difficult nor will it 
> make the code complicated.

To have syntax m[x][y] you can create a range representing a row 
that knows its parent range + start offset (equal to x * 
row.length) and return it from m[x]. This way if m and m[x] are 
both stored on stack (or in the same cache block) you will not 
have to pay for additional indirection: to resolve m[x][y] just 
calculate an index as a sum of start offset and y. You may 
alternatively simply return a row, but the former approach easily 
generalizes for slicing by column first (in that case you would 
need to pick up appropriate syntax, probably a method call).


More information about the Digitalmars-d-learn mailing list