2018-04-06 23:55:56 -07:00
|
|
|
#include "ref_trie.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
void libab_ref_trie_init(libab_ref_trie* trie) { trie->head = NULL; }
|
2018-04-06 23:55:56 -07:00
|
|
|
|
2018-05-11 14:40:01 -07:00
|
|
|
void _libab_ref_trie_free(libab_ref_trie_node* node) {
|
|
|
|
if (node == NULL)
|
|
|
|
return;
|
|
|
|
_libab_ref_trie_free(node->next);
|
|
|
|
_libab_ref_trie_free(node->child);
|
|
|
|
libab_ref_free(&node->ref);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
libab_result _libab_ref_trie_copy(const libab_ref_trie_node* copy_of,
|
|
|
|
libab_ref_trie_node** copy_into) {
|
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (copy_of == NULL) {
|
2018-05-11 14:40:01 -07:00
|
|
|
*copy_into = NULL;
|
2018-05-17 14:53:48 -07:00
|
|
|
} else if (((*copy_into) = malloc(sizeof(**copy_into)))) {
|
2018-05-11 14:40:01 -07:00
|
|
|
(*copy_into)->child = NULL;
|
|
|
|
(*copy_into)->next = NULL;
|
|
|
|
|
|
|
|
result = _libab_ref_trie_copy(copy_of->next, &(*copy_into)->next);
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-05-11 14:40:01 -07:00
|
|
|
result = _libab_ref_trie_copy(copy_of->child, &(*copy_into)->child);
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-05-11 14:40:01 -07:00
|
|
|
(*copy_into)->key = copy_of->key;
|
|
|
|
libab_ref_copy(©_of->ref, &(*copy_into)->ref);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = LIBAB_MALLOC;
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result != LIBAB_SUCCESS && *copy_into) {
|
2018-05-11 14:40:01 -07:00
|
|
|
_libab_ref_trie_free((*copy_into)->next);
|
|
|
|
_libab_ref_trie_free((*copy_into)->child);
|
|
|
|
free(*copy_into);
|
|
|
|
*copy_into = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_result libab_ref_trie_init_copy(libab_ref_trie* trie,
|
2018-05-11 14:40:01 -07:00
|
|
|
const libab_ref_trie* copy_of) {
|
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
|
|
|
|
libab_ref_trie_init(trie);
|
|
|
|
result = _libab_ref_trie_copy(copy_of->head, &trie->head);
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result != LIBAB_SUCCESS) {
|
2018-05-11 14:40:01 -07:00
|
|
|
libab_ref_trie_free(trie);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_result _libab_ref_trie_put(libab_ref_trie_node** node, const char* key,
|
|
|
|
libab_ref* ref) {
|
2018-04-06 23:55:56 -07:00
|
|
|
libab_result result = LIBAB_SUCCESS;
|
2018-04-21 14:09:01 -07:00
|
|
|
if ((*node = malloc(sizeof(**node)))) {
|
2018-04-06 23:55:56 -07:00
|
|
|
(*node)->key = *key;
|
|
|
|
(*node)->next = NULL;
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (*(key + 1)) {
|
2018-04-06 23:55:56 -07:00
|
|
|
libab_ref_null(&(*node)->ref);
|
|
|
|
result = _libab_ref_trie_put(&(*node)->child, key + 1, ref);
|
|
|
|
} else {
|
|
|
|
libab_ref_copy(ref, &(*node)->ref);
|
|
|
|
(*node)->child = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = LIBAB_MALLOC;
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (result != LIBAB_SUCCESS) {
|
|
|
|
if (*node)
|
|
|
|
libab_ref_free(&(*node)->ref);
|
2018-04-06 23:55:56 -07:00
|
|
|
free(*node);
|
|
|
|
*node = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_result libab_ref_trie_put(libab_ref_trie* trie, const char* key,
|
|
|
|
libab_ref* ref) {
|
2018-04-06 23:55:56 -07:00
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
libab_ref_trie_node** current = &trie->head;
|
|
|
|
char search;
|
2018-04-21 14:09:01 -07:00
|
|
|
while (*key) {
|
2018-04-06 23:55:56 -07:00
|
|
|
search = *key;
|
2018-04-21 14:09:01 -07:00
|
|
|
while (*current && (*current)->key != search) {
|
2018-04-06 23:55:56 -07:00
|
|
|
current = &(*current)->next;
|
|
|
|
}
|
2018-04-21 14:09:01 -07:00
|
|
|
if (*current) {
|
|
|
|
if (*(key + 1)) {
|
2018-04-06 23:55:56 -07:00
|
|
|
current = &(*current)->child;
|
|
|
|
} else {
|
|
|
|
libab_ref_free(&(*current)->ref);
|
|
|
|
libab_ref_copy(ref, &(*current)->ref);
|
|
|
|
}
|
|
|
|
key++;
|
|
|
|
} else {
|
|
|
|
result = _libab_ref_trie_put(current, key, ref);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
void libab_ref_trie_get(const libab_ref_trie* trie, const char* key,
|
|
|
|
libab_ref* into) {
|
2018-04-06 23:55:56 -07:00
|
|
|
libab_ref_trie_node* current = trie->head;
|
2018-04-21 14:09:01 -07:00
|
|
|
while (current && *key) {
|
|
|
|
while (current && current->key != *key) {
|
2018-04-06 23:55:56 -07:00
|
|
|
current = current->next;
|
|
|
|
}
|
2018-04-21 14:09:01 -07:00
|
|
|
if (current == NULL)
|
|
|
|
break;
|
2018-04-06 23:55:56 -07:00
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (*(key + 1)) {
|
2018-04-06 23:55:56 -07:00
|
|
|
current = current->child;
|
|
|
|
key++;
|
|
|
|
} else {
|
2018-05-14 18:34:30 -07:00
|
|
|
libab_ref_copy(¤t->ref, into);
|
|
|
|
return;
|
2018-04-06 23:55:56 -07:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 18:34:30 -07:00
|
|
|
libab_ref_null(into);
|
2018-04-06 23:55:56 -07:00
|
|
|
}
|
|
|
|
|
2018-06-16 20:51:54 -07:00
|
|
|
libab_result _ref_trie_foreach(libab_ref_trie_node* node,
|
|
|
|
char** str, size_t* str_size, size_t depth,
|
|
|
|
libab_result (*func)(const libab_ref*, const char*, va_list),
|
|
|
|
va_list args) {
|
|
|
|
va_list args_copy;
|
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
if(node == NULL) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(depth + 1 >= *str_size) {
|
|
|
|
char* new_str = realloc(*str, (*str_size) *= 2);
|
|
|
|
if(new_str) {
|
|
|
|
*str = new_str;
|
|
|
|
} else {
|
|
|
|
result = LIBAB_MALLOC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == LIBAB_SUCCESS) {
|
|
|
|
(*str)[depth] = node->key;
|
|
|
|
(*str)[depth + 1] = '\0';
|
|
|
|
|
|
|
|
if(libab_ref_get(&node->ref)) {
|
|
|
|
va_copy(args_copy, args);
|
|
|
|
result = func(&node->ref, (*str), args_copy);
|
|
|
|
va_end(args_copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result == LIBAB_SUCCESS)
|
|
|
|
result = _ref_trie_foreach(node->child, str, str_size, depth + 1, func, args);
|
|
|
|
if(result == LIBAB_SUCCESS)
|
|
|
|
result = _ref_trie_foreach(node->next, str, str_size, depth, func, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
libab_result libab_ref_trie_foreach(const libab_ref_trie* trie,
|
|
|
|
libab_result (*func)(const libab_ref*, const char*, va_list),
|
|
|
|
...) {
|
|
|
|
va_list args;
|
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
char* str;
|
|
|
|
size_t string_size = 4;
|
|
|
|
|
|
|
|
if((str = malloc(sizeof(*str) * 4))) {
|
|
|
|
va_start(args, func);
|
|
|
|
result = _ref_trie_foreach(trie->head, &str, &string_size, 0, func, args);
|
|
|
|
va_end(args);
|
|
|
|
} else {
|
|
|
|
result = LIBAB_MALLOC;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-06 23:55:56 -07:00
|
|
|
void libab_ref_trie_free(libab_ref_trie* trie) {
|
|
|
|
_libab_ref_trie_free(trie->head);
|
|
|
|
}
|