1 /*****************************************************************************
\r
2 ** FILE IDENTIFICATION
\r
6 ** This is part of the CTSim program
\r
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
\r
9 ** $Id: hashtable.cpp,v 1.1 2000/12/27 20:09:19 kevin Exp $
\r
11 ** This program is free software; you can redistribute it and/or modify
\r
12 ** it under the terms of the GNU General Public License (version 2) as
\r
13 ** published by the Free Software Foundation.
\r
15 ** This program is distributed in the hope that it will be useful,
\r
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
18 ** GNU General Public License for more details.
\r
20 ** You should have received a copy of the GNU General Public License
\r
21 ** along with this program; if not, write to the Free Software
\r
22 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
23 ******************************************************************************/
\r
29 #include "ctsupport.h"
\r
33 KeywordCodeEntry::KeywordCodeEntry (const char* const pszKeyword, int iCode)
\r
34 : m_iCode (iCode), m_pNext(NULL)
\r
36 int nLength = strlen (pszKeyword);
\r
37 char* pszCopy = new char [ nLength + 1];
\r
38 for (int i = 0; i < nLength; i++)
\r
39 pszCopy[i] = tolower (pszKeyword[i]);
\r
40 pszCopy[nLength] = 0;
\r
42 m_strKeyword = pszCopy;
\r
49 KeywordCodeEntry::matchesKeyword (const char* const pszCompare) const
\r
51 int nLength = strlen (pszCompare);
\r
52 char* pszCopy = new char [ nLength + 1];
\r
53 for (int i = 0; i < nLength; i++)
\r
54 pszCopy[i] = tolower (pszCompare[i]);
\r
55 pszCopy[nLength] = 0;
\r
57 bool bMatch = false;
\r
58 if (m_strKeyword.compare (pszCompare) == 0)
\r
67 // inittable (table)
\r
68 // clear symbol table
\r
71 KeywordCodeHashTable::initTable ()
\r
75 for (i = 0; i < HASHSIZE; i++)
\r
76 m_hashTable[i] = NULL;
\r
79 // freetable (table)
\r
80 // free all memory allocated to table, then clear table
\r
83 KeywordCodeHashTable::freeTable ()
\r
86 KeywordCodeEntry *p, *np;
\r
88 for (i = 0; i < HASHSIZE; i++) {
\r
89 np = m_hashTable [i];
\r
90 while (np != NULL) {
\r
100 // form hash value of string s
\r
102 KeywordCodeHashTable::hash (const char* s)
\r
106 while (*s != EOS) {
\r
107 hashval += tolower(*s);
\r
111 return (hashval % HASHSIZE);
\r
115 /* Look for s in hash table */
\r
117 KeywordCodeHashTable::lookup (const char* const pszLookup)
\r
119 KeywordCodeEntry *found = NULL;
\r
120 for (KeywordCodeEntry* np = m_hashTable[ hash( pszLookup ) ]; np != NULL; np = np->getNext())
\r
121 if (np->matchesKeyword (pszLookup)) {
\r
122 found = np; // found it
\r
130 KeywordCodeHashTable::installKeywordCode (const char* const pszKeyword, int iCode)
\r
132 KeywordCodeEntry *np = lookup (pszKeyword);
\r
134 if (np == NULL) { // not found
\r
135 np = new KeywordCodeEntry (pszKeyword, iCode);
\r
136 int hashval = hash (np->getKeyword());
\r
137 np->setNext (m_hashTable[ hashval ]);
\r
138 m_hashTable[hashval] = np;
\r
139 } else // already defined
\r
140 np->setCode (iCode);
\r