- (do ((index start (1+ index)))
- ((or (and end (= index (the fixnum end)))
- (atom current)))
- (declare (fixnum index))
- (if (or (and from-end
- (not (if test-not
- (member (apply-key key (car current))
- (nthcdr (1+ start) result)
- :test-not test-not
- :key key)
+ (if hash
+ (do ((index start (1+ index)))
+ ((or (and end (= index (the fixnum end)))
+ (atom current)))
+ (declare (fixnum index))
+ ;; The hash table contains links from values that are
+ ;; already in result to the cons cell *preceding* theirs
+ ;; in the list. That is, for each value v in the list,
+ ;; v and (cadr (gethash v hash)) are equal under TEST.
+ (let ((prev (gethash (car current) hash)))
+ (cond
+ ((not prev)
+ (setf (gethash (car current) hash) splice)
+ (setq splice (cdr (rplacd splice (list (car current))))))
+ ((not from-end)
+ (let* ((old (cdr prev))
+ (next (cdr old)))
+ (if next
+ (let ((next-val (car next)))
+ ;; (assert (eq (gethash next-val hash) old))
+ (setf (cdr prev) next
+ (gethash next-val hash) prev
+ (gethash (car current) hash) splice
+ splice (cdr (rplacd splice (list (car current))))))
+ (setf (car old) (car current)))))))
+ (setq current (cdr current)))
+ (do ((index start (1+ index)))
+ ((or (and end (= index (the fixnum end)))
+ (atom current)))
+ (declare (fixnum index))
+ (if (or (and from-end
+ (not (if test-not
+ (member (apply-key key (car current))
+ (nthcdr (1+ start) result)
+ :test-not test-not
+ :key key)