refactor: don't compile trie as separate library
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
55474b485f
commit
cc8cfaeace
7 changed files with 65 additions and 107 deletions
114
include/ternarytrie.h
Normal file
114
include/ternarytrie.h
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
#ifndef AD3_TERNARYTRIE
|
||||
#define AD3_TERNARYTRIE
|
||||
|
||||
#define ALPHABET_SIZE 256
|
||||
#define DELIMITER '\0'
|
||||
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
|
||||
|
||||
/**
|
||||
* 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 TernaryTrie;
|
||||
|
||||
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
|
||||
*/
|
||||
TernaryTrie *ternarytrie_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 ternarytrie_populate(TernaryTrie *trie, const char *file_path);
|
||||
|
||||
/**
|
||||
* De-allocate a trie by freeing the memory occupied by this trie.
|
||||
*
|
||||
* @param trie which should be freed
|
||||
*/
|
||||
void ternarytrie_free(TernaryTrie *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 *ternarytrie_search(TernaryTrie *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 ternarytrie_add(TernaryTrie *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 the generated key
|
||||
*/
|
||||
char *ternarytrie_add_random(TernaryTrie *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 ternarytrie_remove(TernaryTrie *trie, const char *key);
|
||||
|
||||
/**
|
||||
* Returns the number of entries in this trie.
|
||||
*
|
||||
* @param trie
|
||||
* @return the number of entries in this trie
|
||||
*/
|
||||
size_t ternarytrie_size(TernaryTrie *trie);
|
||||
|
||||
#endif // AD3_TERNARYTRIE
|
||||
Loading…
Add table
Add a link
Reference in a new issue