This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

A few minor hash questions.



I've got a couple of hash table questions that don't seem to be
answered in guile-ref.

First of all, what's legitimate for a hash table's format?  Are you
supposed to allocate a vector of an "appropriate size"?  Something
like (make-vector 17)?  If so, is there an appropriate default value,
or is just letting the empty buckets be #<unspecified> OK?

Also, do you have to stick with one pair of matched setters/getters
for a given hash?  i.e. if you use hashq-set! to build the table, do
you have to use hashq-ref to access it?  I would have expected
hash-ref to have worked too, since equal? is a superset of eq?, but it
doesn't appear to:

  guile> (define some-hash (make-vector 5 '()))
  guile>   (hashq-set! some-hash 'a 1)
  1
  guile>   (hashq-set! some-hash 'b 2)
  2
  guile>   (hashq-set! some-hash 'c 3)
  3
  guile> some-hash
  #(() ((c . 3)) () ((b . 2)) ((a . 1)))
  guile> (hash-ref some-hash 'a)
  #f

Oh, and presuming that right now hashes will always be vectors of
(possibly improper) lists, then I think this is a suitable
hash-for-each for the current implementation, for anyone who needs it:

  (define (hash-for-each thunk hashtable)
    (array-for-each
     (lambda (bucket)
       (if (pair? bucket)
           ;; Can't use for-each here since a bucket doesn't have to be
           ;; a proper list.  (make-vector 17) is a functional hash table.
           (let loop ((remaining-items bucket))
             (if (pair? remaining-items)
                 (begin
                   (thunk (car remaining-items))
                   (loop (cdr remaining-items)))))))
     hashtable))

Thanks

-- 
Rob Browning <rlb@cs.utexas.edu> PGP=E80E0D04F521A094 532B97F5D64E3930

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]