hackerbase/src/util-bst-ldict.scm

66 lines
1.2 KiB
Scheme

(declare (unit util-bst-ldict))
(import duck)
(module*
util-bst-ldict
#:doc ("Reimplementation of old list-based symbol dictionary using new BST backend.")
(
make-ldict
ldict?
ldict-empty?
ldict-contains?
ldict-ref
ldict-remove
ldict-set
ldict-keys
ldict-map
ldict-filter
ldict-reduce
ldict-equal?
)
(import scheme
util-bst
util-proc)
(define (symbol<? a b)
(string<? (symbol->string a)
(symbol->string b)))
(define (make-ldict)
(make-bst 'symbol eq? symbol<?))
(define ldict? (bst? 'symbol))
(define ldict-empty? bst-empty?)
(define ldict-contains? bst-contains?)
(define ldict-ref bst-ref)
(define ldict-remove bst-remove)
(define ldict-set bst-set)
(define ldict-keys bst-keys)
(define (ldict-map proc ld)
(let ((i 0)
(both? ((procedure-arity>=? 2) proc))
(index? ((procedure-arity>=? 3) proc)))
(bst-map-bst ld
(lambda (k v)
(let ((r (if both?
(if index?
(proc k v i)
(proc k v))
(proc v))))
(set! i (add1 i))
r)))))
(define (ldict-filter pred? ld)
(bst-filter ld pred?))
(define (ldict-reduce init proc ld)
(bst-reduce ld proc init))
(define ldict-equal? bst-equal?)
)