feat: use char hashmap for child search
This commit is contained in:
parent
e4f8292477
commit
e6bb483dc2
8
README
8
README
|
@ -30,10 +30,12 @@ TECHNICALS
|
||||||
|
|
||||||
TODO
|
TODO
|
||||||
|
|
||||||
* Characters "hashmap" to remove linear checking of children nodes
|
* Detect strings which are already inside the tree
|
||||||
* Remove memory leaks
|
* Memory leaks
|
||||||
* Add alloc/free functions
|
* Alloc/free functions
|
||||||
* NULL checking
|
* NULL checking
|
||||||
|
* Memset for initialization
|
||||||
|
* gentree with custom seperator
|
||||||
|
|
||||||
COMMITS
|
COMMITS
|
||||||
|
|
||||||
|
|
75
streecmp.c
75
streecmp.c
|
@ -1,43 +1,48 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
#include "streecmp.h"
|
#include "streecmp.h"
|
||||||
|
|
||||||
int strs_cnt = 0;
|
int strs_cnt = 0;
|
||||||
|
|
||||||
struct nod *mknod(struct nod *prnt) {
|
struct nod *allocnod(void) {
|
||||||
if (prnt == NULL) {
|
struct nod *nod = (struct nod *) calloc(1, sizeof(struct nod));
|
||||||
|
if (nod == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
prnt->pool_size++;
|
nod->pool = (struct nod **) calloc(256, sizeof(struct nod *));
|
||||||
|
if (nod->pool == NULL) {
|
||||||
prnt->pool = (struct nod *) realloc(prnt->pool,
|
free(nod);
|
||||||
sizeof(struct nod)*prnt->pool_size);
|
|
||||||
if (prnt->pool == NULL) {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct nod *child = &prnt->pool[prnt->pool_size-1];
|
return nod;
|
||||||
child->pool_size = 0;
|
|
||||||
child->pool = NULL;
|
|
||||||
child->val.cval = 0;
|
|
||||||
child->val.ival = 0;
|
|
||||||
|
|
||||||
return child;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int mkstr(struct nod *rot, char *str) {
|
struct nod *mknod(struct nod *nod, int loc) {
|
||||||
for (int i = 0; i < rot->pool_size; i++) {
|
if (nod == NULL) {
|
||||||
struct nod *child = &rot->pool[i];
|
return allocnod();
|
||||||
if (child->val.cval != *str) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
if (new == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -52,7 +57,7 @@ int mkstr(struct nod *rot, char *str) {
|
||||||
return strs_cnt;
|
return strs_cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
int gentree(struct nod *rot, char *strs) {
|
int gentree(struct nod *nod, char *strs) {
|
||||||
char *strs_cpy = strdup(strs);
|
char *strs_cpy = strdup(strs);
|
||||||
if (strs_cpy == NULL) {
|
if (strs_cpy == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -60,7 +65,7 @@ int gentree(struct nod *rot, char *strs) {
|
||||||
|
|
||||||
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok
|
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok
|
||||||
= strtok(NULL, "\n")) {
|
= strtok(NULL, "\n")) {
|
||||||
int ret = mkstr(rot,tok);
|
int ret = mkstr(nod, tok);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -69,19 +74,15 @@ int gentree(struct nod *rot, char *strs) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int streecmp(struct nod *rot, char *str) {
|
int streecmp(struct nod *nod, char *str) {
|
||||||
for (int i = 0; i < rot->pool_size; i++) {
|
struct nod *target = nod->pool[*str];
|
||||||
struct nod *child = &rot->pool[i];
|
if (target == NULL) {
|
||||||
if (child->val.cval != *str) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (child->val.cval == '\0') {
|
|
||||||
return child->val.ival;
|
|
||||||
}
|
|
||||||
|
|
||||||
return streecmp(child, str+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target->val.cval == '\0') {
|
||||||
|
return target->val.ival;
|
||||||
|
}
|
||||||
|
|
||||||
|
return streecmp(target, str+1);
|
||||||
}
|
}
|
||||||
|
|
11
streecmp.h
11
streecmp.h
|
@ -9,12 +9,13 @@ struct nodval {
|
||||||
struct nod {
|
struct nod {
|
||||||
struct nodval val;
|
struct nodval val;
|
||||||
int pool_size;
|
int pool_size;
|
||||||
struct nod *pool;
|
struct nod **pool;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct nod *mknod(struct nod *prnt);
|
struct nod *allocnod(void);
|
||||||
int mkstr(struct nod *rot, char *str);
|
struct nod *mknod(struct nod *nod, int loc);
|
||||||
int gentree(struct nod *rot, char *strs);
|
int mkstr(struct nod *nod, char *str);
|
||||||
int streecmp(struct nod *rot, char *str);
|
int gentree(struct nod *nod, char *strs);
|
||||||
|
int streecmp(struct nod *nod, char *str);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
6
test.c
6
test.c
|
@ -114,7 +114,7 @@ char *strs = "A-IM\n"
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int ret;
|
int ret;
|
||||||
struct nod *rot = (struct nod *) calloc(1, sizeof(struct nod));
|
struct nod *rot = allocnod();
|
||||||
if (rot == NULL) {
|
if (rot == NULL) {
|
||||||
fprintf(stderr, "[-] Failed: not enough dynamic memory\n");
|
fprintf(stderr, "[-] Failed: not enough dynamic memory\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -140,10 +140,10 @@ int main(void) {
|
||||||
int found = streecmp(rot, tok);
|
int found = streecmp(rot, tok);
|
||||||
if (found == cnt) {
|
if (found == cnt) {
|
||||||
fprintf(stdout, "[+][%d\t] Success: could find"
|
fprintf(stdout, "[+][%d\t] Success: could find"
|
||||||
"'%s' in tree\n", cnt, tok);
|
" '%s' in tree\n", cnt, tok);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "[-][%d\t] Error: could not find"
|
fprintf(stderr, "[-][%d\t] Error: could not find"
|
||||||
"'%s' in tree\n", cnt, tok);
|
" '%s' in tree\n", cnt, tok);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user