HASH_NODE *hashInsert (HASH_TABLE *Table, char *text, int type, int lineNumber) { HASH_NODE *node; int address; // Check if the table is getting full and resize it if (Table->usedEntries > hash_i*HASH_SIZE/2) { hashResize(Table); } address = 0; node = hashFind(Table, text, type); // First check if it is in the hash if (node == 0) { address = hashAddress(Table, text); HASH_NODE *newNode; newNode = (HASH_NODE*)calloc(1, sizeof(HASH_NODE)); newNode -> type = type; newNode -> lineNumber = lineNumber; newNode -> text = (char*)calloc(sizeof(strlen(text)+1), sizeof(strlen(text)+1)); strcpy(newNode -> text, text); newNode -> next = Table -> node[address]; Table -> node[address] = newNode; Table -> usedEntries++; return newNode; } else { return node; } }
HASH_NODE *hashInsert(char *str, int type) { int address; HASH_NODE *newNode; // if it is a string or char, we should strip it before anything else if(type == LIT_STRING || type == LIT_CHAR) { str++; str[strlen(str) - 1] = '\0'; } // "str" is already on hashtable if((newNode = hashFind(str, type))) return newNode; // new node allocation if(!(newNode = (HASH_NODE *) malloc(sizeof(HASH_NODE))) || !(newNode->text = (char *) malloc(sizeof(char) * strlen(str)))) { fprintf(stderr, "Error: out of memory\n"); exit(1); // abort? } newNode->type = type; strncpy(newNode->text, str, strlen(str)); address = hashAddress(str); newNode->next = _symbolTable[address]; _symbolTable[address] = newNode; return newNode; }
HashNode* hashFind(char* key){ int address = hashAddress(key); HashNode * node = NULL; for(node = symbolTable[address]; node != NULL; node = node->next) if(strcmp(node->key, key) == 0) break; return node; }
HASH_NODE *hashFind(char *str, int type) { int address = hashAddress(str); HASH_NODE *node = _symbolTable[address]; while(node) { if(!strncmp(node->text, str, strlen(str)) && node->type == type) return node; node = node->next; } return NULL; }
Hash_Node* hashFind(char *text) { int address; address = hashAddress(text); Hash_Node *node = table[address]; while(node != NULL) { if (!strcmp(node->text,text)) return node; node = node->next; } return NULL; }
HashNode* hashInsert(char* key, int type){ HashNode* node; int address = hashAddress(key); if( (node = hashFind(key)) != NULL ) return node; node = (HashNode*) malloc(sizeof(HashNode)); node->key = (char*) malloc(strlen(key)); strcpy(node->key, key); node->type = type; node->next = symbolTable[address]; symbolTable[address] = node; return node; }
Hash_Node* hashInsert(char *text, int token) { int address; if (hashFind(text) == NULL) { address = hashAddress(text); Hash_Node *node = calloc(1,sizeof(Hash_Node)); node->text = calloc(strlen(text), sizeof(char)); strcpy(node->text,text); node->type = token; node->next = table[address]; table[address] = node; return node; } else return NULL; }
HASH_NODE *hashFind (HASH_TABLE *Table, char *text, int type) { int address; int i; HASH_NODE *pt; address = hashAddress(Table, text); for (i = 0; i < hash_i*HASH_SIZE; ++i) { for (pt = Table -> node[i]; pt; pt = pt -> next) { if (!strcmp(pt -> text, text)){ return pt; } } } return 0; }