diff --git a/source/tanya/algorithm/mutation.d b/source/tanya/algorithm/mutation.d index 425718b..46ded92 100644 --- a/source/tanya/algorithm/mutation.d +++ b/source/tanya/algorithm/mutation.d @@ -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); +} diff --git a/source/tanya/container/array.d b/source/tanya/container/array.d index 4d58650..2501fad 100644 --- a/source/tanya/container/array.d +++ b/source/tanya/container/array.d @@ -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). diff --git a/source/tanya/math/mp.d b/source/tanya/math/mp.d index 5ee1f39..b7eb86b 100644 --- a/source/tanya/math/mp.d +++ b/source/tanya/math/mp.d @@ -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;