[Submission] D Slices
eles
eles at eles.com
Tue May 31 11:07:44 PDT 2011
== Quote from Andrej Mitrovic (andrej.mitrovich at gmail.com)'s article
> On 5/31/11, eles <eles at eles.com> wrote:
> > Nice picture, but then why foo[1] is rather 8 than 4? And what is
foo
> > [9]?
> foo[9] is out of range. If foo[1] was actually referring to 4, then
> foo[0] would be out of range too. I've only used this picture
because
> back years ago when I was trying to understand how this whole system
> works there was an explanation similar to this one in some book, the
> name of which I forgot. But it did 'click' with me when I saw it
> presented like that.
Yes. Except that if you assume that indexes of an array are
represented, lets say on an unsigned type UTYPE of n bytes and let's
note UTYPE_MAX the 2^n-1 value (i.e. the maximum representable value
on n bytes) and if you have an array of exactly UTYPE_MAX elements:
a[0],...,a[UTYPE_MAX]
then, to slice something including the last element, you would need
to represent the UINT_MAX+1 value on n bytes, which is of course
impossible.
In your example, assume that n=3. So, to slice a substring of two
elements containing the last element, you cannot write a[7..9]. It
simply overflows. The only choice you have is to write the a[7..$]
thing where, at least conceptually, $ would still be an oxymoron
represented on... n+1 bits (because n are not enough).
Consider such a string and that you need to process slices of two
elements (I remind that length of the array is UTYPE_MAX+1 and that
its indices are going from 0 to UTYPE_MAX):
//closed-limits approach
for(i=0; i<=UTYPE_MAX-1; i++){
b=a[i..i+1]; //still possible even for i=UTYPE_MAX-1
some_process(b);
}
or
for(i=0; i<UTYPE_MAX; i++){
b=a[i..i+1]; //still possible even for the last value of i, which is
UTYPE_MAX-1
some_process(b);
}
while
//for the open-limit approach
for(i=0; i<=UTYPE_MAX-1){
b=a[i..i+2]; //needed to include the last element
some_process(b);
}
or
for(i=0; i<UTYPE_MAX){
b=a[i..i+2]; //needed to include the last element
some_process(b);
}
However, the latest two will overflow when trying to compute i+2 for
i=UTYPE_MAX-1, as it would give... UTYPE_MAX+1.
You would be forced to use some inconsistent behavior such as:
for(i=0; i<UTYPE_MAX-1){ //process all slices except the last
b=a[i..i+2]; //now, it is still possible to compute i+2 for
i=UTYPE_MAX-2, the maximum value of i
some_process(b);
}
some_process(a[UTYPE_MAX-1..$]); //the *only* way to write a slice
containing the last element of the array.
Using closed-limits does not require inconsistency into
representation and processing of data, at least in this case.
More information about the Digitalmars-d
mailing list