Blame view

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