[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Rationale for add-to-ordered-list
From: |
Kim F. Storm |
Subject: |
Re: Rationale for add-to-ordered-list |
Date: |
Wed, 15 Jun 2005 09:28:23 +0200 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) |
Stefan Monnier <address@hidden> 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 <address@hidden> http://www.cua.dk