Basic working version without persistent storage

This commit is contained in:
Jef Roosens 2022-11-15 21:12:08 +01:00
parent cae62ce7d2
commit a2fcbb4224
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
8 changed files with 83 additions and 25 deletions

View file

@ -3,10 +3,10 @@ project(ternarytrie C)
set(CMAKE_C_STANDARD 17)
add_library(ternarytrie STATIC src/ternarytrie.c)
target_include_directories(
ternarytrie PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
target_compile_options(ternarytrie PRIVATE -O3 -flto)

View file

@ -38,7 +38,7 @@ void ternarytrie_free(TernaryTrie* trie);
* @param string
* @return true if the string is contained within this trie, false otherwise
*/
bool ternarytrie_search(TernaryTrie* trie, const char* string);
char * ternarytrie_search(TernaryTrie* trie, const char* string);
/**
* Add a string to this trie.
@ -47,7 +47,7 @@ bool ternarytrie_search(TernaryTrie* trie, const char* string);
* @param string
* @return true if the trie was changed by this operation, false if it was already present
*/
bool ternarytrie_add(TernaryTrie* trie, const char* string);
bool ternarytrie_add(TernaryTrie* trie, const char* string, const char *payload);
/**
* Remove a string from this trie.

View file

@ -94,10 +94,14 @@ SearchResult ternarytrie_search_node(TernaryTrie *trie, const char *string) {
* @param string string to look up
* @return true if the string is present in the trie, false otherwise
*/
bool ternarytrie_search(TernaryTrie *trie, const char *string) {
char * ternarytrie_search(TernaryTrie *trie, const char *string) {
SearchResult res = ternarytrie_search_node(trie, string);
return res.child != NULL;
if (res.child != NULL) {
return res.child->payload;
}
return NULL;
}
/**
@ -108,11 +112,12 @@ bool ternarytrie_search(TernaryTrie *trie, const char *string) {
* @return true if the string wasn't present in the trie and thus added, false
* otherwise
*/
bool ternarytrie_add(TernaryTrie *trie, const char *string) {
bool ternarytrie_add(TernaryTrie *trie, const char *string, const char *payload) {
// Edge case for empty string
if (string[0] == DELIMITER) {
if (trie->root->type == 0) {
trie->root->type = 1;
trie->root->payload = my_strdup(payload);
trie->size++;
return true;
@ -159,6 +164,8 @@ bool ternarytrie_add(TernaryTrie *trie, const char *string) {
new_node->type = 1;
}
new_node->payload = my_strdup(payload);
*node_ptr = new_node;
trie->size++;
@ -181,6 +188,7 @@ bool ternarytrie_add(TernaryTrie *trie, const char *string) {
}
(*node_ptr)->type = 1;
(*node_ptr)->payload = my_strdup(payload);
trie->size++;

View file

@ -30,6 +30,7 @@ typedef struct ttnode {
TernaryTrieInnerNode *root;
char *string;
} ptr;
char *payload;
// What type of node this is
// 0: regular non-representing node
// 1: regular representing node
@ -100,6 +101,10 @@ void ttnode_free(TernaryTrieNode *node) {
ttinode_free_cascade(node->ptr.root);
}
if (node->payload != NULL) {
free(node->payload);
}
free(node);
}
@ -213,8 +218,11 @@ void ttnode_split(TernaryTrieNode *node) {
new_node->type = 1;
}
new_node->payload = node->payload;
node->type = 0;
node->size = 0;
node->payload = NULL;
free(node->ptr.string);
node->ptr.string = NULL;