(cl-defmethod seq-uniq ((sequence list) &optional testfn)
(let ((result nil))
(if (not testfn)
- ;; Fast path.
- (while sequence
- (unless (member (car sequence) result)
- (push (car sequence) result))
- (pop sequence))
+ ;; Fast path. If the list is long, use a hash table to speed
+ ;; things up even more.
+ (let ((l (length sequence)))
+ (if (> l 100)
+ (let ((hash (make-hash-table :test #'equal :size l)))
+ (while sequence
+ (unless (gethash (car sequence) hash)
+ (setf (gethash (car sequence) hash) t)
+ (push (car sequence) result))
+ (setq sequence (cdr sequence))))
+ ;; Short list.
+ (while sequence
+ (unless (member (car sequence) result)
+ (push (car sequence) result))
+ (pop sequence))))
;; Slower path.
(while sequence
(unless (seq-find (lambda (elem)
(substring "2")
(substring "1"))))
(should (equal (seq-uniq list) '("1" "2" "3")))
- (should (equal (seq-uniq list #'eq) '("1" "2" "3" "2" "1")))))
+ (should (equal (seq-uniq list #'eq) '("1" "2" "3" "2" "1"))))
+ ;; Long lists have a different code path.
+ (let ((list (seq-map-indexed (lambda (_ i) i)
+ (make-list 10000 nil))))
+ (should (= (length list) 10000))
+ (should (= (length (seq-uniq (append list list))) 10000))))
(provide 'seq-tests)
;;; seq-tests.el ends here