home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!utcsri!torn!newshub.ccs.yorku.ca!newshub.ccs.yorku.ca!oz
- From: oz@ursa.sis.yorku.ca (Ozan Yigit)
- Subject: Re: Need good hashing functions
- In-Reply-To: sjs@netcom.com's message of Wed, 9 Dec 1992 21: 30:41 GMT
- Message-ID: <OZ.92Dec14124433@ursa.sis.yorku.ca>
- Sender: news@newshub.ccs.yorku.ca (USENET News System)
- Organization: York U. Student Information Systems Project
- References: <1992Dec9.213041.16259@netcom.com>
- Date: Mon, 14 Dec 1992 17:44:33 GMT
- Lines: 157
-
- Stephen Schow writes:
-
- I am looking for hashing functions for a school project. If anyone has any
- ideas and/or book suggestions that can help me find some good hashing
- functions to use in my project, I would appreciate it.
-
- Note - We are hashing single english words.
-
- The following is required reading:
-
- B. J. McKenzie, R. Harries, T. Bell.
- Selecting a Hashing Algorithm
- Software - Practice and Experience,
- 20(2):209-224, Feb 1990.
-
- Here are the functions used in that paper, from a posting by
- raghu@norway.fishkill.ibm.com (Raghu Hudli) to comp.programming,
- slightly re-formatted by me.
-
- enjoy... oz
- ---
- With diligence, it is possible to | electric: oz@sis.yorku.ca
- make anything run slowly. -T.Duff | ph:[416] 736 2100 x 33976
-
- --- cut here ---
-
- /*
- * return the least significant n bits of h
- */
- static unsigned int
- BITS(unsigned int h, unsigned int n)
- {
- register int bits = 0;
- while (n--) {
- bits = bits << 1;
- bits += 1;
- }
- return (bits & h);
- }
-
- /*
- * ETH Modula-2 Cross Compiler
- * Recommended table length = 1699
- */
- unsigned int
- hasheth(const char *key)
- {
- int tableLength = 1699;
- register unsigned int h = 0;
- register const char *c = key;
-
- while (*c)
- h = BITS(*c++, 8) * (h % 257) + 1;
- return (h % tableLength);
- }
-
- /*
- * GNU C preprocessor
- * Recomended table length=1403
- */
- unsigned int
- hashgnuccpp(const char *key)
- {
- int tableLength = 1403;
- register unsigned int h = 0;
- register const char *c = key;
-
- while (*c)
- h = 4 * h + *c++;
- return (BITS(h, 31) % tableLength);
- }
-
- /*
- * GNU C compiler front end
- * Recomended table length=1008
- */
- unsigned int
- hashgnucc1(const char *key)
- {
- int tableLength = 1008;
- register unsigned int h = 0;
- register const char *c = key;
-
- while (*c)
- h = 613 * h + *c++;
- return (BITS(h, 30) % tableLength);
- }
-
- /*
- * Portable C Compiler
- * Recomended table length=1013
- */
- unsigned int
- hashpcc(const char *key)
- {
-
- int tableLength = 1013;
- register int h = 0;
- register const char *c = key;
-
- while (*c)
- h = 2 * h + *c++;
- return (((unsigned int) BITS(h, 15)) % tableLength);
- }
-
- /*
- * Unix 4.3BSD C preprocessor based
- * Recomended table length=2000
- */
- unsigned int
- hashcpp(const char *key)
- {
- int tableLength = 2000;
- register int h = 0;
- register const char *c = key;
- register unsigned int index;
-
- while (*c)
- h = 2 * h + *c++;
-
- if ((index = h % 2000) >= 0)
- return (index % tableLength);
- else
- return (index % tableLength + 2000);
- }
-
- /*
- * AT&T C++ compiler based.
- * Recomended table length=257
- */
- unsigned int
- hashcplusplus(const char *key)
- {
- int tableLength = 257;
- register unsigned int h = 0;
- register const char *c = key;
-
- while (*c)
- h = 2 * h + *c++;
- return (h % tableLength);
- }
-
- /*
- * ICON translator
- * Recommended table length = 257
- */
- unsigned int
- hashicon(const char *key)
- {
- int tableLength = 257;
- register unsigned int h = 0;
- register const char *c = key;
-
- while (*c)
- h += BITS(*c++, 8);
- return (h % tableLength);
- }
-