trie: support preallocations

This commit is contained in:
Matheus
2026-07-12 22:40:58 -03:00
parent 3e7228dde1
commit 80399880d1
2 changed files with 84 additions and 15 deletions
+10
View File
@@ -1,10 +1,12 @@
#ifndef _TRIE_H
#define _TRIE_H
#include <stddef.h>
#include <stdint.h>
#define TRIE_BITS 6
#define TRIE_NODE_COUNT (1 << TRIE_BITS)
#define TRIE_MAX_DEPTH (2 * ((64 + TRIE_BITS - 1) / TRIE_BITS) - 1)
typedef struct trie_node trie_node_t;
struct trie_node {
@@ -15,6 +17,11 @@ struct trie_node {
};
};
typedef struct {
size_t allocation_count;
trie_node_t *allocations[TRIE_MAX_DEPTH];
} trie_preallocation_t;
typedef struct {
uint8_t height;
union {
@@ -26,7 +33,10 @@ typedef struct {
typedef void (trie_iterate_fn_t)(void *item);
void trie_init(trie_t *trie);
trie_preallocation_t *trie_preallocate(void);
int trie_insert(trie_t *trie, uint64_t key, void *value);
int trie_insert_preallocated(trie_t *trie, uint64_t key, void *value, trie_preallocation_t *preallocation);
void trie_free_preallocation(trie_preallocation_t *preallocation);
int trie_lookup(trie_t *trie, uint64_t key, void **ret);
int trie_remove(trie_t *trie, uint64_t key);
void trie_truncate(trie_t *trie, uint64_t max_key);