lander/include/trie.h

149 lines
3.6 KiB
C
Raw Normal View History

2022-11-15 16:21:27 +01:00
#ifndef AD3_TERNARYTRIE
#define AD3_TERNARYTRIE
#define ALPHABET_SIZE 256
#define DELIMITER '\0'
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
2022-12-03 13:27:34 +01:00
// Should not be higher than 254 or stuff will break
#define TRIE_MAX_SKIP_SIZE 8
2022-11-15 16:21:27 +01:00
/**
* The implementation of a Ternary Trie.
*
* 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>
#include <string.h>
2022-11-15 16:21:27 +01:00
static const char charset[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static const size_t charset_len = sizeof(charset) - 1;
2022-11-21 12:03:16 +01:00
// 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
typedef enum entry_type { Redirect, Paste, Unknown } EntryType;
typedef struct entry {
EntryType type;
char *string;
} Entry;
typedef enum trie_exit_code {
Ok = 0,
NotFound,
AlreadyPresent,
FileError
} TrieExitCode;
Entry *entry_new(EntryType type, const char *string);
2022-11-15 16:21:27 +01:00
2022-11-21 14:46:53 +01:00
/**
* Allocate & initialize a new trie, and populate it with the data from the
* given data file.
2022-11-21 14:46:53 +01:00
*
* @return 0 if everything was successful, non-zero otherwise
2022-11-21 14:46:53 +01:00
*/
TrieExitCode trie_init(Trie **trie_ptr, 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
*/
2022-11-29 11:27:28 +01:00
void trie_free(Trie *trie);
2022-11-15 16:21:27 +01:00
/**
* Search for an entry in the trie.
2022-11-15 16:21:27 +01:00
*
* @param trie
* @param entry_ptr pointer to Entry will be stored here, if found
* @param key key representing the entry
* @return 0 if the search was successful, 1 if not found
2022-11-15 16:21:27 +01:00
*/
TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key);
2022-11-15 16:21:27 +01:00
2023-05-29 18:53:52 +02:00
TrieExitCode trie_search_len(Trie *trie, Entry **entry_ptr, const char *key,
size_t key_len);
2022-11-15 16:21:27 +01:00
/**
* Add a string to this trie.
*
* @param trie
* @param key key to represent entry with
* @param entry entry to add
* @return 0 if added, 1 if already in trie, something else if other errors
2022-11-15 16:21:27 +01:00
*/
TrieExitCode trie_add(Trie *trie, const char *key, Entry *entry);
2023-05-30 19:36:58 +02:00
TrieExitCode trie_add_len(Trie *trie, const char *key, size_t key_len,
Entry *entry);
2022-11-21 12:03:16 +01:00
/**
* Add an entry by generating a random string as the key.
2022-11-21 12:03:16 +01:00
*
* @param trie
* @param entry entry to add
2022-11-21 21:02:33 +01:00
* @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.
2022-11-21 12:03:16 +01:00
*/
TrieExitCode trie_add_random(Trie *trie, char **key_ptr, Entry *entry,
bool secure);
2022-11-21 12:03:16 +01:00
2022-11-15 16:21:27 +01:00
/**
* Remove an entry from this trie given its key.
2022-11-15 16:21:27 +01:00
*
* @param trie
* @param key key representing entry
* @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
/**
* Returns the number of entries in this trie.
2022-11-15 16:21:27 +01:00
*
* @param trie
* @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
/*
* 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