Add algorithm.mutation.uninitializedFill

This commit is contained in:
Eugen Wissner 2018-10-20 10:42:01 +02:00
parent a04a04bb96
commit 3b5709821a
3 changed files with 44 additions and 5 deletions

View File

@ -14,6 +14,7 @@
*/ */
module tanya.algorithm.mutation; module tanya.algorithm.mutation;
import tanya.conv;
static import tanya.memory.op; static import tanya.memory.op;
import tanya.meta.trait; import tanya.meta.trait;
import tanya.meta.transform; import tanya.meta.transform;
@ -398,7 +399,7 @@ do
* range = Input range. * range = Input range.
* value = Filler. * value = Filler.
*/ */
void fill(Range, Value)(Range range, Value value) void fill(Range, Value)(Range range, auto ref Value value)
if (isInputRange!Range && isAssignable!(ElementType!Range, Value)) if (isInputRange!Range && isAssignable!(ElementType!Range, Value))
{ {
static if (!isDynamicArray!Range && is(typeof(range[] = value))) static if (!isDynamicArray!Range && is(typeof(range[] = value)))
@ -461,3 +462,43 @@ if (isInputRange!Range && isAssignable!(ElementType!Range, Value))
fill(range, 0); fill(range, 0);
assert(slicingCalled); assert(slicingCalled);
} }
/**
* Fills $(D_PARAM range) with $(D_PARAM value) assuming the elements of the
* $(D_PARAM range) aren't initialized.
*
* Params:
* Range = Input range type.
* Value = Initializer type.
* range = Input range.
* value = Initializer.
*/
void uninitializedFill(Range, Value)(Range range, auto ref Value value)
if (isInputRange!Range && hasLvalueElements!Range
&& isAssignable!(ElementType!Range, Value))
{
static if (hasElaborateDestructor!(ElementType!Range))
{
for (; !range.empty; range.popFront())
{
ElementType!Range* p = &range.front;
emplace!(ElementType!Range)(cast(void[]) (p[0 .. 1]), value);
}
}
else
{
fill(range, value);
}
}
///
@nogc nothrow pure @safe unittest
{
import tanya.algorithm.comparison : equal;
int[6] actual = void;
const int[6] expected = [1, 1, 1, 1, 1, 1];
uninitializedFill(actual[], 1);
assert(equal(actual[], expected[]));
}

View File

@ -15,9 +15,7 @@
module tanya.container.array; module tanya.container.array;
import core.checkedint; import core.checkedint;
import std.algorithm.mutation : bringToFront, import std.algorithm.mutation : bringToFront, initializeAll;
initializeAll,
uninitializedFill;
import tanya.algorithm.comparison; import tanya.algorithm.comparison;
import tanya.algorithm.mutation; import tanya.algorithm.mutation;
import tanya.exception; import tanya.exception;

View File

@ -14,7 +14,7 @@
*/ */
module tanya.memory; module tanya.memory;
import std.algorithm.mutation : uninitializedFill; import tanya.algorithm.mutation;
import tanya.conv; import tanya.conv;
import tanya.exception; import tanya.exception;
public import tanya.memory.allocator; public import tanya.memory.allocator;