-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.h
80 lines (64 loc) · 1.74 KB
/
type.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
75
76
77
78
79
80
#ifndef TYPE_H
#define TYPE_H
#include <iostream>
using namespace std;
enum SymbolType{
NonTerm,
Term
};
class Symbol {
char val;
SymbolType type; // 0 nonterm, 1 term
bool start;
public:
// Symbol() {
// val = '\0';
// type = SymbolType::Term;
// start = false;
// }
Symbol(char _val='\0', SymbolType _type=SymbolType::NonTerm, bool _start=false) {
val = _val;
type = _type;
start = _start;
}
bool operator==(const Symbol& a) const {
return type == a.type && val == a.val;
}
bool operator!=(const Symbol& a) const {
return type != a.type || val != a.val;
}
bool isStart() const {
return start;
}
char getVal() const {
return val;
}
SymbolType getType() const {
return type;
}
friend ostream& operator<<(std::ostream &s, const Symbol& symbol) {
return s << symbol.getVal() << " ";
}
};
template <>
struct std::hash<Symbol>
{
std::size_t operator()(const Symbol& symbol) const noexcept {
if (symbol.getType() == SymbolType::Term) {
return hash<string>{}(to_string(symbol.getVal()) + "@term");
} else {
return hash<string>{}(to_string(symbol.getVal()) + "@nonterm");
}
}
};
template <>
struct std::hash<pair<Symbol, Symbol>>
{
std::size_t operator()(const pair<Symbol, Symbol>& p) const noexcept {
string concat;
concat += to_string(p.first.getVal()) + "@" + ((p.first.getType() == SymbolType::Term) ? "Term" : "NonTerm") + "@";
concat += to_string(p.second.getVal()) + "@" + ((p.second.getType() == SymbolType::Term) ? "Term" : "NonTerm");
return hash<string>{}(concat);
}
};
#endif