Basic working version without persistent storage
This commit is contained in:
parent
cae62ce7d2
commit
a2fcbb4224
8 changed files with 83 additions and 25 deletions
|
|
@ -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++;
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue