From: Alan Mackenzie <acm@muc.de>
To: emacs-devel@gnu.org
Subject: New function safe-copy-tree.
Date: Mon, 6 Mar 2023 21:14:13 +0000 [thread overview]
Message-ID: <ZAZXpdm5QzPFX6Pb@ACM> (raw)
Hello, Emacs.
In order to fix bug #61962, I'm intending to introduce a new function
safe-copy-tree in subr.el. It is like copy-tree, except it works with
circular lists as well as normal ones.
My current implementation looks like this:
diff --git a/lisp/subr.el b/lisp/subr.el
index 8ff3b868fab..2066be581d1 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -845,6 +845,59 @@ copy-tree
(aset tree i (copy-tree (aref tree i) vecp)))
tree)
tree)))
+
+(defvar safe-copy-tree--seen nil
+ "A hash table for conses/vectors/records already seen by safe-copy-tree-1.
+It's key is a cons or vector/record seen by the algorithm, and its value is
+the corresponding cons/vector/record in the copy.")
+
+(defun safe-copy-tree--1 (tree &optional vecp)
+ "Make a copy of TREE, taking circular structure into account.
+If TREE is a cons cell, this recursively copies both its car and its cdr.
+Contrast to `copy-sequence', which copies only along the cdrs. With second
+argument VECP, this copies vectors and records as well as conses."
+ (cond
+ ((gethash tree safe-copy-tree--seen))
+ ((consp tree)
+ (let* ((result (cons (car tree) (cdr tree)))
+ (newcons result)
+ hash)
+ (while (and (not hash) (consp tree))
+ (if (setq hash (gethash tree safe-copy-tree--seen))
+ (setq newcons hash)
+ (puthash tree newcons safe-copy-tree--seen))
+ (setq tree newcons)
+ (unless hash
+ (if (or (consp (car tree))
+ (and vecp (or (vectorp (car tree)) (recordp (car tree)))))
+ (let ((newcar (safe-copy-tree--1 (car tree) vecp)))
+ (setcar tree newcar)))
+ (setq newcons (if (consp (cdr tree))
+ (cons (cadr tree) (cddr tree))
+ (cdr tree)))
+ (setcdr tree newcons)
+ (setq tree (cdr tree))))
+ (nconc result
+ (if (and vecp (or (vectorp tree) (recordp tree)))
+ (safe-copy-tree--1 tree vecp) tree))))
+ ((and vecp (or (vectorp tree) (recordp tree)))
+ (let* ((newvec (copy-sequence tree))
+ (i (length newvec)))
+ (puthash tree newvec safe-copy-tree--seen)
+ (setq tree newvec)
+ (while (>= (setq i (1- i)) 0)
+ (aset tree i (safe-copy-tree--1 (aref tree i) vecp)))
+ tree))
+ (t tree)))
+
+(defun safe-copy-tree (tree &optional vecp)
+ "Make a copy of TREE, taking circular structure into account.
+If TREE is a cons cell, this recursively copies both its car and its cdr.
+Contrast to `copy-sequence', which copies only along the cdrs. With second
+argument VECP, this copies vectors and records as well as conses."
+ (setq safe-copy-tree--seen (make-hash-table :test #'eq))
+ (safe-copy-tree--1 tree vecp))
+
\f
;;;; Various list-search functions.
Comments and criticism would be welcome.
--
Alan Mackenzie (Nuremberg, Germany).
next reply other threads:[~2023-03-06 21:14 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-06 21:14 Alan Mackenzie [this message]
2023-03-07 12:12 ` New function safe-copy-tree Eli Zaretskii
2023-03-07 13:37 ` Mattias Engdegård
2023-03-07 15:52 ` Alan Mackenzie
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZAZXpdm5QzPFX6Pb@ACM \
--to=acm@muc.de \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).