summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2019-10-16 07:57:15 -0700
committerKaz Kylheku <kaz@kylheku.com>2019-10-16 07:57:15 -0700
commite91d406a7ff82b8dfd57b3d84065597b640328db (patch)
tree763f4eb9a23d77efe9905c1accd3b2d77fec2b18
parent8c907a932436a8bdc2c52d2a28e14115df525654 (diff)
downloadtxr-e91d406a7ff82b8dfd57b3d84065597b640328db.tar.gz
txr-e91d406a7ff82b8dfd57b3d84065597b640328db.tar.bz2
txr-e91d406a7ff82b8dfd57b3d84065597b640328db.zip
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.
-rw-r--r--lib.c14
-rw-r--r--txr.128
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