From e91d406a7ff82b8dfd57b3d84065597b640328db Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 16 Oct 2019 07:57:15 -0700 Subject: copy-cons: more efficient; copies lconses. * lib.c (copy_cons): Rewrite to copy the object in a more low-level way, rather than going through the accessors and constructors. Now copies LCONS as LCONS, including its update function. * txr.1: copy-cons re-documented. --- lib.c | 14 ++++++++++++-- txr.1 | 28 ++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/lib.c b/lib.c index b6f22971..aa5fcde8 100644 --- a/lib.c +++ b/lib.c @@ -8160,9 +8160,19 @@ val alist_nremove1(val list, val key) return list; } -val copy_cons(val c) +val copy_cons(val cell) { - return cons(car(c), cdr(c)); + switch (type(cell)) { + case CONS: + case LCONS: + { + val obj = make_obj(); + *obj = *cell; + return obj; + } + default: + type_mismatch(lit("copy-cons: ~s is not a cons"), cell, nao); + } } val copy_alist(val list) diff --git a/txr.1 b/txr.1 index 5a536e50..0d357c56 100644 --- a/txr.1 +++ b/txr.1 @@ -19377,12 +19377,28 @@ for the empty list .desc The .code copy-cons -function creates a fresh cons cell, whose -.code car -and -.code cdr -fields are copied from -.codn cons . +function creates and returns a new object that is a replica of +.metn cons . + +The +.meta cons +argument must be either a +.code cons +cell, or else a lazy cons: an object of type +.codn lcons . + +A new cell of the same type as +.meta cons +is created, and all of its fields are initialized by +copying the corresponding fields from +.metn cons . + +If +.meta cons +is lazy, the newly created object is in the same +state as the original. If the original has not yet been updated +and thus has an update function, the copy also has not yet been +updated and has the same update function. .coNP Functions @ reverse and @ nreverse .synb -- cgit v1.2.3