From 7b482d64e934bb5828ed734260f82f79e5e87651 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 6 Mar 2019 21:04:33 -0800 Subject: trie: Add a function to get an arbitrary leaf This is useful if the stored values need to be cleaned up. --- trie.c | 4 ++++ trie.h | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/trie.c b/trie.c index ff0e7be..c95dbfa 100644 --- a/trie.c +++ b/trie.c @@ -218,6 +218,10 @@ static struct trie_leaf *trie_representative(const struct trie *trie, const void return trie_decode_leaf(ptr); } +struct trie_leaf *trie_first_leaf(const struct trie *trie) { + return trie_representative(trie, NULL, 0); +} + struct trie_leaf *trie_find_str(const struct trie *trie, const char *key) { return trie_find_mem(trie, key, strlen(key) + 1); } diff --git a/trie.h b/trie.h index fcdd630..48f69cc 100644 --- a/trie.h +++ b/trie.h @@ -53,6 +53,16 @@ struct trie_leaf { */ void trie_init(struct trie *trie); +/** + * Get the first (lexicographically earliest) leaf in the trie. + * + * @param trie + * The trie to search. + * @return + * The first leaf, or NULL if the trie is empty. + */ +struct trie_leaf *trie_first_leaf(const struct trie *trie); + /** * Find the leaf for a string key. * -- cgit v1.2.3