Skip to content

Commit

Permalink
Merge pull request #4013 from MetaLang/iota-length
Browse files Browse the repository at this point in the history
Clean up iota.Result.length
  • Loading branch information
schveiguy committed Apr 19, 2016
2 parents 650ea0b + 65cf74d commit 5e91307
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions std/range/package.d
Expand Up @@ -4717,17 +4717,13 @@ unittest
auto iota(B, E, S)(B begin, E end, S step)
if ((isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)))
&& isIntegral!S)
in
{
assert(!(step == 0 && begin != end));
}
body
{
import std.conv : unsigned;

alias Value = CommonType!(Unqual!B, Unqual!E);
alias StepType = Unqual!S;
alias IndexType = typeof(unsigned((end - begin) / step));

assert(step != 0 || begin == end);

static struct Result
{
Expand All @@ -4743,11 +4739,16 @@ body
this.current = current;
if (step > 0)
{
assert(unsigned((pastLast - current) / step) <= size_t.max);

this.pastLast = pastLast - 1;
this.pastLast -= (this.pastLast - current) % step;
}
else
{
if (step < 0)
assert(unsigned((current - pastLast) / -step) <= size_t.max);

this.pastLast = pastLast + 1;
this.pastLast += (current - this.pastLast) % -step;
}
Expand Down Expand Up @@ -4789,20 +4790,14 @@ body
}
@property size_t length() const
{
IndexType ret;
if (step > 0)
{
ret = unsigned((pastLast - current) / step);
return cast(size_t)((pastLast - current) / step);
}
else
{
ret = unsigned((current - pastLast) / -step);
}
static if (!is(IndexType : size_t))
{
assert(ret <= size_t.max);
return cast(size_t)((current - pastLast) / -step);
}
return cast(size_t) ret;
}

alias opDollar = length;
Expand All @@ -4825,7 +4820,6 @@ if (isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)))
import std.conv : unsigned;

alias Value = CommonType!(Unqual!B, Unqual!E);
alias IndexType = typeof(unsigned(end - begin));

static struct Result
{
Expand All @@ -4835,6 +4829,8 @@ if (isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)))
{
if (current < pastLast)
{
assert(unsigned(pastLast - current) <= size_t.max);

this.current = current;
this.pastLast = pastLast;
}
Expand Down Expand Up @@ -4872,12 +4868,7 @@ if (isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)))
}
@property size_t length() const
{
immutable ret = unsigned(pastLast - current);
static if (!is(IndexType : size_t))
{
assert(ret <= size_t.max);
}
return cast(size_t) ret;
return cast(size_t)(pastLast - current);
}

alias opDollar = length;
Expand Down Expand Up @@ -5021,6 +5012,19 @@ unittest
assert(r1.back == a.ptr + a.length - 1);
}

unittest
{
import std.parallelism;

assert(__traits(compiles, { foreach (i; iota(0, 100UL).parallel) {} }));
assert(iota(1UL, 0UL).length == 0);
assert(iota(1UL, 0UL, 1).length == 0);
assert(iota(0, 1, 1).length == 1);
assert(iota(1, 0, -1).length == 1);
assert(iota(0, 1, -1).length == 0);
assert(iota(ulong.max, 0).length == 0);
}

@safe unittest
{
import std.math : approxEqual, nextUp, nextDown;
Expand Down

0 comments on commit 5e91307

Please sign in to comment.