Sometimes it's neccessary to organize compiletime loop/unroll another loop for better perfomance.
I'd make that simple template:
template IndexTuple(int e, int s = 0, T...) {
static if(s == e)
alias IndexTuple = T;
else
static if(s > e)
alias IndexTuple = IndexTuple!(e, s - 1, T, s);
else
alias IndexTuple = IndexTuple!(e, s + 1, T, s);
}
Use:
foreach(idx; IndexTuple!10)
writeln(idx); // prints 0, 1, ..., 9
foreach(idx; IndexTuple!(10, 2))
writeln(idx); // prints 2, 3, ..., 9
foreach(idx; IndexTuple!-10)
writeln(idx); // prints 0, -1, ..., -9
Comment #1 by monarchdodra — 2013-07-25T03:12:47Z
(In reply to comment #0)
> Sometimes it's neccessary to organize compiletime loop/unroll another loop for
> better perfomance.
>
> I'd make that simple template:
>
> template IndexTuple(int e, int s = 0, T...) {
> static if(s == e)
> alias IndexTuple = T;
> else
> static if(s > e)
> alias IndexTuple = IndexTuple!(e, s - 1, T, s);
> else
> alias IndexTuple = IndexTuple!(e, s + 1, T, s);
> }
>
> Use:
> foreach(idx; IndexTuple!10)
> writeln(idx); // prints 0, 1, ..., 9
>
> foreach(idx; IndexTuple!(10, 2))
> writeln(idx); // prints 2, 3, ..., 9
>
> foreach(idx; IndexTuple!-10)
> writeln(idx); // prints 0, -1, ..., -9
It think this already exists in phobos somewhere, though I don't remember where though, and it probably wasn't good for public use.
In any case, I think the useage should more closely resemble what iota does. For example:
IndexTuple!(2, 10)) vs IndexTuple!(10, 2))
or
IndexTuple!(0, -10, -1)) vs IndexTuple!(0, -10))
Arguably, iota accepts "iota(10)", but I (and others) think that is a retarded idea, when typing "iota(0, 10)", is just and easy. I think we should avoid making the same mistakes.
Also, it should be parameterizable on iteration type. Here is a rough sketch of an implementation that does this.
import std.stdio, std.traits;
template IndexTuple(alias l, alias h)
{
alias IndexTuple = IndexTupleImpl!(l, h, 1);
}
template IndexTuple(alias l, alias h, alias inc)
{
alias IndexTuple = IndexTupleImpl!(l, h, inc);
}
template IndexTupleImpl(alias l, alias h, alias inc, T...)
{
alias E = CommonType!(l, h, inc);
static if (inc == 0)
static assert(0, "increment must be non-0");
else static if (inc > 0 && l >= h)
alias IndexTupleImpl = T;
else static if(inc < 0 && l <= h)
alias IndexTupleImpl = T;
else
alias IndexTupleImpl = IndexTupleImpl!(cast(E)(l + inc), h, inc, T, l);
}
void main()
{
foreach(idx; IndexTuple!(0, 0))
write(idx, ' '); // prints
writeln();
foreach(idx; IndexTuple!(0, 10))
write(idx, ' '); // prints 0, 1, ..., 9
writeln();
foreach(idx; IndexTuple!(2, 10))
write(idx, ' '); // prints 2, 3, ..., 9
writeln();
foreach(idx; IndexTuple!(0, -10, -1))
write(idx, ' '); // prints 0, -1, ..., -9
writeln();
foreach_reverse(idx; IndexTuple!(-9, 1))
write(idx, ' '); // prints 0, -1, ..., -9
writeln();
foreach(idx; IndexTuple!(0.5, 10))
write(idx, ' '); // prints 0.5, 1.5, ..., 9.5
writeln();
foreach(idx; IndexTuple!(0, 1, 0.1))
write(idx, ' '); // prints 0 0.1 ... 0.9
writeln();
foreach(idx; IndexTuple!('a', cast(char)('z' + 1), cast(char)1))
write(idx, ' '); // prints a b ... z
writeln();
}
Comment #2 by bearophile_hugs — 2013-07-25T03:52:36Z
Dupe of Issue 4085 ?
> Arguably, iota accepts "iota(10)", but I (and others) think that is a retarded
> idea, when typing "iota(0, 10)", is just and easy. I think we should avoid
> making the same mistakes.
I requested that to Andrei. And I still think it's a good idea, it comes from Python iterations:
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Discussing iota(10) is off topic here, but if you want you can explain here or in D.learn.
Comment #3 by bearophile_hugs — 2013-07-25T03:58:35Z
(In reply to comment #2)
> >>> range(10)
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>
> Discussing iota(10) is off topic here, but if you want you can explain here or
> in D.learn.
In D the single argument iota allows you to write UFCS chains like:
....reduce!(...).iota....
If you require the zero it breaks the linearity, and the zero is very common:
0.iota(....reduce!(...))....
Comment #4 by monarchdodra — 2013-07-25T04:11:02Z
(In reply to comment #2)
> Dupe of Issue 4085 ?
Looks like it. I also like the name "Iota!(0, 10, 2)": Imediatly clear.
> > Arguably, iota accepts "iota(10)", but I (and others) think that is a retarded
> > idea, when typing "iota(0, 10)", is just and easy. I think we should avoid
> > making the same mistakes.
>
> I requested that to Andrei. And I still think it's a good idea, it comes from
> Python iterations:
>
> >>> range(10)
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>
> Discussing iota(10) is off topic here, but if you want you can explain here or
> in D.learn.
I think I remember a thread about this. But if there is existing reasons for doing it that way, I'm not going to go against it.
(In reply to comment #3)
> (In reply to comment #2)
>
> > >>> range(10)
> > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> >
> > Discussing iota(10) is off topic here, but if you want you can explain here or
> > in D.learn.
>
> In D the single argument iota allows you to write UFCS chains like:
>
> ....reduce!(...).iota....
That's a good point.
Closing as dup.
*** This issue has been marked as a duplicate of issue 4085 ***