Blame view

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