From 8e042435e5f764370e0a0864e463c4666a52b715 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 23 Jun 2018 13:26:46 +0200 Subject: [PATCH] Update comment and use of a different hash function. --- example/ex-mph.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/example/ex-mph.c b/example/ex-mph.c index 18de8c79..ce6681e0 100644 --- a/example/ex-mph.c +++ b/example/ex-mph.c @@ -58,22 +58,32 @@ ARRAY_DEF(array_string, string_t) -// The algorithm may not converge on all inputs. -// Example: dictionnary with only 'a' and 'c' for which there is no seed -// where the hash value diverge on the final bit. -// The hash function doesn't provide the needed property. +/* The algorithm may not converge on all inputs if the hash function + * doesn't randomly distribute the inputs: + * str1 != str2 ==> \exist seed \such_as hash(seed,str1) != hash(seed, str2) + * The first hash function below doesn't provide the needed property for + * inputs with two words 'a' and 'c' + * The second one is more robust but may still have this issue. + */ static uint32_t hash(int32_t seed, const char str[]) { +#if 0 if (seed == 0) seed = 0x01000193; while (*str) { seed = ((seed * 0x01000193) ^ *str); str++; } return seed; +#else + if (seed == 0) seed = 0x811C9DC5; + while (*str) { + seed = (seed ^ *str) * 16777619; + str++; + } + return seed ^ (seed >> 16); +#endif } - - // Step 1: place all key keys into buckets static void CreateMinimalPerfectHash1(array_seed_t seed, array_value_t value,