unbalanced-map.scm 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. (library (unbalanced-map)
  2. (export make-empty-set
  3. set-insert
  4. set-empty?
  5. set-member?)
  6. (import (except (rnrs base) error vector-map)
  7. (only (guile)
  8. lambda*
  9. λ)
  10. ;; SRFI-9 for structs
  11. (srfi srfi-9 gnu)
  12. ;; let-values
  13. (srfi srfi-11)
  14. (ice-9 exceptions)
  15. (binary-tree))
  16. ;; Maybe it would be appropriate to wrap the unbalanced binary
  17. ;; search tree in a record, to have predicates and be able to store
  18. ;; the less operation.
  19. (define-immutable-record-type <unbalanced-map>
  20. (construct-empty-set items less)
  21. map?
  22. (items map-items set-map-items)
  23. (less map-less set-map-less))
  24. (define make-empty-map
  25. (λ (less)
  26. (construct-empty-map (make-empty-tree) less)))
  27. (define map-empty?
  28. (λ (map)
  29. (tree-empty? (map-items map))))
  30. (define map-member?
  31. (λ (map elem)
  32. (cond
  33. [(map-empty? map) #f]
  34. [else
  35. (tree-member? (map-items map)
  36. elem
  37. (map-less map))])))
  38. (define map-insert
  39. (λ (map elem)
  40. ;; note: copying the map struct here, which could be avoided by
  41. ;; not using a struct
  42. (set-map-items map
  43. (tree-insert (map-items map)
  44. elem
  45. (map-less map))))))