Blame view

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