/* * =================================================================== * hashtable.c : Functions for Hash Table * * 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. * * Written by: Mark Austin February 1994 * =================================================================== */ #include #include #include "hashtable.h" #include "miscellaneous.h" enum{ HashTableSize = 41 }; static HASHNODE *saHashTable[ HashTableSize ]; /* * ============================================================= * Hash function * * Input : char *cpName -- Pointer to name item in hash table. * Output : int -- Hash Value. * ============================================================= */ #ifdef __STDC__ int hash( char * cpName ) #else /* start case not STDC */ int hash( cpName ) char *cpName; #endif /* end case not STDC */ { int iMid, iLen, iValue; iLen = strlen( cpName ); iMid = (iLen + 1) / 2; iValue = cpName[0] + iMid * cpName[ iMid - 1 ] + iLen * cpName[ iLen - 1]; return ( iValue % HashTableSize ); } /* * ===================== * Initialize Hash Table * ===================== */ hashTableInit() { HASHNODE *spNext; HASHNODE *spNode; int ii; for(ii = 0; ii < HashTableSize; ii++) { spNode = saHashTable[ ii ]; while (spNode != NULL) { spNext = spNode->spNext; switch ((int) spNode->eType) { case AiscSection: free((char *) spNode->uNode.spAisc ); break; default: break; } free((char *) spNode->cpName ); free((char *) spNode ); spNode = spNext; } saHashTable[ ii ] = NULL; } } /* * ============================================================= * Lookup string name. return NULL if not found * * Input : char *cpName -- Pointer to name item in hash table. * Output : HASHNODE * -- Pointer to node in hash table. * ============================================================= */ #ifdef __STDC__ HASHNODE *hashTableLookup( char * cpName ) #else /* start case not STDC */ HASHNODE *hashTableLookup( cpName ) char *cpName; #endif /* end case not STDC */ { HASHNODE *spNode; int iHashValue; iHashValue = hash( cpName ); for( spNode = saHashTable[ iHashValue ]; spNode != NULL; spNode = spNode->spNext) { if(strcmp( cpName , spNode->cpName) == 0) return spNode; } return NULL; } /* * ============================================================= * Install new node into Hash Table if not already present * * Input : char *cpName -- Pointer to name item in hash table. * Output : HASHNODE * -- Pointer to new hash table node. * ============================================================= */ #ifdef __STDC__ HASHNODE *hashTableInstall( char * cpName ) #else /* start case not STDC */ HASHNODE *hashTableInstall( cpName ) char *cpName; #endif /* end case not STDC */ { HASHNODE *spNode; int iHashValue; /* [a] : Make sure node isn't already in hash table */ spNode = hashTableLookup( cpName ); if( spNode != NULL) { printf("WARNING : Node \"%s\" already in hash table\n", cpName ); return spNode; } /* [b] : Allocate memory for new hash node */ spNode = (HASHNODE *) safeMalloc( sizeof(HASHNODE), __FILE__, __LINE__ ); spNode->cpName = saveString( cpName, __FILE__, __LINE__ ); /* [c] : Link new node into front of list */ iHashValue = hash( cpName ); spNode->spNext = saHashTable[ iHashValue ]; saHashTable[ iHashValue ] = spNode; return spNode; } /* * ============================ * Print contents of Hash Table * ============================ */ enum { Ncol = 4 }; void hashTablePrint() { HASHNODE *spNode; int ii, iCount; for(ii = 0; ii < HashTableSize; ii++) { iCount = 0; printf("\n INFO >> ii = %3d : ", ii); for(spNode = saHashTable[ ii ]; spNode != NULL; spNode = spNode->spNext) { printf("%17s ", spNode->cpName ); iCount = iCount + 1; if( iCount%Ncol == 0 && spNode->spNext != NULL) { iCount = 0; printf("\n "); } } } printf("\n"); }