/* * ====================================================================== * Test Program for Simple Hash Function * * Copyright (C) 1993-96 by Mark Austin and David Mazzoni. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * ====================================================================== */ #include #include enum { HashTableSize = 101 }; int hash( char *cpName ) { int iValue = 0; int ii; for( ii = 0; ii < strlen( cpName ); ii = ii + 1) iValue += cpName[ ii ]; printf("*** iValue = %4d : ", iValue); return ( iValue % HashTableSize ); } int main( void ) { char *cpaHashTable [ HashTableSize ]; char *cpOneCharacter = "a"; char *cpMaterial = "Steel"; char *cpSteelSection = "W16x30"; int ii, iKey; /* [a] : Initialize hash table entries */ for (ii = 0 ; ii < HashTableSize; ii = ii + 1 ) cpaHashTable [ ii ] = (char *) NULL; /* [b] : Build hash table */ iKey = hash( cpOneCharacter ); cpaHashTable [ iKey ] = cpOneCharacter; printf("hash( %12s ) = %4d\n", cpOneCharacter, iKey ); iKey = hash( cpMaterial ); cpaHashTable [ iKey ] = cpMaterial; printf("hash( %12s ) = %4d\n", cpMaterial, iKey ); iKey = hash( cpMaterial ); cpaHashTable [ iKey ] = cpSteelSection; printf("hash( %12s ) = %4d\n", cpSteelSection, iKey ); printf("\n*** SIMULATE COLLISION *** \n\n"); printf("hash( %12s ) = %4d\n", "ae", hash( "ae" )); }