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