Skip to content

Commit 4da8b4a

Browse files
committed
Add hash-table-keys, -values, -entries to sample impl
1 parent 9422b2f commit 4da8b4a

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

srfi/250.sld

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
hash-table=
3030
hash-table-find
3131
hash-table-count
32+
hash-table-keys
33+
hash-table-values
34+
hash-table-entries
3235
;; Low-level iteration
3336
hash-table-cursor-first
3437
hash-table-cursor-last

srfi/250/hash-tables.scm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,37 @@
513513
(>= cur (vector-length (hash-table-keys-vector ht)))
514514
(unfilled? (vector-ref (hash-table-keys-vector ht) cur))))
515515

516+
;; for when cursor-based iteration is unsafe
517+
518+
(define (hash-table-keys ht)
519+
(if (hash-table-mutable? ht)
520+
(let* ((size (hash-table-size ht))
521+
(vec (make-vector size)))
522+
(let loop ((cur (hash-table-cursor-first ht))
523+
(idx 0))
524+
(unless (>= idx size)
525+
(vector-set! vec idx (hash-table-cursor-key ht cur))
526+
(loop (hash-table-cursor-next ht cur)
527+
(+ idx 1)))
528+
vec))
529+
(hash-table-keys-vector ht)))
530+
(define (hash-table-values ht)
531+
(if (hash-table-mutable? ht)
532+
(let* ((size (hash-table-size ht))
533+
(vec (make-vector size)))
534+
(let loop ((cur (hash-table-cursor-first ht))
535+
(idx 0))
536+
(unless (>= idx size)
537+
(vector-set! vec idx (hash-table-cursor-value ht cur))
538+
(loop (hash-table-cursor-next ht cur)
539+
(+ idx 1)))
540+
vec))
541+
(hash-table-values-vector ht)))
542+
543+
(define (hash-table-entries ht)
544+
(values (hash-table-keys ht)
545+
(hash-table-values ht)))
546+
516547
(define (hash-table-fold proc seed ht)
517548
(let loop ((cur (hash-table-cursor-first ht))
518549
(acc seed))

srfi/:250.sls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
hash-table=
3030
hash-table-find
3131
hash-table-count
32+
hash-table-keys
33+
hash-table-values
34+
hash-table-entries
3235
;; Low-level iteration
3336
hash-table-cursor-first
3437
hash-table-cursor-last

srfi/:250/hash-tables.sls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
hash-table=
3030
hash-table-find
3131
hash-table-count
32+
hash-table-keys
33+
hash-table-values
34+
hash-table-entries
3235
;; Low-level iteration
3336
hash-table-cursor-first
3437
hash-table-cursor-last

srfi/srfi-250.scm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
hash-table=
4646
hash-table-find
4747
hash-table-count
48+
hash-table-keys
49+
hash-table-values
50+
hash-table-entries
4851
;; Low-level iteration
4952
hash-table-cursor-first
5053
hash-table-cursor-last

0 commit comments

Comments
 (0)