Merge pull request #1398 from monarchdodra/mapSlice

Better slicing for map
This commit is contained in:
Hara Kenji 2013-07-15 05:24:06 -07:00
commit 50c7b20959
2 changed files with 32 additions and 10 deletions

View file

@ -465,16 +465,33 @@ private struct MapResult(alias fun, Range)
alias length opDollar; alias length opDollar;
} }
static if (!isInfinite!R && hasSlicing!R) static if (hasSlicing!R)
{ {
static if (is(typeof(_input[ulong.max .. ulong.max]))) static if (is(typeof(_input[ulong.max .. ulong.max])))
private alias ulong opSlice_t; private alias opSlice_t = ulong;
else else
private alias uint opSlice_t; private alias opSlice_t = uint;
auto opSlice(opSlice_t lowerBound, opSlice_t upperBound) static if (hasLength!R)
{ {
return typeof(this)(_input[lowerBound..upperBound]); auto opSlice(opSlice_t low, opSlice_t high)
{
return typeof(this)(_input[low .. high]);
}
}
else static if (is(typeof(_input[opSlice_t.max .. $])))
{
struct DollarToken{}
enum opDollar = DollarToken.init;
auto opSlice(opSlice_t low, DollarToken)
{
return typeof(this)(_input[low .. $]);
}
auto opSlice(opSlice_t low, opSlice_t high)
{
return this[low .. $].take(high - low);
}
} }
} }
@ -612,6 +629,15 @@ unittest
const floatStep = 0.02; const floatStep = 0.02;
static assert(__traits(compiles, map!(i => i)(iota(floatBegin, floatEnd, floatStep)))); static assert(__traits(compiles, map!(i => i)(iota(floatBegin, floatEnd, floatStep))));
} }
unittest
{
//slicing infinites
auto rr = iota(0, 5).cycle().map!"a * a"();
alias RR = typeof(rr);
static assert(hasSlicing!RR);
rr = rr[6 .. $]; //Advances 1 cycle and 1 unit
assert(equal(rr[0 .. 5], [1, 4, 9, 16, 0]));
}
/** /**
$(D auto reduce(Args...)(Args args) $(D auto reduce(Args...)(Args args)

View file

@ -3863,11 +3863,7 @@ struct Cycle(Range)
} }
private static struct DollarToken {} private static struct DollarToken {}
enum opDollar = DollarToken.init;
DollarToken opDollar()
{
return DollarToken.init;
}
auto opSlice(size_t i, size_t j) auto opSlice(size_t i, size_t j)
{ {