Modules

Deep_Copy

moduleSource

Available on macOS, Linux, Windows, Wasm

#import "Deep_Copy";

Deep copies of values and the data they point to, with Deep_Copy.

Example↗
n: s64 = 5;
copy := Deep_Copy(*n, .{});
assert(copy != *n && copy.* == 5);

Module parameters#

DC_DEBUG := false
Tracks the depth of each copy, for debugging Deep_Copy itself.
DEBUG_PRINT := false
With DC_DEBUG, prints every value the copy visits.

Procedures#

Deep_Copysince 0.31.0Source
(value: *$T, config: Deep_Copy_Config, preallocated_root: *void = null, pointer_map: *Pointer_Map = null, loc := #caller_location) -> *T

Copies value and everything it points to, and returns the copy (null for null). Pointers that appear more than once are copied once, so shared and cyclic data keeps its shape. The copy is allocated with context.allocator.

Node :: struct { value: s64; next: *Node; }
tail := Node.{2, null};
head := Node.{1, *tail};
copy := Deep_Copy(*head, .{});
assert(copy != *head && copy.next != *tail && copy.next.value == 2);
↗

Types#

:: struct

Options for Deep_Copy: a proc to rewrite every string it copies, and whether to follow pointers to structs.

string_mapper_proc: (input: string, data: *void) -> (output: string)
string_mapper_data: *void
follow_struct_pointers: := true
:: struct

Remembers which source pointers were already copied, so shared and cyclic data is copied once. Pass one to several Deep_Copy calls to keep that sharing across them.

table: Table(*void, *void)