Enable keeping track of associated values.
The tvm_tree structure should optionally be able to keep track of values associated with the strings by which its nodes are sorted. In the case of defines, this is the replacement string. In the case of variables, this will be a pointer to the variable's location in memory. Searching should return the value, or NULL. To opt out of storing a value, pass NULL and 0 as the val and len arguments.
This commit is contained in:
parent
62066d614f
commit
58f11eba57
2 changed files with 28 additions and 19 deletions
|
|
@ -3,14 +3,15 @@
|
|||
|
||||
typedef struct tvm_tree_t
|
||||
{
|
||||
char *str;
|
||||
char *keystr;
|
||||
char *val;
|
||||
struct tvm_tree_t *left;
|
||||
struct tvm_tree_t *right;
|
||||
} tvm_tree_t;
|
||||
|
||||
tvm_tree_t *tvm_tree_create(tvm_tree_t *node, const char *str);
|
||||
int tvm_tree_add(tvm_tree_t *node, const char *str);
|
||||
int tvm_tree_find(tvm_tree_t *node, const char *str);
|
||||
tvm_tree_t *tvm_tree_create(tvm_tree_t *node, const char *str, const void *val, int len);
|
||||
int tvm_tree_add(tvm_tree_t *node, const char *str, const void *val, int len);
|
||||
void *tvm_tree_find(tvm_tree_t *node, const char *str);
|
||||
int tvm_tree_destroy(tvm_tree_t *node);
|
||||
|
||||
#endif
|
||||
|
|
@ -3,17 +3,24 @@
|
|||
|
||||
#include <tvm/tvm_tree.h>
|
||||
|
||||
tvm_tree_t *tvm_tree_create(tvm_tree_t *node, const char *str)
|
||||
tvm_tree_t *tvm_tree_create(tvm_tree_t *node, const char *str, const void *val, int len)
|
||||
{
|
||||
int len = strlen(str);
|
||||
node = calloc(1, sizeof(tvm_tree_t) + len + 1);
|
||||
node->str = (char *)node + sizeof(tvm_tree_t);
|
||||
memcpy(node->str, str, len);
|
||||
int keylen = strlen(str);
|
||||
node = calloc(1, sizeof(tvm_tree_t) + len + keylen + 1);
|
||||
|
||||
if(val && len)
|
||||
{
|
||||
node->val = (char *)node + sizeof(tvm_tree_t);
|
||||
memcpy(node->val, val, len);
|
||||
}
|
||||
|
||||
node->keystr = (char *)node + sizeof(tvm_tree_t) + len;
|
||||
memcpy(node->keystr, str, keylen);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
int tvm_tree_add(tvm_tree_t *node, const char *str)
|
||||
int tvm_tree_add(tvm_tree_t *node, const char *str, const void *val, int len)
|
||||
{
|
||||
if (!str || !node)
|
||||
return 1;
|
||||
|
|
@ -22,7 +29,7 @@ int tvm_tree_add(tvm_tree_t *node, const char *str)
|
|||
tvm_tree_t *current = node;
|
||||
while(!done)
|
||||
{
|
||||
diff = strcmp(current->str, str);
|
||||
diff = strcmp(current->keystr, str);
|
||||
if(!diff)
|
||||
done = 1;
|
||||
else if(diff < 0)
|
||||
|
|
@ -33,7 +40,7 @@ int tvm_tree_add(tvm_tree_t *node, const char *str)
|
|||
}
|
||||
else
|
||||
{
|
||||
current->left = tvm_tree_create(NULL, str);
|
||||
current->left = tvm_tree_create(NULL, str, val, len);
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -45,7 +52,7 @@ int tvm_tree_add(tvm_tree_t *node, const char *str)
|
|||
}
|
||||
else
|
||||
{
|
||||
current->right = tvm_tree_create(NULL, str);
|
||||
current->right = tvm_tree_create(NULL, str, val, len);
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -58,25 +65,26 @@ int tvm_tree_add(tvm_tree_t *node, const char *str)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int tvm_tree_find(tvm_tree_t *node, const char *str)
|
||||
void *tvm_tree_find(tvm_tree_t *node, const char *str)
|
||||
{
|
||||
if(!node || !str)
|
||||
return 0;
|
||||
|
||||
int diff, found = 0;
|
||||
int diff;
|
||||
void *value = NULL;
|
||||
tvm_tree_t *current = node;
|
||||
while(current && !found)
|
||||
while(current && !value)
|
||||
{
|
||||
diff = strcmp(current->str, str);
|
||||
diff = strcmp(current->keystr, str);
|
||||
if(!diff)
|
||||
found = 1;
|
||||
value = current->val;
|
||||
else if(diff < 0)
|
||||
current = current->left;
|
||||
else if(diff > 0)
|
||||
current = current->right;
|
||||
}
|
||||
|
||||
return found;
|
||||
return value;
|
||||
}
|
||||
|
||||
void tvm_destroy(tvm_tree_t *node)
|
||||
|
|
|
|||
Loading…
Reference in a new issue