Memory footprint of multi-dimensional arrays

Ben Mitchell Arisian at gmail.com
Fri Apr 18 16:49:16 PDT 2008


Simen Kjaeraas Wrote:
> 
> Now, using a struct with overloaded opIndex (and opSlice, if you want)
> should be lighter. Something like this (untested)?
> 
> 
> struct Foo
> {
>    ubyte[] data;
>    size_t sz1, sz2;
> 
>    static Foo opCall(size_t size1, size_t size2)
>    {
>      Foo tmp;
>      tmp.data.length = size1 * size2;
>      tmp.sz1 = size1;
>      tmp.sz2 = size2;
>      return tmp;
>    }
> 
>    ubyte[] opIndex(size_t index)
>    {
>      return data[index * size2..index * size2 + size1];
>    }
> 
>    void opIndexAssign(ubyte[] value, size_t index)
>    {
>      data[index * size2..index * size2 + size1] = value;
>    }
> }
> 
> -- Simen

Yeah, that was more or less what I'd come up with, too; it just seemed sort of inelegant, and I was hoping that there was some clean, built in way to do this that I just didn't know about.  If there's not, I think that some built-in syntax for non-ragged multi dimensional arrays would be nice, given that it's a pretty common thing to need in scientific computation, and it doesn't seem like it should be that hard an addition to make.

   - Ben



More information about the Digitalmars-d mailing list