From: Tino Calancha <tino.calancha@gmail.com>
To: "Eli Zaretskii" <eliz@gnu.org>,
"Clément Pit--Claudel" <clement.pit@gmail.com>
Cc: tino.calancha@gmail.com, emacs-devel@gnu.org
Subject: Re: [PATCH] assq-delete-all, rassq-delete-all: Avoid duplication of code
Date: Tue, 29 Nov 2016 17:28:57 +0900 [thread overview]
Message-ID: <87oa0ysn92.fsf@gmail.com> (raw)
In-Reply-To: <faa341a7-5d8b-fd18-b81f-a7f7f499db52@gmail.com> ("Clément Pit--Claudel"'s message of "Mon, 28 Nov 2016 13:17:08 -0500")
Clément Pit--Claudel <clement.pit@gmail.com> writes:
> On 2016-11-28 12:24, Eli Zaretskii wrote:
>>> From: Tino Calancha <tino.calancha@gmail.com>
>>> Date: Mon, 28 Nov 2016 18:52:36 +0900
>>> Cc: tino.calancha@gmail.com
>>>
>>> how about following patch?
>>> It prevent some duplication of code in subr.el, and it adds
>>> a new test.
>>
>> What about the overhead of a function call? These functions are
>> likely to be invoked in loops.
>>
>> Should we make the common part a defsubst?
>
> Would defsubst be enough? I think you'd want a defmacro. Otherwise,
> you'll still pay for all the funcalls to #'car and #'cdr, won't you?
Following patch makes `assq-delete-all-1' a macro:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
From 310fc091f1adbf7781e7069b313c03bb31e735a8 Mon Sep 17 00:00:00 2001
From: Tino Calancha <tino.calancha@gmail.com>
Date: Tue, 29 Nov 2016 17:15:30 +0900
Subject: [PATCH] assq-delete-all, rassq-delete-all: Avoid duplication of code
See discussion in:
https://lists.gnu.org/archive/html/emacs-devel/2016-11/msg00592.html
* lisp/subr.el (assq-delete-all-1): New macro.
(assq-delete-all, rassq-delete-all): Use it.
* test/lisp/subr-tests.el (subr-test-assq-delete-all): New test.
---
lisp/subr.el | 39 +++++++++++++++++++--------------------
test/lisp/subr-tests.el | 12 ++++++++++++
2 files changed, 31 insertions(+), 20 deletions(-)
diff --git a/lisp/subr.el b/lisp/subr.el
index 5da5bf8..69827be 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -570,35 +570,34 @@ member-ignore-case
(setq list (cdr list)))
list)
+(defmacro assq-delete-all-1 (elt alist rassq)
+ (let ((lst (make-symbol "alist"))
+ (tail (make-symbol "tail"))
+ (entry (make-symbol "entry")))
+ `(let ((,lst ,alist)
+ (,tail ,alist))
+ (while (and (consp (car ,lst))
+ (eq ,(if rassq `(cdar ,lst) `(caar ,lst)) ,elt))
+ (setq ,lst (cdr ,lst)))
+ (while (cdr ,tail)
+ (let ((,entry (cdr ,tail)))
+ (if (and (consp (car ,entry))
+ (eq ,(if rassq `(cdar ,entry) `(caar ,entry)) ,elt))
+ (setcdr ,tail (cdr ,entry))
+ (setq ,tail ,entry))))
+ ,lst)))
+
(defun assq-delete-all (key alist)
"Delete from ALIST all elements whose car is `eq' to KEY.
Return the modified alist.
Elements of ALIST that are not conses are ignored."
- (while (and (consp (car alist))
- (eq (car (car alist)) key))
- (setq alist (cdr alist)))
- (let ((tail alist) tail-cdr)
- (while (setq tail-cdr (cdr tail))
- (if (and (consp (car tail-cdr))
- (eq (car (car tail-cdr)) key))
- (setcdr tail (cdr tail-cdr))
- (setq tail tail-cdr))))
- alist)
+ (assq-delete-all-1 key alist nil))
(defun rassq-delete-all (value alist)
"Delete from ALIST all elements whose cdr is `eq' to VALUE.
Return the modified alist.
Elements of ALIST that are not conses are ignored."
- (while (and (consp (car alist))
- (eq (cdr (car alist)) value))
- (setq alist (cdr alist)))
- (let ((tail alist) tail-cdr)
- (while (setq tail-cdr (cdr tail))
- (if (and (consp (car tail-cdr))
- (eq (cdr (car tail-cdr)) value))
- (setcdr tail (cdr tail-cdr))
- (setq tail tail-cdr))))
- alist)
+ (assq-delete-all-1 value alist 'rassq))
(defun alist-get (key alist &optional default remove)
"Return the value associated with KEY in ALIST, using `assq'.
diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el
index ce21290..018c13b 100644
--- a/test/lisp/subr-tests.el
+++ b/test/lisp/subr-tests.el
@@ -224,5 +224,17 @@
(error-message-string (should-error (version-to-list "beta22_8alpha3")))
"Invalid version syntax: `beta22_8alpha3' (must start with a number)"))))
+(ert-deftest subr-test-assq-delete-all ()
+ "Tests for `assq-delete-all' and `rassq-delete-all'."
+ (let ((alist '((foo . 1) (bar . 1) (baz . 1) (foo . 2))))
+ (should (equal '((bar . 1) (baz . 1))
+ (assq-delete-all 'foo (copy-tree alist))))
+ (should (equal '((foo . 2)) (rassq-delete-all 1 (copy-tree alist))))
+ (should (equal alist (assq-delete-all 'qux (copy-tree alist))))
+ (should (equal alist (rassq-delete-all 9 (copy-tree alist))))
+ (should (equal alist
+ (assq-delete-all (make-symbol "foo") (copy-tree alist))))
+ (should (equal alist (rassq-delete-all 1.0 (copy-tree alist))))))
+
(provide 'subr-tests)
;;; subr-tests.el ends here
--
2.10.2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
In GNU Emacs 26.0.50.1 (x86_64-pc-linux-gnu, GTK+ Version 3.22.4)
of 2016-11-28
Repository revision: 2c8a7e50d24daf19ea7d86f1cfeaa98a41c56085
prev parent reply other threads:[~2016-11-29 8:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-11-28 9:52 [PATCH] assq-delete-all, rassq-delete-all: Avoid duplication of code Tino Calancha
2016-11-28 13:58 ` Herring, Davis
2016-11-28 14:35 ` Tino Calancha
2016-11-28 17:24 ` Eli Zaretskii
2016-11-28 18:17 ` Clément Pit--Claudel
2016-11-29 8:28 ` Tino Calancha [this message]
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=87oa0ysn92.fsf@gmail.com \
--to=tino.calancha@gmail.com \
--cc=clement.pit@gmail.com \
--cc=eliz@gnu.org \
--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).