| 1 |
/***************************************************************************** |
| 2 |
** FILE IDENTIFICATION |
| 3 |
** |
| 4 |
** Hash Table Class |
| 5 |
** |
| 6 |
** This is part of the CTSim program |
| 7 |
** Copyright (c) 1983-2009 Kevin Rosenberg |
| 8 |
** |
| 9 |
** This program is free software; you can redistribute it and/or modify |
| 10 |
** it under the terms of the GNU General Public License (version 2) as |
| 11 |
** published by the Free Software Foundation. |
| 12 |
** |
| 13 |
** This program is distributed in the hope that it will be useful, |
| 14 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
** GNU General Public License for more details. |
| 17 |
** |
| 18 |
** You should have received a copy of the GNU General Public License |
| 19 |
** along with this program; if not, write to the Free Software |
| 20 |
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
******************************************************************************/ |
| 22 |
|
| 23 |
#include "ct.h" |
| 24 |
#include <math.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include "ctsupport.h" |
| 28 |
#include "pol.h" |
| 29 |
|
| 30 |
|
| 31 |
KeywordCodeEntry::KeywordCodeEntry (const char* const pszKeyword, int iCode) |
| 32 |
: m_iCode (iCode), m_pNext(NULL) |
| 33 |
{ |
| 34 |
int nLength = strlen (pszKeyword); |
| 35 |
char* pszCopy = new char [ nLength + 1]; |
| 36 |
for (int i = 0; i < nLength; i++) |
| 37 |
pszCopy[i] = tolower (pszKeyword[i]); |
| 38 |
pszCopy[nLength] = 0; |
| 39 |
|
| 40 |
m_strKeyword = pszCopy; |
| 41 |
|
| 42 |
delete pszCopy; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
bool |
| 47 |
KeywordCodeEntry::matchesKeyword (const char* const pszCompare) const |
| 48 |
{ |
| 49 |
int nLength = strlen (pszCompare); |
| 50 |
char* pszCopy = new char [ nLength + 1]; |
| 51 |
for (int i = 0; i < nLength; i++) |
| 52 |
pszCopy[i] = tolower (pszCompare[i]); |
| 53 |
pszCopy[nLength] = 0; |
| 54 |
|
| 55 |
bool bMatch = false; |
| 56 |
if (m_strKeyword.compare (pszCompare) == 0) |
| 57 |
bMatch = true; |
| 58 |
|
| 59 |
delete pszCopy; |
| 60 |
|
| 61 |
return bMatch; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
// inittable (table) |
| 66 |
// clear symbol table |
| 67 |
|
| 68 |
void |
| 69 |
KeywordCodeHashTable::initTable () |
| 70 |
{ |
| 71 |
int i; |
| 72 |
|
| 73 |
for (i = 0; i < HASHSIZE; i++) |
| 74 |
m_hashTable[i] = NULL; |
| 75 |
} |
| 76 |
|
| 77 |
// freetable (table) |
| 78 |
// free all memory allocated to table, then clear table |
| 79 |
|
| 80 |
void |
| 81 |
KeywordCodeHashTable::freeTable () |
| 82 |
{ |
| 83 |
int i; |
| 84 |
KeywordCodeEntry *p, *np; |
| 85 |
|
| 86 |
for (i = 0; i < HASHSIZE; i++) { |
| 87 |
np = m_hashTable [i]; |
| 88 |
while (np != NULL) { |
| 89 |
p = np->getNext(); |
| 90 |
delete np; |
| 91 |
np = p; |
| 92 |
} |
| 93 |
} |
| 94 |
initTable (); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// form hash value of string s |
| 99 |
int |
| 100 |
KeywordCodeHashTable::hash (const char* s) |
| 101 |
{ |
| 102 |
int hashval = 0; |
| 103 |
|
| 104 |
while (*s != EOS) { |
| 105 |
hashval += tolower(*s); |
| 106 |
s++; |
| 107 |
} |
| 108 |
|
| 109 |
return (hashval % HASHSIZE); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
/* Look for s in hash table */ |
| 114 |
KeywordCodeEntry * |
| 115 |
KeywordCodeHashTable::lookup (const char* const pszLookup) |
| 116 |
{ |
| 117 |
KeywordCodeEntry *found = NULL; |
| 118 |
for (KeywordCodeEntry* np = m_hashTable[ hash( pszLookup ) ]; np != NULL; np = np->getNext()) |
| 119 |
if (np->matchesKeyword (pszLookup)) { |
| 120 |
found = np; // found it |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
return (found); |
| 125 |
} |
| 126 |
|
| 127 |
void |
| 128 |
KeywordCodeHashTable::installKeywordCode (const char* const pszKeyword, int iCode) |
| 129 |
{ |
| 130 |
KeywordCodeEntry *np = lookup (pszKeyword); |
| 131 |
|
| 132 |
if (np == NULL) { // not found |
| 133 |
np = new KeywordCodeEntry (pszKeyword, iCode); |
| 134 |
int hashval = hash (np->getKeyword()); |
| 135 |
np->setNext (m_hashTable[ hashval ]); |
| 136 |
m_hashTable[hashval] = np; |
| 137 |
} else // already defined |
| 138 |
np->setCode (iCode); |
| 139 |
} |