home *** CD-ROM | disk | FTP | other *** search
- #include "assert.h"
- #include "emTypes.h"
- #include "system.h"
- #include <sys/file.h>
- #include "opNames.h"
- #include <stdio.h>
-
- #ifndef EMDIR
- #define EMDIR "/scratch/eric/emerald/"
- #endif
-
- static char FILENAME[] = "/EC/OperationNames/Names";
-
- typedef unsigned int HashValue;
- #define MULTIPLIER 17
-
- static void CheckOutHashTable();
- static char *FileName;
- static HashValue Hash(s)
- register char *s;
- {
- register HashValue value = 0;
- while (*s) value = value * MULTIPLIER + *s++;
- return(value);
- }
-
- static struct {
- int hashTableSize;
- int hashTableSizeInBytes;
- int charTableMaxSize;
- int charTableCurrentSize;
- OID nextOIDToAllocate;
- OID maxOIDToAllocate;
- } header;
-
- typedef struct OpNameEntry {
- int offset; /* into charTable of the string */
- OID id; /* what we want */
- } HashTableEntry;
-
- static HashTableEntry *hashTable;
- static char *charTable;
-
- void ON_initialize()
- {
- int fd;
-
- FileName = (char *) malloc((unsigned)(strlen(EMDIR)+strlen(FILENAME)+1));
- (void) strcpy(FileName, EMDIR);
- (void) strcat(FileName, FILENAME);
- fd = open(FileName, O_RDONLY, 0);
- assert(fd != -1);
- if (read(fd, (char *)&header, sizeof(header)) != sizeof(header)) assert(0);
- charTable = (char *) malloc((unsigned)header.charTableMaxSize);
- hashTable = (HashTableEntry *) malloc((unsigned)header.hashTableSizeInBytes);
- if (read(fd, (char *)hashTable, header.hashTableSizeInBytes) !=
- header.hashTableSizeInBytes) assert (0);
- if (read(fd, charTable, header.charTableMaxSize) !=
- header.charTableMaxSize) assert(0);
- if (close (fd) != 0) assert(FALSE);
- #ifdef lint
- CheckOutHashTable();
- #endif
- }
-
- void ON_finalize()
- {
- int fd;
- fd = open(FileName, O_WRONLY, 0);
- assert (fd != -1);
- if (write(fd, (char *)&header, sizeof(header)) != sizeof(header)) assert(0);
- if (write(fd, (char *)hashTable, header.hashTableSizeInBytes) !=
- header.hashTableSizeInBytes) assert(0);
- if (write(fd, charTable, header.charTableMaxSize) !=
- header.charTableMaxSize) assert(0);
- if (close(fd) != 0) assert(FALSE);
- }
-
- OID AllocateOID()
- {
- assert (header.nextOIDToAllocate <= header.maxOIDToAllocate);
- return(header.nextOIDToAllocate++);
- }
-
- OID AllocateOIDS()
- {
- OID result;
- assert (header.nextOIDToAllocate < header.maxOIDToAllocate - OIDBLOCKSIZE);
- result = header.nextOIDToAllocate;
- header.nextOIDToAllocate += OIDBLOCKSIZE;
- return(result);
- }
-
- static int SaveString(s)
- register char *s;
- {
- int i = strlen(s), result;
- register char *t;
-
- if (header.charTableCurrentSize + i + 1 > header.charTableMaxSize) {
- t = (char *)malloc((unsigned)(2 * header.charTableMaxSize));
- bcopy(charTable, t, (unsigned)header.charTableCurrentSize);
- free(charTable);
- charTable = t;
- header.charTableMaxSize *= 2;
- }
- result = header.charTableCurrentSize;
- t = charTable + header.charTableCurrentSize;
- header.charTableCurrentSize += (i + 1);
- while (*s) *t++ = *s++;
- *t = '\0';
- return(result);
- }
-
- static void ExpandHashTable()
- {
- register HashTableEntry *nh, *oe, *ne;
- register int oldHashTableSize = header.hashTableSize, i;
- register HashValue hash;
- int index;
-
- header.hashTableSize *= 2;
- header.hashTableSizeInBytes *= 2;
- nh = (HashTableEntry *) malloc((unsigned)header.hashTableSizeInBytes);
- bzero((char *)nh, header.hashTableSizeInBytes);
- for (i = 0; i < oldHashTableSize; i++) {
- oe = &hashTable[i];
- hash = Hash(charTable + oe->offset);
- index = hash % header.hashTableSize;
- assert (index >= 0 && index < header.hashTableSize);
- while (1) {
- ne = &nh[index];
- if (ne->id == 0) {
- ne->id = oe->id;
- ne->offset = oe->offset;
- break;
- } else {
- index++;
- if (index >= header.hashTableSize) index = 0;
- }
- }
- }
- free((char *)hashTable);
- hashTable = nh;
- }
-
- OID ON_Translate(s)
- char *s;
- {
- register HashValue hash = Hash(s);
- int index = hash % header.hashTableSize;
- int firstIndex = index;
- register HashTableEntry *e;
-
- while (1) {
- e = &hashTable[index];
- if (e->id == 0) { /* we didn't find it */
- e->id = AllocateOID();
- e->offset = SaveString(s);
- return (e->id);
- } else if (!strcmp(s, charTable + e->offset)) {
- return (e->id);
- } else {
- index ++;
- if (index >= header.hashTableSize) index = 0;
- if (index == firstIndex) {
- ExpandHashTable();
- return(ON_Translate(s));
- }
- }
- }
- }
-
- static void CheckOutHashTable()
- {
- int i;
- HashValue hash;
- register HashTableEntry *realElement, *e;
- int index, firstIndex;
-
- for (i = 0; i < header.hashTableSize; i++) {
- realElement = &hashTable[i];
- if (realElement->id != 0) {
- hash = Hash(charTable + realElement->offset);
- index = hash % header.hashTableSize;
- firstIndex = index;
- while (1) {
- e = &hashTable[index];
- if (e->id == 0) { /* we didn't find it */
- break;
- } else if (!strcmp(charTable + realElement->offset,
- charTable + e->offset)) {
- break;
- } else {
- index++;
- if (index >= header.hashTableSize) index = 0;
- if (index == firstIndex) {
- index = -1;
- }
- }
- }
- fprintf(stderr,
- "Element \"%s\", hash %x, rightIndex %d, realIndex %d\n",
- charTable+realElement->offset, hash, firstIndex, index);
- }
- }
- }
-
- char *ON_Name(id)
- OID id;
- {
- register int i;
- register HashTableEntry *te;
- for (i = 0; i < header.hashTableSize; i++) {
- te = &hashTable[i];
- if (te->id == id) return (charTable + te->offset);
- }
- return(NULL);
- }
-