negative lengths and reverse pointers

Reiner Pope reiner.pope at REMOVE.THIS.gmail.com
Fri Oct 20 01:59:42 PDT 2006


Ameer Armaly wrote:
> Hi all.  After thinking about foreach_reverse and arrays, a rather strange 
> idea hit me: what if we had reverse arrays, where whenever you tried to 
> access an element it would go backwards instead of forwards in a memmory 
> block to find it?  This could be indicated by a negative length; if length 
> is negative, the pointer points to the end of the memmory chunk as opposed 
> to the beginning.  When you assign a negative length, all the compiler would 
> have to do is find the endpoint of the positive version of the length you 
> gave it and set the pointer to point to that.  This way array.reverse simply 
> = array.length * -1, which would allow you to go through an array backwards 
> with much more efficiency.  Kind of random, but might be useful; ideas?
> 
This would be allowed by Norbert Nemec's strided arrays proposal (it's 
for multidimensional arrays, but it can also be useful for 1-dimensional 
arrays). This can indeed be a powerful feature, and his proposal milks 
that power, while syntactically separating strided arrays from normal 
arrays to avoid the overhead of checking the stride (which is equivalent 
to checking whether the length is negative in your example).

Cheers,

Reiner



More information about the Digitalmars-d mailing list