/* * hash.h * SDBM Hash function * * Created by Sunrise Telephone Systems Ltd. * * This file ("hash.c") is hereby placed into the public domain. * */ #include #include "globaldefs.h" #include "hash.h" // -------------------------------------------------------------------------- // function: calc_hash(string) // -------------------------------------------------------------------------- // // Returns the hash value of the null terminated C string 'string' using the // SDBM hash algorithm. The number of significant characters for which the // hash value will be calculated is limited to MAXIMUM_LENGTH_FOR_STRINGS. unsigned int calc_hash(const char *string) { register unsigned int len, index, hash = 0; register char ch; len = strlen(string); if (len > MAXIMUM_LENGTH_FOR_STRINGS) { len = MAXIMUM_LENGTH_FOR_STRINGS; } // end if for (index = 0; index < len; index++) { ch = string[index]; hash = ch + (hash << 6) + (hash << 16) - hash; } // end for return (hash & 0x7FFFFFFF); } // end calc_hash // -------------------------------------------------------------------------- // function: calc_hash_with_limit(string, limit) // -------------------------------------------------------------------------- // // Returns the hash value of the null terminated C string 'string' using the // SDBM hash algorithm. The number of significant characters for which the // hash value will be calculated is limited to 'limit'. unsigned int calc_hash_with_limit(const char *string, unsigned int limit) { register unsigned int len, index, hash = 0; register char ch; len = strlen(string); if (len > limit) { len = limit; } // end if for (index = 0; index < len; index++) { ch = string[index]; hash = ch + (hash << 6) + (hash << 16) - hash; } // end for return (hash & 0x7FFFFFFF); } // end calc_hash_with_limit // END OF FILE