>From 57108d43800b1f24856735841de01c3b4c79a8d1 Mon Sep 17 00:00:00 2001 From: Christian Kellermann Date: Mon, 16 Jul 2012 11:39:10 +0200 Subject: [PATCH] Set hash-table size to number of entries in hash-table-copy As reported by John Croisant before this patch hash-table-copied tables would report a size of 0. This is due to *make-hash-table setting the number of entries to 0 regardless of the size of the hash-table's vector. This patch also adds a test to the testsuite. --- srfi-69.scm | 34 ++++++++++++++++++---------------- tests/hash-table-tests.scm | 9 +++++++++ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/srfi-69.scm b/srfi-69.scm index 67ee4a8..d8a2239 100644 --- a/srfi-69.scm +++ b/srfi-69.scm @@ -668,22 +668,24 @@ (lambda (ht) (let* ([vec1 (##sys#slot ht 1)] [len (##sys#size vec1)] - [vec2 (make-vector len '())] ) - (do ([i 0 (fx+ i 1)]) - [(fx>= i len) - (*make-hash-table - (##sys#slot ht 3) (##sys#slot ht 4) - (##sys#slot ht 2) - (##sys#slot ht 5) (##sys#slot ht 6) - (##sys#slot ht 7) (##sys#slot ht 8) - (##sys#slot ht 9) vec2)] - (##sys#setslot vec2 i - (let copy-loop ([bucket (##sys#slot vec1 i)]) - (if (null? bucket) - '() - (let ([pare (##sys#slot bucket 0)]) - (cons (cons (##sys#slot pare 0) (##sys#slot pare 1)) - (copy-loop (##sys#slot bucket 1))))))) ) ) ) ) ) + [vec2 (make-vector len '())] + [ht2 (do ([i 0 (fx+ i 1)]) + [(fx>= i len) + (*make-hash-table + (##sys#slot ht 3) (##sys#slot ht 4) + (##sys#slot ht 2) + (##sys#slot ht 5) (##sys#slot ht 6) + (##sys#slot ht 7) (##sys#slot ht 8) + (##sys#slot ht 9) vec2)] + (##sys#setslot vec2 i + (let copy-loop ([bucket (##sys#slot vec1 i)]) + (if (null? bucket) + '() + (let ([pare (##sys#slot bucket 0)]) + (cons (cons (##sys#slot pare 0) (##sys#slot pare 1)) + (copy-loop (##sys#slot bucket 1))))))) )]) + (##sys#setslot ht2 2 (##sys#slot ht 2)) + ht2 ) ) ) ) (define (hash-table-copy ht) (##sys#check-structure ht 'hash-table 'hash-table-copy) diff --git a/tests/hash-table-tests.scm b/tests/hash-table-tests.scm index ff13c83..91134b1 100644 --- a/tests/hash-table-tests.scm +++ b/tests/hash-table-tests.scm @@ -204,3 +204,12 @@ [(fx= i stress-size)] (assert (fx= i (hash-table-ref ht i))) ) ) +(print "HT - copy") +(define l '((1 a) (2 b) (3 c))) +(set! ht (alist->hash-table l)) +(define ht2 (hash-table-copy ht)) +(assert (= (hash-table-size ht2) (hash-table-size ht))) +(print l " -- " (hash-table->alist ht2)) +(assert (equal? l (sort (hash-table->alist ht2) + (lambda (e1 e2) (< (car e1) (car e2)))))) + -- 1.7.9.5