aboutsummaryrefslogtreecommitdiff
path: root/6.2
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
parent329fada630517195a472da703d19d7eced62c5a8 (diff)
6.2: v2, separate compare function that does all the logic
Diffstat (limited to '6.2')
-rw-r--r--6.2/main.c11
-rw-r--r--6.2/tree.c42
-rw-r--r--6.2/tree.h4
3 files changed, 33 insertions, 24 deletions
diff --git a/6.2/main.c b/6.2/main.c
index 57b562a..1d581c6 100644
--- a/6.2/main.c
+++ b/6.2/main.c
@@ -12,14 +12,17 @@ int main(int argc, char *argv[])
{
struct tnode *root;
char word[MAXWORD];
+ int found = NO;
int bar;
bar = (--argc && (**++argv == '-')) ? atoi(argv[0]+1) : DEFLEN;
root = NULL;
- while (getword(word, MAXWORD) != EOF)
- if ((isalpha(word[0]) || (word[0] == '#' && isalpha(word[1]))) &&
- strlen(word) > (size_t) bar)
- root = naddtree(root, word, bar, NO);
+ while (getword(word, MAXWORD) != EOF) {
+ if ((isalpha(word[0]) || (word[0] == '#' && isalpha(word[1])))
+ && strlen(word) > (size_t) bar)
+ root = naddtree(root, word, bar, &found);
+ found = NO;
+ }
ntreeprint(root);
treefree(root);
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;
+}
diff --git a/6.2/tree.h b/6.2/tree.h
index e976495..9c3cee1 100644
--- a/6.2/tree.h
+++ b/6.2/tree.h
@@ -13,8 +13,6 @@ enum { NO, YES };
#define TREE_H
#endif
-struct tnode *naddtree(struct tnode *p, char *w, int bar, int prevmatch);
+struct tnode *naddtree(struct tnode *p, char *w, int bar, int *found);
void ntreeprint(struct tnode *p);
void treefree(struct tnode *p);
-struct tnode *talloc(void);
-char *sneed_strdup(char *s);