feat: use char hashmap for child search

This commit is contained in:
Kevin J. 2024-08-25 22:24:08 +02:00
parent e4f8292477
commit e6bb483dc2
4 changed files with 53 additions and 49 deletions

8
README
View File

@ -30,10 +30,12 @@ TECHNICALS
TODO
* Characters "hashmap" to remove linear checking of children nodes
* Remove memory leaks
* Add alloc/free functions
* Detect strings which are already inside the tree
* Memory leaks
* Alloc/free functions
* NULL checking
* Memset for initialization
* gentree with custom seperator
COMMITS

View File

@ -1,43 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "streecmp.h"
int strs_cnt = 0;
struct nod *mknod(struct nod *prnt) {
if (prnt == NULL) {
struct nod *allocnod(void) {
struct nod *nod = (struct nod *) calloc(1, sizeof(struct nod));
if (nod == NULL) {
return NULL;
}
prnt->pool_size++;
prnt->pool = (struct nod *) realloc(prnt->pool,
sizeof(struct nod)*prnt->pool_size);
if (prnt->pool == NULL) {
nod->pool = (struct nod **) calloc(256, sizeof(struct nod *));
if (nod->pool == NULL) {
free(nod);
return NULL;
}
struct nod *child = &prnt->pool[prnt->pool_size-1];
child->pool_size = 0;
child->pool = NULL;
child->val.cval = 0;
child->val.ival = 0;
return child;
return nod;
}
int mkstr(struct nod *rot, char *str) {
for (int i = 0; i < rot->pool_size; i++) {
struct nod *child = &rot->pool[i];
if (child->val.cval != *str) {
continue;
struct nod *mknod(struct nod *nod, int loc) {
if (nod == NULL) {
return allocnod();
}
return mkstr(child, str+1);
if (nod->pool == NULL) {
return NULL;
}
struct nod *new = mknod(rot);
nod->pool_size++;
nod->pool[loc] = allocnod();
return nod->pool[loc];
}
int mkstr(struct nod *nod, char *str) {
struct nod *target = nod->pool[*str];
if (target != NULL) {
return mkstr(target, str+1);
}
struct nod *new = mknod(nod, *str);
if (new == NULL) {
return -1;
}
@ -52,7 +57,7 @@ int mkstr(struct nod *rot, char *str) {
return strs_cnt;
}
int gentree(struct nod *rot, char *strs) {
int gentree(struct nod *nod, char *strs) {
char *strs_cpy = strdup(strs);
if (strs_cpy == NULL) {
return -1;
@ -60,7 +65,7 @@ int gentree(struct nod *rot, char *strs) {
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok
= strtok(NULL, "\n")) {
int ret = mkstr(rot,tok);
int ret = mkstr(nod, tok);
if (ret < 0) {
return -1;
}
@ -69,19 +74,15 @@ int gentree(struct nod *rot, char *strs) {
return 0;
}
int streecmp(struct nod *rot, char *str) {
for (int i = 0; i < rot->pool_size; i++) {
struct nod *child = &rot->pool[i];
if (child->val.cval != *str) {
continue;
}
if (child->val.cval == '\0') {
return child->val.ival;
}
return streecmp(child, str+1);
}
int streecmp(struct nod *nod, char *str) {
struct nod *target = nod->pool[*str];
if (target == NULL) {
return 0;
}
if (target->val.cval == '\0') {
return target->val.ival;
}
return streecmp(target, str+1);
}

View File

@ -9,12 +9,13 @@ struct nodval {
struct nod {
struct nodval val;
int pool_size;
struct nod *pool;
struct nod **pool;
};
struct nod *mknod(struct nod *prnt);
int mkstr(struct nod *rot, char *str);
int gentree(struct nod *rot, char *strs);
int streecmp(struct nod *rot, char *str);
struct nod *allocnod(void);
struct nod *mknod(struct nod *nod, int loc);
int mkstr(struct nod *nod, char *str);
int gentree(struct nod *nod, char *strs);
int streecmp(struct nod *nod, char *str);
#endif

2
test.c
View File

@ -114,7 +114,7 @@ char *strs = "A-IM\n"
int main(void) {
int ret;
struct nod *rot = (struct nod *) calloc(1, sizeof(struct nod));
struct nod *rot = allocnod();
if (rot == NULL) {
fprintf(stderr, "[-] Failed: not enough dynamic memory\n");
return -1;