This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: how to work with (weak) hash tables?
>>>>> "Greg" == Greg Harvey <Greg.Harvey@thezone.net> writes:
Greg> Come to think of it, you can do it in scheme, too; the code
Greg> follows, but this is not something that you should rely on (hash
Greg> tables could stop being scheme vectors for any number of
Greg> reasons, not the least of which being the following code ;):
Greg> (define (hash-for-each proc hash) (let ((len (vector-length
Greg> hash))) (do ((i 0 (+ i 1))) ((= i len) ()) (for-each proc
Greg> (vector-ref hash i)))))
guile> (define x (make-hash-table 10))
Greg> ; Prime, shmime ;)
guile> (hashq-set! x 'a 1)
Greg> 1
guile> (hashq-set! x 'b 1)
Greg> 1
guile> (hash-for-each (lambda (x) (set-cdr! x (+ (cdr x) 1))) x)
Greg> ()
guile> x
Greg> #(((a . 2)) () () () ((b . 2)) () () () () ())
Wow, it is surprisingly easy to corrupt everything:
guile> (hash-for-each
... (lambda (x)
... (set-car! x
... (if (eq? (car x) 'a) 'b 'a))) x)
guile> x
#(((b . 2)) () () () ((a . 2)) () () () () ())
guile> (hash-get-handle x 'a)
#f
guile> (hash-get-handle x 'b)
#f
Can not say, I like this...
What do you think about adding a function `hash-for-each-value!' to
hashtab.c (with evident behavior) to exclude such possibilities, and
to be independent on current realization of hashtables?
--
Best regards,
Valentin.