r460: no message
[ctsim.git] / libctsupport / hashtable.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Hash Table Class
5 **
6 **  This is part of the CTSim program
7 **  Copyright (c) 1983-2001 Kevin Rosenberg
8 **
9 **  $Id: hashtable.cpp,v 1.3 2001/01/28 19:10:18 kevin Exp $
10 **
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.
14 **
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.
19 **
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 ******************************************************************************/
24
25 #include "ct.h"
26 #include <math.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include "ctsupport.h"
30 #include "pol.h"
31
32
33 KeywordCodeEntry::KeywordCodeEntry (const char* const pszKeyword, int iCode)
34     : m_iCode (iCode), m_pNext(NULL)
35 {
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]);
40    pszCopy[nLength] = 0;
41
42    m_strKeyword = pszCopy;
43
44    delete pszCopy;
45 }
46
47
48 bool
49 KeywordCodeEntry::matchesKeyword (const char* const pszCompare) const
50 {
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]);
55     pszCopy[nLength] = 0;
56
57     bool bMatch = false;
58     if (m_strKeyword.compare (pszCompare) == 0)
59       bMatch = true;
60
61     delete pszCopy;
62
63     return bMatch;
64 }
65
66
67 // inittable (table)
68 //    clear symbol table
69
70 void 
71 KeywordCodeHashTable::initTable ()
72 {
73         int i;
74
75         for (i = 0; i < HASHSIZE; i++)
76             m_hashTable[i] = NULL;
77 }
78
79 // freetable (table)
80 //      free all memory allocated to table, then clear table
81
82 void 
83 KeywordCodeHashTable::freeTable ()
84 {
85         int i;
86         KeywordCodeEntry *p, *np;
87
88         for (i = 0; i < HASHSIZE; i++) {
89             np = m_hashTable [i];
90             while (np != NULL) {
91                     p = np->getNext();
92                     delete np;
93                     np = p;
94       }
95         }
96         initTable ();
97 }
98
99
100 // form hash value of string s 
101 int 
102 KeywordCodeHashTable::hash (const char* s)
103 {
104         int hashval = 0;
105
106   while (*s != EOS) {
107             hashval += tolower(*s);
108       s++;
109   }
110
111         return (hashval % HASHSIZE);
112 }
113
114
115 /* Look for s in hash table */
116 KeywordCodeEntry *
117 KeywordCodeHashTable::lookup (const char* const pszLookup)
118 {
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 
123               break;
124         }
125
126   return (found);
127 }
128
129 void
130 KeywordCodeHashTable::installKeywordCode (const char* const pszKeyword, int iCode)
131 {
132     KeywordCodeEntry *np = lookup (pszKeyword);
133
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
140             np->setCode (iCode);
141 }