guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Does anyone have a better scm_string_hash ?


From: Marius Vollmer
Subject: Re: Does anyone have a better scm_string_hash ?
Date: Mon, 17 Nov 2003 16:42:35 +0100
User-agent: Gnus/5.1002 (Gnus v5.10.2) Emacs/21.3 (gnu/linux)

Roland Orre <address@hidden> writes:

> /* replaced by Roland Orre address@hidden */
> #define hash_mask 0x00FFFFFF
> unsigned long 
> scm_string_hash (const unsigned char *str, size_t len)
> {
>   /* from suggestion at: */
>   /* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */
>   int i;
>   /* originally h=0 was suggested. address@hidden */
>   unsigned long h=177;
>
>   for (i = len-1; i >= 0; i--)
>     {
>       /* h = (str[i] +i+ h*37) & hash_mask; */
>       h = (str[i] + i + (h<<5) + (h<<2) + h) & hash_mask;
>     }
>   return h;
> } /* scm_string_hash */

g_string_hash from glib uses the same approach, but with 31 instead of
37 and with a h=0 seed:

    /* 31 bit hash function */
    guint
    g_str_hash (gconstpointer key)
    {
      const char *p = key;
      guint h = *p;

      if (h)
        for (p += 1; *p != '\0'; p++)
          h = (h << 5) - h + *p;

      return h;
    }

So which one is it? :)

Both should be much better than the one we have right now and I'm
going to install the one from Roland without the hash_mask.

OK?

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405




reply via email to

[Prev in Thread] Current Thread [Next in Thread]