unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: storm@cua.dk (Kim F. Storm)
Cc: emacs-devel@gnu.org
Subject: Re: Rationale for add-to-ordered-list
Date: Wed, 15 Jun 2005 09:28:23 +0200	[thread overview]
Message-ID: <m3aclscc4o.fsf@kfs-l.imdomain.dk> (raw)
In-Reply-To: <jwvekb4onrh.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Tue, 14 Jun 2005 13:25:27 -0400")

Stefan Monnier <monnier@iro.umontreal.ca> 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)))))))))
>  
>  \f
>  ;;; Load history
>
>

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

      reply	other threads:[~2005-06-15  7:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-14 14:33 Rationale for add-to-ordered-list Kim F. Storm
2005-06-14 17:25 ` Stefan Monnier
2005-06-15  7:28   ` Kim F. Storm [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=m3aclscc4o.fsf@kfs-l.imdomain.dk \
    --to=storm@cua.dk \
    --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).