[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/flx 5fe7f8a94a 121/182: Add algorithmic optimizations
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/flx 5fe7f8a94a 121/182: Add algorithmic optimizations |
Date: |
Tue, 13 Dec 2022 03:59:36 -0500 (EST) |
branch: elpa/flx
commit 5fe7f8a94a199ea796cdfa88ecc45e6227d599ad
Author: PythonNut <PythonNut@users.noreply.github.com>
Commit: PythonNut <PythonNut@users.noreply.github.com>
Add algorithmic optimizations
---
flx.el | 128 ++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 68 insertions(+), 60 deletions(-)
diff --git a/flx.el b/flx.el
index ba7b8e8dfe..26b397fe14 100644
--- a/flx.el
+++ b/flx.el
@@ -220,34 +220,6 @@ See documentation for logic."
(cl-return sub)))
sorted-list))
-(defun flx-get-matches (hash query &optional greater-than q-index)
- "Return list of all unique indexes into str where query can match.
-
-That is all character sequences of query that occur in str are returned.
-
-HASH accept as the cached analysis of str.
-sstr
-e.g. (\"aab\" \"ab\") returns
- '((0 2) (1 2)
-"
-
- (setq q-index (or q-index 0))
- (let* ((q-char (aref query q-index))
- (indexes (flx-bigger-sublist
- (gethash q-char hash) greater-than)))
- (if (< q-index (1- (length query)))
- (apply ; `mapcan'
- 'nconc
- (mapcar
- (lambda (index)
- (let ((next-matches-for-rest (flx-get-matches hash query index
(1+ q-index))))
- (when next-matches-for-rest
- (mapcar (lambda (match)
- (cons index match))
- next-matches-for-rest))))
- indexes))
- (mapcar 'list indexes))))
-
(defun flx-make-filename-cache ()
"Return cache hashtable appropraite for storeing filenames."
(flx-make-string-cache 'flx-get-heatmap-file))
@@ -278,38 +250,74 @@ e.g. (\"aab\" \"ab\") returns
"return best score matching QUERY against STR"
(unless (or (zerop (length query))
(zerop (length str)))
- (let* ((info-hash (flx-process-cache str cache))
- (heatmap (gethash 'heatmap info-hash))
- (matches (flx-get-matches info-hash query))
- (query-length (length query))
- (full-match-boost (and (< query-length 5)
- (> query-length 1)))
- (best-score nil))
- (mapc (lambda (match-positions)
- (let ((score (if (and
- full-match-boost
- (= (length match-positions)
- (length str)))
- 10000
- 0))
- (contiguous-count 0)
- last-match)
- (cl-loop for index in match-positions
- do (progn
- (if (and last-match
- (= (1+ last-match) index))
- (cl-incf contiguous-count)
- (setq contiguous-count 0))
- (cl-incf score (aref heatmap index))
- (when (> contiguous-count 0)
- (cl-incf score (+ 45 (* 15 (min contiguous-count
4)))))
- (setq last-match index)))
- (if (or (null best-score)
- (> score (car best-score)))
- (setq best-score (cons score match-positions)))))
- matches)
- best-score)))
-
+ (cl-letf*
+ ((hash (flx-process-cache str cache))
+ (heatmap (gethash 'heatmap hash))
+ (query-length (length query))
+ (full-match-boost (and (< query-length 5)
+ (> query-length 1)))
+
+ ;; Dynamic Programming table
+ (match-cache (make-hash-table :test 'equal :size 10))
+
+ ;; local variables to be used later
+ (best-score most-negative-fixnum)
+ (indexes nil)
+
+ ((symbol-function #'flx-get-matches-worker)
+ (lambda (greater-than q-index)
+ (or
+ (gethash
+ (cons greater-than q-index) match-cache)
+ (puthash
+ (cons greater-than q-index)
+ (progn
+ (setq indexes (flx-bigger-sublist
+ (gethash (aref query q-index) hash)
+ greater-than))
+ (if (>= q-index (1- query-length))
+ (mapcar (lambda (index)
+ (cons (list index)
+ (cons (aref heatmap index) 0))) indexes)
+ (let ((matches) (duplicate-scores most-negative-fixnum))
+ (dolist (index indexes matches)
+ (dolist (elem
+ (mapcar
+ (lambda (object)
+ (cons (cons index (car object))
+ (cons
+ (+ (car (cdr object))
+ (aref heatmap index)
+ (if (= (1- (caar object)) index)
+ (+ (* (min (1+ (cddr object))
+ 4)
+ 15)
+ 45)
+ 0)
+ (if (and full-match-boost
+ (= (1+ (length
+ (car object)))
+ (length str)))
+ 10000
+ 0))
+ (if (= (1- (caar object)) index)
+ (1+ (cddr object))
+ 0))))
+
+ (flx-get-matches-worker index (1+ q-index))))
+
+ ;; we only care about the optimal score
+ (when (> (car (cdr elem))
+ duplicate-scores)
+ (setq duplicate-scores (car (cdr elem)))
+ (push elem matches)))))))
+ match-cache)))))
+
+ ;; postprocess candidate
+ (let ((res (flx-get-matches-worker nil 0)))
+ (and res
+ (cons (car (cdr (car res)))
+ (car (car res))))))))
(defun flx-propertize (obj score &optional add-score)
"Return propertized copy of obj according to score.
- [nongnu] elpa/flx df036210c6 098/182: Switch to 24.4 delete dup runs algorithm., (continued)
- [nongnu] elpa/flx df036210c6 098/182: Switch to 24.4 delete dup runs algorithm., ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 3fdaea3ec7 097/182: Respecting flx-ido-threshhold., ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 3d631fdd38 101/182: Improve behaviour backspacing flx -> flex., ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx c75f14fbc2 104/182: Fix docstring bugs from checkdoc., ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 6797797426 105/182: Update status in README.md., ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx a154058007 108/182: Use ido's name canonicalization., ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx acd4334893 109/182: Increase threshold to 2000., ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx a59c08283d 110/182: Simplify caching, implement own flex., ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 3032920883 117/182: Fix search results changing positions as you type., ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 46a1b29482 118/182: Merge pull request #62 from bsuh/fix61, ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 5fe7f8a94a 121/182: Add algorithmic optimizations,
ELPA Syncer <=
- [nongnu] elpa/flx b9c2d42b67 122/182: Remove deprecated tests, ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 41842ff7b3 123/182: Small performance tweaks, ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 9c8a17f199 127/182: cadar -> cl-cadar, ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx ba2a503873 128/182: use Cask to manage deps, ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 279179b5af 133/182: travis show pwd, ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 321efc25da 132/182: fix Makefile circular dependency, ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 4d625bdfb9 135/182: Test score before building possible match, ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx a9f26b2840 138/182: Fix caching of nil, ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx aba36b564e 139/182: Rename flx-get-matches-worker, ELPA Syncer, 2022/12/13
- [nongnu] elpa/flx 20e3fe8595 141/182: Make flx word separators customizable, ELPA Syncer, 2022/12/13