Thread overview | |||||
---|---|---|---|---|---|
|
July 01, 2017 rank of range | ||||
---|---|---|---|---|
| ||||
Hello! Is there a convenient way to get rank of range a.k.a. count of dimensions in compile time? Like: static assert( rankOf!(uint[]) == 1); static assert( rankOf!(uint[][][]) == 3); |
July 01, 2017 Re: rank of range | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | On 07/01/2017 10:05 AM, drug wrote:
> Hello!
> Is there a convenient way to get rank of range a.k.a. count of
> dimensions in compile time? Like:
>
> static assert( rankOf!(uint[]) == 1);
> static assert( rankOf!(uint[][][]) == 3);
I'm not aware of one but this seems to work:
template rankOf(R) {
import std.range : isInputRange, ElementType;
static if (isInputRange!R) {
enum rankOf = rankOf!(ElementType!R) + 1;
} else {
enum rankOf = 0;
}
}
unittest {
static assert (rankOf!long == 0);
static assert (rankOf!(int[]) == 1);
static assert (rankOf!(int[][][]) == 3);
}
void main() {
}
Ali
|
July 01, 2017 Re: rank of range | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 01.07.2017 20:33, Ali Çehreli пишет:
> On 07/01/2017 10:05 AM, drug wrote:
>> Hello!
>> Is there a convenient way to get rank of range a.k.a. count of
>> dimensions in compile time? Like:
>>
>> static assert( rankOf!(uint[]) == 1);
>> static assert( rankOf!(uint[][][]) == 3);
>
> I'm not aware of one but this seems to work:
>
> template rankOf(R) {
> import std.range : isInputRange, ElementType;
>
> static if (isInputRange!R) {
> enum rankOf = rankOf!(ElementType!R) + 1;
> } else {
> enum rankOf = 0;
> }
> }
>
> unittest {
> static assert (rankOf!long == 0);
> static assert (rankOf!(int[]) == 1);
> static assert (rankOf!(int[][][]) == 3);
> }
>
> void main() {
> }
>
> Ali
>
That's what I've done exactly)) Except I didn't tested against long.
Thanks for reply!
|
Copyright © 1999-2021 by the D Language Foundation