This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.h
74 lines (60 loc) · 1.48 KB
/
rules.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* rules.h: Rules dictionary functions
*
* Author: Giorgio Pristia
*/
#ifndef RULES_H
#define RULES_H
#include "types.h"
typedef unsigned int hash;
struct rule{
state st_from,
st_dest;
symbol ch_from,
ch_dest;
int mv_dest;
};
typedef struct rule_dict rule_dict;
typedef struct rule_list rule_list;
typedef struct rule_dest rule_dest;
struct rule_dict{
rule_list **rule;
size_t size,
count;
};
struct rule_list{
hash hash;
state st;
symbol ch;
rule_dest *dest;
rule_list *next;
};
/* List of non deterministic destinations for each rule */
struct rule_dest{
state st;
int mv;
symbol ch;
rule_dest *next;
};
static inline hash hash_f(state st, symbol ch){
return st * 5 + ch * 3;
}
/* Return the dictionary or NULL if malloc fails */
rule_dict *new_rule_dict ();
static inline rule_list *rule_list_find(rule_list *rule, state st, symbol ch){
for(; rule; rule = rule->next)
if(rule->st == st &&
rule->ch == ch)
return rule;
return NULL;
}
static inline rule_dest *rule_dict_find(rule_dict *dict, state st, symbol ch){
hash h = hash_f(st, ch);
rule_list *r =
rule_list_find(dict->rule[h % dict->size], st, ch);
return r ? r->dest : NULL;
}
/* Return 0 on success, else -1 */
int rule_dict_insert(rule_dict*, struct rule*);
void delete_rule_dict(rule_dict*);
#endif