[Submission] D Slices

Nick Sabalausky a at a.a
Tue May 31 13:29:44 PDT 2011


"eles" <eles at eles.com> wrote in message news:is3g3j$n2a$1 at digitalmars.com...
>> > Actually, I think 0-based vs. 1-based is of little importance in
> the
>> > fact if limits should be closed or open. Why do you think the
>> > contrary?
>> Good question. Actually, I'm not too sure if 1-based is better
> suited
>> with closed. But I can say that it makes a lot of sense for 0-based
>> arrays to be open.
>> Take this array of bytes for instance, where the 'end' pointer is
>> located just after the last element:
>
> However, if the length of the array is exactly the maximum value that
> can be represented on size_t (which is, I assume, the type used
> internally to represent the indexes), addressing the pointer *just
> after the last element* is impossible (through normal index
> operations).
>
> Then, why characterizing an array should depend of something... "just
> after the last element"? Whatif there is nothing there? What if, on a
> microcontroller, the last element of the array is simply the highest
> addressable memory location? (like the "end of the world") Why
> introducing something completely inconsistent with the array (namely,
> someting "just after the last element") to characterize something
> that should be regarded as... self-contained? What if such a concept
> (data after the last element of the array) is simply nonsense on some
> architecture?
>

The need to have an array of *exactly* 2^32 or 2^64 elements, instead of 
(2^32)-1 or (2^64)-1, is extremely rare. But when right-inclusive is in 
effect, there are a *lot* of times you need to use both "index" and 
"index-1" (instead of just "index", which is usually all you need with 
exclusive-right). Not only is that an extra instruction, which can be bad in 
an inner loop, but it's also an extra value that may need to take up an 
extra register. So why should the computer have to deal with all that extra 
baggage, just for the sake of an extremely rare case when someone might want 
that *one* extra element in an already huge array?

>
> I simply fail why not to choose the simplest solution?
>

In my experience, exclusive-right *is* much simpler. It only sounds more 
complex on the surface, but it leads to far greater simplicity in its 
consequences, and *that's* where simplicity really counts. What I've said in 
the paragraph above is only one of those simplified consequences. I've 
mentioned other ways in which it simplifies things in a nother post further 
below. (One such simplification is this nice clean property: 
arr[a..b].length == b-a )





More information about the Digitalmars-d mailing list