Blame view

treeh.c 2.53 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
11c78e6d   bjeanlou   update5 withHash
48
49
  bool is_end(const tree t){return t->isEnd & 21;}//0b00010101
  bool ends_with_apostrophe(const tree t){return t->isEnd & 42;}//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
  
11c78e6d   bjeanlou   update5 withHash
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
  
  
  //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[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;
a637cab8   bjeanlou   update1 withHash
85
  }
11c78e6d   bjeanlou   update5 withHash
86
87
88
89
  bool is_word(const byte endKind){
    return end_Kind!=0;
  }
  
4043090f   bjeanlou   update2 withHash
90
  
435232c3   bjeanlou   update4 withHash
91
92
93
  
  
  //loading functions
f48297ea   bjeanlou   Update1 treeh
94
95
  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
96
    //return wether s is already in t or not
435232c3   bjeanlou   update4 withHash
97
    bool ret;
11c78e6d   bjeanlou   update5 withHash
98
99
    if(s[0]=='\0' || s[0]=='\''){
      ret=t->isEnd & endKind;
f48297ea   bjeanlou   Update1 treeh
100
      t->isEnd=t->isEnd|endKind;
435232c3   bjeanlou   update4 withHash
101
102
      return ret;
    }
11c78e6d   bjeanlou   update5 withHash
103
    if(is_empty(t->next[hash(s[0])])){
f48297ea   bjeanlou   Update1 treeh
104
105
      t->next[hash(s[0])]=make_node(s[0],0);
      addto_tree2(t->next[hash(s[0])],s+1,endKind);
435232c3   bjeanlou   update4 withHash
106
      return false;
4043090f   bjeanlou   update2 withHash
107
    }
435232c3   bjeanlou   update4 withHash
108
    else    
f48297ea   bjeanlou   Update1 treeh
109
      return addto_tree(t->next[hash(s[0])],s+1,endKind);
4043090f   bjeanlou   update2 withHash
110
  }
f48297ea   bjeanlou   Update1 treeh
111
  void addto_tree2(tree t,const string s,char endKind){
c6a94513   bjeanlou   treeh.ch OK
112
113
    //faster than addto_tree
    //used when it is known the word is not yet in dictionnary
f48297ea   bjeanlou   Update1 treeh
114
115
    if(s[0]=='\0' || s[0]=='\''){
      t->isEnd=endKind;
4043090f   bjeanlou   update2 withHash
116
117
      return;
    }
f48297ea   bjeanlou   Update1 treeh
118
119
    t->next[hash(s[0])]=make_node(s[0],0);
    addto_tree2(t->next[hash(s[0])],s+1,endKind);
4043090f   bjeanlou   update2 withHash
120
  }
435232c3   bjeanlou   update4 withHash