aboutsummaryrefslogtreecommitdiff
path: root/6.2/tree.c
diff options
context:
space:
mode:
authorsinanmohd <sinan@firemail.cc>2023-05-07 17:54:35 +0530
committersinanmohd <sinan@firemail.cc>2023-05-07 21:26:06 +0530
commitf84892068fa3b1ab2f85ba6c2f36b4856086e8e5 (patch)
tree7d84361ebeb2291244e5e03f16596bf01d92e854 /6.2/tree.c
parent329fada630517195a472da703d19d7eced62c5a8 (diff)
6.2: v2, separate compare function that does all the logic
Diffstat (limited to '6.2/tree.c')
-rw-r--r--6.2/tree.c42
1 files changed, 25 insertions, 17 deletions
diff --git a/6.2/tree.c b/6.2/tree.c
index 5e061a4..bd81bff 100644
--- a/6.2/tree.c
+++ b/6.2/tree.c
@@ -3,7 +3,11 @@
#include <string.h>
#include "tree.h"
-struct tnode *naddtree(struct tnode *p, char *w, int bar, int prevmatch)
+char *sneed_strdup(char *s);
+struct tnode *talloc(void);
+int compare(char *w, struct tnode *p, int bar, int *found);
+
+struct tnode *naddtree(struct tnode *p, char *w, int bar, int *found)
{
int cond;
@@ -11,19 +15,14 @@ struct tnode *naddtree(struct tnode *p, char *w, int bar, int prevmatch)
p = talloc();
p->word = sneed_strdup(w);
p->count = 1;
- p->match = prevmatch;
+ p->match = *found;
p->left = p->right = NULL;
- } else {
- if ((!strncmp(p->word, w, bar) && strcmp(p->word, w)))
- p->match = prevmatch = YES;
-
- if ((cond = strcmp(w, p->word)) == 0) {
- p->count++;
- } else if (cond < 0) {
- p->left = naddtree(p->left, w, bar, prevmatch);
- } else if (cond > 0) {
- p->right = naddtree(p->right, w, bar, prevmatch);
- }
+ } else if ((cond = compare(w, p, bar, found)) == 0) {
+ p->count++;
+ } else if (cond < 0) {
+ p->left = naddtree(p->left, w, bar, found);
+ } else if (cond > 0) {
+ p->right = naddtree(p->right, w, bar, found);
}
return p;
@@ -41,10 +40,8 @@ void ntreeprint(struct tnode *p)
void treefree(struct tnode *p) {
if (p != NULL) {
- if (p->left != NULL)
- treefree(p->left);
- if (p->right != NULL)
- treefree(p->right);
+ treefree(p->left);
+ treefree(p->right);
free(p->word);
free(p);
}
@@ -65,3 +62,14 @@ char *sneed_strdup(char *s)
return p;
}
+
+int compare(char *w, struct tnode *p, int bar, int *found)
+{
+ int cond;
+
+ if ((cond = strncmp(w, p->word, bar)) == 0)
+ if ((cond = strcmp(w + bar, p->word + bar)) != 0)
+ p->match = *found = YES;
+
+ return cond;
+}