hash function for text in C

Scott C. Mac Phee macphee at convex.COM
Wed Oct 17 23:06:25 AEST 1990


In article <107251 at convex.convex.com> macphee at convex.COM (Scott C. Mac Phee) writes:
>In article <1990Oct16.184951.513 at watdragon.waterloo.edu> cpshelley at violet.waterloo.edu (cameron shelley) writes:
>>
>>  I have been (futily :<) experimenting with hash functions to 
>>hash english words into a large array.  No book I can find around
>>here gives the topic more than cursory discussion.  Does anyone
>>out there know of a good, simple one?  They must exist, and I'm
>>getting a little tired of mine... (er, function that is, I can't
>>afford another data structures book right now :).
>
>Here is some code I've been using.  It is an adapted version of some
>hashing algorithms I've seen pass through this newsgroup.
>
>Hope it helps...
>
>===========================================================================
>
>/*
> * ============================================================================
> *                     HASH TABLE FUNCTIONS
> * ============================================================================
> */
>
>#define HASH_TABLE_SIZE 1000
>static	int	eventhash[HASH_TABLE_SIZE] ;
>static	int	hash_entries,			/* number in table...	*/
>		collisions ;			/* count of collisions	*/



Sorry... amended code...

the hash table is an array of pointers to a structure, not int.

ex: 
struct event {
	char	*	name ;
	struct  event	* next ;
	} eventhash[HASH_TABLE_SIZE] ;

If a collision occurs use the hashed entry as the head of a linked
list and walk the linked list until the end is found, then add
the entry at the end.

I pulled the code out and then added the declarations without looking
at the original code.  Ten minutes later I realized I'd messed up.

Is today Monday, again?

--
===============================================================================
CONVEX Computer Corporation (CVX)                  Richardson, Texas
Scott C. Mac Phee (..uunet.uu.net!convex.com!macphee)   214-497-4772
===============================================================================



More information about the Comp.lang.c mailing list