The OpenD Programming Language

copy

Copies the content of source into target and returns the remaining (unfilled) part of target.

Preconditions: target shall have enough room to accommodate the entirety of source.

TargetRange
copy
(
SourceRange
TargetRange
)
(
SourceRange source
,
TargetRange target
)
if (
isInputRange!SourceRange &&
isOutputRange!(TargetRange, ElementType!SourceRange)
)

Parameters

source SourceRange
target TargetRange

an output range

Return Value

Type: TargetRange

The unfilled part of target

Examples

int[] a = [ 1, 5 ];
int[] b = [ 9, 8 ];
int[] buf = new int[](a.length + b.length + 10);
auto rem = a.copy(buf);    // copy a into buf
rem = b.copy(rem);         // copy b into remainder of buf
assert(buf[0 .. a.length + b.length] == [1, 5, 9, 8]);
assert(rem.length == 10);   // unused slots in buf

As long as the target range elements support assignment from source range elements, different types of ranges are accepted:

float[] src = [ 1.0f, 5 ];
double[] dest = new double[src.length];
src.copy(dest);

To copy at most n elements from a range, you may want to use std.range.take:

import std.range;
int[] src = [ 1, 5, 8, 9, 10 ];
auto dest = new int[](3);
src.take(dest.length).copy(dest);
assert(dest == [ 1, 5, 8 ]);

To copy just those elements from a range that satisfy a predicate, use filter:

import std.algorithm.iteration : filter;
int[] src = [ 1, 5, 8, 9, 10, 1, 2, 0 ];
auto dest = new int[src.length];
auto rem = src
    .filter!(a => (a & 1) == 1)
    .copy(dest);
assert(dest[0 .. $ - rem.length] == [ 1, 5, 9, 1 ]);

std.range.retro can be used to achieve behavior similar to STL's copy_backward':

import std.algorithm, std.range;
int[] src = [1, 2, 4];
int[] dest = [0, 0, 0, 0, 0];
src.retro.copy(dest.retro);
assert(dest == [0, 0, 1, 2, 4]);

Meta