refactor: some small stuff

trie-skips
Jef Roosens 2022-12-03 13:27:34 +01:00
parent f9a5fc14e5
commit 2d89c5e80f
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 10 additions and 8 deletions

View File

@ -4,6 +4,8 @@
#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
/**

View File

@ -236,17 +236,19 @@ bool trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
offset = 0;
child_node_ptr = tnode_search(*node_ptr, string[i], true);
i++;
// We've reached a NULL child, so we add the remaining part of the string
// here
if (*child_node_ptr == NULL) {
child_node = tnode_init();
while (offset < TRIE_MAX_SKIP_SIZE &&
string[i + 1 + offset] != DELIMITER) {
string[i + offset] != DELIMITER) {
offset++;
}
memcpy(child_node->string, string + i + 1, offset);
memcpy(child_node->string, string + i, offset);
child_node->string_len = offset;
*child_node_ptr = child_node;
@ -255,9 +257,9 @@ bool trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
// allowed skip length, we continue through the loop. The next iteration
// will enter this if statement again, and perform the same loop, until
// the string is fully added to the trie.
if (string[i + 1 + offset] != DELIMITER) {
if (string[i + offset] != DELIMITER) {
node_ptr = child_node_ptr;
i += offset + 1;
i += offset;
continue;
}
@ -269,8 +271,6 @@ bool trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
return true;
}
i++;
while (offset < (*child_node_ptr)->string_len) {
// String no longer aligns with edge, so we have to split
if (string[i + offset] != (*child_node_ptr)->string[offset]) {

View File

@ -71,7 +71,7 @@ char** init_string_matrix(int count, int len) {
* @param size_func function to get the size of the given trie
* @return exit code describing failures, if any
*/
int fuzzy_test_trie_seed(FuzzyConfig conf, void* (*init_func) (), void (*free_func) (void*), bool (*add_func) (void*, char*), bool (*remove_func) (void*, char*), size_t (*size_func) (void*)) {
int fuzzy_test_trie_seed(FuzzyConfig conf, void* (*init_func) (), void (*free_func) (void*), bool (*add_func) (void*, char*, void*), bool (*remove_func) (void*, char*), size_t (*size_func) (void*)) {
srand(conf.seed);
char** matrix = init_string_matrix(conf.word_count, conf.word_length);
@ -110,7 +110,7 @@ int fuzzy_test_trie_seed(FuzzyConfig conf, void* (*init_func) (), void (*free_fu
// Add all strings to trie, checking for duplicates
for (int i = 0; i < conf.word_count; i++) {
changed = add_func(ct, matrix[i]);
changed = add_func(ct, matrix[i], NULL);
// if changed is false, *contains_dedupped[i] should be true, as changed
// can only be false if the string is already contained in the trie. if