Change representation of dynamic arrays?
Don Clugston
dac at nospam.com.au
Sun Oct 21 11:49:18 PDT 2007
Walter Bright wrote:
> Currently, arrays are represented under the hood as:
>
> size_t lengthOfArray;
> void* ptrToStartOfArray;
>
> Which works out reasonably well. The problem is if you want to use array
> types as the basis of iterators, and you want to step through the array.
> There's no escaping it being two operations:
>
> decrement the length
> increment the pointer
>
> This puts a brick in any fast implementation of iterators. To fix that,
> we can change the representation to:
>
> void* ptrToStartOfArray;
> void* ptrPastEndOfArray;
This seems to be a more powerful construction than .ptr + .length. If you know
the length, it's usually trivial to calculate the end, but the converse is not
necessarily true.
My first reaction is, does it generalise to multi-dimensional arrays? (I'm
hoping we'll still get them eventually, though not necessarily as built-in types).
Haven't yet thought through it properly, but I think it's probably _better_.
It means that a 'strided slice' (eg, every 4th element) could be implemented
without mucking around with the length element. It's easy to test if two such
arrays overlap.
Many operations can be done entirely at compile time, simply using a cast.
votes++;
I'm less sure about the use of slices as iterators though.
More information about the Digitalmars-d
mailing list