feat: add some more tests; some optimisations
This commit is contained in:
parent
e1e3d7cb46
commit
f9a5fc14e5
7 changed files with 186 additions and 24 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue