From 21d329b22de155be9c840a1430e3c51d701f346a Mon Sep 17 00:00:00 2001 From: "R. Timothy Edwards" Date: Thu, 2 Apr 2026 21:29:04 -0400 Subject: [PATCH] Modified the hash algorithm used by netgen after a discussion with ChatGPT about hash implementations. Switched from SDBM to FNV-1a, which should be a better/stronger hash algorithm. Could do something more sophisticated, but this change can be done in a few minutes. --- base/hash.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/base/hash.c b/base/hash.c index 31c8245..46ea0b3 100644 --- a/base/hash.c +++ b/base/hash.c @@ -137,32 +137,37 @@ static unsigned char uppercase[] = { // horrible things can happen, as, for example, names AOI12 and OAI12 // have exactly the same hash result. Lousy for binning and even // lousier for generating class magic numbers. +// +// Updated again 4/2/2026 to the FNV-1a hash, which is better than +// SDBM for this application, according to ChatGPT. unsigned long hashnocase(char *s, int hashsize) { - unsigned long hashval; - - for (hashval = 0; *s != '\0'; ) - hashval = uppercase[*s++] - + (hashval << 6) + (hashval << 16) - hashval; + unsigned long hashval = 2166136261ul; + for (; *s != '\0'; s++) { + hashval ^= uppercase[*s]; + hashval *= 16777619ul; + } return (hashsize == 0) ? hashval : (hashval % hashsize); } unsigned long hashcase(char *s, int hashsize) { - unsigned long hashval; - - for (hashval = 0; *s != '\0'; ) - hashval = (*s++) + (hashval << 6) + (hashval << 16) - hashval; + unsigned long hashval = 2166136261ul; + for (; *s != '\0'; s++) { + hashval ^= (unsigned char)(*s); + hashval *= 16777619ul; + } return (hashsize == 0) ? hashval : (hashval % hashsize); } unsigned long genhash(char *s, int c, int hashsize) { - unsigned long hashval; - - for (hashval = (unsigned long)c; *s != '\0'; ) - hashval = (*s++) + (hashval << 6) + (hashval << 16) - hashval; + unsigned long hashval = 2166136261ul; + for (; *s != '\0'; s++) { + hashval ^= (unsigned long)c; + hashval *= 16777619ul; + } return (hashsize == 0) ? hashval : (hashval % hashsize); }