D-Programming-Language/phobos — pull request #838 by monarchdodra (2012-10-05 14:23:44+00:00)
Title: fill(value) reimplementation

Something I had done a while ago. Re-submitting in a smaller independent package.

Don't know why the old implementation went out of it's way to be so complicated...

New one is pretty straight forward:

void fill(Range, Value)(Range range, Value filler)
if (isInputRange!Range && is(typeof(range.front = filler)))
{
static if (is(typeof(range[] = filler)))
{
range[] = filler;
}
else static if (is(typeof(range[] = T(filler))))
{
range[] = T(filler);
}
else
{
for ( ; !range.empty; range.popFront() )
{
range.front = filler;
}
}
}

Note that:

This can potentially forward to Range.opSliceAssign, such a for Array or Chain: +1 for performance.

Certain types of infinite ranges can define opSliceAssign (thinking about circular buffers here), making fill a neat way to fill an infinite range in a finite amount of time.