From 56c891f9f87cebf0469334368dabe5d6b6e874ee Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 24 Mar 2018 20:40:42 -0700 Subject: [PATCH] Implement foreach for the trie. --- include/trie.h | 3 ++- src/trie.c | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/trie.h b/include/trie.h index 00863c2..57cfd01 100644 --- a/include/trie.h +++ b/include/trie.h @@ -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 diff --git a/src/trie.c b/src/trie.c index aba38e3..6040f03 100644 --- a/src/trie.c +++ b/src/trie.c @@ -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;