View source code
Display the source code in std/algorithm/mutation.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Function std.algorithm.mutation.copy
Copies the content of into source and returns the
remaining (unfilled) part of target.
target
Prototype
TargetRange copy(SourceRange, TargetRange)( SourceRange source, TargetRange target );
Preconditions
shall have enough room to accomodate
the entirety of target.
source
See Also
Example
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
Example
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);
Example
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 ]);
Example
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 ]);
Example
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]);