Rename htab functions; rewrite find to be generic.

The htab functions find_str and add_str have been renamed to include _ref in
the places they previously noted _str. These are intended to manage an htab
containing references to any data; string or not.

The htab_find function has been divided up. htab_find_core now handles actually
finding the correct node. htab_find and htab_find_ref are just outward facing
functions for retrieving a specific kind of data from the node, depending on
what the htab is used for.
This commit is contained in:
Payton Turnage 2014-01-14 19:09:35 -05:00
parent 27a090e704
commit ae63e45c65
4 changed files with 31 additions and 25 deletions

View file

@ -23,8 +23,8 @@ tvm_htab_t* htab_create();
void htab_destroy(tvm_htab_t *htab);
int htab_add(tvm_htab_t *htab, const char *key, int value);
int htab_add_str(tvm_htab_t *htab, const char *key, const void *valptr, int len);
int htab_add_ref(tvm_htab_t *htab, const char *key, const void *valptr, int len);
int htab_find(tvm_htab_t *htab, const char *key);
char *htab_find_str(tvm_htab_t *htab, const char *key);
char *htab_find_ref(tvm_htab_t *htab, const char *key);
#endif

View file

@ -69,7 +69,7 @@ static void htab_rehash(tvm_htab_t *orig, unsigned int size)
next = node->next;
if (node->valptr)
{
htab_add_str(new, node->key, node->valptr, strlen(node->valptr) + 1);
htab_add_ref(new, node->key, node->valptr, strlen(node->valptr) + 1);
free(node->valptr);
}
else
@ -124,7 +124,7 @@ int htab_add(tvm_htab_t *htab, const char *k, int v)
return hash;
}
int htab_add_str(tvm_htab_t *htab, const char *key, const void *valptr, int len)
int htab_add_ref(tvm_htab_t *htab, const char *key, const void *valptr, int len)
{
int hash = htab_add(htab, key, 0);
int found = 0;
@ -143,32 +143,38 @@ int htab_add_str(tvm_htab_t *htab, const char *key, const void *valptr, int len)
return hash;
}
int htab_find(tvm_htab_t *htab, const char *key)
static tvm_htab_node_t *htab_find_core(tvm_htab_t *htab, const char *key)
{
int hash = htab_hash(key, htab->size);
tvm_htab_node_t *node = htab->nodes[hash];
while(node)
while (node)
{
if(!strcmp(node->key, key))
return node->value;
node = node->next;
}
return -1;
}
char *htab_find_str(tvm_htab_t *htab, const char *key)
{
int hash = htab_hash(key, htab->size);
tvm_htab_node_t *node = htab->nodes[hash];
while(node)
{
if(!strcmp(node->key, key))
return node->valptr;
node = node->next;
return node;
else
node = node->next;
}
return NULL;
}
int htab_find(tvm_htab_t *htab, const char *key)
{
tvm_htab_node_t *node = htab_find_core(htab, key);
if(!node)
return -1;
return node->value;
}
char *htab_find_ref(tvm_htab_t *htab, const char *key)
{
tvm_htab_node_t *node = htab_find_core(htab, key);
if (!node)
return NULL;
return node->valptr;
}

View file

@ -60,7 +60,7 @@ void lex(tvm_lexer_t *lexer, char *source, tvm_htab_t *defines)
for(j = 0; (pToken && j < MAX_TOKENS); j++)
{
char *token = htab_find_str(defines, pToken);
char *token = htab_find_ref(defines, pToken);
token = token ? token : pToken;
lexer->tokens[i][j] = (char *)calloc(1, (strlen(token) + 1));

View file

@ -86,7 +86,7 @@ int tvm_preprocess(char *src, int *src_len, tvm_htab_t *defines)
}
if(htab_find(defines, keystr) < 0)
htab_add_str(defines, keystr, valstr, strlen(valstr) + 1);
htab_add_ref(defines, keystr, valstr, strlen(valstr) + 1);
else
{
printf("Multiple definitions for %s.\n", keystr);