-/*****************************************************************************\r
-** FILE IDENTIFICATION\r
-**\r
-** Hash Table Class\r
-**\r
-** This is part of the CTSim program\r
-** Copyright (C) 1983-2000 Kevin Rosenberg\r
-**\r
-** $Id: hashtable.cpp,v 1.1 2000/12/27 20:09:19 kevin Exp $\r
-**\r
-** This program is free software; you can redistribute it and/or modify\r
-** it under the terms of the GNU General Public License (version 2) as\r
-** published by the Free Software Foundation.\r
-**\r
-** This program is distributed in the hope that it will be useful,\r
-** but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-** GNU General Public License for more details.\r
-**\r
-** You should have received a copy of the GNU General Public License\r
-** along with this program; if not, write to the Free Software\r
-** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
-******************************************************************************/\r
-\r
-#include "ct.h"\r
-#include <math.h>\r
-#include <stdio.h>\r
-#include <ctype.h>\r
-#include "ctsupport.h"\r
-#include "pol.h"\r
-\r
-\r
-KeywordCodeEntry::KeywordCodeEntry (const char* const pszKeyword, int iCode)\r
- : m_iCode (iCode), m_pNext(NULL)\r
-{\r
- int nLength = strlen (pszKeyword);\r
- char* pszCopy = new char [ nLength + 1];\r
- for (int i = 0; i < nLength; i++) \r
- pszCopy[i] = tolower (pszKeyword[i]);\r
- pszCopy[nLength] = 0;\r
-\r
- m_strKeyword = pszCopy;\r
-\r
- delete pszCopy;\r
-}\r
-\r
-\r
-bool\r
-KeywordCodeEntry::matchesKeyword (const char* const pszCompare) const\r
-{\r
- int nLength = strlen (pszCompare);\r
- char* pszCopy = new char [ nLength + 1];\r
- for (int i = 0; i < nLength; i++) \r
- pszCopy[i] = tolower (pszCompare[i]);\r
- pszCopy[nLength] = 0;\r
-\r
- bool bMatch = false;\r
- if (m_strKeyword.compare (pszCompare) == 0)\r
- bMatch = true;\r
-\r
- delete pszCopy;\r
-\r
- return bMatch;\r
-}\r
-\r
-\r
-// inittable (table)\r
-// clear symbol table\r
-\r
-void \r
-KeywordCodeHashTable::initTable ()\r
-{\r
- int i;\r
-\r
- for (i = 0; i < HASHSIZE; i++)\r
- m_hashTable[i] = NULL;\r
-}\r
-\r
-// freetable (table)\r
-// free all memory allocated to table, then clear table\r
-\r
-void \r
-KeywordCodeHashTable::freeTable ()\r
-{\r
- int i;\r
- KeywordCodeEntry *p, *np;\r
-\r
- for (i = 0; i < HASHSIZE; i++) {\r
- np = m_hashTable [i];\r
- while (np != NULL) {\r
- p = np->getNext();\r
- delete np;\r
- np = p;\r
- }\r
- }\r
- initTable ();\r
-}\r
-\r
-\r
-// form hash value of string s \r
-int \r
-KeywordCodeHashTable::hash (const char* s)\r
-{\r
- int hashval = 0;\r
-\r
- while (*s != EOS) {\r
- hashval += tolower(*s);\r
- s++;\r
- }\r
-\r
- return (hashval % HASHSIZE);\r
-}\r
-\r
-\r
-/* Look for s in hash table */\r
-KeywordCodeEntry *\r
-KeywordCodeHashTable::lookup (const char* const pszLookup)\r
-{\r
- KeywordCodeEntry *found = NULL;\r
- for (KeywordCodeEntry* np = m_hashTable[ hash( pszLookup ) ]; np != NULL; np = np->getNext())\r
- if (np->matchesKeyword (pszLookup)) {\r
- found = np; // found it \r
- break;\r
- }\r
-\r
- return (found);\r
-}\r
-\r
-void\r
-KeywordCodeHashTable::installKeywordCode (const char* const pszKeyword, int iCode)\r
-{\r
- KeywordCodeEntry *np = lookup (pszKeyword);\r
-\r
- if (np == NULL) { // not found \r
- np = new KeywordCodeEntry (pszKeyword, iCode);\r
- int hashval = hash (np->getKeyword());\r
- np->setNext (m_hashTable[ hashval ]);\r
- m_hashTable[hashval] = np;\r
- } else // already defined\r
- np->setCode (iCode);\r
-}\r
+/*****************************************************************************
+** FILE IDENTIFICATION
+**
+** Hash Table Class
+**
+** This is part of the CTSim program
+** Copyright (C) 1983-2000 Kevin Rosenberg
+**
+** $Id: hashtable.cpp,v 1.2 2001/01/02 16:02:13 kevin Exp $
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License (version 2) as
+** published by the Free Software Foundation.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+******************************************************************************/
+
+#include "ct.h"
+#include <math.h>
+#include <stdio.h>
+#include <ctype.h>
+#include "ctsupport.h"
+#include "pol.h"
+
+
+KeywordCodeEntry::KeywordCodeEntry (const char* const pszKeyword, int iCode)
+ : m_iCode (iCode), m_pNext(NULL)
+{
+ int nLength = strlen (pszKeyword);
+ char* pszCopy = new char [ nLength + 1];
+ for (int i = 0; i < nLength; i++)
+ pszCopy[i] = tolower (pszKeyword[i]);
+ pszCopy[nLength] = 0;
+
+ m_strKeyword = pszCopy;
+
+ delete pszCopy;
+}
+
+
+bool
+KeywordCodeEntry::matchesKeyword (const char* const pszCompare) const
+{
+ int nLength = strlen (pszCompare);
+ char* pszCopy = new char [ nLength + 1];
+ for (int i = 0; i < nLength; i++)
+ pszCopy[i] = tolower (pszCompare[i]);
+ pszCopy[nLength] = 0;
+
+ bool bMatch = false;
+ if (m_strKeyword.compare (pszCompare) == 0)
+ bMatch = true;
+
+ delete pszCopy;
+
+ return bMatch;
+}
+
+
+// inittable (table)
+// clear symbol table
+
+void
+KeywordCodeHashTable::initTable ()
+{
+ int i;
+
+ for (i = 0; i < HASHSIZE; i++)
+ m_hashTable[i] = NULL;
+}
+
+// freetable (table)
+// free all memory allocated to table, then clear table
+
+void
+KeywordCodeHashTable::freeTable ()
+{
+ int i;
+ KeywordCodeEntry *p, *np;
+
+ for (i = 0; i < HASHSIZE; i++) {
+ np = m_hashTable [i];
+ while (np != NULL) {
+ p = np->getNext();
+ delete np;
+ np = p;
+ }
+ }
+ initTable ();
+}
+
+
+// form hash value of string s
+int
+KeywordCodeHashTable::hash (const char* s)
+{
+ int hashval = 0;
+
+ while (*s != EOS) {
+ hashval += tolower(*s);
+ s++;
+ }
+
+ return (hashval % HASHSIZE);
+}
+
+
+/* Look for s in hash table */
+KeywordCodeEntry *
+KeywordCodeHashTable::lookup (const char* const pszLookup)
+{
+ KeywordCodeEntry *found = NULL;
+ for (KeywordCodeEntry* np = m_hashTable[ hash( pszLookup ) ]; np != NULL; np = np->getNext())
+ if (np->matchesKeyword (pszLookup)) {
+ found = np; // found it
+ break;
+ }
+
+ return (found);
+}
+
+void
+KeywordCodeHashTable::installKeywordCode (const char* const pszKeyword, int iCode)
+{
+ KeywordCodeEntry *np = lookup (pszKeyword);
+
+ if (np == NULL) { // not found
+ np = new KeywordCodeEntry (pszKeyword, iCode);
+ int hashval = hash (np->getKeyword());
+ np->setNext (m_hashTable[ hashval ]);
+ m_hashTable[hashval] = np;
+ } else // already defined
+ np->setCode (iCode);
+}