www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Three questions on std.range

reply Sam Hu <samhudotsamhu gmail.com> writes:
Hello,
Just get to my point to save time:

1.retro:

void testRetro()
{
int[] arr=[1,2,3,4,5];
auto retArr=retro(arr);
for(int i=0;i<retArr;i++)
writef("%d ",retArr[i]);
}
void main()
{
testRetro;
}

output:
0 0 5 4 3 //not 5 4 3 2 1 ?what's wrong here?

2.Continue to #1,In the source:
static if (isRandomAccessRange!(R) && hasLength!(R))
        ref ElementType!(R) opIndex(uint n)
        {
            return _input[_input.length - n + 1];
        }
Per my understanding,the above will return the nth element of _input in reverse
order. I try to count my fingers with length-n+1 but cannot get a correct
result.

3. About isInfinte:
template isInfinite(Range)
{
    static if (isInputRange!(Range) && is(char[1 + Range.empty]))
        enum bool isInfinite = !Range.empty;
    else
        enum bool isInfinite = false;
}
In the is expression is(char[1+ Range.empty]),below is my understanding;

Step 1:
is(char[1+Range.empty]) actually returns is(char[1+Range.empty?0:1])
Step 2:
But what does this help with determining whether Range is infinite or not?say
char[1+0],char[1+1]?

Any help would be much much appreciated.

Regards,
Sam
Jun 16 2009
next sibling parent reply Max Samukha <outer space.com> writes:
On Wed, 17 Jun 2009 00:04:51 -0400, Sam Hu <samhudotsamhu gmail.com>
wrote:

Hello,
Just get to my point to save time:

1.retro:

void testRetro()
{
int[] arr=[1,2,3,4,5];
auto retArr=retro(arr);
for(int i=0;i<retArr;i++)
writef("%d ",retArr[i]);
}
void main()
{
testRetro;
}

output:
0 0 5 4 3 //not 5 4 3 2 1 ?what's wrong here?

2.Continue to #1,In the source:
static if (isRandomAccessRange!(R) && hasLength!(R))
        ref ElementType!(R) opIndex(uint n)
        {
            return _input[_input.length - n + 1];
        }
Per my understanding,the above will return the nth element of _input in reverse
order. I try to count my fingers with length-n+1 but cannot get a correct
result.

3. About isInfinte:
template isInfinite(Range)
{
    static if (isInputRange!(Range) && is(char[1 + Range.empty]))
        enum bool isInfinite = !Range.empty;
    else
        enum bool isInfinite = false;
}
In the is expression is(char[1+ Range.empty]),below is my understanding;

Step 1:
is(char[1+Range.empty]) actually returns is(char[1+Range.empty?0:1])
Step 2:
But what does this help with determining whether Range is infinite or not?say
char[1+0],char[1+1]?

Any help would be much much appreciated.

Regards,
Sam
3. This is a (hackish) way to detect if Range.empty's value is statically known. Infinite ranges must define a boolean 'empty' member that evaluates at compile time to false. Probably, there should be a more generic way to do it. Something like isCT(alias expression) template. I kinda have one but it doesn't work in all cases due to a compiler bug.
Jun 17 2009
parent Sam Hu <samhudotsamhu gmail.com> writes:
Max Samukha Wrote:
 3. This is a (hackish) way to detect if Range.empty's value is
 statically known. Infinite ranges must define a boolean 'empty' member
 that evaluates at compile time to false.
 
 Probably, there should be a more generic way to do it. Something like
 isCT(alias expression) template. I kinda have one but it doesn't work
 in all cases due to a compiler bug. 
Thank you so much for all your help! Regards, Sam
Jun 17 2009
prev sibling parent Max Samukha <outer space.com> writes:
On Wed, 17 Jun 2009 00:04:51 -0400, Sam Hu <samhudotsamhu gmail.com>
wrote:

Hello,
Just get to my point to save time:

1.retro:

void testRetro()
{
int[] arr=[1,2,3,4,5];
auto retArr=retro(arr);
for(int i=0;i<retArr;i++)
writef("%d ",retArr[i]);
}
void main()
{
testRetro;
}

output:
0 0 5 4 3 //not 5 4 3 2 1 ?what's wrong here?

2.Continue to #1,In the source:
static if (isRandomAccessRange!(R) && hasLength!(R))
        ref ElementType!(R) opIndex(uint n)
        {
            return _input[_input.length - n + 1];
        }
Per my understanding,the above will return the nth element of _input in reverse
order. I try to count my fingers with length-n+1 but cannot get a correct
result.
Looks like a bug. The fix is easy: static if (isRandomAccessRange!(R) && hasLength!(R)) ref ElementType!(R) opIndex(uint n) { return _input[_input.length - n - 1]; } or static if (isRandomAccessRange!(R) && hasLength!(R)) ref ElementType!(R) opIndex(uint n) { return _input[_input.length - (n + 1)]; }
3. About isInfinte:
template isInfinite(Range)
{
    static if (isInputRange!(Range) && is(char[1 + Range.empty]))
        enum bool isInfinite = !Range.empty;
    else
        enum bool isInfinite = false;
}
In the is expression is(char[1+ Range.empty]),below is my understanding;

Step 1:
is(char[1+Range.empty]) actually returns is(char[1+Range.empty?0:1])
Step 2:
But what does this help with determining whether Range is infinite or not?say
char[1+0],char[1+1]?

Any help would be much much appreciated.

Regards,
Sam
Jun 17 2009