[Submission] D Slices

Michel Fortin michel.fortin at michelf.com
Tue May 31 11:23:17 PDT 2011


On 2011-05-31 12:10:24 -0400, eles <eles at eles.com> said:

>> I don't think you can enter this debate without bringing the other war
>> about zero-based indices vs. one-based indices. Matlab first's index
>> number is number one, and I think this fits very naturally with the
>> closed-right limit. In many fields, one-based indices are common
>> because they are easier to reason with. But in computers, where the
>> index is generally an offset from a base address, zero-based is much
>> more prevalent as it better represents what the machine is actually
>> doing. And it follows that open-ended limits on the right are the norm.
> 
> 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:

	.        begin                                     end
	address: 0xA0  0xA1  0xA2  0xA3  0xA4  0xA5  0xA6  0xA8  0xA9 ...
	index:  [   0,    1,    2,    3,    4,    5,    6 ]

Now, observe those properties (where '&' means "address of"):

	length = &end - &begin
	&end = &begin + length
	&begin = &end - length

Now, let's say end is *on* the last element instead of after it:

	.        begin                               end
	address: 0xA0  0xA1  0xA2  0xA3  0xA4  0xA5  0xA6  0xA8  0xA9 ...
	index:  [   0,    1,    2,    3,    4,    5,    6 ]

	length = &end - &begin + 1
	&end = &begin + length - 1
	&begin = &end - length + 1

Not only you always need to add or substract one from the result, but 
since the "end" index ('$' in D) for an empty array is now -1 you can't 
use unsigned integers anymore (at least not without huge complications).


> Why it is "natural" to use open-limit for 0-based? I think it is
> quite subjective.

Similar observations can be made with zero-based indices:

	&elem = &begin + index
	index = &begin - &elem

and one-based indices:

	&elem = &begin + index - 1
	index = &begin - &elem + 1

So they have pretty much the same problem that you must do +1/-1 
everywhere. Although now the "end" index for an empty array becomes 
zero again (one less than the first index), so we've solved our 
unsigned integer problem.

Now, we could make the compiler automatically add those +1/-1 
everywhere, but that won't erase the time those extra calculations take 
on your processor. In a tight loop this can be noticeable.


> Why to the right and not to the left?

Because you start counting indices from the right. If you make it open 
on the left instead, you'll have to insert those +1/-1 everywhere again.

That said, in the rare situations where indices are counted from the 
right it certainly make sense to make it open on the left and closed on 
the right. C++'s reverse iterators and D's retro are open ended on the 
left... although depending on your point of view you could say they 
just swap what's right and left and that it's still open on the right. 
:-)


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list