refactor: start using void* with trie

This commit is contained in:
Jef Roosens 2022-12-07 23:20:39 +01:00
parent 01eb5ece55
commit c99bc83015
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
8 changed files with 83 additions and 57 deletions

View file

@ -1,12 +1,15 @@
#ifndef AD3_TERNARYTRIE
#define AD3_TERNARYTRIE
#define ALPHABET_SIZE 256
#define DELIMITER '\0'
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
// Should not be higher than 254 or stuff will break
#define TRIE_MAX_SKIP_SIZE 8
// The compiler aligns structs to be multiple of 8 bytes. Therefore, we have a
// few "free" bytes that are allocated whether we use them or not, so we might
// as, hence the seemingly weird number. This is the largest number we can use
// where the size of TernaryTrieNode is still 32 bytes.
//
// NOTE: Should not be higher than 254 or stuff will break.
#define TRIE_MAX_SKIP_SIZE 13
/**
* The implementation of a Ternary Trie.
@ -17,6 +20,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
static const char charset[] =
@ -50,6 +54,8 @@ typedef enum trie_exit_code {
Entry *entry_new(EntryType type, const char *string);
void entry_free(Entry *entry);
/**
* Allocate & initialize a new trie, and populate it with the data from the
* given data file.
@ -73,7 +79,7 @@ void trie_free(Trie *trie);
* @param key key representing the entry
* @return 0 if the search was successful, 1 if not found
*/
TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key);
TrieExitCode trie_search(Trie *trie, void **data_ptr, const char *key);
/**
* Add a string to this trie.
@ -83,7 +89,7 @@ TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key);
* @param entry entry to add
* @return 0 if added, 1 if already in trie, something else if other errors
*/
TrieExitCode trie_add(Trie *trie, const char *key, Entry *entry);
TrieExitCode trie_add(Trie *trie, const char *key, void *data);
/**
* Add an entry by generating a random string as the key.
@ -94,7 +100,7 @@ TrieExitCode trie_add(Trie *trie, const char *key, Entry *entry);
* @return pointer to the generated key. This pointer is safe to use after
* unlocking the trie, and should be freed manually.
*/
TrieExitCode trie_add_random(Trie *trie, char **key_ptr, Entry *entry,
TrieExitCode trie_add_random(Trie *trie, char **key_ptr, void *data,
bool secure);
/**
@ -113,7 +119,7 @@ bool trie_remove(Trie *trie, const char *key);
* @param trie
* @return the number of entries in this trie
*/
size_t trie_size(Trie *trie);
uint64_t trie_size(Trie *trie);
/*
* Acquire a read lock on the trie.