From e4f8292477ade2cb8ee7fa80c30375e040d9ed5c Mon Sep 17 00:00:00 2001 From: Kevin Jerebica Date: Sun, 25 Aug 2024 14:19:42 +0200 Subject: [PATCH] repo: add readme and split 80col code --- README | 46 ++++++++++++++++++++++++++++++++++++++++++++++ streecmp.c | 6 ++++-- test.c | 9 ++++++--- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000..79a0d09 --- /dev/null +++ b/README @@ -0,0 +1,46 @@ ++==============+ +| streecmp | ++==============+ + + Fast*er* string comparison - an alternative to thousands of strcmp calls + + RES: en.wikipedia.org/wiki/Trie + RES: en.wikipedia.org/wiki/Suffix_tree + RES: en.wikipedia.org/wiki/Radix_tree + RES: facweb.cs.depaul.edu/mobasher/classes/csc575/Suffix_Trees/index.html + +TECHNICALS + + struct nod + * data structure that represents a node of the tree + struct nod *mknod(struct nod *prnt) + * create a child node below given parent node + * returns: pointer to said node or NULL if fails + int mkstr(struct nod *rot, char *str) + * fit string into tree if it does not already exist + * warning: string has to be null-byte terminated + * returns: the assigned ID of the string or -1 if fails + int gentree(struct nod *rot, char *strs) + * generate tree from character seperated strings + * warning: seperation character is '\n' + * returns: 0 if ok or -1 if fails + int streecmp(struct nod *rot, char *str) + * traverse tree and search for str + * returns: id of string or 0 if there is no match + +TODO + + * Characters "hashmap" to remove linear checking of children nodes + * Remove memory leaks + * Add alloc/free functions + * NULL checking + +COMMITS + + Each commit is prefixed with an indicator token of what the change is + *mostly* about. List of tokens is: + + * repo: change docs or code style + * feat: implement new change + * fix: implement fix for a bug + diff --git a/streecmp.c b/streecmp.c index 2f9032b..7ff170b 100644 --- a/streecmp.c +++ b/streecmp.c @@ -12,7 +12,8 @@ struct nod *mknod(struct nod *prnt) { prnt->pool_size++; - prnt->pool = (struct nod *) realloc(prnt->pool, sizeof(struct nod)*prnt->pool_size); + prnt->pool = (struct nod *) realloc(prnt->pool, + sizeof(struct nod)*prnt->pool_size); if (prnt->pool == NULL) { return NULL; } @@ -57,7 +58,8 @@ int gentree(struct nod *rot, char *strs) { return -1; } - for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok = strtok(NULL, "\n")) { + for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok + = strtok(NULL, "\n")) { int ret = mkstr(rot,tok); if (ret < 0) { return -1; diff --git a/test.c b/test.c index f71b107..f14498d 100644 --- a/test.c +++ b/test.c @@ -135,12 +135,15 @@ int main(void) { fprintf(stdout, "------------------------\n"); int cnt = 1; - for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok = strtok(NULL, "\n"), cnt++) { + for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok + = strtok(NULL, "\n"), cnt++) { int found = streecmp(rot, tok); if (found == cnt) { - fprintf(stdout, "[+][%d\t] Success: could find '%s' in tree\n", cnt, tok); + fprintf(stdout, "[+][%d\t] Success: could find" + "'%s' in tree\n", cnt, tok); } else { - fprintf(stderr, "[-][%d\t] Error: could not find '%s' in tree\n", cnt, tok); + fprintf(stderr, "[-][%d\t] Error: could not find" + "'%s' in tree\n", cnt, tok); } }