Work on bst-equal implementation.
This commit is contained in:
parent
f5e7dfe055
commit
1d9edaa320
2 changed files with 47 additions and 0 deletions
|
@ -571,6 +571,14 @@ accept two arguments.
|
||||||
Like generic reduce, the proc gets accumulator, key and value
|
Like generic reduce, the proc gets accumulator, key and value
|
||||||
arguments.
|
arguments.
|
||||||
|
|
||||||
|
### bst-equal? [procedure]
|
||||||
|
|
||||||
|
(bst-equal? b1
|
||||||
|
b2
|
||||||
|
(equality? equal?))
|
||||||
|
|
||||||
|
Returns true if both BSTs contain the same keys and values.
|
||||||
|
|
||||||
## util-bst-bdict [module]
|
## util-bst-bdict [module]
|
||||||
|
|
||||||
(import util-bst-bdict)
|
(import util-bst-bdict)
|
||||||
|
|
|
@ -38,6 +38,8 @@
|
||||||
bst-filter
|
bst-filter
|
||||||
bst-reduce
|
bst-reduce
|
||||||
|
|
||||||
|
bst-equal?
|
||||||
|
|
||||||
util-bst-tests!
|
util-bst-tests!
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -243,6 +245,23 @@
|
||||||
(proc (bst-node-kv n))
|
(proc (bst-node-kv n))
|
||||||
(loop (bst-node-right n))))))
|
(loop (bst-node-right n))))))
|
||||||
|
|
||||||
|
;; Used for iterating over multiple BSTs simultaneously
|
||||||
|
(define (bst-kv-iterator bst)
|
||||||
|
(let* ((break #f)
|
||||||
|
(resume #f)
|
||||||
|
(yield (lambda (val)
|
||||||
|
(call/cc
|
||||||
|
(lambda (r)
|
||||||
|
(set! resume r)
|
||||||
|
(break val))))))
|
||||||
|
(lambda ()
|
||||||
|
(call/cc
|
||||||
|
(lambda (cc)
|
||||||
|
(set! break cc)
|
||||||
|
(if resume
|
||||||
|
(resume '())
|
||||||
|
(bst-iter-kv bst yield))
|
||||||
|
#f)))))
|
||||||
|
|
||||||
(define/doc (bst-keys bst)
|
(define/doc (bst-keys bst)
|
||||||
("Returns all the keys contained in given dictionary.")
|
("Returns all the keys contained in given dictionary.")
|
||||||
|
@ -395,6 +414,26 @@ arguments.")
|
||||||
(set! acc (proc acc (car kv) (cdr kv)))))
|
(set! acc (proc acc (car kv) (cdr kv)))))
|
||||||
acc))
|
acc))
|
||||||
|
|
||||||
|
;; Returns true if these are compatible BSTs
|
||||||
|
(define (bst-compat? b1 b2)
|
||||||
|
(and (eq? (bst-tag b1)
|
||||||
|
(bst-tag b2))
|
||||||
|
(eq? (bst-subtag b1)
|
||||||
|
(bst-subtag b2))
|
||||||
|
(eq? (bst-EQ? b1)
|
||||||
|
(bst-EQ? b2))
|
||||||
|
(eq? (bst-<? b1)
|
||||||
|
(bst-<? b2))))
|
||||||
|
|
||||||
|
(define*/doc (bst-equal? b1 b2 (equality? equal?))
|
||||||
|
("Returns true if both BSTs contain the same keys and values.")
|
||||||
|
(if (bst-compat? b1 b2)
|
||||||
|
(let-comparators
|
||||||
|
(EQ? <? b1)
|
||||||
|
#t
|
||||||
|
)
|
||||||
|
#f))
|
||||||
|
|
||||||
;; Module self-tests
|
;; Module self-tests
|
||||||
(define (util-bst-tests!)
|
(define (util-bst-tests!)
|
||||||
(run-tests
|
(run-tests
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue