treeh.c
2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "treeh.h"
//initializers & destroyer
tree make_empty_tree(){
return NULL;
}
node* make_empty_node(){
node*n=malloc(sizeof(node));
n->letter='\0';
n->isEnd=0;
for(int i=0;i<NBCHAR;i++)
n->next[i]=make_empty_tree();
return n;
}
node* make_node(char l,int end){
node*n=malloc(sizeof(node));
n->letter=l;
n->isEnd=end;
for(int i=0;i<NBCHAR;i++)
n->next[i]=make_empty_tree();
return n;
}
void delete_tree(tree t){
if(is_empty(t))return;
for(int i=0;i<NBCHAR;i++)
delete_tree(t->next[i]);
free(t);
}
//Casual functions
bool is_empty(const tree t){
return t==NULL;
}
bool is_followed(const tree t){
int i;
for(i=0;i<NBCHAR;i++){
if(t->next[i]!=NULL)
return true;
}
return false;
}
//functions is_end
bool is_end(const tree t){return t->isEnd & 13;}//0b00010101
bool ends_with_apostrophe(const tree t){return t->isEnd & 26;}//0b00101010
bool is_common_end(const tree t){return t->isEnd & 3;}//0b00000011
bool is_proper_end(const tree t){return t->isEnd & 12;}//0b00001100
bool is_acronyme_end(const tree t){return t->isEnd & 48;}//0b00110000
int hash(char c){
//needs to check c wether isalpha
return c%32-1;
}
//loading functions
bool addto_tree(tree t,const string s,char endKind){
//recursive, need to check all letter in s are alpha or "\'s"
//return wether s is already in t or not
bool ret;
if(s[0]=='\0'){
ret=is_end(t);
t->isEnd=t->isEnd|endKind;
return ret;
}
if(s[0]=='\''){
ret=ends_with_apostrophe(t);
t->isEnd=t->isEnd|endKind;
return ret;
}
if(t->next[hash(s[0])]==NULL){
t->next[hash(s[0])]=make_node(s[0],0);
addto_tree2(t->next[hash(s[0])],s+1,endKind);
return false;
}
else
return addto_tree(t->next[hash(s[0])],s+1,endKind);
}
void addto_tree2(tree t,const string s,char endKind){
//faster than addto_tree
//used when it is known the word is not yet in dictionnary
if(s[0]=='\0' || s[0]=='\''){
t->isEnd=endKind;
return;
}
t->next[hash(s[0])]=make_node(s[0],0);
addto_tree2(t->next[hash(s[0])],s+1,endKind);
}