lander/trie/include/trie.h

145 lines
3.4 KiB
C

#ifndef AD3_TERNARYTRIE
#define AD3_TERNARYTRIE
#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
/**
* 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>
#include <string.h>
static const char charset[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static const size_t charset_len = sizeof(charset) - 1;
// Length of randomly generated keys
#define RANDOM_KEY_LENGTH_SHORT 4
#define RANDOM_KEY_LENGTH_LONG 16
/**
* 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 Trie;
typedef enum entry_type { Redirect, Paste, Unknown } EntryType;
typedef struct entry {
EntryType type;
char *string;
} Entry;
Entry *entry_new(EntryType type, const char *string);
/**
* Allocate and initialize an empty Trie.
*
* @return a pointer to an empty Trie struct
*/
Trie *trie_init();
/**
* 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
*/
int trie_populate(Trie *trie, const char *file_path);
/**
* De-allocate a trie by freeing the memory occupied by this trie.
*
* @param trie which should be freed
*/
void trie_free(Trie *trie);
/**
* Search for an entry in the trie.
*
* @param trie
* @param key key representing the entry
* @return pointer to entry; NULL if not found
*/
Entry *trie_search(Trie *trie, const char *key);
/**
* Add a string to this trie.
*
* @param trie
* @param key key to represent entry with
* @param entry entry to add
* @return true if the trie was changed by this operation, false if it was
* already present
*/
bool trie_add(Trie *trie, const char *key, Entry *entry);
bool trie_add_no_lock(Trie *trie, const char *key, Entry *entry);
/**
* Add an entry by generating a random string as the key.
*
* @param trie
* @param entry entry to add
* @param secure whether to generate a longer, more secure random key
* @return pointer to the generated key. This pointer is safe to use after
* unlocking the trie, and should be freed manually.
*/
char *trie_add_random(Trie *trie, Entry *entry, bool secure);
/**
* Remove an entry from this trie given its key.
*
* @param trie
* @param key key representing entry
* @return true if the entry was present and has been removed, false if it was
* not present
*/
bool trie_remove(Trie *trie, const char *key);
/**
* Returns the number of entries in this trie.
*
* @param trie
* @return the number of entries in this trie
*/
size_t trie_size(Trie *trie);
/*
* Acquire a read lock on the trie.
*
* @return 0 if successful, non-zero otherwise (return value of
* pthread_rwlock_rdlock)
*/
int trie_rlock(Trie *trie);
/*
* Acquire a write lock on the trie.
*
* @return 0 if successful, non-zero otherwise (return value of
* pthread_rwlock_wrlock)
*/
int trie_wlock(Trie *trie);
/*
* Release the lock on a trie after having acquired it beforehand.
*
* @return 0 if successful, non-zero otherwise (return value of
* pthread_rwlock_unlock)
*/
int trie_unlock(Trie *trie);
#endif // AD3_TERNARYTRIE