opIndex, opSlice, length for joiner?
Maverick Chardet via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jan 22 02:18:20 PST 2016
On Friday, 22 January 2016 at 01:02:10 UTC, Andrei Alexandrescu
wrote:
> On 01/21/2016 05:22 PM, Maverick Chardet wrote:
>> Just a question: is it possible for a range to be considered
>> infinite
>> and at the same time have a length? I suppose that if it is
>> possible,
>> such a range would be ill-formed...
>
> Indeed that range would be ill-formed. -- Andrei
Okay so I'll just use an assert!
Something is bothering me though, I'm wondering why walkLength
does not save the range when possible (when it is an input
range)? For now it's just making a copy, couldn't it be
problematic in some cases? The current code is:
auto walkLength(Range)(Range range)
if (isInputRange!Range && !isInfinite!Range)
{
static if (hasLength!Range)
return range.length;
else
{
size_t result;
for ( ; !range.empty ; range.popFront() )
++result;
return result;
}
}
Why not something like this:
auto walkLength(Range)(const ref Range range)
if (isInputRange!Range && !isInfinite!Range)
{
static if (hasLength!Range)
return range.length;
else
{
size_t result;
static if (isForwardRange!range)
{
Range rangeCopy = range.save;
}
else
{
Range rangeCopy = range;
}
for ( ; !rangeCopy.empty ; rangeCopy.popFront() )
++result;
return result;
}
}
Maverick Chardet
More information about the Digitalmars-d
mailing list