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