branch: externals/hotfuzz
commit b7faa7a81735dbd6bf5c03dc5ef41d4bac34bb8f
Author: Axel Forsman <[email protected]>
Commit: Axel Forsman <[email protected]>

    Clean up code
---
 hotfuzz.el | 62 ++++++++++++++++++++++++++++++--------------------------------
 1 file changed, 30 insertions(+), 32 deletions(-)

diff --git a/hotfuzz.el b/hotfuzz.el
index 17b9ecaef9..83adb3b7f7 100644
--- a/hotfuzz.el
+++ b/hotfuzz.el
@@ -1,11 +1,8 @@
-;;; hotfuzz.el --- Completion style -*- lexical-binding: t; -*-
+;;; hotfuzz.el --- Fuzzy completion style  -*- lexical-binding: t; -*-
 ;;; See: Optimal alignments in linear space
 
 (eval-when-compile (require 'cl-lib))
 
-(defconst hotfuzz-g 100)
-(defconst hotfuzz-h 10)
-
 ;; Since we pre-allocate the vectors the common optimization where
 ;; symmetricity w.r.t. to insertions/deletions means it suffices to
 ;; allocate MIN(#needle, #haystack) for c/d when only calculating the
@@ -38,13 +35,13 @@ on the current character state.")
   "LUT of the `hotfuzz--bonus-prev-luts' index based on the current 
character.")
 
 (defun hotfuzz--calc-bonus (haystack)
-  ""
+  "Precompute all potential bonuses for matching certain characters in 
HAYSTACK."
   (cl-loop for ch across haystack and i = 0 then (1+ i) and lastch = ?/ then 
ch do
            (aset hotfuzz--bonus i
                  (aref (aref hotfuzz--bonus-prev-luts (aref 
hotfuzz--bonus-cur-lut ch)) lastch))))
 
 (defun hotfuzz--match-row (a b i nc nd pc pd)
-  "The inner loop.
+  "Calculate costs for transforming Aᵢ to Bⱼ with deletions for all j.
 
 The vectors nc/pc and nd/pd respectively may alias.
 
@@ -54,22 +51,23 @@ i - the row
 j - the column"
   (cl-loop
    with oldc
+   and g = 100 and h = 5 ; Every k-symbol gap is penalized by g+hk
    ;; s threads the old value C[i-1][j-1] throughout the loop
-   for j below (length b) and s = (if (zerop i) 0 (+ hotfuzz-g (* hotfuzz-h 
i))) then oldc do
+   for j below (length b) and s = (if (zerop i) 0 (+ g (* h i))) then oldc do
    (setq oldc (aref pc j))
-   (aset nc j (min (aset nd j (+ (min (aref pd j) (+ oldc hotfuzz-g)) 
hotfuzz-h))
+   (aset nc j (min (aset nd j (+ (min (aref pd j) (+ oldc g)) h))
                    (if (char-equal (aref a i) (aref b j))
                        (- s (aref hotfuzz--bonus i))
                      most-positive-fixnum)))))
 
 (defun hotfuzz--score (needle haystack)
-  (let* ((n (length needle)) (m (length haystack))
-         (c hotfuzz--c) (d hotfuzz--d))
+  (let ((n (length needle)) (m (length haystack))
+        (c hotfuzz--c) (d hotfuzz--d))
     (fillarray c 10000)
     (fillarray d 10000)
     (hotfuzz--calc-bonus haystack)
-    (cl-loop for i below m do (hotfuzz--match-row haystack needle i c d c d)
-             finally return (aref c (1- n))))) ; Final cost
+    (dotimes (i m) (hotfuzz--match-row haystack needle i c d c d))
+    (aref c (1- n)))) ; Final cost
 
 ;;;###autoload
 (defun hotfuzz-filter (string candidates)
@@ -80,9 +78,8 @@ j - the column"
                                                 (regexp-quote (char-to-string 
char))))
                          string ""))
           (case-fold-search t))
-      (sort (cl-loop for x in candidates
-                     if (string-match re x)
-                     do (setq x (copy-sequence x))
+      (sort (cl-loop for x in candidates if (string-match re x) do
+                     (setq x (copy-sequence x))
                      (put-text-property 0 1 'completion-score (hotfuzz--score 
string x) x)
                      and collect x)
             (lambda (a b) (< (get-text-property 0 'completion-score a)
@@ -90,27 +87,28 @@ j - the column"
 
 (defun hotfuzz-highlight (needle haystack)
   "Highlight the characters that NEEDLE matched in HAYSTACK."
-  (let* ((n (length needle)) (m (length haystack))
-         (c hotfuzz--c) (d hotfuzz--d)
-         (case-fold-search t))
+  (let ((n (length needle)) (m (length haystack))
+        (c hotfuzz--c) (d hotfuzz--d)
+        (case-fold-search t))
     (if (> n hotfuzz--max-match-len)
         haystack ; Bail out if search string is too long
       (fillarray c 10000)
       (fillarray d 10000)
       (hotfuzz--calc-bonus haystack)
-      (let ((rows (cl-loop
-                   with nc = nil and nd = nil
-                   for i below m and pc = c then nc and pd = d then nd with 
res = nil do
-                   (setq nc (make-vector n 0) nd (make-vector n 0))
-                   (hotfuzz--match-row haystack needle i nc nd pc pd)
-                   (push `(,nc . ,nd) res)
-                   finally return res)))
-        ;; Backtrack to find optimal matching positions
-        (cl-loop for j from (1- n) downto 0 with i = m do
-                 (while (cl-destructuring-bind (c . d) (pop rows)
-                          (cl-decf i)
-                          (<= (aref d j) (aref c j))))
-                 (add-face-text-property i (1+ i) 'completions-common-part nil 
haystack)
-                 finally return haystack)))))
+      (cl-loop
+       with rows = (cl-loop
+                    with nc = nil and nd = nil
+                    for i below m and pc = c then nc and pd = d then nd with 
res = nil do
+                    (setq nc (make-vector n 0) nd (make-vector n 0))
+                    (hotfuzz--match-row haystack needle i nc nd pc pd)
+                    (push `(,nc . ,nd) res)
+                    finally return res)
+       ;; Backtrack to find optimal matching positions
+       for j from (1- n) downto 0 with i = m do
+       (while (cl-destructuring-bind (c . d) (pop rows)
+                (cl-decf i)
+                (<= (aref d j) (aref c j))))
+       (add-face-text-property i (1+ i) 'completions-common-part nil haystack)
+       finally return haystack))))
 
 (provide 'hotfuzz)

Reply via email to