summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source/tanya/algorithm/mutation.d43
-rw-r--r--source/tanya/container/array.d4
-rw-r--r--source/tanya/memory/package.d2
3 files changed, 44 insertions, 5 deletions
diff --git a/source/tanya/algorithm/mutation.d b/source/tanya/algorithm/mutation.d
index 46ded92..daff07c 100644
--- a/source/tanya/algorithm/mutation.d
+++ b/source/tanya/algorithm/mutation.d
@@ -14,6 +14,7 @@
*/
module tanya.algorithm.mutation;
+import tanya.conv;
static import tanya.memory.op;
import tanya.meta.trait;
import tanya.meta.transform;
@@ -398,7 +399,7 @@ do
* range = Input range.
* 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))
{
static if (!isDynamicArray!Range && is(typeof(range[] = value)))
@@ -461,3 +462,43 @@ if (isInputRange!Range && isAssignable!(ElementType!Range, Value))
fill(range, 0);
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[]));
+}
diff --git a/source/tanya/container/array.d b/source/tanya/container/array.d
index f8c1032..17082d1 100644
--- a/source/tanya/container/array.d
+++ b/source/tanya/container/array.d
@@ -15,9 +15,7 @@
module tanya.container.array;
import core.checkedint;
-import std.algorithm.mutation : bringToFront,
- initializeAll,
- uninitializedFill;
+import std.algorithm.mutation : bringToFront, initializeAll;
import tanya.algorithm.comparison;
import tanya.algorithm.mutation;
import tanya.exception;
diff --git a/source/tanya/memory/package.d b/source/tanya/memory/package.d
index a98cf4a..f0a1c0d 100644
--- a/source/tanya/memory/package.d
+++ b/source/tanya/memory/package.d
@@ -14,7 +14,7 @@
*/
module tanya.memory;
-import std.algorithm.mutation : uninitializedFill;
+import tanya.algorithm.mutation;
import tanya.conv;
import tanya.exception;
public import tanya.memory.allocator;