X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=libctsupport%2Fhashtable.cpp;fp=libctsupport%2Fhashtable.cpp;h=a4dd2833b7385da79c1847971d2dc08024ae1a31;hb=df761d2b3cac3d347f4ef0f9f69e88d3bb66b2fa;hp=0000000000000000000000000000000000000000;hpb=65732cc5d8dbf867ed56a021c07c5636cea93b5a;p=ctsim.git diff --git a/libctsupport/hashtable.cpp b/libctsupport/hashtable.cpp new file mode 100644 index 0000000..a4dd283 --- /dev/null +++ b/libctsupport/hashtable.cpp @@ -0,0 +1,141 @@ +/***************************************************************************** +** FILE IDENTIFICATION +** +** Hash Table Class +** +** This is part of the CTSim program +** Copyright (C) 1983-2000 Kevin Rosenberg +** +** $Id: hashtable.cpp,v 1.1 2000/12/27 20:09:19 kevin Exp $ +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License (version 2) as +** published by the Free Software Foundation. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +******************************************************************************/ + +#include "ct.h" +#include +#include +#include +#include "ctsupport.h" +#include "pol.h" + + +KeywordCodeEntry::KeywordCodeEntry (const char* const pszKeyword, int iCode) + : m_iCode (iCode), m_pNext(NULL) +{ + int nLength = strlen (pszKeyword); + char* pszCopy = new char [ nLength + 1]; + for (int i = 0; i < nLength; i++) + pszCopy[i] = tolower (pszKeyword[i]); + pszCopy[nLength] = 0; + + m_strKeyword = pszCopy; + + delete pszCopy; +} + + +bool +KeywordCodeEntry::matchesKeyword (const char* const pszCompare) const +{ + int nLength = strlen (pszCompare); + char* pszCopy = new char [ nLength + 1]; + for (int i = 0; i < nLength; i++) + pszCopy[i] = tolower (pszCompare[i]); + pszCopy[nLength] = 0; + + bool bMatch = false; + if (m_strKeyword.compare (pszCompare) == 0) + bMatch = true; + + delete pszCopy; + + return bMatch; +} + + +// inittable (table) +// clear symbol table + +void +KeywordCodeHashTable::initTable () +{ + int i; + + for (i = 0; i < HASHSIZE; i++) + m_hashTable[i] = NULL; +} + +// freetable (table) +// free all memory allocated to table, then clear table + +void +KeywordCodeHashTable::freeTable () +{ + int i; + KeywordCodeEntry *p, *np; + + for (i = 0; i < HASHSIZE; i++) { + np = m_hashTable [i]; + while (np != NULL) { + p = np->getNext(); + delete np; + np = p; + } + } + initTable (); +} + + +// form hash value of string s +int +KeywordCodeHashTable::hash (const char* s) +{ + int hashval = 0; + + while (*s != EOS) { + hashval += tolower(*s); + s++; + } + + return (hashval % HASHSIZE); +} + + +/* Look for s in hash table */ +KeywordCodeEntry * +KeywordCodeHashTable::lookup (const char* const pszLookup) +{ + KeywordCodeEntry *found = NULL; + for (KeywordCodeEntry* np = m_hashTable[ hash( pszLookup ) ]; np != NULL; np = np->getNext()) + if (np->matchesKeyword (pszLookup)) { + found = np; // found it + break; + } + + return (found); +} + +void +KeywordCodeHashTable::installKeywordCode (const char* const pszKeyword, int iCode) +{ + KeywordCodeEntry *np = lookup (pszKeyword); + + if (np == NULL) { // not found + np = new KeywordCodeEntry (pszKeyword, iCode); + int hashval = hash (np->getKeyword()); + np->setNext (m_hashTable[ hashval ]); + m_hashTable[hashval] = np; + } else // already defined + np->setCode (iCode); +}