/usr/share/gauche-0.9/0.9.1/lib/util/trie.scm is in gauche 0.9.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | ;; trie.scm - trie
;;
;; Copyright (c) 2005 OOHASHI Daichi, All rights reserved.
;; Copyright (c) 2006-2010 Shiro Kawai <shiro@acm.org>
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;;
;; 3. Neither the name of the authors nor the names of its contributors
;; may be used to endorse or promote products derived from this
;; software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;; [SK] Various people contributed to this module to improve it.
;; See http://practical-scheme.net/wiliki/wiliki.cgi/Gauche:Trie for
;; the details of the discussion (in Japanese). I ended up rewriting
;; almost everything during the course of adjusting APIs to other
;; parts of Gauche, but the discussion was the source of inspiration anyway.
(define-module util.trie
(use srfi-1)
(use gauche.sequence)
(use util.list)
(export <trie>
make-trie trie trie-with-keys
trie? trie-num-entries trie-exists?
trie-get trie-put! trie-update! trie-delete!
trie-common-prefix
trie-common-prefix-keys
trie-common-prefix-values
trie-common-prefix-fold
trie-common-prefix-map
trie-common-prefix-for-each
trie->list trie->hash-table
trie-keys trie-values trie-fold trie-map trie-for-each
call-with-iterator call-with-builder size-of lazy-size-of
))
(select-module util.trie)
;; Trie node structure
;;
;; <node> := (<table> <terminal> ...)
;;
;; <terminal> := (<sequence> . <value>)
;;
;; We may have more than one terminal if the Trie contains heterogeneous
;; sequences, e.g. "foo" and (#\f #\o #\o).
;;
;; <table> can be any structure that can map an element of the sequence
;; to other nodes. Hashtables and assoc-lists are handy ones.
;; The actual table can be specified by a set of procedures given
;; to make-trie:
;;
;; tab-make :: () -> table construct an empty table
;; tab-get table, key -> node or #f
;; lookup the table by the key. return #f if there's
;; no entry for the key.
;; tab-put! :: table, key, node -> table
;; put a key&node pair into the table, or delete the
;; entry if node is #f. returned table is used for
;; subsequent opration.
;; tab-fold table, (key, node, seed -> seed), seed -> seed
;; iterator on the table entries.
(define-class <trie-meta> (<class>)
())
(define-class <trie> (<collection>)
((root :init-form (%make-node))
(size :init-value 0)
(tab-make :init-keyword :tab-make
:init-value (cut make-hash-table 'eqv?))
(tab-get :init-keyword :tab-get
:init-value (cut hash-table-get <> <> #f))
(tab-put! :init-keyword :tab-put!
:init-value (lambda (t k v)
(if v
(hash-table-put! t k v)
(hash-table-delete! t k))
t))
(tab-fold :init-keyword :tab-fold
:init-value hash-table-fold)
)
:metaclass <trie-meta>)
;;;===========================================================
;;; Constructors etc.
;;;
(define (make-trie :optional (tab-make #f) (tab-get #f) (tab-put! #f)
(tab-fold #f))
(apply make <trie>
(cond-list
(tab-make @ `(:tab-make ,tab-make))
(tab-get @ `(:tab-get ,tab-get))
(tab-put! @ `(:tab-put! ,tab-put!))
(tab-fold @ `(:tab-fold ,tab-fold)))))
(define (trie params . keys&vals)
(rlet1 t (apply make-trie params)
(for-each (lambda (p) (trie-put! t (car p) (cdr p))) keys&vals)))
(define (trie-with-keys params . seqs)
(rlet1 t (apply make-trie params)
(for-each (lambda (seq) (trie-put! t seq seq)) seqs)))
(define (trie? x)
(is-a? x <trie>))
(define (trie-num-entries trie)
(slot-ref trie 'size))
;;;===========================================================
;;; Lookup and modification
;;;
;; internal: node constructor
(define (%make-node) (list #f))
(define %node-table car)
(define %node-terminals cdr)
(define (%node-table-create trie node)
(rlet1 tab ((slot-ref trie 'tab-make))
(set! (%node-table node) tab)))
;; We don't need to compare entire sequence, for we know all the elements
;; would match. We only need to make sure the class of the sequence match.
(define (%node-find-terminal node seq)
(let1 c (class-of seq)
(find (lambda (p) (eq? c (class-of (car p)))) (cdr node))))
(define (%no-key seq)
(error "Trie does not have an entry for a key:" seq))
;; internal: Trie, [a] -> Maybe Node
(define (%trie-get-node trie seq create?)
(define (lookup parent tab elt)
(if-let1 node ((slot-ref trie 'tab-get) tab elt)
node
(and create?
(rlet1 node (%make-node)
(set! (%node-table parent)
((slot-ref trie 'tab-put!) tab elt node))))))
(define (descent node elt)
(let1 tab (%node-table node)
(cond [tab (lookup node tab elt)]
[create? (lookup node (%node-table-create trie node) elt)]
[else #f])))
;; In creation mode, we don't need to break during traversal, so
;; we save creation of continuation.
(if create?
(fold (lambda (elt node) (descent node elt))
(slot-ref trie 'root)
seq)
(let/cc break
(fold (lambda (elt node) (or (descent node elt) (break #f)))
(slot-ref trie 'root)
seq))))
(define (trie-exists? trie seq) (boolean (%trie-get-node trie seq #f)))
(define (trie-get trie seq . opt)
(or (and-let* ((node (%trie-get-node trie seq #f))
(p (%node-find-terminal node seq)))
(cdr p))
(get-optional opt (%no-key seq))))
(define (trie-put! trie seq val)
(let* ((node (%trie-get-node trie seq #t))
(p (%node-find-terminal node seq)))
(cond
(p (set-cdr! p val))
(else
(push! (%node-terminals node) (cons seq val))
(inc! (slot-ref trie 'size)))))
(undefined))
(define (trie-update! trie seq proc . opt)
(let* ((node (%trie-get-node trie seq #t))
(p (%node-find-terminal node seq)))
(cond
(p (update! (cdr p) proc))
(else
(push! (%node-terminals node)
(cons seq (proc (get-optional opt (%no-key seq)))))
(inc! (slot-ref trie 'size))))
(undefined)))
(define (trie-delete! trie seq)
;; TODO: prune a table if it becomes empty
(and-let* ((c (class-of seq))
(node (%trie-get-node trie seq #f)))
(update! (cdr node)
(lambda (terminals)
(remove (lambda (p)
(and (eq? (class-of (car p)) c)
(dec! (slot-ref trie 'size))
#t))
terminals))))
(undefined))
;;;===========================================================
;;; Scanning
;;;
;; iterate keys under the given node, depth-first.
(define (%trie-node-fold trie node proc seed)
(define (fold-descendants seed)
(or (and-let* ((tab (%node-table node)))
((slot-ref trie 'tab-fold)
tab
(lambda (elt node seed) (%trie-node-fold trie node proc seed))
seed))
seed))
(define (fold-siblings seed)
(fold (lambda (p seed) (proc (car p) (cdr p) seed))
seed
(%node-terminals node)))
(fold-siblings (fold-descendants seed)))
(define (%trie-prefix-collect trie prefix collector)
(or (and-let* ((node (%trie-get-node trie prefix #f)))
;; NB: we don't need to reverse, since the order of entries
;; are unspecified anyway.
(%trie-node-fold trie node collector '()))
'()))
(define (trie-common-prefix trie prefix)
(%trie-prefix-collect trie prefix acons))
(define (trie-common-prefix-keys trie prefix)
(%trie-prefix-collect trie prefix (lambda (k v s) (cons k s))))
(define (trie-common-prefix-values trie prefix)
(%trie-prefix-collect trie prefix (lambda (k v s) (cons v s))))
(define (trie-common-prefix-fold trie prefix proc seed)
(%trie-node-fold trie (or (%trie-get-node trie prefix #f) '()) proc seed))
(define (trie-common-prefix-map trie prefix proc)
(trie-common-prefix-fold trie prefix
(lambda (k v s) (cons (proc k v) s))
'()))
(define (trie-common-prefix-for-each trie prefix proc)
(trie-common-prefix-fold trie prefix
(lambda (k v s) (proc k v))
#f)
(undefined))
(define (trie->list trie)
(trie-common-prefix trie '()))
(define (trie->hash-table trie htype)
(rlet1 ht (make-hash-table htype)
(trie-for-each trie (cut hash-table-put! ht <> <>))))
(define (trie-keys trie)
(trie-common-prefix-keys trie '()))
(define (trie-values trie)
(trie-common-prefix-values trie '()))
(define (trie-fold trie proc seed)
(trie-common-prefix-fold trie '() proc seed))
(define (trie-map trie proc)
(trie-common-prefix-map trie '() proc))
(define (trie-for-each trie proc)
(trie-common-prefix-for-each trie '() proc))
;;;===========================================================
;;; Collection framework
;;;
(define-method call-with-iterator ((trie <trie>) proc . opts)
(define count 0)
(define (next)
(let/cc return
(%trie-node-fold trie (slot-ref trie 'root)
(lambda (key value seed)
(let/cc restart
(inc! count)
(set! next (lambda () (restart #f)))
(return (cons key value))))
#f)))
(proc (lambda () (= count (trie-num-entries trie)))
(lambda () (next))))
(define-method call-with-builder ((class <trie-meta>) proc . opts)
(let1 trie (apply make-trie (get-keyword :trie-options opts '()))
(proc (lambda (val)
(unless (pair? val)
(error "pair required to build a trie, but got" val))
(trie-put! trie (car val) (cdr val)))
(lambda () trie))))
(define-method size-of ((trie <trie>))
(trie-num-entries trie))
(define-method lazy-size-of ((trie <trie>))
(trie-num-entries trie))
;; some shortcuts, so that we can avoid call/cc trampolines
;;
(define-method coerce-to ((class <list-meta>) (trie <trie>))
(trie->list trie))
(define-method coerce-to ((class <vector-meta>) (trie <trie>))
(rlet1 vec (make-vector (trie-num-entries trie))
(trie-fold trie
(lambda (k v ind)
(vector-set! vec ind (cons k v))
(+ ind 1))
0)))
(define-method coerce-to ((class <hash-table-meta>) (trie <trie>))
(trie->hash-table trie 'equal?))
|