Function: corfu-history--sort

corfu-history--sort is a byte-compiled function defined in corfu-history.el.

Signature

(corfu-history--sort CANDS)

Documentation

Sort CANDS by history.

Source Code

;; Defined in ~/.emacs.d/elpa/corfu-20260813.950/corfu-history.el
(defun corfu-history--sort (cands)
  "Sort CANDS by history."
  (unless corfu-history--hash
    (let ((ht (make-hash-table :test #'equal :size (length corfu-history)))
          (decay (/ -1.0 (* corfu-history-duplicate corfu-history-decay))))
      (cl-loop for elem in corfu-history for idx from 0
               for r = (if-let* ((r (gethash elem ht)))
                           ;; Reduce duplicate rank with exponential decay.
                           (- r (round (* corfu-history-duplicate (exp (* decay idx)))))
                         ;; Never outrank the most recent element.
                         (if (= idx 0) (/ most-negative-fixnum 2) idx))
               do (puthash elem r ht))
      (setq corfu-history--hash ht)))
  (cl-loop for ht = corfu-history--hash for max = most-positive-fixnum
           for cand on cands do
           (setcar cand (cons (car cand) (gethash (car cand) ht max))))
  (setq cands (sort cands #'corfu-history--sort-predicate))
  (cl-loop for cand on cands do (setcar cand (caar cand)))
  cands)