[Top][All Lists]
[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: |
Thu, 20 Nov 2003 18:29:21 +0100 |
User-agent: |
Gnus/5.1002 (Gnus v5.10.2) Emacs/21.3 (gnu/linux) |
Stephen Compall <address@hidden> writes:
> /* This one comes from `recode', and performs a bit better than the above as
> per a few experiments. It is inspired from a hashing routine found in the
> very old Cyber `snoop', itself written in typical Greg Mansfield style.
> (By the way, what happened to this excellent man? Is he still alive?) */
>
> size_t
> hash_string (const char *string, size_t n_buckets)
> {
> size_t value = 0;
>
> while (*string)
> value = (value * 31 + (unsigned char) *string++) % n_buckets;
> return value;
> }
This is essentially(?) the one used by glib. We use it with 37
instead of 31 and like glib, compute the module after the loop.
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 */
unsigned long h = 0;
while (len-- > 0)
h = *str++ + h*37;
return h;
}
--
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3 331E FAF8 226A D5D4 E405