June 04, 2020 Re: How templates work (bonus) - Full instantiation of Iota!(1,5) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Thursday, 4 June 2020 at 11:33:48 UTC, Stefan Koch wrote:
> As part of my series of templates
> I have done a a full instantiation of Iota!(1,5).
> (By hand!)
> And here is the result:
>
> // ...
>
> Because it has been done manually there are probably some bugs.
> Can you find them all?
Is that really full? I.e. you're not counting the instantiations of Seq on purpose? :)
If I understand correctly, the below (basically manually written version of std.meta.aliasSeqOf) would just be Iota!(1, 5) => Iota?
template Iota(size_t first, size_t last) {
struct Result {
static foreach (i; first .. last)
mixin("auto e", i, " = ", i, ";");
}
enum Iota = Result.init.tupleof;
}
|
June 04, 2020 Re: How templates work (bonus) - Full instantiation of Iota!(1,5) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | On Thursday, 4 June 2020 at 13:18:12 UTC, Simen Kjærås wrote:
>
> Not at all - take it, paint it red, say you wrote it, whatever. :)
>
> --
> Simen
I will credit you of course!
You deserve it for suffering through it.
|
June 04, 2020 Re: How templates work (bonus) - Full instantiation of Iota!(1,5) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On Thursday, 4 June 2020 at 13:21:08 UTC, Stanislav Blinov wrote:
> On Thursday, 4 June 2020 at 11:33:48 UTC, Stefan Koch wrote:
>> As part of my series of templates
>> I have done a a full instantiation of Iota!(1,5).
>> (By hand!)
>> And here is the result:
>>
>> // ...
>>
>> Because it has been done manually there are probably some bugs.
>> Can you find them all?
>
> Is that really full? I.e. you're not counting the instantiations of Seq on purpose? :)
>
> If I understand correctly, the below (basically manually written version of std.meta.aliasSeqOf) would just be Iota!(1, 5) => Iota?
>
> template Iota(size_t first, size_t last) {
> struct Result {
> static foreach (i; first .. last)
> mixin("auto e", i, " = ", i, ";");
> }
> enum Iota = Result.init.tupleof;
> }
I cannot answer that right now.
My head is already spinning from the mental exercise.
|
June 04, 2020 Re: How templates work (bonus) - Full instantiation of Iota!(1,5) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On Thursday, 4 June 2020 at 13:21:08 UTC, Stanislav Blinov wrote:
> On Thursday, 4 June 2020 at 11:33:48 UTC, Stefan Koch wrote:
>> As part of my series of templates
>> I have done a a full instantiation of Iota!(1,5).
>> (By hand!)
>> And here is the result:
>>
>> // ...
>>
>> Because it has been done manually there are probably some bugs.
>> Can you find them all?
>
> Is that really full? I.e. you're not counting the instantiations of Seq on purpose? :)
>
> If I understand correctly, the below (basically manually written version of std.meta.aliasSeqOf) would just be Iota!(1, 5) => Iota?
>
> template Iota(size_t first, size_t last) {
> struct Result {
> static foreach (i; first .. last)
> mixin("auto e", i, " = ", i, ";");
> }
> enum Iota = Result.init.tupleof;
> }
That is a cool (but pretty darn ugly) trick. Of course, it uses CTFE, so there's a whole nother set of complications that we haven't yet covered. But if we ignore those the instantiation graph would be this:
Iota!(1, 5):
{
enum Iota = (1, 2, 3, 4);
}.Iota = (1, 2, 3, 4)
--
Simen
|
June 04, 2020 Re: How templates work (bonus) - Full instantiation of Iota!(1,5) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On 6/4/20 9:21 AM, Stanislav Blinov wrote:
> On Thursday, 4 June 2020 at 11:33:48 UTC, Stefan Koch wrote:
>> As part of my series of templates
>> I have done a a full instantiation of Iota!(1,5).
>> (By hand!)
>> And here is the result:
>>
>> // ...
>>
>> Because it has been done manually there are probably some bugs.
>> Can you find them all?
>
> Is that really full? I.e. you're not counting the instantiations of Seq on purpose? :)
>
> If I understand correctly, the below (basically manually written version of std.meta.aliasSeqOf) would just be Iota!(1, 5) => Iota?
>
> template Iota(size_t first, size_t last) {
> struct Result {
> static foreach (i; first .. last)
> mixin("auto e", i, " = ", i, ";");
> }
> enum Iota = Result.init.tupleof;
> }
That's a cool trick. I like the fact that it uses mixins to output the numbers as well.
It might be faster though, to generate using a fully CTFE function instead of using static foreach. Can't use the numbers directly, so I have to write a crude "number to string" converter.
template iota(size_t first, size_t last)
{
import std.meta : AliasSeq;
string genMixin()
{
char[11] num = "0x00000000,";
string result = "alias iota = AliasSeq!(";
immutable char[16] vals = "0123456789abcdef";
foreach(i; first .. last)
{
int j = num.length - 1;
while(i)
{
num[--j] = vals[i & 0xf];
i >>= 4;
}
result ~= num;
}
return result ~ ");";
}
mixin(genMixin());
}
alias iota(size_t last) = iota!(0, last);
And of course, the fastest would be for the compiler to just do it.
-Steve
|
June 04, 2020 Re: How templates work (bonus) - Full instantiation of Iota!(1,5) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 4 June 2020 at 17:03:38 UTC, Steven Schveighoffer wrote:
> On 6/4/20 9:21 AM, Stanislav Blinov wrote:
>> [...]
>
> That's a cool trick. I like the fact that it uses mixins to output the numbers as well.
>
> [...]
Preallocate. or it'll be slow.
|
June 04, 2020 Re: How templates work (bonus) - Full instantiation of Iota!(1,5) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 4 June 2020 at 17:03:38 UTC, Steven Schveighoffer wrote: > char[11] num = "0x00000000,"; thar be dragons here matey https://issues.dlang.org/show_bug.cgi?id=20811 |
June 04, 2020 Re: How templates work (bonus) - Full instantiation of Iota!(1,5) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On 6/4/20 1:12 PM, Adam D. Ruppe wrote:
> On Thursday, 4 June 2020 at 17:03:38 UTC, Steven Schveighoffer wrote:
>> char[11] num = "0x00000000,";
>
> thar be dragons here matey
>
> https://issues.dlang.org/show_bug.cgi?id=20811
Easy to fix.
auto num = "0x00000000,".dup;
-Steve
|
June 04, 2020 Re: How templates work (bonus) - Full instantiation of Iota!(1,5) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On 6/4/20 1:09 PM, Stefan Koch wrote:
> On Thursday, 4 June 2020 at 17:03:38 UTC, Steven Schveighoffer wrote:
>> On 6/4/20 9:21 AM, Stanislav Blinov wrote:
>>> [...]
>>
>> That's a cool trick. I like the fact that it uses mixins to output the numbers as well.
>>
>> [...]
>
> Preallocate. or it'll be slow.
Sure, I could do that.
But my point is not to submit a proposal for static iota, but to show that CTFE can handle the whole thing, we don't need to use static foreach (which as I understand it can be very slow).
-Steve
|
June 04, 2020 Re: How templates work (bonus) - Full instantiation of Iota!(1,5) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 4 June 2020 at 17:16:48 UTC, Steven Schveighoffer wrote:
> On 6/4/20 1:09 PM, Stefan Koch wrote:
>> On Thursday, 4 June 2020 at 17:03:38 UTC, Steven Schveighoffer wrote:
>>> On 6/4/20 9:21 AM, Stanislav Blinov wrote:
>>>> [...]
>>>
>>> That's a cool trick. I like the fact that it uses mixins to output the numbers as well.
>>>
>>> [...]
>>
>> Preallocate. or it'll be slow.
>
> Sure, I could do that.
>
> But my point is not to submit a proposal for static iota, but to show that CTFE can handle the whole thing, we don't need to use static foreach (which as I understand it can be very slow).
>
> -Steve
Ideally we would not pile a hack on top of a hack.
|
Copyright © 1999-2021 by the D Language Foundation