r316: c++ conversions to POL
[ctsim.git] / libctsupport / hashtable.cpp
diff --git a/libctsupport/hashtable.cpp b/libctsupport/hashtable.cpp
new file mode 100644 (file)
index 0000000..a4dd283
--- /dev/null
@@ -0,0 +1,141 @@
+/*****************************************************************************\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