Plan 9 from Bell Labs’s /usr/web/sources/contrib/mycroftiv/unreleased/hashfuncs/prihash.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


/* 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 */

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].