feat: allow skips up to 8 characters long

This commit is contained in:
Jef Roosens 2022-11-29 15:08:07 +01:00
parent 4bcdd5c4d9
commit 88ea0db2ee
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
4 changed files with 224 additions and 196 deletions

View file

@ -8,10 +8,10 @@
* Represents a node of the binary tree contained within each non-leaf
* TrieNode.
*/
typedef struct ttinode {
struct ttinode *left;
struct ttinode *right;
struct ttnode *next;
typedef struct tinode {
struct tinode *left;
struct tinode *right;
struct tnode *next;
char key;
} TrieInnerNode;
@ -26,25 +26,21 @@ typedef struct ttinode {
* to be stored as a single node. Its size will be zero, represents its true,
* and its string pointer is initialized.
*/
typedef struct ttnode {
union {
TrieInnerNode *root;
char *string;
} ptr;
typedef struct tnode {
Entry *entry;
// What type of node this is
// 0: regular non-representing node
// 1: regular representing node
// 2: full leaf
uint8_t type;
// Dependent on type
// 0, 1: size of underlying binary tree
// 2: length of string
uint8_t size;
TrieInnerNode* tree;
uint8_t tree_size;
// Skips are at most 8 characters, and are stored in the nodes
char string[TRIE_MAX_SKIP_SIZE];
uint8_t string_len: 4;
bool represents : 1;
} TrieNode;
// Required for recursively freeing tree structure
void ttnode_free(TrieNode *node);
void tnode_free(TrieNode *node);
/**
* Allocate and initialize a new TrieInnerNode representing a given
@ -53,7 +49,7 @@ void ttnode_free(TrieNode *node);
* @param c character to represent
* @return pointer to newly allocated struct
*/
TrieInnerNode *ttinode_init(char c) {
TrieInnerNode *tinode_init(char c) {
TrieInnerNode *node = calloc(1, sizeof(TrieInnerNode));
node->key = c;
@ -65,7 +61,15 @@ TrieInnerNode *ttinode_init(char c) {
*
* @return pointer to newly allocated struct
*/
TrieNode *ttnode_init() { return calloc(1, sizeof(TrieNode)); }
TrieNode *tnode_init() {
TrieNode *node = malloc(sizeof(TrieNode));
node->tree_size = 0;
node->string_len = 0;
node->represents = false;
return node;
}
/**
* Free a TrieInnerNode and its underlying tree structure. This should
@ -74,17 +78,17 @@ TrieNode *ttnode_init() { return calloc(1, sizeof(TrieNode)); }
*
* @param node node whose tree to free
*/
void ttinode_free_cascade(TrieInnerNode *node) {
void tinode_free_cascade(TrieInnerNode *node) {
if (node->left != NULL) {
ttinode_free_cascade(node->left);
tinode_free_cascade(node->left);
}
if (node->right != NULL) {
ttinode_free_cascade(node->right);
tinode_free_cascade(node->right);
}
if (node->next != NULL) {
ttnode_free(node->next);
tnode_free(node->next);
}
free(node);
@ -95,11 +99,9 @@ void ttinode_free_cascade(TrieInnerNode *node) {
*
* @param node node to free
*/
void ttnode_free(TrieNode *node) {
if (node->type == 2) {
free(node->ptr.string);
} else if (node->size != 0) {
ttinode_free_cascade(node->ptr.root);
void tnode_free(TrieNode *node) {
if (node->tree_size > 0) {
tinode_free_cascade(node->tree);
}
// TODO properly free entry
@ -110,18 +112,6 @@ void ttnode_free(TrieNode *node) {
free(node);
}
/**
* Add the string to the given node & set its type accordingely.
*
* @param node node to add string to
* @param string string to add
*/
void ttnode_set_string(TrieNode *node, const char *string) {
node->type = 2;
node->size = strlen(string);
node->ptr.string = strdup(string);
}
/**
* This function performs a lookup in the underlying binary tree of the given
* TrieNode. If found, the return value is a pointer to the memory
@ -140,26 +130,21 @@ void ttnode_set_string(TrieNode *node, const char *string) {
* node represents a leaf with a string, because the struct and therefore the
* address is created if it doesn't exist yet.
*/
TrieNode **ttnode_search(TrieNode *node, const char c,
TrieNode **tnode_search(TrieNode *node, const char c,
bool create) {
// Full leafs will always return NULL
if (node->type == 2) {
return NULL;
}
// It can happen that the node has no initialized root yet
if (node->size == 0) {
if (node->tree_size == 0) {
if (create) {
node->size++;
node->ptr.root = ttinode_init(c);
node->tree_size++;
node->tree = tinode_init(c);
return &node->ptr.root->next;
return &node->tree->next;
}
return NULL;
}
TrieInnerNode *parent = node->ptr.root;
TrieInnerNode *parent = node->tree;
TrieInnerNode *child;
// Iterate through the tree until we either find the character or realize it's
@ -186,7 +171,7 @@ TrieNode **ttnode_search(TrieNode *node, const char c,
// If create is true, we create the new node so that we can still return a
// non-NULL pointer.
if (create) {
TrieInnerNode *new_node = ttinode_init(c);
TrieInnerNode *new_node = tinode_init(c);
if (c < parent->key) {
parent->left = new_node;
@ -194,7 +179,7 @@ TrieNode **ttnode_search(TrieNode *node, const char c,
parent->right = new_node;
}
node->size++;
node->tree_size++;
return &new_node->next;
}
@ -208,37 +193,37 @@ TrieNode **ttnode_search(TrieNode *node, const char c,
*
* @param node node to split
*/
void ttnode_split(TrieNode *node) {
TrieNode *new_node = ttnode_init();
char key = node->ptr.string[0];
/* void tnode_split(TrieNode *node) { */
/* TrieNode *new_node = tnode_init(); */
/* char key = node->ptr.string[0]; */
// There's a chance the remaining string was only 1 character, meaning the new
// node doesn't have to store a string
if (node->ptr.string[1] != DELIMITER) {
ttnode_set_string(new_node, node->ptr.string + 1);
} else {
new_node->type = 1;
}
/* // There's a chance the remaining string was only 1 character, meaning the new */
/* // node doesn't have to store a string */
/* if (node->ptr.string[1] != DELIMITER) { */
/* tnode_set_string(new_node, node->ptr.string + 1); */
/* } else { */
/* new_node->type = 1; */
/* } */
new_node->entry = node->entry;
/* new_node->entry = node->entry; */
node->type = 0;
node->size = 0;
node->entry = NULL;
/* node->type = 0; */
/* node->size = 0; */
/* node->entry = NULL; */
free(node->ptr.string);
node->ptr.string = NULL;
/* free(node->ptr.string); */
/* node->ptr.string = NULL; */
// Initialize node's binary tree with the correct character
TrieNode **node_ptr = ttnode_search(node, key, true);
*node_ptr = new_node;
}
/* // Initialize node's binary tree with the correct character */
/* TrieNode **node_ptr = tnode_search(node, key, true); */
/* *node_ptr = new_node; */
/* } */
/*
* Remove the given character from a TrieInnerNode's subtree. The
* function assumes the character is indeed in the subtree.
*/
void ttinode_remove(TrieInnerNode *node, const char c) {
void tinode_remove(TrieInnerNode *node, const char c) {
TrieInnerNode **to_remove_ptr = &node;
// We use pointers to pointers here so we can later free the removed node
@ -311,12 +296,8 @@ void ttinode_remove(TrieInnerNode *node, const char c) {
* @param node node to remove character from
* @param c character to remove
*/
void ttnode_remove(TrieNode *node, const char c) {
ttinode_remove(node->ptr.root, c);
void tnode_remove(TrieNode *node, const char c) {
tinode_remove(node->tree, c);
node->size--;
if (node->size == 0) {
node->ptr.root = NULL;
}
node->tree_size--;
}