treeh.c 2.58 KB
#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,byte 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;}
bool is_straight_end(const tree t){return t->isEnd & STRAIGHT_END;}
bool ends_with_apostrophe(const tree t){return t->isEnd & APOSTROPHE_END;}
 
bool is_common_end(const tree t){return t->isEnd & COMMON_END ;}
bool is_proper_end(const tree t){return t->isEnd & PROPER_END;}
bool is_acronyme_end(const tree t){return t->isEnd & ACRONYME_END;}



//needs to check c wether isalpha
int hash(char c){return c%32-1;}
bool ischar_of_word(char c){return isalpha(c) || c=='\'';}

byte end_kind(const string s){
  byte endKind=0;
  int i=1;
  if(!isalpha(s[0]))
    return 0;
  
  if(islower(s[0])){
    endKind=1;
    while(islower(s[i]));
  }
  else {//if isupper(s[0])
      endKind=4;
    if(isalpha(s[1])){
      i++;
      if(islower(s[1]))
	while(islower(s[i]));
      else{//if isupper(s[1])
	while(isupper(s[i]));
	endKind=16;
      }
    }
  }
  endKind*=( (s[i]=='\0') + 2* (s[i]=='\''&&s[i+1]=='s'&&s[i+2]=='\0') );
  return endKind;
}
bool is_word(const byte endKind){
  return endKind!=0;
}




//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' || s[0]=='\''){
    ret=t->isEnd & endKind;
    t->isEnd=t->isEnd|endKind;
    return ret;
  }
  if(is_empty(t->next[hash(s[0])])){
    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);
}