tbl.scm 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. (module (arguile data tbl)
  2. #:export (tbl tbl? tbl-t tbl-t! tbl-fn tbl-fn!))
  3. (use (arguile base)
  4. (arguile data))
  5. ;;; TODO: allow init size and comparison operators
  6. (trans tbl (t)
  7. #:init (%mke-tbl t)
  8. #:app (fn-case
  9. (() (tbl-t self))
  10. ((k) (hash-ref (tbl-t self) k))
  11. ((k v) (hash-set! (tbl-t self) k v))))
  12. (defp mke-tbl (#:o (n 0))
  13. (%mke-tbl (make-hash-table n)))
  14. (defp tbl: (t k) (t k))
  15. (defp tbl! (t k obj) (t k obj))
  16. (defp tbl-del! (t k) (hash-remove! (t) k))
  17. (defp tblq: (t k) (hashq-ref (t) k))
  18. (defp tblq! (t k obj) (hashq-set! (t) k obj))
  19. (defp tblq-del! (t k) (hashq-remove! (t) k))
  20. (defp tblv: (t k) (hashv-ref (t) k))
  21. (defp tblv! (t k obj) (hashv-set! (t) k obj))
  22. (defp tblv-del! (t k) (hashv-remove! (t) k))
  23. (defp tblx: (hash assoc t k) (hashx-ref hash assoc (t) k))
  24. (defp tblx! (hash assoc t k obj) (hashx-set! hash assoc (t) k obj))
  25. (defp tblx-del! (hash assoc t k) (hashx-remove! hash assoc (t) k))
  26. (defp tbl-cnt (pred t) (hash-count pred (t)))
  27. (defp tbl-clr! (t) (hash-clear! (t)))
  28. (defp tbl-fold (f init t) (hash-fold fun init (t)))
  29. (defp tbl-each (f t) (hash-for-each fun (t)))
  30. (defp tbl-map->lst (f t) (hash-map->list f (t)))