Blame view

treeh.c 1.99 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';
08822d77   bjeanlou   Update 9 withHash
10
    n->isEnd=NOT_AN_END;
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
  }
fd1ee590   bjeanlou   Update7 withHash
15
  node* make_node(char l,byte 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();
a89bb625   bjeanlou   Update 8 withHash
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
fd1ee590   bjeanlou   Update7 withHash
48
  bool is_end(const tree t){return t->isEnd;}
08822d77   bjeanlou   Update 9 withHash
49
50
  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
   
08822d77   bjeanlou   Update 9 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
  
  
  //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){
a89bb625   bjeanlou   Update 8 withHash
63
    byte endKind;
11c78e6d   bjeanlou   update5 withHash
64
    int i=1;
a89bb625   bjeanlou   Update 8 withHash
65
    //1st, let's consider all letters
11c78e6d   bjeanlou   update5 withHash
66
    if(!isalpha(s[0]))
a89bb625   bjeanlou   Update 8 withHash
67
      return NOT_AN_END;
11c78e6d   bjeanlou   update5 withHash
68
69
    
    if(islower(s[0])){
a89bb625   bjeanlou   Update 8 withHash
70
      endKind=COMMON_END;
8c671959   bjeanlou   No more seg fault
71
      while(islower(s[i]))i++;
11c78e6d   bjeanlou   update5 withHash
72
73
    }
    else {//if isupper(s[0])
11c78e6d   bjeanlou   update5 withHash
74
75
      if(isalpha(s[1])){
        i++;
a89bb625   bjeanlou   Update 8 withHash
76
77
        if(islower(s[1])){
  	endKind=PROPER_END;
8c671959   bjeanlou   No more seg fault
78
  	while(islower(s[i]))i++;
a89bb625   bjeanlou   Update 8 withHash
79
        }
11c78e6d   bjeanlou   update5 withHash
80
        else{//if isupper(s[1])
a89bb625   bjeanlou   Update 8 withHash
81
  	endKind=ACRONYME_END;
8c671959   bjeanlou   No more seg fault
82
  	while(isupper(s[i]))i++;
11c78e6d   bjeanlou   update5 withHash
83
84
85
86
  	endKind=16;
        }
      }
    }
a89bb625   bjeanlou   Update 8 withHash
87
    //then let's consider the end
08822d77   bjeanlou   Update 9 withHash
88
89
90
91
92
93
    if(s[i]=='\0'){
      return (endKind & STRAIGHT_END);
    }
    if(s[i]=='\'' && s[i+1]=='s' && s[i+2]=='\0'){
      return (endKind & APOSTROPHE_END);
    }
a89bb625   bjeanlou   Update 8 withHash
94
    return NOT_AN_END;
a637cab8   bjeanlou   update1 withHash
95
  }
11c78e6d   bjeanlou   update5 withHash
96
  bool is_word(const byte endKind){
fd1ee590   bjeanlou   Update7 withHash
97
    return endKind!=0;
11c78e6d   bjeanlou   update5 withHash
98
  }