diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2025-02-05 13:29:05 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2025-02-06 10:38:45 -0500 |
commit | a85565334708840dd44b849a91c1fa09b9038093 (patch) | |
tree | 29599824ab1f5ef02713e558bc68c7f3388c32c8 /src/trie.c | |
parent | f8aca1c316b116c2de11d42010fdda0ddb418750 (diff) | |
download | bfs-a85565334708840dd44b849a91c1fa09b9038093.tar.xz |
trie: New trie_{get,set}_{str,mem}() functions
Diffstat (limited to 'src/trie.c')
-rw-r--r-- | src/trie.c | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -240,6 +240,16 @@ struct trie_leaf *trie_find_mem(const struct trie *trie, const void *key, size_t return trie_find_mem_impl(trie, key, length); } +void *trie_get_str(const struct trie *trie, const char *key) { + const struct trie_leaf *leaf = trie_find_str(trie, key); + return leaf ? leaf->value : NULL; +} + +void *trie_get_mem(const struct trie *trie, const void *key, size_t length) { + const struct trie_leaf *leaf = trie_find_mem(trie, key, length); + return leaf ? leaf->value : NULL; +} + _trie_clones static struct trie_leaf *trie_find_postfix_impl(const struct trie *trie, const char *key) { size_t length = strlen(key); @@ -626,6 +636,26 @@ struct trie_leaf *trie_insert_mem(struct trie *trie, const void *key, size_t len return trie_insert_mem_impl(trie, key, length); } +int trie_set_str(struct trie *trie, const char *key, const void *value) { + struct trie_leaf *leaf = trie_insert_str(trie, key); + if (leaf) { + leaf->value = (void *)value; + return 0; + } else { + return -1; + } +} + +int trie_set_mem(struct trie *trie, const void *key, size_t length, const void *value) { + struct trie_leaf *leaf = trie_insert_mem(trie, key, length); + if (leaf) { + leaf->value = (void *)value; + return 0; + } else { + return -1; + } +} + /** Free a chain of singleton nodes. */ static void trie_free_singletons(struct trie *trie, uintptr_t ptr) { while (trie_is_node(ptr)) { |