hackerbase/src/util-bst-bdict.scm

69 lines
1.6 KiB
Scheme

;;
;; util-bst-bdict.scm
;;
;; BST-based number dictionary.
;;
;; ISC License
;;
;; Copyright 2023 Brmlab, z.s.
;; Dominik Pantůček <dominik.pantucek@trustica.cz>
;;
;; Permission to use, copy, modify, and/or distribute this software
;; for any purpose with or without fee is hereby granted, provided
;; that the above copyright notice and this permission notice appear
;; in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
;; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
;; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
;; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
;; OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
;; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(declare (unit util-bst-bdict))
(import duck)
(module*
util-bst-bdict
#:doc ("Reimplementation of old number-only BST dictionary.")
(
list->bdict
bdict-find-value
bdict-ref
bdict-filter-values
bdict-keys
bdict-map-list
bdict-map-dict
bdict-update
)
(import scheme
util-bst)
(define (list->bdict lst)
(list->bst lst 'fixnum eq? <))
(define (bdict-find-value d p?)
(let ((kv (bst-find-pair d p?)))
(if kv
(cdr kv)
#f)))
(define bdict-ref bst-ref)
(define (bdict-filter-values d p?)
(map cdr (bst-filter-pairs d p?)))
(define bdict-keys bst-keys)
(define bdict-map-list bst-map-list)
(define bdict-map-dict bst-map-bst)
(define bdict-update bst-update)
)