[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [AUCTeX-devel] Call for patches for last release on 11.x series

From: David Kastrup
Subject: Re: [AUCTeX-devel] Call for patches for last release on 11.x series
Date: Sun, 12 Nov 2017 18:01:29 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

David Kastrup <address@hidden> writes:

> David Kastrup <address@hidden> writes:
>>> (defun delete-dups (list)
>>>   "Destructively remove `equal' duplicates from LIST.
>>> Store the result in LIST and return it.  LIST must be a proper list.
>>> Of several `equal' occurrences of an element in LIST, the first
>>> one is kept."
>>>   (let ((l (length list)))
>>>     (if (> l 100)
>>>         (let ((hash (make-hash-table :test #'equal :size l))
>>>               (tail list) retail)
>>>           (puthash (car list) t hash)
>>>           (while (setq retail (cdr tail))
>>>             (let ((elt (car retail)))
>>>               (if (gethash elt hash)
>>>                   (setcdr tail (cdr retail))
>>>                 (puthash elt t hash)
>>>                 (setq tail retail)))))
>>>       (let ((tail list))
>>>         (while tail
>>>           (setcdr tail (delete (car tail) (cdr tail)))
>>>           (setq tail (cdr tail))))))
>>>   list)
>> I'm pretty sure I created an efficient version of delete-dups (didn't
>> use a hash table but rather some algorithm based on sorting) for RefTeX
>> at one point of time.  Can we use that?
>> Have I committed it?
> Ok, so RefTeX is now in the Emacs repo and it neither defines nor uses
> anything called akin to delete-dups.  This is serious "Huh?" domain.
> Pretty sure I am not dreaming this up.
> At any rate: not sure whether XEmacs has the same
> make-hash-table/gethash/puthash calls.

Ah, it's there, in reftex.el after all.

(defun reftex-uniquify (list &optional sort)
  ;; Return a list of all strings in LIST, but each only once, keeping order
  ;; unless SORT is set (faster!).
  (setq list (copy-sequence list))
  (if sort
        (setq list (sort list 'string<))
        (let ((p list))
          (while (cdr p)
            (if (string= (car p) (car (cdr p)))
                (setcdr p (cdr (cdr p)))
              (setq p (cdr p)))))
    (let ((p list) lst elt)
      ;; push all sublists into lst in reverse(!) order
      (while p
        (push p lst)
        (setq p (cdr p)))
      ;; sort all sublists
      (setq lst (sort lst (lambda (x1 x2) (string< (car x1) (car x2)))))
      (while (cdr lst)
        (setq elt (car (car lst)))
        ;; for equal elements in the sorted sublist, replace the
        ;; last(!) original list member with nil
        (when (string= elt (car (cadr lst)))
          (setcar (pop lst) nil)
          (while (and (cdr lst) (string= elt (car (cadr lst))))
            (setcar (pop lst) nil)))
        (pop lst)))
    ;; weed out all nils and return.
    (delq nil list)))

But that's specialized on strings, and a different package anyway.

David Kastrup

reply via email to

[Prev in Thread] Current Thread [Next in Thread]