[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dict-tree 3a18a06 042/154: Modified dictree-populate-fr
From: |
Stefan Monnier |
Subject: |
[elpa] externals/dict-tree 3a18a06 042/154: Modified dictree-populate-from-file to read linearly by default, |
Date: |
Mon, 14 Dec 2020 12:21:40 -0500 (EST) |
branch: externals/dict-tree
commit 3a18a0626820b528f86f50ace89c7e3d36f2b2fa
Author: Toby Cubitt <toby-predictive@dr-qubit.org>
Commit: tsc25 <toby-predictive@dr-qubit.org>
Modified dictree-populate-from-file to read linearly by default,
from median outwards as an option.
---
dict-tree.el | 57 ++++++++++++++++++++++++++++++++-------------------------
1 file changed, 32 insertions(+), 25 deletions(-)
diff --git a/dict-tree.el b/dict-tree.el
index d0b3f7d..46dfdf4 100644
--- a/dict-tree.el
+++ b/dict-tree.el
@@ -2636,7 +2636,7 @@ is the prefix argument."
;; ----------------------------------------------------------------
;; Dumping and restoring contents
-(defun dictree-populate-from-file (dict file)
+(defun dictree-populate-from-file (dict file &optional balance)
"Populate dictionary DICT from the key list in file FILE.
Each line of FILE should contain a key, either a string
@@ -2656,8 +2656,11 @@ Interactively, DICT and FILE are read from the
mini-buffer.
Technicalities:
The key, data and property list are read as lisp expressions
-using `read'. There is no guarantee that the keys will be read
-from FILE in order."
+using `read'. The keys will be read from FILE in order, unless
+BALANCE is non-nil, in which case they are read from the median
+element outwards (which can help ensure efficient data structures
+are created when using a trie that is not self-balancing, see
+`dictree-create')."
(interactive (list (read-dict "Dictionary: ")
(read-file-name "File to populate from: " nil "" t)))
@@ -2665,18 +2668,20 @@ from FILE in order."
(message "Dictionary %s NOT populated" (dictree-name dict))
(save-excursion
- (let ((buff (generate-new-buffer " *dictree-populate*")))
- ;; insert the key list into a temporary buffer
+ (let ((buff (find-file-noselect file)))
(set-buffer buff)
- (insert-file-contents file)
;; insert the keys starting from the median to ensure a reasonably
;; well-balanced tree
(let* ((lines (count-lines (point-min) (point-max)))
(midpt (+ (/ lines 2) (mod lines 2)))
entry)
+ (message "Inserting keys in %s...(1 of %d)"
+ (dictree-name dict) lines)
;; insert the median key and set the dictionary's modified flag
- (dictree--goto-line midpt)
+ (if balance
+ (dictree--goto-line midpt)
+ (goto-char (point-min)))
(when (setq entry
(condition-case nil
(dictree--read-line dict)
@@ -2685,12 +2690,12 @@ from FILE in order."
(dictree-insert dict (car entry) (nth 1 entry))
(setf (dictree--cell-plist (dictree--lookup dict (car entry) nil))
(nth 2 entry)))
- (message "Inserting keys in %s...(1 of %d)"
- (dictree-name dict) lines)
;; insert keys successively further away from the median in both
;; directions
- (dotimes (i (1- midpt))
- (dictree--goto-line (+ midpt i 1))
+ (dotimes (i (1- (if balance midpt lines)))
+ (if balance
+ (dictree--goto-line (+ midpt i 1))
+ (forward-line 1))
(when (setq entry
(condition-case nil
(dictree--read-line dict)
@@ -2702,19 +2707,21 @@ from FILE in order."
(when (= 49 (mod i 50))
(message "Inserting keys in %s...(%d of %d)"
(dictree-name dict) (+ (* 2 i) 2) lines))
- (dictree--goto-line (- midpt i 1))
- (when (setq entry
- (condition-case nil
- (dictree--read-line dict)
- (error (error "Error reading line %d of %s"
- (- midpt i 1) file))))
- (dictree-insert dict (car entry) (nth 1 entry))
- (setf (dictree--cell-plist (dictree--lookup dict (car entry) nil))
- (nth 2 entry))))
-
- ;; if file contains an even number of keys, we still have to add
- ;; the last one
- (when (= 0 (mod lines 2))
+ (when balance
+ (dictree--goto-line (- midpt i 1))
+ (when (setq entry
+ (condition-case nil
+ (dictree--read-line dict)
+ (error (error "Error reading line %d of %s"
+ (- midpt i 1) file))))
+ (dictree-insert dict (car entry) (nth 1 entry))
+ (setf
+ (dictree--cell-plist (dictree--lookup dict (car entry) nil))
+ (nth 2 entry)))))
+
+ ;; if inserting from mid-point out, and file contains an even number
+ ;; of keys, we still have to add the last one
+ (when (and balance (= 0 (mod lines 2)))
(dictree--goto-line lines)
(when (setq entry
(condition-case nil
@@ -2724,8 +2731,8 @@ from FILE in order."
(dictree-insert dict (car entry) (nth 1 entry))
(setf (dictree--cell-plist (dictree--lookup dict (car entry) nil))
(nth 2 entry))))
- (message "Inserting keys in %s...done" (dictree-name dict)))
+ (message "Inserting keys in %s...done" (dictree-name dict)))
(kill-buffer buff)))))
- [elpa] externals/dict-tree 87bb7e8 019/154: Allow custom write and load functions when saving and dumping dictionaries;, (continued)
- [elpa] externals/dict-tree 87bb7e8 019/154: Allow custom write and load functions when saving and dumping dictionaries;, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 2978fc7 024/154: ...and already a bug-fix to the new cache updating code, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 7d9b258 033/154: Improved handling of dictionary file names in dictree-load, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree f46da76 008/154: Minor bug fixes., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 405d11b 023/154: Implemented the other cache and cache-update policies, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 57d59eb 021/154: Bug-fixes to predictive-auto-learn and read-dict., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 76140ec 026/154: Converted function wrapping macros into functions, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 9120845 032/154: Make weird variable names used to avoid dynamic scoping bugs more consistent, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 6c21fcb 038/154: Fix dictree--query and dictree-complete to return results in correct format, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 8a575f6 039/154: Added new trie functions to dictree--create-custom., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 3a18a06 042/154: Modified dictree-populate-from-file to read linearly by default,,
Stefan Monnier <=
- [elpa] externals/dict-tree ca2c562 050/154: Bug-fix to dictree-complete, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree e1a9f19 051/154: Documentation updates related to wildcard searches and predictive features that make use of them, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree bf4002d 063/154: Bug-fixes to dictree--write-dict-code; allow loading non-matching filenames in read-dict, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 6d9921e 067/154: Updated docstrings for regexp-related functions and others., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree d88b867 002/154: Version 0.10.3 of the predictive completion package., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 7b52ebd 005/154: Version 0.13.1 of the predictive completion package., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree ac40f3c 004/154: Version 0.12.2 of the predictive completion package., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 2bca928 003/154: Version 0.12 of the predictive completion package., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 3ecd763 006/154: Added minor miscellaneous new features, mostly to predictive.el., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 26f5dd3 011/154: Work around byte-compilation and goto-line bugs., Stefan Monnier, 2020/12/14