aboutsummaryrefslogtreecommitdiffstats
path: root/trie.scm
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2024-09-05 03:03:22 -0400
committerGravatar Peter McGoron 2024-09-05 03:03:22 -0400
commit1e93de26d50888cd5663d0fed4c0eee0111ae2b6 (patch)
tree228801c8c24b45f9f095fa0f916fe5db9ada2ac1 /trie.scm
parenttrie: add with test (diff)
fix set and trie, add compat COND-EXPAND for chez
Diffstat (limited to '')
-rw-r--r--trie.scm31
1 files changed, 22 insertions, 9 deletions
diff --git a/trie.scm b/trie.scm
index 1e70e37..c384e12 100644
--- a/trie.scm
+++ b/trie.scm
@@ -34,29 +34,37 @@
;;; ;;;;;;;;;;
;;; Char tries
+;;;
+;;; Tries are pairs, CAR = backing map from chars to trie nodes, and
+;;; CDR = function tied to this part of the trie.
;;; ;;;;;;;;;;
+;;; (TRIE:NEW TABLE FUNCTION)
(define trie:new cons)
-(define trie:empty (trie:new #f '()))
+(define trie:empty (trie:new '() #f))
+
+;;; Get the function inside of trie NODE.
(define trie:function
(lambda (node)
(if (null? node)
#f
- (car node))))
-(define %trie:set
+ (cdr node))))
+
+;;; Get the backing set inside of trie NODE.
+(define trie:backing
(lambda (node)
(if (null? node)
'()
- (cdr node))))
+ (car node))))
;;; Insert STRING-AS-LIST into NODE with value FUNCTION.
(define trie:insert
(lambda (node string-as-list function)
(if (null? string-as-list)
- (trie:new function (%trie:set node))
+ (trie:new (trie:backing node) function)
(let ((ch (car string-as-list))
(string-as-list (cdr string-as-list)))
- (let ((newtree (charmap:update (%trie:set node) ch
+ (let ((newtree (charmap:update (trie:backing node) ch
(lambda (node oldnode)
(if oldnode
(map:node-new-val
@@ -70,12 +78,17 @@
trie:empty
string-as-list
function)))))))
- (trie:new (trie:function node) newtree))))))
+ (trie:new newtree (trie:function node)))))))
(define trie:insert-many
(lambda (node lst)
(fold (lambda (pair node)
- (trie:insert node (string->list (car pair)) (cdr pair)))
+ (let ((key (car pair)))
+ (let ((key (cond
+ ((list? key) key)
+ ((string? key) (string->list key))
+ ((char? key) (list key)))))
+ (trie:insert node key (cdr pair)))))
node lst)))
;;; Search for CH in NODE.
@@ -83,7 +96,7 @@
(lambda (ch node)
(if (null? node)
'()
- (let ((node (charmap:search (%trie:set node) ch)))
+ (let ((node (charmap:search (trie:backing node) ch)))
(if (null? node)
'()
(map:val node))))))