Memory footprint of multi-dimensional arrays

Bill Baxter dnewsgroup at billbaxter.com
Fri Apr 18 16:54:49 PDT 2008


Ben Mitchell wrote:
> 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

May be too heavy-weight for your needs, but my Murray lib supports 
arbitrary N-dimensional arrays using a single allocation strategy like that.

And my DFLAT lib supports matrices (just 2-dimensional arrays) in a 
similar way (but also supports different storage formats like banded, 
triangular, and various sparse matrix formats).

both are at http://www.dsource.org/projects/multiarray

--bb



More information about the Digitalmars-d mailing list