From 2d89c5e80f3b3adc7f22eec1b6fc2b0962d685c8 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 3 Dec 2022 13:27:34 +0100 Subject: [PATCH] refactor: some small stuff --- include/trie.h | 2 ++ src/trie/trie.c | 12 ++++++------ test/fuzzy.h | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/trie.h b/include/trie.h index d235d3f..0bcf49b 100644 --- a/include/trie.h +++ b/include/trie.h @@ -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 /** diff --git a/src/trie/trie.c b/src/trie/trie.c index 439d439..bad46e2 100644 --- a/src/trie/trie.c +++ b/src/trie/trie.c @@ -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]) { diff --git a/test/fuzzy.h b/test/fuzzy.h index 2382083..e30af06 100644 --- a/test/fuzzy.h +++ b/test/fuzzy.h @@ -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