mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-26 07:26:15 +01:00
g_str_hash: switch to using DJB hash
This is the same as what we were already doing with 2 changes: - use an initial value of 5381 instead of 0 - multiply by 33 in each round instead of 31
This commit is contained in:
parent
f50a99e782
commit
354d655ba8
@ -121,20 +121,26 @@ g_str_equal (gconstpointer v1,
|
|||||||
* @v: a string key
|
* @v: a string key
|
||||||
*
|
*
|
||||||
* Converts a string to a hash value.
|
* Converts a string to a hash value.
|
||||||
* It can be passed to g_hash_table_new() as the @hash_func
|
*
|
||||||
* parameter, when using strings as keys in a #GHashTable.
|
* This function implements the widely used "djb" hash apparently posted
|
||||||
|
* by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
|
||||||
|
* unsigned hash value starts at 5381 and for each byte 'c' in the
|
||||||
|
* string, is updated: <literal>hash = hash * 33 + c</literal>. This
|
||||||
|
* function uses the signed value of each byte.
|
||||||
|
*
|
||||||
|
* It can be passed to g_hash_table_new() as the @hash_func parameter,
|
||||||
|
* when using strings as keys in a #GHashTable.
|
||||||
*
|
*
|
||||||
* Returns: a hash value corresponding to the key
|
* Returns: a hash value corresponding to the key
|
||||||
*/
|
**/
|
||||||
guint
|
guint
|
||||||
g_str_hash (gconstpointer v)
|
g_str_hash (gconstpointer v)
|
||||||
{
|
{
|
||||||
/* 31 bit hash function */
|
|
||||||
const signed char *p;
|
const signed char *p;
|
||||||
guint32 h = 0;
|
guint32 h = 5381;
|
||||||
|
|
||||||
for (p = v; *p != '\0'; p++)
|
for (p = v; *p != '\0'; p++)
|
||||||
h = (h << 5) - h + *p;
|
h = (h << 5) + h + *p;
|
||||||
|
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user