Skip to content

Commit

Permalink
hashtables: Add (yuni hashtables)
Browse files Browse the repository at this point in the history
  • Loading branch information
okuoku committed Jul 15, 2018
1 parent 1920e3a commit 8ff3723
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
46 changes: 45 additions & 1 deletion lib-compat/r6rs-common-yuni/compat/hashtables.sls
Expand Up @@ -28,5 +28,49 @@
string-hash
string-ci-hash
symbol-hash

;; Yuni extensions
make-integer-hashtable
make-string-hashtable
make-symbol-hashtable
hashtable-for-each
hashtable-fold
)
(import (rnrs hashtables)))
(import
(rnrs base)
(rnrs control)
(rnrs hashtables))

(define make-integer-hashtable make-eqv-hashtable)
(define make-symbol-hashtable make-eq-hashtable)
(define (make-string-hashtable . k?)
(if (null? k?)
(make-hashtable string-hash string=?)
(make-hashtable string-hash string=? (car k?))))

(define (hashtable-for-each proc ht)
(call-with-values
(lambda () (hashtable-entries ht))
(lambda (kv ev)
(let ((end (vector-length kv)))
(let loop ((idx 0))
(unless (= idx end)
(proc (vector-ref kv idx)
(vector-ref ev idx))
(loop (+ idx 1))))))))

(define (hashtable-fold proc s ht)
(call-with-values
(lambda () (hashtable-entries ht))
(lambda (kv ev)
(let ((end (vector-length kv)))
(let loop ((cur s)
(idx 0))
(if (= idx end)
cur
(loop (proc cur
(vector-ref kv idx)
(vector-ref ev idx))
idx)))))))

)
23 changes: 23 additions & 0 deletions lib/yuni/hashtables.sls
@@ -0,0 +1,23 @@
(library (yuni hashtables)
(export
;; Yuni extension
; Constructors
make-integer-hashtable
make-string-hashtable
make-symbol-hashtable
; Iter
hashtable-for-each
hashtable-fold

;; R6RS
; Constructor
make-eq-hashtable
; Mutators
hashtable-set!
hashtable-update!
hashtable-entries
; Accessor
hashtable-ref
; Query
hashtable-size)
(import (yuni compat hashtables)))

0 comments on commit 8ff3723

Please sign in to comment.