Add algorithm.mutation.fill()

This commit is contained in:
Eugen Wissner 2018-10-06 16:00:08 +02:00
parent a8b18d7603
commit 4f9927a8c3
3 changed files with 74 additions and 3 deletions

View File

@ -388,3 +388,76 @@ do
assert(copy(source[], target).value == 5);
}
/**
* Fills $(D_PARAM range) with $(D_PARAM value).
*
* Params:
* Range = Input range type.
* Value = Filler type.
* range = Input range.
* value = Filler.
*/
void fill(Range, Value)(Range range, Value value)
if (isInputRange!Range && isAssignable!(ElementType!Range, Value))
{
static if (!isDynamicArray!Range && is(typeof(range[] = value)))
{
range[] = value;
}
else
{
for (; !range.empty; range.popFront())
{
range.front = value;
}
}
}
///
@nogc nothrow pure @safe unittest
{
import tanya.algorithm.comparison : equal;
int[6] actual;
const int[6] expected = [1, 1, 1, 1, 1, 1];
fill(actual[], 1);
assert(equal(actual[], expected[]));
}
// [] is called where possible
@nogc nothrow pure @system unittest
{
static struct Slice
{
bool* slicingCalled;
int front() @nogc nothrow pure @safe
{
return 0;
}
void front(int) @nogc nothrow pure @safe
{
}
void popFront() @nogc nothrow pure @safe
{
}
bool empty() @nogc nothrow pure @safe
{
return true;
}
void opIndexAssign(int) @nogc nothrow pure @safe
{
*this.slicingCalled = true;
}
}
bool slicingCalled;
auto range = Slice(&slicingCalled);
fill(range, 0);
assert(slicingCalled);
}

View File

@ -16,7 +16,6 @@ module tanya.container.array;
import core.checkedint;
import std.algorithm.mutation : bringToFront,
fill,
initializeAll,
uninitializedFill;
import tanya.algorithm.comparison;
@ -25,7 +24,7 @@ import tanya.exception;
import tanya.memory;
import tanya.meta.trait;
import tanya.meta.transform;
import tanya.range.primitive;
import tanya.range;
/**
* Random-access range for the $(D_PSYMBOL Array).

View File

@ -14,7 +14,6 @@
*/
module tanya.math.mp;
import std.algorithm.mutation : fill;
import tanya.algorithm.comparison;
import tanya.algorithm.iteration;
import tanya.algorithm.mutation;