2022-11-15 16:21:27 +01:00
|
|
|
#ifndef AD3_TERNARYTRIE
|
|
|
|
#define AD3_TERNARYTRIE
|
|
|
|
|
2022-11-27 20:20:09 +01:00
|
|
|
#define ALPHABET_SIZE 256
|
|
|
|
#define DELIMITER '\0'
|
|
|
|
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
|
2022-11-29 15:08:07 +01:00
|
|
|
#define TRIE_MAX_SKIP_SIZE 8
|
2022-11-27 20:20:09 +01:00
|
|
|
|
2022-11-15 16:21:27 +01:00
|
|
|
/**
|
|
|
|
* The implementation of a Ternary Trie.
|
|
|
|
*
|
2022-11-27 20:20:09 +01:00
|
|
|
* Each node should be represented by a binary tree in order to reduce the
|
|
|
|
* memory usage.
|
2022-11-15 16:21:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
2022-11-27 20:20:09 +01:00
|
|
|
#include <string.h>
|
2022-11-15 16:21:27 +01:00
|
|
|
|
2022-11-27 20:20:09 +01:00
|
|
|
static const char charset[] =
|
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
2022-11-21 12:03:16 +01:00
|
|
|
static const size_t charset_len = sizeof(charset) - 1;
|
|
|
|
|
|
|
|
// Length of randomly generated keys
|
2022-11-21 21:02:33 +01:00
|
|
|
#define RANDOM_KEY_LENGTH_SHORT 4
|
|
|
|
#define RANDOM_KEY_LENGTH_LONG 16
|
2022-11-21 12:03:16 +01:00
|
|
|
|
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.
|
|
|
|
*/
|
2022-11-29 11:27:28 +01:00
|
|
|
typedef struct ttrie Trie;
|
2022-11-15 16:21:27 +01:00
|
|
|
|
2022-11-27 20:20:09 +01:00
|
|
|
typedef enum entry_type { Redirect, Paste, Unknown } EntryType;
|
2022-11-21 15:28:02 +01:00
|
|
|
|
|
|
|
typedef struct entry {
|
|
|
|
EntryType type;
|
|
|
|
char *string;
|
|
|
|
} Entry;
|
|
|
|
|
|
|
|
Entry *entry_new(EntryType type, const char *string);
|
|
|
|
|
2022-11-15 16:21:27 +01:00
|
|
|
/**
|
|
|
|
* Allocate and initialize an empty Trie.
|
|
|
|
*
|
|
|
|
* @return a pointer to an empty Trie struct
|
|
|
|
*/
|
2022-11-29 11:27:28 +01:00
|
|
|
Trie *trie_init();
|
2022-11-15 16:21:27 +01:00
|
|
|
|
2022-11-21 14:46:53 +01:00
|
|
|
/**
|
|
|
|
* Populate trie with entries stored in the given file.
|
|
|
|
*
|
|
|
|
* @param trie
|
|
|
|
* @param file_path path to file containing entries
|
|
|
|
* @return amount of entries added; -1 if an error occured
|
|
|
|
*/
|
2022-11-29 11:27:28 +01:00
|
|
|
int trie_populate(Trie *trie, const char *file_path);
|
2022-11-15 22:49:36 +01:00
|
|
|
|
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
|
|
|
|
*/
|
2022-11-29 11:27:28 +01:00
|
|
|
void trie_free(Trie *trie);
|
2022-11-15 16:21:27 +01:00
|
|
|
|
|
|
|
/**
|
2022-11-21 15:28:02 +01:00
|
|
|
* Search for an entry in the trie.
|
2022-11-15 16:21:27 +01:00
|
|
|
*
|
|
|
|
* @param trie
|
2022-11-21 15:28:02 +01:00
|
|
|
* @param key key representing the entry
|
|
|
|
* @return pointer to entry; NULL if not found
|
2022-11-15 16:21:27 +01:00
|
|
|
*/
|
2022-11-29 11:27:28 +01:00
|
|
|
Entry *trie_search(Trie *trie, const char *key);
|
2022-11-15 16:21:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a string to this trie.
|
|
|
|
*
|
|
|
|
* @param trie
|
2022-11-21 15:28:02 +01:00
|
|
|
* @param key key to represent entry with
|
2022-11-27 20:20:09 +01:00
|
|
|
* @param entry entry to add
|
|
|
|
* @return true if the trie was changed by this operation, false if it was
|
|
|
|
* already present
|
2022-11-15 16:21:27 +01:00
|
|
|
*/
|
2022-11-29 11:27:28 +01:00
|
|
|
bool trie_add(Trie *trie, const char *key, Entry *entry);
|
2022-11-15 16:21:27 +01:00
|
|
|
|
2022-11-21 12:03:16 +01:00
|
|
|
/**
|
2022-11-21 15:28:02 +01:00
|
|
|
* Add an entry by generating a random string as the key.
|
2022-11-21 12:03:16 +01:00
|
|
|
*
|
|
|
|
* @param trie
|
2022-11-21 15:28:02 +01:00
|
|
|
* @param entry entry to add
|
2022-11-21 21:02:33 +01:00
|
|
|
* @param secure whether to generate a longer, more secure random key
|
2022-11-21 12:03:16 +01:00
|
|
|
* @return the generated key
|
|
|
|
*/
|
2022-11-29 11:27:28 +01:00
|
|
|
char *trie_add_random(Trie *trie, Entry *entry, bool secure);
|
2022-11-21 12:03:16 +01:00
|
|
|
|
2022-11-15 16:21:27 +01:00
|
|
|
/**
|
2022-11-21 15:28:02 +01:00
|
|
|
* Remove an entry from this trie given its key.
|
2022-11-15 16:21:27 +01:00
|
|
|
*
|
|
|
|
* @param trie
|
2022-11-21 15:28:02 +01:00
|
|
|
* @param key key representing entry
|
2022-11-27 20:20:09 +01:00
|
|
|
* @return true if the entry was present and has been removed, false if it was
|
|
|
|
* not present
|
2022-11-15 16:21:27 +01:00
|
|
|
*/
|
2022-11-29 11:27:28 +01:00
|
|
|
bool trie_remove(Trie *trie, const char *key);
|
2022-11-15 16:21:27 +01:00
|
|
|
|
|
|
|
/**
|
2022-11-21 15:28:02 +01:00
|
|
|
* Returns the number of entries in this trie.
|
2022-11-15 16:21:27 +01:00
|
|
|
*
|
|
|
|
* @param trie
|
2022-11-21 15:28:02 +01:00
|
|
|
* @return the number of entries in this trie
|
2022-11-15 16:21:27 +01:00
|
|
|
*/
|
2022-11-29 11:27:28 +01:00
|
|
|
size_t trie_size(Trie *trie);
|
2022-11-15 16:21:27 +01:00
|
|
|
|
2022-11-27 20:20:09 +01:00
|
|
|
#endif // AD3_TERNARYTRIE
|