This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Hash tables


The key 53gqi _does_ have a value, (cdr '("53gqi")) => '().  So, your
default doesn't get used.  The default is only used if there is no mapping
for the key at all:

alist->hash-table alist [ equal? [ hash [ size-hint]]] → hash-table

    Takes an association list alist and creates a hash table hash-table
which maps the car of every element in alist to the cdr of corresponding
elements in alist. The equal?, hash, and size-hint parameters are
interpreted as in make-hash-table. If some key occurs multiple times in
alist, the value in the first association will take precedence over later
ones. (Note: the choice of using cdr (instead of cadr) for values tries to
strike balance between the two approaches: using cadr would render this
procedure unusable for cdr alists, but not vice versa.)



hash-table-ref hash-table key [ thunk ] → value

    This procedure returns the value associated to key in hash-table. If no
value is associated to key and thunk is given, it is called with no
arguments and its value is returned; if thunk is not given, an error is
signalled. Given a good hash function, this operation should have an
(amortised) complexity of O(1) with respect to the number of associations in
hash-table.

Function: hash-table-ref/default hash-table key default → value

    Evaluates to the same value as (hash-table-ref hash-table key (lambda ()
default)). Given a good hash function, this operation should have an
(amortised) complexity of O(1) with respect to the number of associations in
hash-table.


'robert


On 1/16/08 10:01 AM, "Daniel Terhorst" <daniel.s.terhorst@gmail.com> wrote:

> I ran into some unexpected behavior with hash tables today, rather by
> coincidence, it seems. If I create a hash-table with two keys, "53gqi"
> and "6vxk4", I become unable to delete the first from the table. If I
> change the keys in any way or try to remove the second one first, it
> works as expected, so this seems to be due to some kind of collision.
> I wouldn't expect that to cause delete to no-op, however, so I suspect
> it's just a bug that hasn't been detected until now.
> 
> 
> 
> ;; -*- scheme -*-
> 
> (require 'srfi-69)
> 
> (let ((H (alist->hash-table '(("53gqi")
>                               ("6vxk4"))))
>       (key "53gqi"))
>   (values (html:p (hash-table-ref H key))
>           (hash-table-delete! H key)
>           (html:p (hash-table-ref/default H key "GONE =)"))))
> 
> #\newline
> 
> 
> 
> ---
> 
> Expected:
> ()
> GONE =)
> 
> 
> Actual:
> ()
> ()


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