Skip to content

Instantly share code, notes, and snippets.

@yamasushi
Created April 10, 2013 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamasushi/5351959 to your computer and use it in GitHub Desktop.
Save yamasushi/5351959 to your computer and use it in GitHub Desktop.
diff --git a/lib/util/trie.scm b/lib/util/trie.scm
index f0ad54a..b8ed6a8 100644
--- a/lib/util/trie.scm
+++ b/lib/util/trie.scm
@@ -39,6 +39,7 @@
(define-module util.trie
(use srfi-1)
(use gauche.sequence)
+ (use gauche.dictionary)
(use util.list)
(export <trie>
make-trie trie trie-with-keys
@@ -53,6 +54,10 @@
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
+ ;
+ dict-put! dict-get dict-exists? dict-delete! dict-keys dict-values
+ dict-update! dict-for-each dict-map dict-fold dict->alist alist->trie
+ ;
))
(select-module util.trie)
@@ -85,7 +90,7 @@
(define-class <trie-meta> (<class>)
())
-(define-class <trie> (<collection>)
+(define-class <trie> ( <dictionary> <collection> )
((root :init-form (%make-node))
(size :init-value 0)
(tab-make :init-keyword :tab-make
@@ -339,3 +344,57 @@
(define-method coerce-to ((class <hash-table-meta>) (trie <trie>))
(trie->hash-table trie 'equal?))
+;; dictionary framework
+;;; The minimal requirements for dictionary framework implementors:
+;;;
+;;; dict-get dict key [default]
+;;; dict-put! dict key value
+;;; dict-exists? dict key
+;;; dict-delete! dict key
+; dict-put!
+(define-method dict-put! ((dict <trie>) key value )
+ (trie-put! dict key value) )
+
+; dict-get
+(define-method dict-get ((dict <trie>) key :optional default )
+ (trie-get dict key default) )
+
+; dict-exists? dict key
+(define-method dict-exists? ((dict <trie>) key)
+ (trie-exists? dict key) )
+
+; dict-delete! dict key
+(define-method dict-delete! ((dict <trie>) key)
+ (trie-delete! dict key) )
+
+; dict-keys
+(define-method dict-keys ((dict <trie>))
+ (trie-keys dict) )
+
+; dict-values
+(define-method dict-values ((dict <trie>))
+ (trie-values dict) )
+
+; dict-update
+(define-method dict-update! ((dict <trie>) key proc :optional default)
+ (trie-update! dict key proc default) )
+
+; dict-for-each
+(define-method dict-for-each ((dict <trie>) proc)
+ (trie-for-each dict proc) )
+
+; dict-map
+(define-method dict-map((dict <trie>) proc)
+ (trie-map dict proc) )
+
+; dict-fold
+(define-method dict-fold((dict <trie>) proc seed)
+ (trie-fold dict proc seed) )
+
+; dict->alist
+(define-method dict->alist ((dict <trie>)) (trie->list dict))
+
+; alist->trie
+(define (alist->trie alist . rest)
+ (apply trie rest alist) )
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment