feat: switch to C-based compilation, start of simple event loop

This commit is contained in:
Jef Roosens 2023-05-24 09:03:22 +02:00
parent 01eb5ece55
commit 11cd537759
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
7 changed files with 1238 additions and 94 deletions

View file

@ -19,9 +19,9 @@
#include <stddef.h>
#include <string.h>
static const char charset[] =
const static char charset[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static const size_t charset_len = sizeof(charset) - 1;
const static size_t charset_len = sizeof(charset) - 1;
// Length of randomly generated keys
#define RANDOM_KEY_LENGTH_SHORT 4

View file

@ -2,46 +2,7 @@
#include <stdint.h>
#include <stdlib.h>
#include "trie.h"
/**
* Represents a node of the binary tree contained within each non-leaf
* TrieNode.
*/
typedef struct tinode {
struct tinode *left;
struct tinode *right;
struct tnode *next;
char key;
} TrieInnerNode;
/**
* Represents a node inside a Trie. A node can be in one of three states:
* - Internal node: a node that's part of a path to a leaf node. This node will
* always have a size greater than one, and an initialized root.
* - Leaf: a node solely used to represent a string ending there. Its size is 0,
* its ptr is unitialized and represents is true.
* - Full leaf: a leaf node that contains a string. This occurs when a string is
* added whose path is not fully in the tree yet, causing its remaining suffix
* to be stored as a single node. Its size will be zero, represents its true,
* and its string pointer is initialized.
*/
typedef struct tnode {
Entry *entry;
TrieInnerNode *tree;
uint8_t tree_size;
// Skips are at most TRIE_MAX_SKIP_SIZE characters, and are stored in the
// nodes
char string[TRIE_MAX_SKIP_SIZE];
uint8_t string_len;
bool represents;
} TrieNode;
// Required for recursively freeing tree structure
void tnode_free(TrieNode *node);
#include "trie_node.h"
/**
* Allocate and initialize a new TrieInnerNode representing a given

53
trie/src/trie_node.h Normal file
View file

@ -0,0 +1,53 @@
#include <stdint.h>
#include "trie.h"
/**
* Represents a node of the binary tree contained within each non-leaf
* TrieNode.
*/
typedef struct tinode {
struct tinode *left;
struct tinode *right;
struct tnode *next;
char key;
} TrieInnerNode;
/**
* Represents a node inside a Trie. A node can be in one of three states:
* - Internal node: a node that's part of a path to a leaf node. This node will
* always have a size greater than one, and an initialized root.
* - Leaf: a node solely used to represent a string ending there. Its size is 0,
* its ptr is unitialized and represents is true.
* - Full leaf: a leaf node that contains a string. This occurs when a string is
* added whose path is not fully in the tree yet, causing its remaining suffix
* to be stored as a single node. Its size will be zero, represents its true,
* and its string pointer is initialized.
*/
typedef struct tnode {
Entry *entry;
TrieInnerNode *tree;
uint8_t tree_size;
// Skips are at most TRIE_MAX_SKIP_SIZE characters, and are stored in the
// nodes
char string[TRIE_MAX_SKIP_SIZE];
uint8_t string_len;
bool represents;
} TrieNode;
TrieInnerNode *tinode_init(char c);
TrieNode *tnode_init();
void tinode_free_cascade(TrieInnerNode *node);
void tnode_free(TrieNode *node);
TrieNode **tnode_search(TrieNode *node, const char c, bool create);
void tinode_remove(TrieInnerNode *node, const char c);
void tnode_remove(TrieNode *node, const char c);