Add algorithm.mutation.copy

This commit is contained in:
Eugen Wissner 2018-08-05 07:19:30 +02:00
parent b04928d2c8
commit abd286064b
4 changed files with 61 additions and 11 deletions

View File

@ -14,8 +14,9 @@
*/ */
module tanya.algorithm.mutation; module tanya.algorithm.mutation;
import tanya.memory.op; static import tanya.memory.op;
import tanya.meta.trait; import tanya.meta.trait;
import tanya.range;
private void deinitialize(bool zero, T)(ref T value) private void deinitialize(bool zero, T)(ref T value)
{ {
@ -39,11 +40,12 @@ private void deinitialize(bool zero, T)(ref T value)
} }
static if (zero) static if (zero)
{ {
fill!0((cast(void*) &value)[0 .. size]); tanya.memory.op.fill!0((cast(void*) &value)[0 .. size]);
} }
else else
{ {
copy(typeid(T).initializer()[0 .. size], (&value)[0 .. 1]); tanya.memory.op.copy(typeid(T).initializer()[0 .. size],
(&value)[0 .. 1]);
} }
} }
} }
@ -81,7 +83,7 @@ do
{ {
static if (is(T == struct) || isStaticArray!T) static if (is(T == struct) || isStaticArray!T)
{ {
copy((&source)[0 .. 1], (&target)[0 .. 1]); tanya.memory.op.copy((&source)[0 .. 1], (&target)[0 .. 1]);
static if (hasElaborateCopyConstructor!T || hasElaborateDestructor!T) static if (hasElaborateCopyConstructor!T || hasElaborateDestructor!T)
{ {
@ -273,3 +275,53 @@ void swap(T)(ref T a, ref T b) @trusted
assert(a == 5); assert(a == 5);
assert(b == 3); assert(b == 3);
} }
/**
* Copies the $(D_PARAM source) range into the $(D_PARAM target) range.
*
* Params:
* Source = Input range type.
* Target = Output range type.
* source = Source input range.
* target = Target output range.
*
* Precondition: $(D_PARAM target) should be large enough to accept all
* $(D_PARAM source) elements.
*/
void copy(Source, Target)(Source source, Target target)
if (isInputRange!Source && isOutputRange!(Target, Source))
in
{
static if (hasLength!Source && hasLength!Target)
{
assert(target.length >= source.length);
}
}
do
{
alias E = ElementType!Source;
static if (isDynamicArray!Source
&& is(Source == Target)
&& !hasElaborateCopyConstructor!E
&& !hasElaborateAssign!E)
{
tanya.memory.op.copy(source, target);
}
else
{
for (; !source.empty; source.popFront())
{
put(target, source.front);
}
}
}
///
@nogc nothrow pure @safe unittest
{
int[2] actual;
int[2] expected = [2, 3];
copy(actual[], expected[]);
assert(actual == expected);
}

View File

@ -27,7 +27,7 @@
module tanya.container.string; module tanya.container.string;
import std.algorithm.comparison : cmp; import std.algorithm.comparison : cmp;
import std.algorithm.mutation : bringToFront, copy; import std.algorithm.mutation : bringToFront;
import std.algorithm.searching; import std.algorithm.searching;
import tanya.algorithm.comparison; import tanya.algorithm.comparison;
import tanya.algorithm.mutation; import tanya.algorithm.mutation;

View File

@ -15,14 +15,13 @@
module tanya.math.mp; module tanya.math.mp;
import std.algorithm.comparison : cmp; import std.algorithm.comparison : cmp;
import std.algorithm.mutation : copy, fill, reverse; import std.algorithm.mutation : fill, reverse;
import std.range; import std.range;
import tanya.algorithm.comparison; import tanya.algorithm.comparison;
import tanya.algorithm.mutation; import tanya.algorithm.mutation;
import tanya.container.array; import tanya.container.array;
import tanya.encoding.ascii; import tanya.encoding.ascii;
import tanya.memory; import tanya.memory;
static import tanya.memory.op;
import tanya.meta.trait; import tanya.meta.trait;
import tanya.meta.transform; import tanya.meta.transform;
@ -211,7 +210,7 @@ struct Integer
this(this) @nogc nothrow pure @safe this(this) @nogc nothrow pure @safe
{ {
auto tmp = allocator.resize!digit(null, this.size); auto tmp = allocator.resize!digit(null, this.size);
tanya.memory.op.copy(this.rep[0 .. this.size], tmp); copy(this.rep[0 .. this.size], tmp);
this.rep = tmp; this.rep = tmp;
} }
@ -344,8 +343,7 @@ struct Integer
if (is(Unqual!T == Integer)) if (is(Unqual!T == Integer))
{ {
this.rep = allocator.resize(this.rep, value.size); this.rep = allocator.resize(this.rep, value.size);
tanya.memory.op.copy(value.rep[0 .. value.size], copy(value.rep[0 .. value.size], this.rep[0 .. value.size]);
this.rep[0 .. value.size]);
this.size = value.size; this.size = value.size;
this.sign = value.sign; this.sign = value.sign;

View File

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