/* the prihash function operates on incoming null terminated character arrays and produce hashes of the given data of the requested size*/
#include <u.h>
#include <libc.h>
extern uint primes[266];
uint
prihash(uint ntbl, char *data)
{
uint off;
uint size;
size = strlen(data);
/* our algorithm for this hash is to represent each possible value for a character byte by a prime number, and for each character we multiply by that prime by our running hash and add then add the result to half itself and increment by one - no clue as to the originality of the algorithm, it seemed like a simple and effective hash and testing indicates it performs well. unsigned ints avoid the possibility of overflow. */
off = primes[(uint)(*data)];
for(int i = 1; i <= size; i++){
off = off * primes[(uint)(*(data+i))];
off += off/2;
off++;
}
return off % ntbl;
}
/* implemented by mycroftiv */
|