[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dict-tree f6e9ba1 054/154: Added optional RESULTFUN arg
From: |
Stefan Monnier |
Subject: |
[elpa] externals/dict-tree f6e9ba1 054/154: Added optional RESULTFUN argument to trie query functions, |
Date: |
Mon, 14 Dec 2020 12:21:43 -0500 (EST) |
branch: externals/dict-tree
commit f6e9ba1c9d3e2d07d89c770d75035dd8c73dd695
Author: Toby Cubitt <toby-predictive@dr-qubit.org>
Commit: tsc25 <toby-predictive@dr-qubit.org>
Added optional RESULTFUN argument to trie query functions,
and used it to implement more efficient data stripping and grouping data
processing.
---
dict-tree.el | 79 +++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 46 insertions(+), 33 deletions(-)
diff --git a/dict-tree.el b/dict-tree.el
index 6b190a1..7a3c44e 100644
--- a/dict-tree.el
+++ b/dict-tree.el
@@ -301,10 +301,6 @@ If START or END is negative, it counts from the end."
(,rankfun (cons (car a) (dictree--cell-data (cdr a)))
(cons (car b) (dictree--cell-data (cdr b))))))
-(defun dictree--wrap-filter (filter) ; INTERNAL USE ONLY
- ;; return wrapped filter function to deal with data wrapping
- `(lambda (key data) (,filter key (dictree--cell-data data))))
-
(defun dictree--wrap-combfun (combfun) ; INTERNAL USE ONLY
`(lambda (cell1 cell2)
(cons (,combfun (dictree--cell-data cell1)
@@ -312,6 +308,15 @@ If START or END is negative, it counts from the end."
(append (dictree--cell-plist cell1)
(dictree--cell-plist cell2)))))
+(defun dictree--wrap-filter (filter) ; INTERNAL USE ONLY
+ ;; return wrapped filter function to deal with data wrapping
+ `(lambda (key data) (,filter key (dictree--cell-data data))))
+
+(defun dictree--wrap-resultfun (resultfun) ; INTERNAL USE ONLY
+ ;; return wrapped result function to deal with data wrapping
+ `(lambda (key data) (,resultfun key (dictree--cell-data data))))
+
+
;; ----------------------------------------------------------------
;; The dictionary data structures
@@ -1884,7 +1889,7 @@ Returns nil if the stack is empty."
(defun dictree--query (dict arg cachefun cacheparamfun triefun stackfun
&optional
rank-function maxnum reverse no-cache filter
- strip-data)
+ resultfun)
;; Return results of querying DICT's trie with argument ARG using TRIEFUN or
;; STACKFUN. If result of calling CACHEPARAMFUN on DICT is non-nil, look
;; first for cached result in cache returned by calling CACHEFUN on DICT,
@@ -1914,8 +1919,8 @@ Returns nil if the stack is empty."
(setq cmpl
(dictree--do-query dic arg triefun stackfun
rank-function maxnum reverse
- (when filter
- (dictree--wrap-filter filter)))))
+ (when filter (dictree--wrap-filter filter))
+ resultfun)))
;; if there's a cached result with enough completions, use it
@@ -1938,7 +1943,8 @@ Returns nil if the stack is empty."
(let (time)
(setq time (float-time))
(setq cmpl (dictree--do-query dic arg triefun stackfun
- rank-function maxnum reverse nil))
+ rank-function maxnum reverse
+ nil resultfun))
(setq time (- (float-time) time))
;; if we're above the dictionary's completion cache threshold, cache
;; the result
@@ -1969,21 +1975,18 @@ Returns nil if the stack is empty."
(car a) (car b))))
nil maxnum)))
- ;; return completions list, stripping all data or just meta-data
- (if strip-data
- (mapcar 'car completions)
- (mapcar (lambda (el) (cons (car el) (dictree--cell-data (cdr el))))
- completions))
- ))
+ ;; return completions list
+ completions))
-(defun dictree--do-query (dict arg triefun stackfun
- &optional
- rank-function maxnum reverse filter)
+(defun dictree--do-query
+ (dict arg triefun stackfun
+ &optional rank-function maxnum reverse filter resultfun)
;; Return first MAXNUM results of querying DICT with ARG using TRIEFUN or
;; STACKFUN that satisfy FILTER, ordered according to RANK-FUNCTION
- ;; (defaulting to "lexical" order).
+ ;; (defaulting to "lexical" order), and processed by calling RESULTFUn
+ ;; before adding them to the final list.
;; for a meta-dict, use a dictree-stack
(if (dictree--meta-dict-p dict)
@@ -2000,6 +2003,12 @@ Returns nil if the stack is empty."
(setq cmpl (dictree-stack-pop stack)))
;; check completion passes FILTER
(when (or (null filter) (funcall filter cmpl))
+ ;; process result with RESULTFUN, if specified, otherwise just
+ ;; strip meta-data
+ (if resultfun
+ (setq cmpl (funcall resultfun (car cmpl)
+ (dictree--cell-data (cdr cmpl))))
+ (setq cmpl (cons (car cmpl) (dictree--cell-data (cdr cmpl)))))
(if rank-function
(heap-add heap cmpl) ; for ranked query, add to heap
(push cmpl completions)) ; for lexical query, add to list
@@ -2022,7 +2031,8 @@ Returns nil if the stack is empty."
(funcall triefun
(dictree--trie dict) arg
(when rank-function (dictree--wrap-rankfun rank-function))
- maxnum reverse filter)))
+ maxnum reverse filter
+ (dictree--wrap-resultfun resultfun))))
@@ -2031,7 +2041,7 @@ Returns nil if the stack is empty."
(defun dictree-complete
(dict prefix
- &optional rank-function maxnum reverse no-cache filter strip-data)
+ &optional rank-function maxnum reverse no-cache filter resultfun)
"Return an alist containing all completions of PREFIX in DICT
along with their associated data, sorted according to
RANK-FUNCTION (defaulting to \"lexical\" order, i.e. the order
@@ -2073,8 +2083,11 @@ arguments: the completion, and its associated data. If
the filter
function returns nil, the completion is not included in the
results, and doesn't count towards MAXNUM.
-If STRIP-DATA is non-nil, a list of completions is
-returned (rather than an alist), without the data."
+RESULTFUN defines a function used to process results before
+adding them to the final result list. If specified, it should
+accept two arguments: a key and its associated data. It's return
+value is what gets added to the final result list, instead of the
+default key-data cons cell."
;; run completion query
(dictree--query
dict prefix
@@ -2089,7 +2102,7 @@ returned (rather than an alist), without the data."
(if (functionp rank-function)
rank-function
(dictree-rank-function (if (listp dict) (car dict) dict))))
- maxnum reverse no-cache filter strip-data))
+ maxnum reverse no-cache filter resultfun))
@@ -2105,10 +2118,8 @@ Note that PREDICATE will be called with two arguments:
the
completion, and its associated data."
(let ((completions
(dictree-complete dict string nil nil nil nil
- predicate 'strip-data)))
- (if all
- completions
- (try-completion "" completions))))
+ predicate (lambda (key data) key))))
+ (if all completions (try-completion "" completions))))
@@ -2117,7 +2128,7 @@ completion, and its associated data."
(defun dictree-wildcard-search
(dict pattern
- &optional rank-function maxnum reverse no-cache filter strip-data)
+ &optional rank-function maxnum reverse no-cache filter resultfun)
"Return an alist containing all matches for PATTERN in DICT
along with their associated data, in the order defined by
RANKFUN, defaulting to \"lexical\" order (i.e. the order defined
@@ -2133,8 +2144,7 @@ syntax:
* wildcard
Matches zero or more characters. May *only* appear at the end
- of the pattern if DICT is a meta-dictionary (or is a list
- including a meta-dictionary).
+ of the pattern.
? wildcard
Matches any single character.
@@ -2214,8 +2224,11 @@ arguments: the matching key, and its associated data. If
the
filter function returns nil, the match is not included in the
results, and does not count towards MAXNUM.
-If STRIP-DATA is non-nil, a list of matches is returned (rather
-than an alist), without the data."
+RESULTFUN defines a function used to process results before
+adding them to the final result list. If specified, t should
+accept two arguments: a key and its associated data. It's return
+value is what gets added to the final result list, instead of the
+default key-data cons cell."
;; run wildcard query
(dictree--query
dict pattern
@@ -2230,7 +2243,7 @@ than an alist), without the data."
(if (functionp rank-function)
rank-function
(dictree-rank-function (if (listp dict) (car dict) dict))))
- maxnum reverse no-cache filter strip-data))
+ maxnum reverse no-cache filter resultfun))
- [elpa] externals/dict-tree ce48d61 104/154: Use symbol-value instead of eval., (continued)
- [elpa] externals/dict-tree ce48d61 104/154: Use symbol-value instead of eval., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 9eb7d29 014/154: Bumped version number and updated commentary., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 034a2d4 020/154: Fixed dictree-member-p bug; dictionaries no longer 'provide' feature., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 104fd29 022/154: Roll back changes to read-dict - not clear that they're necessary even under windoze, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 6b05d61 025/154: Another bug-fix to the new cache policy code, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree daad3f1 040/154: Fixed bugs in dictree--write-dict-code and dictree-save, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 0aed5d4 043/154: Modified dictree--read-line to call data and plist load functions with null argument, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 7a5d669 044/154: Allow insertion and load functions to be overriden in dictree-populate-from-file,, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 4a77581 046/154: Tidied up dictree--query and related code, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree eccfe92 047/154: Bug fix in dictree--do-query, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree f6e9ba1 054/154: Added optional RESULTFUN argument to trie query functions,,
Stefan Monnier <=
- [elpa] externals/dict-tree a07802a 057/154: Bug-fix to dictree-load, which was screwing around with dictree-file data, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree f4aa81c 059/154: Disable caching for unnamed meta-dicts, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree b6175f6 058/154: Bug-fix to meta-dicts: don't add them to meta-dict-lists of constituent lists if caching is disabled,, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree d31ddac 065/154: Bug-fixes to meta-dictionary handling, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree c553822 066/154: Small bug-fix in dictree--do-query, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 15ca12e 069/154: Re-filled to 72 chars/line, for mailing to gnu-emacs-sources list, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree dca8bcd 070/154: Advised edebug-prin1 and edebug-prin1-to-string to prevent edebug hanging, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 5f549f0 073/154: Bug-fixes to dictree--write-meta-dict-code, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 0ef5b58 078/154: Revert "Replaced advice with cedet-edebug.el for pretty-printing", Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree df22e8e 074/154: Bug-fixes to edebug advice, Stefan Monnier, 2020/12/14