diff --git a/treeh.c b/treeh.c index f4975b4..489ea31 100644 --- a/treeh.c +++ b/treeh.c @@ -7,16 +7,18 @@ tree make_empty_tree(){ node* make_empty_node(){ node*n=malloc(sizeof(node)); n->letter='\0'; - n->isEnd=false; + n->isEnd=0; for(int i=0;inext[i]=make_empty_tree(); + return n; } -node* make_node(char l,bool end){ +node* make_node(char l,int end){ node*n=malloc(sizeof(node)); n->letter=l; n->isEnd=end; for(int i=0;inext[i]=make_empty_tree(); + return n; } void delete_tree(tree t){ if(is_empty(t))return; @@ -30,10 +32,10 @@ void delete_tree(tree t){ //Casual functions -bool is_empty(tree t){ +bool is_empty(const tree t){ return t==NULL; } -bool is_followed(tree t){ +bool is_followed(const tree t){ int i; for(i=0;inext[i]!=NULL) @@ -42,50 +44,52 @@ bool is_followed(tree t){ return false; } -bool is_end(tree t){return t->isEnd%2;} -bool ends_with_apostrophe(tree t){return (t->isEnd/4)%2;} +//functions is_end +bool is_end(const tree t){return t->isEnd & 1;} +bool ends_with_apostrophe(const tree t){return t->isEnd & 2;} -bool is_common_end(tree t){return !(t->isEnd/4);} -bool is_proper_end(tree t){return (t->isEnd/4)%2;} -bool is_acronyme_end(tree t){return t->isEnd/8;} +bool is_common_end(const tree t){return !(t->isEnd & 12);} +bool is_proper_end(const tree t){return t->isEnd & 4;} +bool is_acronyme_end(const tree t){return t->isEnd & 8;} + int hash(char c){ //needs to check c wether isalpha return c%32-1; } -bool is_proper_end(tree);//if true 1st letter is upper ;//if true all letters are lower //loading functions -bool addto_tree(tree t,string s){ - //recursive, need to check all letter in s are alpha or '\'s' +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=true; + 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],false); - addto_tree2(t->next[hash(s[0])],s+1); + t->next[hash(s[0])]=make_node(s[0],0); + addto_tree2(t->next[hash(s[0])],s+1,endKind); return false; } else - addto_tree(t->next[hash(s[0])],s+1,isIn); + return addto_tree(t->next[hash(s[0])],s+1,endKind); } -void addto_tree2(tree t,string s){ +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'){ - t->isEnd=true; + if(s[0]=='\0' || s[0]=='\''){ + t->isEnd=endKind; return; } - if(s[0]=='\'') - t->next[hash(s[0])]=make_node(s[0],false); - addto_tree2(t->next[hash(s[0])],s+1); + t->next[hash(s[0])]=make_node(s[0],0); + addto_tree2(t->next[hash(s[0])],s+1,endKind); } diff --git a/treeh.h b/treeh.h index d42ea04..551ba9a 100644 --- a/treeh.h +++ b/treeh.h @@ -8,11 +8,11 @@ #define NBCHAR 26 //A-Z - +typedef char *string,byte; typedef struct _node node, *tree; struct _node{ char letter; - char isEnd; + byte isEnd; //0 no,1 yes,+2 if ends with 's //+4if proper, +8if allUpper node* next[NBCHAR]; @@ -20,23 +20,24 @@ struct _node{ tree make_empty_tree();//create a null node* node* make_empty_node();//malloc a node and initialize it -node* make_node(char,bool);//id +node* make_node(char,byte);//id void delete_tree(tree);//free(tree) and delete tree on all t->next -bool is_empty(tree);//==NULL -bool is_followed(tree);//if true tree has following letters +bool is_empty(const tree);//==NULL +bool is_followed(const tree);//if true tree has following letters -bool is_end(tree);//if true word can end here -bool is_proper_end(tree);//if true 1st letter is upper -bool is_common_end(tree);//if true all letters are lower -bool is_acronyme_end(tree);//if true all letters are upper -bool ends_with_apostrophe(tree);//if true word can be word's +bool is_end(const tree);//if true word can end here +bool ends_with_apostrophe(const tree);//if true word can be word's +bool is_common_end(const tree);//if true all letters are lower +bool is_proper_end(const tree);//if true 1st letter is upper +bool is_acronyme_end(const tree);//if true all letters are upper int hash(char);//need to check if isalpha //recursive can only be called in a dico instance -void addto_tree(tree,string,int); -void addto_tree2(tree,string); +// +bool addto_tree(tree,const string,byte); +void addto_tree2(tree,const string,byte); #endif //TREEH_H -- libgit2 0.21.2