feat: add some more tests; some optimisations

This commit is contained in:
Jef Roosens 2022-11-29 20:59:28 +01:00
parent e1e3d7cb46
commit f9a5fc14e5
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
7 changed files with 186 additions and 24 deletions

View file

@ -102,7 +102,7 @@ int main() {
exit(1);
} else {
std::cout << "Added " << count << " entries to trie." << std::endl;
std::cout << "Added " << count << " (" << trie_size(trie) << ") entries to trie." << std::endl;
}
// Create pastes directory if not present

View file

@ -149,27 +149,34 @@ SearchResult trie_search_node(Trie *trie, const char *key) {
}
i++;
offset = 0;
// We iterate over each character on the edge and compare it to the string.
while (offset < (*child_ptr)->string_len) {
// Our string ends in the middle of an edge, so it's definitely not in
// the trie.
if (key[i + offset] == DELIMITER) {
return out;
}
// We compare each character with the characters in the skipped
// substring. If they don't match, we know the string isn't in the
// trie.
if (key[i + offset] != ((*child_ptr)->string[offset])) {
return out;
}
offset++;
if (memcmp((*child_ptr)->string, key + i, (*child_ptr)->string_len) != 0) {
return out;
}
i += offset;
i += (*child_ptr)->string_len;
/* offset = 0; */
/* // We iterate over each character on the edge and compare it to the string. */
/* while (offset < (*child_ptr)->string_len) { */
/* // Our string ends in the middle of an edge, so it's definitely not in */
/* // the trie. */
/* if (key[i + offset] == DELIMITER) { */
/* return out; */
/* } */
/* // We compare each character with the characters in the skipped */
/* // substring. If they don't match, we know the string isn't in the */
/* // trie. */
/* if (key[i + offset] != ((*child_ptr)->string[offset])) { */
/* return out; */
/* } */
/* offset++; */
/* } */
/* i += offset; */
if (key[i] != DELIMITER) {
node_ptr = child_ptr;
@ -236,10 +243,11 @@ bool trie_add_no_lock(Trie *trie, const char *string, Entry *entry) {
while (offset < TRIE_MAX_SKIP_SIZE &&
string[i + 1 + offset] != DELIMITER) {
child_node->string[offset] = string[i + 1 + offset];
offset++;
}
memcpy(child_node->string, string + i + 1, offset);
child_node->string_len = offset;
*child_node_ptr = child_node;

View file

@ -32,11 +32,12 @@ typedef struct tnode {
TrieInnerNode *tree;
uint8_t tree_size;
// Skips are at most 8 characters, and are stored in the nodes
// Skips are at most TRIE_MAX_SKIP_SIZE characters, and are stored in the
// nodes
char string[TRIE_MAX_SKIP_SIZE];
uint8_t string_len : 4;
uint8_t string_len;
bool represents : 1;
bool represents;
} TrieNode;
// Required for recursively freeing tree structure