From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Rationale for add-to-ordered-list Date: Tue, 14 Jun 2005 13:25:27 -0400 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 1118769954 16243 80.91.229.2 (14 Jun 2005 17:25:54 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 14 Jun 2005 17:25:54 +0000 (UTC) Cc: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Jun 14 19:25:44 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1DiF9L-000272-HA for ged-emacs-devel@m.gmane.org; Tue, 14 Jun 2005 19:24:31 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1DiFEN-00029j-Pc for ged-emacs-devel@m.gmane.org; Tue, 14 Jun 2005 13:29:43 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1DiFDd-0001md-Uc for emacs-devel@gnu.org; Tue, 14 Jun 2005 13:28:58 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1DiFDX-0001gV-0D for emacs-devel@gnu.org; Tue, 14 Jun 2005 13:28:54 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1DiFDU-0001f8-LH for emacs-devel@gnu.org; Tue, 14 Jun 2005 13:28:48 -0400 Original-Received: from [132.204.24.67] (helo=mercure.iro.umontreal.ca) by monty-python.gnu.org with esmtp (Exim 4.34) id 1DiFBe-0001VA-F1 for emacs-devel@gnu.org; Tue, 14 Jun 2005 13:26:54 -0400 Original-Received: from hidalgo.iro.umontreal.ca (hidalgo.iro.umontreal.ca [132.204.27.50]) by mercure.iro.umontreal.ca (Postfix) with ESMTP id 4A5112CF42A; Tue, 14 Jun 2005 13:25:32 -0400 (EDT) Original-Received: from asado.iro.umontreal.ca (asado.iro.umontreal.ca [132.204.24.84]) by hidalgo.iro.umontreal.ca (Postfix) with ESMTP id 1B0474AC008; Tue, 14 Jun 2005 13:25:28 -0400 (EDT) Original-Received: by asado.iro.umontreal.ca (Postfix, from userid 20848) id 0587EE6C1C; Tue, 14 Jun 2005 13:25:27 -0400 (EDT) Original-To: storm@cua.dk (Kim F. Storm) In-Reply-To: (Kim F. Storm's message of "Tue, 14 Jun 2005 16:33:05 +0200") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) X-DIRO-MailScanner-Information: Please contact the ISP for more information X-DIRO-MailScanner: Found to be clean X-DIRO-MailScanner-SpamCheck: n'est pas un polluriel, SpamAssassin (score=-4.843, requis 5, autolearn=not spam, AWL 0.06, BAYES_00 -4.90) X-MailScanner-From: monnier@iro.umontreal.ca 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:38823 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:38823 > 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! 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