Much faster to search and add to the string pool as a binary tree

This commit is contained in:
Eric Fischer
2014-09-22 22:31:55 -07:00
parent d7cdbec980
commit 581105dc9a
3 changed files with 32 additions and 7 deletions
+23 -6
View File
@@ -139,25 +139,42 @@ struct pool_val *pool(struct pool *p, char *s, int type) {
if (cmp == 0) {
return *v;
} else if (cmp < 0) {
v = &((*v)->left);
} else {
v = &((*v)->right);
}
v = &((*v)->next);
}
*v = malloc(sizeof(struct pool_val));
(*v)->left = NULL;
(*v)->right = NULL;
(*v)->next = NULL;
(*v)->s = s;
(*v)->type = type;
(*v)->n = p->n++;
if (p->tail != NULL) {
p->tail->next = *v;
}
p->tail = *v;
if (p->head == NULL) {
p->head = *v;
}
return *v;
}
void pool_free(struct pool *p) {
while (p->vals != NULL) {
struct pool_val *next = p->vals->next;
free(p->vals);
p->vals = next;
while (p->head != NULL) {
struct pool_val *next = p->head->next;
free(p->head);
p->head = next;
}
p->head = NULL;
p->tail = NULL;
p->vals = NULL;
}