1 //-< SYMTAB.CPP >----------------------------------------------------*--------* 2 // FastDB Version 1.0 (c) 1999 GARRET * ? * 3 // (Main Memory Database Management System) * /\| * 4 // * / \ * 5 // Created: 20-Nov-98 K.A. Knizhnik * / [] \ * 6 // Last update: 20-Nov-98 K.A. Knizhnik * GARRET * 7 //-------------------------------------------------------------------*--------* 8 // Symbol table implementation 9 //-------------------------------------------------------------------*--------* 10 11 #define INSIDE_FASTDB 12 13 #include "stdtp.h" 14 #include "sync.h" 15 #include "symtab.h" 16 17 BEGIN_FASTDB_NAMESPACE 18 19 const size_t hashTableSize = 1009; 20 dbSymbolTable::HashTableItem* dbSymbolTable::hashTable[hashTableSize]; 21 22 dbSymbolTable dbSymbolTable::instance; 23 ~dbSymbolTable()24dbSymbolTable::~dbSymbolTable() { 25 for (int i = hashTableSize; --i >= 0;) { 26 HashTableItem *ip, *next; 27 for (ip = hashTable[i]; ip != NULL; ip = next) { 28 next = ip->next; 29 delete ip; 30 } 31 } 32 } 33 #ifdef IGNORE_CASE 34 #define strcmp(x,y) stricmp(x,y) 35 #endif 36 add(char * & str,int tag,bool allocate)37int dbSymbolTable::add(char* &str, int tag, bool allocate) { 38 static dbMutex mutex; 39 dbCriticalSection cs(mutex); 40 unsigned hash = 0; 41 byte* p = (byte*)str; 42 while (*p != 0) { 43 byte b = *p++; 44 #ifdef IGNORE_CASE 45 b = tolower(b); 46 #endif 47 hash = hash*31 + b; 48 } 49 int index = hash % hashTableSize; 50 HashTableItem *ip; 51 for (ip = hashTable[index]; ip != NULL; ip = ip->next) { 52 if (ip->hash == hash && strcmp(ip->str, str) == 0) { 53 str = ip->str; 54 if (tag > ip->tag) { 55 ip->tag = tag; 56 } 57 return ip->tag; 58 } 59 } 60 ip = new HashTableItem; 61 ip->allocated = false; 62 if (allocate) { 63 char* dupstr = new char[strlen(str) + 1]; 64 strcpy(dupstr, str); 65 str = dupstr; 66 ip->allocated = true; 67 } 68 ip->str = str; 69 ip->hash = hash; 70 ip->tag = tag; 71 ip->next = hashTable[index]; 72 hashTable[index] = ip; 73 return tag; 74 } 75 76 END_FASTDB_NAMESPACE 77