Re: error in hash table reference implementation
Panu Kalliokoski 10 Aug 2005 06:47 UTC
On Tue, Aug 09, 2005 at 09:13:41PM -0400, Jonathan Kraut wrote:
> But before you do, I found an error in the reference implementation:
> (alist->hash-table '((a . b) (c . d)))
> calls make-hash-table with incorrect arguments:
> (make-hash-table #f #f 64),
> which succeeds but is wrong.
This is similar to Per's notice about (make-hash-table) not using
hash-by-identity; it's a sign of (my) bad coding style that
implementation details of some functions -- like hash-by-identity,
make-hash-table -- are exposed in other functions -- like
make-hash-table and alist->hash-table, respectively.
Again, it is only a problem for implementors that make partial use of
the SRFI implementation. I could fix them, but I also think that hash
tables are of such a central nature that implementors should provide
native support for them. The SRFI code is not the greatest,
most-optimised, neatest code ever; it just shows one way to implement
the requirements of the SRFI.
> Here is a fixed version:
> (define (alist->hash-table alist . args)
> (let ((hash-table
> (make-hash-table
> (if (not (null? args))
> (car args)
> equal?)
> (if (and (not (null? args)) (not (null? (cdr args))))
> (cadr args)
> hash)
> (if (and (not (null? args)) (not (null? (cdr args)))
> (not (null? (cddr args))))
> (caddr args)
> (max *default-table-size* (* 2 (length alist)))))))
> (for-each (lambda (elem)
> (hash-table-set! hash-table (car elem) (cdr elem)))
> alist)
> hash-table))
This does not account for the special case of string-ci=?. To really
fix this, we should extract the hash function deduction logic into a
separate function. I can do that if there are some seconds to declare
this important enough.
Panu
--
personal contact: xxxxxx@iki.fi, +35841 5323835, +3589 85619369
work contact: xxxxxx@helsinki.fi, +35850 3678003
kotisivu (henkkoht): http://www.iki.fi/atehwa/
homepage (technical): http://sange.fi/~atehwa/