Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!zaphod.mps.ohio-state.edu!usc!apple!sun-barr!newstop!texsun!convex!convex.COM From: macphee@convex.COM (Scott C. Mac Phee) Newsgroups: comp.lang.c Subject: Re: hash function for text in C Message-ID: <107254@convex.convex.com> Date: 17 Oct 90 13:06:25 GMT References: <1990Oct16.184951.513@watdragon.waterloo.edu> <107251@convex.convex.com> Sender: news@convex.com Reply-To: macphee@convex.COM (Scott C. Mac Phee) Organization: Convex Computer Corporation, Richardson, Tx. Lines: 54 In article <107251@convex.convex.com> macphee@convex.COM (Scott C. Mac Phee) writes: >In article <1990Oct16.184951.513@watdragon.waterloo.edu> cpshelley@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 ===============================================================================