From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: storm@cua.dk (Kim F. Storm) Newsgroups: gmane.emacs.devel Subject: Re: Rationale for add-to-ordered-list Date: Wed, 15 Jun 2005 09:28:23 +0200 Message-ID: References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1118821396 17062 80.91.229.2 (15 Jun 2005 07:43:16 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Wed, 15 Jun 2005 07:43:16 +0000 (UTC) Cc: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Jun 15 09:43:07 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1DiSXL-0007id-L7 for ged-emacs-devel@m.gmane.org; Wed, 15 Jun 2005 09:42:11 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1DiScW-0002oQ-1p for ged-emacs-devel@m.gmane.org; Wed, 15 Jun 2005 03:47:32 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1DiSLC-0007ly-DT for emacs-devel@gnu.org; Wed, 15 Jun 2005 03:29:39 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1DiSL8-0007j2-Bg for emacs-devel@gnu.org; Wed, 15 Jun 2005 03:29:35 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1DiSL6-0007fw-VX for emacs-devel@gnu.org; Wed, 15 Jun 2005 03:29:33 -0400 Original-Received: from [195.41.46.235] (helo=pfepa.post.tele.dk) by monty-python.gnu.org with esmtp (Exim 4.34) id 1DiSL0-0007qo-9V for emacs-devel@gnu.org; Wed, 15 Jun 2005 03:29:26 -0400 Original-Received: from kfs-l.imdomain.dk.cua.dk (unknown [80.165.4.124]) by pfepa.post.tele.dk (Postfix) with SMTP id 0C18847FE2E; Wed, 15 Jun 2005 09:27:54 +0200 (CEST) Original-To: Stefan Monnier In-Reply-To: (Stefan Monnier's message of "Tue, 14 Jun 2005 13:25:27 -0400") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:38861 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:38861 Stefan Monnier writes: >> A general solution is non-trivial, as there is a potential risk of memory >> leaks, as associating ordering information with arbitrary lisp objects >> means that we must store a pointer to such objects somewhere, and thus >> may leave references to otherwise unused data. > > How 'bout the patch below? Look 'ma, no leak! Very clever -- I have to tune into weak hash tables more often :-) I'm all for it. Remember to update doc string and lispref too. > > > Stefan > > > --- subr.el 14 jun 2005 13:16:55 -0400 1.464 > +++ subr.el 14 jun 2005 13:23:20 -0400 > @@ -974,25 +974,21 @@ > `list-order' property. > > The return value is the new value of LIST-VAR." > - (let* ((ordering (get list-var 'list-order)) > - (cur (and (symbolp element) (assq element ordering)))) > + (let ((ordering (get list-var 'list-order))) > + (unless ordering > + (put list-var 'list-order > + (setq ordering (make-hash-table :weakness 'key :test 'eq)))) > (when order > - (unless (symbolp element) > - (error "cannot specify order for non-symbols")) > - (if cur > - (setcdr cur order) > - (setq cur (cons element order)) > - (setq ordering (cons cur ordering)) > - (put list-var 'list-order ordering))) > + (puthash element order ordering)) > (add-to-list list-var element) > (set list-var (sort (symbol-value list-var) > (lambda (a b) > - (let ((oa (and (symbolp a) (assq a ordering))) > - (ob (and (symbolp b) (assq b ordering)))) > + (let ((oa (gethash a ordering)) > + (ob (gethash b ordering))) > (cond > ((not oa) nil) > ((not ob) t) > - (t (< (cdr oa) (cdr ob)))))))))) > + (t (< oa ob))))))))) > > > ;;; Load history > > -- Kim F. Storm http://www.cua.dk