Commit f48297ea420e811f8942115d1819717f4cdfe01a
1 parent
1fe839ff
Update1 treeh
Showing
2 changed files
with
40 additions
and
35 deletions
Show diff stats
treeh.c
@@ -7,16 +7,18 @@ tree make_empty_tree(){ | @@ -7,16 +7,18 @@ tree make_empty_tree(){ | ||
7 | node* make_empty_node(){ | 7 | node* make_empty_node(){ |
8 | node*n=malloc(sizeof(node)); | 8 | node*n=malloc(sizeof(node)); |
9 | n->letter='\0'; | 9 | n->letter='\0'; |
10 | - n->isEnd=false; | 10 | + n->isEnd=0; |
11 | for(int i=0;i<NBCHAR;i++) | 11 | for(int i=0;i<NBCHAR;i++) |
12 | n->next[i]=make_empty_tree(); | 12 | n->next[i]=make_empty_tree(); |
13 | + return n; | ||
13 | } | 14 | } |
14 | -node* make_node(char l,bool end){ | 15 | +node* make_node(char l,int end){ |
15 | node*n=malloc(sizeof(node)); | 16 | node*n=malloc(sizeof(node)); |
16 | n->letter=l; | 17 | n->letter=l; |
17 | n->isEnd=end; | 18 | n->isEnd=end; |
18 | for(int i=0;i<NBCHAR;i++) | 19 | for(int i=0;i<NBCHAR;i++) |
19 | n->next[i]=make_empty_tree(); | 20 | n->next[i]=make_empty_tree(); |
21 | + return n; | ||
20 | } | 22 | } |
21 | void delete_tree(tree t){ | 23 | void delete_tree(tree t){ |
22 | if(is_empty(t))return; | 24 | if(is_empty(t))return; |
@@ -30,10 +32,10 @@ void delete_tree(tree t){ | @@ -30,10 +32,10 @@ void delete_tree(tree t){ | ||
30 | 32 | ||
31 | 33 | ||
32 | //Casual functions | 34 | //Casual functions |
33 | -bool is_empty(tree t){ | 35 | +bool is_empty(const tree t){ |
34 | return t==NULL; | 36 | return t==NULL; |
35 | } | 37 | } |
36 | -bool is_followed(tree t){ | 38 | +bool is_followed(const tree t){ |
37 | int i; | 39 | int i; |
38 | for(i=0;i<NBCHAR;i++){ | 40 | for(i=0;i<NBCHAR;i++){ |
39 | if(t->next[i]!=NULL) | 41 | if(t->next[i]!=NULL) |
@@ -42,50 +44,52 @@ bool is_followed(tree t){ | @@ -42,50 +44,52 @@ bool is_followed(tree t){ | ||
42 | return false; | 44 | return false; |
43 | } | 45 | } |
44 | 46 | ||
45 | -bool is_end(tree t){return t->isEnd%2;} | ||
46 | -bool ends_with_apostrophe(tree t){return (t->isEnd/4)%2;} | 47 | +//functions is_end |
48 | +bool is_end(const tree t){return t->isEnd & 1;} | ||
49 | +bool ends_with_apostrophe(const tree t){return t->isEnd & 2;} | ||
47 | 50 | ||
48 | -bool is_common_end(tree t){return !(t->isEnd/4);} | ||
49 | -bool is_proper_end(tree t){return (t->isEnd/4)%2;} | ||
50 | -bool is_acronyme_end(tree t){return t->isEnd/8;} | 51 | +bool is_common_end(const tree t){return !(t->isEnd & 12);} |
52 | +bool is_proper_end(const tree t){return t->isEnd & 4;} | ||
53 | +bool is_acronyme_end(const tree t){return t->isEnd & 8;} | ||
54 | + | ||
51 | int hash(char c){ | 55 | int hash(char c){ |
52 | //needs to check c wether isalpha | 56 | //needs to check c wether isalpha |
53 | return c%32-1; | 57 | return c%32-1; |
54 | } | 58 | } |
55 | -bool is_proper_end(tree);//if true 1st letter is upper ;//if true all letters are lower | ||
56 | 59 | ||
57 | 60 | ||
58 | 61 | ||
59 | //loading functions | 62 | //loading functions |
60 | -bool addto_tree(tree t,string s){ | ||
61 | - //recursive, need to check all letter in s are alpha or '\'s' | 63 | +bool addto_tree(tree t,const string s,char endKind){ |
64 | + //recursive, need to check all letter in s are alpha or "\'s" | ||
62 | //return wether s is already in t or not | 65 | //return wether s is already in t or not |
63 | bool ret; | 66 | bool ret; |
64 | if(s[0]=='\0'){ | 67 | if(s[0]=='\0'){ |
65 | ret=is_end(t); | 68 | ret=is_end(t); |
66 | - t->isEnd=true; | 69 | + t->isEnd=t->isEnd|endKind; |
67 | return ret; | 70 | return ret; |
68 | } | 71 | } |
69 | if(s[0]=='\''){ | 72 | if(s[0]=='\''){ |
70 | - | 73 | + ret=ends_with_apostrophe(t); |
74 | + t->isEnd=t->isEnd|endKind; | ||
75 | + return ret; | ||
71 | } | 76 | } |
72 | if(t->next[hash(s[0])]==NULL){ | 77 | if(t->next[hash(s[0])]==NULL){ |
73 | - t->next[hash(s[0])]=make_node(s[0],false); | ||
74 | - addto_tree2(t->next[hash(s[0])],s+1); | 78 | + t->next[hash(s[0])]=make_node(s[0],0); |
79 | + addto_tree2(t->next[hash(s[0])],s+1,endKind); | ||
75 | return false; | 80 | return false; |
76 | } | 81 | } |
77 | else | 82 | else |
78 | - addto_tree(t->next[hash(s[0])],s+1,isIn); | 83 | + return addto_tree(t->next[hash(s[0])],s+1,endKind); |
79 | } | 84 | } |
80 | -void addto_tree2(tree t,string s){ | 85 | +void addto_tree2(tree t,const string s,char endKind){ |
81 | //faster than addto_tree | 86 | //faster than addto_tree |
82 | //used when it is known the word is not yet in dictionnary | 87 | //used when it is known the word is not yet in dictionnary |
83 | - if(s[0]=='\0'){ | ||
84 | - t->isEnd=true; | 88 | + if(s[0]=='\0' || s[0]=='\''){ |
89 | + t->isEnd=endKind; | ||
85 | return; | 90 | return; |
86 | } | 91 | } |
87 | - if(s[0]=='\'') | ||
88 | - t->next[hash(s[0])]=make_node(s[0],false); | ||
89 | - addto_tree2(t->next[hash(s[0])],s+1); | 92 | + t->next[hash(s[0])]=make_node(s[0],0); |
93 | + addto_tree2(t->next[hash(s[0])],s+1,endKind); | ||
90 | } | 94 | } |
91 | 95 |
treeh.h
@@ -8,11 +8,11 @@ | @@ -8,11 +8,11 @@ | ||
8 | 8 | ||
9 | #define NBCHAR 26 //A-Z | 9 | #define NBCHAR 26 //A-Z |
10 | 10 | ||
11 | - | 11 | +typedef char *string,byte; |
12 | typedef struct _node node, *tree; | 12 | typedef struct _node node, *tree; |
13 | struct _node{ | 13 | struct _node{ |
14 | char letter; | 14 | char letter; |
15 | - char isEnd; | 15 | + byte isEnd; |
16 | //0 no,1 yes,+2 if ends with 's | 16 | //0 no,1 yes,+2 if ends with 's |
17 | //+4if proper, +8if allUpper | 17 | //+4if proper, +8if allUpper |
18 | node* next[NBCHAR]; | 18 | node* next[NBCHAR]; |
@@ -20,23 +20,24 @@ struct _node{ | @@ -20,23 +20,24 @@ struct _node{ | ||
20 | 20 | ||
21 | tree make_empty_tree();//create a null node* | 21 | tree make_empty_tree();//create a null node* |
22 | node* make_empty_node();//malloc a node and initialize it | 22 | node* make_empty_node();//malloc a node and initialize it |
23 | -node* make_node(char,bool);//id | 23 | +node* make_node(char,byte);//id |
24 | void delete_tree(tree);//free(tree) and delete tree on all t->next | 24 | void delete_tree(tree);//free(tree) and delete tree on all t->next |
25 | 25 | ||
26 | -bool is_empty(tree);//==NULL | ||
27 | -bool is_followed(tree);//if true tree has following letters | 26 | +bool is_empty(const tree);//==NULL |
27 | +bool is_followed(const tree);//if true tree has following letters | ||
28 | 28 | ||
29 | -bool is_end(tree);//if true word can end here | ||
30 | -bool is_proper_end(tree);//if true 1st letter is upper | ||
31 | -bool is_common_end(tree);//if true all letters are lower | ||
32 | -bool is_acronyme_end(tree);//if true all letters are upper | ||
33 | -bool ends_with_apostrophe(tree);//if true word can be word's | 29 | +bool is_end(const tree);//if true word can end here |
30 | +bool ends_with_apostrophe(const tree);//if true word can be word's | ||
31 | +bool is_common_end(const tree);//if true all letters are lower | ||
32 | +bool is_proper_end(const tree);//if true 1st letter is upper | ||
33 | +bool is_acronyme_end(const tree);//if true all letters are upper | ||
34 | 34 | ||
35 | int hash(char);//need to check if isalpha | 35 | int hash(char);//need to check if isalpha |
36 | 36 | ||
37 | //recursive can only be called in a dico instance | 37 | //recursive can only be called in a dico instance |
38 | -void addto_tree(tree,string,int); | ||
39 | -void addto_tree2(tree,string); | 38 | +// |
39 | +bool addto_tree(tree,const string,byte); | ||
40 | +void addto_tree2(tree,const string,byte); | ||
40 | 41 | ||
41 | 42 | ||
42 | #endif //TREEH_H | 43 | #endif //TREEH_H |