Skip to content

Commit e8728a8

Browse files
committed
data.trie - Finish supporting custom tab-empty? proc
Apparantly adding support of custom tab-empty? proc wasn't completed. Related: #1174
1 parent 796ea03 commit e8728a8

3 files changed

Lines changed: 40 additions & 10 deletions

File tree

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2025-09-28 Shiro Kawai <shiro@acm.org>
2+
3+
* libsrc/data/trie.scm: Custon tab-empty? procedure hasn't been
4+
fully supported.
5+
https://github.com/shirok/Gauche/issues/1174
6+
17
2025-09-24 Shiro Kawai <shiro@acm.org>
28

39
* src/compile.scm (global-syntax=?): Compare two identifiers iff

ext/data/test-trie.scm

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@
201201
(let1 h (coerce-to <hash-table> t6)
202202
(every (cut hash-table-get h <>) strs)))
203203
)
204+
205+
;; customizing tables w/ tag-empty?
206+
(let1 t7 (make-trie (cut list 'fooo)
207+
(^[tab elt] (assoc-ref (cdr tab) elt))
208+
(^[tab elt child-node]
209+
(if child-node
210+
(set! (cdr tab) (assoc-set! (cdr tab) elt child-node))
211+
(set! (cdr tab) (alist-delete elt (cdr tab) equal?)))
212+
tab)
213+
(^[tab proc seed]
214+
(fold (^[e s] (proc (car e) (cdr e) s)) seed (cdr tab)))
215+
(^[tab] (null? (cdr tab))))
216+
(test* "trie (empty?):" (length strs)
217+
(begin
218+
(dolist [s strs]
219+
(trie-put! t7 s s))
220+
(trie-num-entries t7)))
221+
222+
(test* "trie-partial-key?" #t (trie-partial-key? t7 "kan"))
223+
(test* "trie-partial-key?" #t (trie-partial-key? t7 "kana"))
224+
(test* "trie-partial-key?" #f (trie-partial-key? t7 "kanaono"))
225+
)
204226
)
205227

206228
(test-end)

libsrc/data/trie.scm

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,14 @@
117117
;;;
118118

119119
(define (make-trie :optional (tab-make #f) (tab-get #f) (tab-put! #f)
120-
(tab-fold #f))
120+
(tab-fold #f) (tab-empty? #f))
121121
(apply make <trie>
122122
(cond-list
123123
(tab-make @ `(:tab-make ,tab-make))
124124
(tab-get @ `(:tab-get ,tab-get))
125125
(tab-put! @ `(:tab-put! ,tab-put!))
126-
(tab-fold @ `(:tab-fold ,tab-fold)))))
126+
(tab-fold @ `(:tab-fold ,tab-fold))
127+
(tab-empty? @ `(:tab-empty? ,tab-empty?)))))
127128

128129
(define (trie params . keys&vals)
129130
(rlet1 t (apply make-trie params)
@@ -223,14 +224,15 @@
223224

224225
;; internal: Trie, Node -> Boolean
225226
(define (%trie-node-empty? trie node)
226-
(cond
227-
[(slot-ref trie'tab-empty?) => (^[empty?] (empty? node))]
228-
;; some heuristics
229-
[(and (hash-table? (%node-table node))
230-
(eq? (slot-ref trie'tab-fold) hash-table-fold))
231-
(zero? (hash-table-num-entries (%node-table node)))]
232-
[(%node-table node) => (cut (slot-ref trie'tab-fold) <> (^[k n s] #t) #f)]
233-
[else #t]))
227+
(if-let1 tab (%node-table node)
228+
(cond
229+
[(slot-ref trie'tab-empty?) => (^[empty?] (empty? tab))]
230+
;; some heuristics
231+
[(and (hash-table? tab)
232+
(eq? (slot-ref trie'tab-fold) hash-table-fold))
233+
(zero? (hash-table-num-entries tab))]
234+
[else (slot-ref trie'tab-fold) tab (^[k n s] #t) #f])
235+
#t))
234236

235237
;; Public APIs
236238

0 commit comments

Comments
 (0)