From 42ed96353d186078e34e4c541e7c92d433cda138 Mon Sep 17 00:00:00 2001 From: Kevin Jerebica Date: Mon, 2 Sep 2024 21:29:29 +0200 Subject: [PATCH] feat: add streencmp --- README | 13 ++++++++++++- streecmp.c | 25 +++++++++++++++++++++++++ streecmp.h | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README b/README index 903a3f3..ce3121f 100644 --- a/README +++ b/README @@ -4,6 +4,17 @@ Fast*er* string comparison - an alternative to thousands of strcmp calls + This is an O(n) solution - n being the length of the string to be checked. + A plus is that this method will stay O(n) *no matter the length of the + string nor the number of strings to be checked against*. + + Potential minuses might be the additional memory needed to store the trie + data structure or the computational time required to generate the tree + before utilizing it. Therefore, users are advised to use a secure, fast + hashmap instead of streecmp. streecmp is only applied to specific scenarios + where a rather simple or fast solution for string comparison is required + so the project moves on. + RES: en.wikipedia.org/wiki/Trie RES: en.wikipedia.org/wiki/Suffix_tree RES: en.wikipedia.org/wiki/Radix_tree @@ -48,7 +59,7 @@ LEAKS TODO - * everything done so far + * feat: ++strong++ and --loose-- comparison COMMITS diff --git a/streecmp.c b/streecmp.c index 4a52dae..6c904a4 100644 --- a/streecmp.c +++ b/streecmp.c @@ -115,3 +115,28 @@ int streecmp(struct nod *nod, char *str) { return streecmp(target, str+1); } + +int streencmp(struct nod *nod, char *str, int len) { + struct nod *nptr = nod; + char *sptr = str; + + for (int i = 0; i < len; i++) { + struct nod *target = nptr->pool[*sptr]; + if (target == NULL) { + goto _not_found; + } + + nptr = target; + sptr++; + } + + struct nod *end = nptr->pool['\0']; + if (end == NULL) { + goto _not_found; + } + + return end->val.ival; + +_not_found: + return 0; +} diff --git a/streecmp.h b/streecmp.h index 318df1c..92a12a3 100644 --- a/streecmp.h +++ b/streecmp.h @@ -21,5 +21,6 @@ struct nod *mknod(struct nod *nod, int loc); int mkstr(struct nod *nod, char *str); int gentree(struct nod *nod, char *strs, char *delim); int streecmp(struct nod *nod, char *str); +int streencmp(struct nod *nod, char *str, int len); #endif