Implement foreach for the trie.

This commit is contained in:
Danila Fedorin 2018-03-24 20:40:42 -07:00
parent 3f2859a119
commit 56c891f9f8
2 changed files with 20 additions and 2 deletions

View File

@ -57,7 +57,8 @@ typedef struct libab_trie_node_s libab_trie_node;
void libab_trie_init(libab_trie* trie);
libab_result libab_trie_put(libab_trie* trie, const char* key, void* value);
const ll* libab_trie_get(libab_trie* trie, const char* key);
const ll* libab_trie_get(const libab_trie* trie, const char* key);
int libab_trie_foreach(const libab_trie* trie, void* data, compare_func compare, foreach_func foreach);
void libab_trie_free(libab_trie* trie);
#endif

View File

@ -68,7 +68,7 @@ libab_result libab_trie_put(libab_trie* trie, const char* key, void* value) {
return result;
}
const ll* libab_trie_get(libab_trie* trie, const char* key) {
const ll* libab_trie_get(const libab_trie* trie, const char* key) {
libab_trie_node* current = trie->head;
while(current && *key) {
while(current && current->key != *key) {
@ -86,6 +86,23 @@ const ll* libab_trie_get(libab_trie* trie, const char* key) {
return &trie->empty_list;
}
int _libab_trie_foreach(libab_trie_node* node, void* data, compare_func compare, foreach_func foreach) {
int return_code;
if(node == NULL) return 0;
return_code = ll_foreach(&node->values, data, compare, foreach);
if(return_code == 0) {
return_code = _libab_trie_foreach(node->child, data, compare, foreach);
}
if(return_code == 0) {
return_code = _libab_trie_foreach(node->next, data, compare, foreach);
}
return return_code;
}
int libab_trie_foreach(const libab_trie* trie, void* data, compare_func compare, foreach_func foreach) {
return _libab_trie_foreach(trie->head, data, compare, foreach);
}
void libab_trie_free(libab_trie* trie) {
_libab_trie_free(trie->head);
trie->head = NULL;