1 ;;; radix-tree.el --- A simple library of radix trees -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2016-2017 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; There are many different options for how to represent radix trees
26 ;; in Elisp. Here I chose a very simple one. A radix-tree can be either:
27 ;; - a node, of the form ((PREFIX . PTREE) . RTREE) where PREFIX is a string
28 ;; meaning that everything that starts with PREFIX is in PTREE,
29 ;; and everything else in RTREE. It also has the property that
30 ;; everything that starts with the first letter of PREFIX but not with
31 ;; that whole PREFIX is not in RTREE (i.e. is not in the tree at all).
32 ;; - anything else is taken as the value to associate with the empty string.
33 ;; So every node is basically an (improper) alist where each mapping applies
34 ;; to a different leading letter.
36 ;; The main downside of this representation is that the lookup operation
37 ;; is slower because each level of the tree is an alist rather than some kind
38 ;; of array, so every level's lookup is O(N) rather than O(1). We could easily
39 ;; solve this by using char-tables instead of alists, but that would make every
40 ;; level take up a lot more memory, and it would make the resulting
41 ;; data structure harder to read (by a human) when printed out.
45 (defun radix-tree--insert (tree key val i
)
47 (`((,prefix .
,ptree
) .
,rtree
)
48 (let* ((ni (+ i
(length prefix
)))
49 (cmp (compare-strings prefix nil nil key i ni
)))
51 (let ((nptree (radix-tree--insert ptree key val ni
)))
52 `((,prefix .
,nptree
) .
,rtree
))
53 (let ((n (if (< cmp
0) (- -
1 cmp
) (- cmp
1))))
55 (let ((nrtree (radix-tree--insert rtree key val i
)))
56 `((,prefix .
,ptree
) .
,nrtree
))
57 (let* ((nprefix (substring prefix
0 n
))
58 (kprefix (substring key
(+ i n
)))
59 (pprefix (substring prefix n
))
60 (ktree (if (equal kprefix
"") val
61 `((,kprefix .
,val
)))))
63 .
((,pprefix .
,ptree
) .
,ktree
))
66 (if (= (length key
) i
) val
67 (let ((prefix (substring key i
)))
68 `((,prefix .
,val
) .
,tree
))))))
70 (defun radix-tree--remove (tree key i
)
72 (`((,prefix .
,ptree
) .
,rtree
)
73 (let* ((ni (+ i
(length prefix
)))
74 (cmp (compare-strings prefix nil nil key i ni
)))
76 (pcase (radix-tree--remove ptree key ni
)
78 (`((,pprefix .
,pptree
))
79 `((,(concat prefix pprefix
) .
,pptree
) .
,rtree
))
80 (nptree `((,prefix .
,nptree
) .
,rtree
)))
81 (let ((n (if (< cmp
0) (- -
1 cmp
) (- cmp
1))))
83 (let ((nrtree (radix-tree--remove rtree key i
)))
84 `((,prefix .
,ptree
) .
,nrtree
))
87 (if (= (length key
) i
) nil tree
))))
90 (defun radix-tree--lookup (tree string i
)
92 (`((,prefix .
,ptree
) .
,rtree
)
93 (let* ((ni (+ i
(length prefix
)))
94 (cmp (compare-strings prefix nil nil string i ni
)))
96 (radix-tree--lookup ptree string ni
)
97 (let ((n (if (< cmp
0) (- -
1 cmp
) (- cmp
1))))
99 (radix-tree--lookup rtree string i
)
102 (if (and val
(equal (length string
) i
))
103 (if (integerp val
) `(t .
,val
) val
)
106 ;; (defun radix-tree--trim (tree string i)
107 ;; (if (= i (length string))
110 ;; (`((,prefix . ,ptree) . ,rtree)
111 ;; (let* ((ni (+ i (length prefix)))
112 ;; (cmp (compare-strings prefix nil nil string i ni))
113 ;; ;; FIXME: We could compute nrtree more efficiently
114 ;; ;; whenever cmp is not -1 or 1.
115 ;; (nrtree (radix-tree--trim rtree string i)))
117 ;; (pcase (radix-tree--trim ptree string ni)
119 ;; (`((,pprefix . ,pptree))
120 ;; `((,(concat prefix pprefix) . ,pptree) . ,nrtree))
121 ;; (nptree `((,prefix . ,nptree) . ,nrtree)))
122 ;; (let ((n (if (< cmp 0) (- -1 cmp) (- cmp 1))))
124 ;; ((equal (+ n i) (length string))
125 ;; `((,prefix . ,ptree) . ,nrtree))
129 (defun radix-tree--prefixes (tree string i prefixes
)
131 (`((,prefix .
,ptree
) .
,rtree
)
132 (let* ((ni (+ i
(length prefix
)))
133 (cmp (compare-strings prefix nil nil string i ni
))
134 ;; FIXME: We could compute prefixes more efficiently
135 ;; whenever cmp is not -1 or 1.
136 (prefixes (radix-tree--prefixes rtree string i prefixes
)))
138 (radix-tree--prefixes ptree string ni prefixes
)
143 (cons (cons (substring string
0 i
)
144 (if (eq (car-safe val
) t
) (cdr val
) val
))
147 (defun radix-tree--subtree (tree string i
)
148 (if (equal (length string
) i
) tree
150 (`((,prefix .
,ptree
) .
,rtree
)
151 (let* ((ni (+ i
(length prefix
)))
152 (cmp (compare-strings prefix nil nil string i ni
)))
154 (radix-tree--subtree ptree string ni
)
155 (let ((n (if (< cmp
0) (- -
1 cmp
) (- cmp
1))))
157 ((zerop n
) (radix-tree--subtree rtree string i
))
158 ((equal (+ n i
) (length string
))
159 (let ((nprefix (substring prefix n
)))
160 `((,nprefix .
,ptree
))))
166 (defconst radix-tree-empty nil
167 "The empty radix-tree.")
169 (defun radix-tree-insert (tree key val
)
170 "Insert a mapping from KEY to VAL in radix TREE."
171 (when (consp val
) (setq val
`(t .
,val
)))
172 (if val
(radix-tree--insert tree key val
0)
173 (radix-tree--remove tree key
0)))
175 (defun radix-tree-lookup (tree key
)
176 "Return the value associated to KEY in radix TREE.
177 If not found, return nil."
178 (pcase (radix-tree--lookup tree key
0)
183 (defun radix-tree-subtree (tree string
)
184 "Return the subtree of TREE rooted at the prefix STRING."
185 (radix-tree--subtree tree string
0))
187 ;; (defun radix-tree-trim (tree string)
188 ;; "Return a TREE which only holds entries \"related\" to STRING.
189 ;; \"Related\" is here defined as entries where there's a `string-prefix-p' relation
190 ;; between STRING and the key."
191 ;; (radix-tree-trim tree string 0))
193 (defun radix-tree-prefixes (tree string
)
194 "Return an alist of all bindings in TREE for prefixes of STRING."
195 (radix-tree--prefixes tree string
0 nil
))
198 (pcase-defmacro radix-tree-leaf
(vpat)
199 ;; FIXME: We'd like to use a negative pattern (not consp), but pcase
200 ;; doesn't support it. Using `atom' works but generates sub-optimal code.
201 `(or `(t .
,,vpat
) (and (pred atom
) ,vpat
))))
203 (defun radix-tree-iter-subtrees (tree fun
)
204 "Apply FUN to every immediate subtree of radix TREE.
205 FUN is called with two arguments: PREFIX and SUBTREE.
206 You can test if SUBTREE is a leaf (and extract its value) with the
207 pcase pattern (radix-tree-leaf PAT)."
210 (`((,prefix .
,ptree
) .
,rtree
)
211 (funcall fun prefix ptree
)
213 (_ (funcall fun
"" tree
)
216 (defun radix-tree-iter-mappings (tree fun
&optional prefix
)
217 "Apply FUN to every mapping in TREE.
218 FUN is called with two arguments: KEY and VAL.
219 PREFIX is only used internally."
220 (radix-tree-iter-subtrees
223 (let ((nprefix (concat prefix p
)))
225 ((radix-tree-leaf v
) (funcall fun nprefix v
))
226 (_ (radix-tree-iter-mappings s fun nprefix
)))))))
228 ;; (defun radix-tree->alist (tree)
230 ;; (radix-tree-iter-mappings tree (lambda (p v) (push (cons p v) al)))
233 (defun radix-tree-count (tree)
235 (radix-tree-iter-mappings tree
(lambda (_k _v
) (setq i
(1+ i
))))
238 (defun radix-tree-from-map (map)
239 ;; Aka (cl-defmethod map-into (map (type (eql radix-tree)))) ...)
242 (map-apply (lambda (k v
) (setq rt
(radix-tree-insert rt k v
))) map
)
245 (provide 'radix-tree
)
246 ;;; radix-tree.el ends here