negative lengths and reverse pointers

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Oct 19 16:59:49 PDT 2006


"Ameer Armaly" <ameer_armaly at hotmail.com> wrote in message 
news:eh8mkk$728$1 at digitaldaemon.com...
> 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?

I really like the idea, though I'm not sure what kind of overhead it'd add 
to the implementation.. even in release mode, it'd have to figure out 
whether to index forwards or backwards based on the length of the array. 
Maybe someone could knock up a simple templated test case to see how it 
performs? 





More information about the Digitalmars-d mailing list