Built-in range type

Lionello Lunesu lio at lunesu.remove.com
Thu Apr 29 00:27:16 PDT 2010


I appreciate your comments.

The introduction of a range type (as they appear in slices and foreach)
should not have to introduce new features. A simple int..int (or
uint/long/ulong) 'tuple' would do the trick. Internally the compiler
already has SliceExp with 'lwr' and 'upr' bounds. Exposing this to the
programmer might be useful.

Anyway, probably not useful enough :)

L.

On 29-4-2010 1:04, bearophile wrote:
> Lionello Lunesu:
> 
>> Wouldn't it be nice to introduce a built-in for these int..int ranges?<
> 
> It was discussed in the past, but there are some design problems.
> 
> In theory it can be used to define overload of slices operator as:
> opSliceAssign(int v, Slice s)
> 
> A problem is that slices can have more than just size_t indices, this code works and 'i' is of type long:
> 
> import std.stdio: writeln;
> void main() {
>     foreach (i; (long.max - 10) .. (long.max - 1))
>         writeln(typeid(typeof(i)), " ", i);
> }
> 
> So the (immutable) Slice struct has to be a template:
> opSliceAssign(int v, Slice!size_t s)
> 
> 
> struct Slice(TIndex) {
>     immutable TIndex start, stop;
>     immutable int stride; // simplified
>     immutable SliceType type;
>     this(...) {...}
>     // few methods: in operator, length, subslicing, etc.
> }
> 
> So it's usually 4 CPU words long on 32 bit CPUs (it can be just 3 words long on 64 bit CPUs if stride and type are presented in 32 bit each).
> 
> The stride is the third optional value, usually it's 1, its syntax can be:
> start .. stop : stride
> 
> The stride is an int because it can be negative. If start and stop are long, you can't have a stride larger than an int min-max values.
> 
> 'type' is a bitfield that encodes the kind of Slice. I think the cases are:
> 
> 1 .. 7
> 0 .. $
> 0 .. $-10
> 1 .. 7 : 2
> 0 .. $ : 2
> 0 .. $-10 : 2
>   .. 7
>   .. $
>   .. $-10
>   .. 7 : 2
>   .. $ : 2
>   .. $-10 : 2
> 
> 
> (The cases with no start are meant to be useful for intervals infinite on the left. I am not sure this is an important group of cases.)
> 
> 
> The Chapel language generalizes this idea into the idea of "domain", a first-class index set.
> I have explained it here, and I think it's an useful idea:
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=87311
> 
> Bye,
> bearophile




More information about the Digitalmars-d mailing list