2022-11-15 16:21:27 +01:00
|
|
|
#ifndef AD3_TERNARYTRIE
|
|
|
|
#define AD3_TERNARYTRIE
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The implementation of a Ternary Trie.
|
|
|
|
*
|
|
|
|
* Each node should be represented by a binary tree in order to reduce the memory usage.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2022-11-21 12:03:16 +01:00
|
|
|
static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
static const size_t charset_len = sizeof(charset) - 1;
|
|
|
|
|
|
|
|
// Length of randomly generated keys
|
|
|
|
#define RANDOM_KEY_LENGTH 4
|
|
|
|
|
2022-11-15 16:21:27 +01:00
|
|
|
/**
|
|
|
|
* Type definition for the struct representing the current Trie.
|
|
|
|
*
|
|
|
|
* You can (and should) redefine this in your c-file with the concrete fields.
|
|
|
|
*/
|
|
|
|
typedef struct ttrie TernaryTrie;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate and initialize an empty Trie.
|
|
|
|
*
|
|
|
|
* @return a pointer to an empty Trie struct
|
|
|
|
*/
|
|
|
|
TernaryTrie* ternarytrie_init();
|
|
|
|
|
2022-11-15 22:49:36 +01:00
|
|
|
void ternarytrie_populate(TernaryTrie* trie, const char* file_path);
|
|
|
|
|
2022-11-15 16:21:27 +01:00
|
|
|
/**
|
|
|
|
* De-allocate a trie by freeing the memory occupied by this trie.
|
|
|
|
*
|
|
|
|
* @param trie which should be freed
|
|
|
|
*/
|
|
|
|
void ternarytrie_free(TernaryTrie* trie);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search whether a string is contained in this trie.
|
|
|
|
*
|
|
|
|
* @param trie
|
|
|
|
* @param string
|
|
|
|
* @return true if the string is contained within this trie, false otherwise
|
|
|
|
*/
|
2022-11-15 21:12:08 +01:00
|
|
|
char * ternarytrie_search(TernaryTrie* trie, const char* string);
|
2022-11-15 16:21:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a string to this trie.
|
|
|
|
*
|
|
|
|
* @param trie
|
|
|
|
* @param string
|
|
|
|
* @return true if the trie was changed by this operation, false if it was already present
|
|
|
|
*/
|
2022-11-15 21:12:08 +01:00
|
|
|
bool ternarytrie_add(TernaryTrie* trie, const char* string, const char *payload);
|
2022-11-15 16:21:27 +01:00
|
|
|
|
2022-11-21 12:03:16 +01:00
|
|
|
/**
|
|
|
|
* Add a payload by generating a random string as the key.
|
|
|
|
*
|
|
|
|
* @param trie
|
|
|
|
* @return the generated key
|
|
|
|
*/
|
|
|
|
char *ternarytrie_add_random(TernaryTrie *trie, const char *payload);
|
|
|
|
|
2022-11-15 16:21:27 +01:00
|
|
|
/**
|
|
|
|
* Remove a string from this trie.
|
|
|
|
*
|
|
|
|
* Note: strings added to this trie are considered to be "owned" by the caller.
|
|
|
|
* Removing the string from the trie should not free the string's memory.
|
|
|
|
*
|
|
|
|
* @param trie
|
|
|
|
* @param string
|
|
|
|
* @return true if the string was present and has been removed, false if it was not present
|
|
|
|
*/
|
|
|
|
bool ternarytrie_remove(TernaryTrie* trie, const char* string);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of strings in this trie.
|
|
|
|
*
|
|
|
|
* @param trie
|
|
|
|
* @return the number of strings in this trie
|
|
|
|
*/
|
|
|
|
size_t ternarytrie_size(TernaryTrie* trie);
|
|
|
|
|
|
|
|
#endif //AD3_TERNARYTRIE
|