-
Notifications
You must be signed in to change notification settings - Fork 0
/
automaton.h
61 lines (58 loc) · 1.48 KB
/
automaton.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
#pragma once
#include "fast_erase_vector.h"
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
using std::vector;
using std::string;
using std::ofstream;
using std::ifstream;
class AutomatonBuilder;
class Automaton {
struct Edge {
char ch;
int to;
explicit Edge(char c = '\0', int t = -1): ch(c), to(t) { }
bool operator<(const Edge& edge) const {
if (ch < edge.ch) {
return true;
} else if (ch > edge.ch) {
return false;
} else {
return to < edge.to;
}
}
bool operator==(const Edge& edge) const {
return ch == edge.ch && to == edge.to;
}
unsigned hash() const;
};
class Node {
public:
bool isTerm;
vector<Edge> edges;
Node(): isTerm(false) { }
explicit Node(bool term): isTerm(term) { }
Node(const Node& node): isTerm(node.isTerm), edges(node.edges) { }
unsigned hash() const;
int next(char c) const;
void link(char c, int node);
int relink(char c, int node);
bool operator==(const Node& node) const;
void writeToBinaryStream(ofstream& out) const;
bool readFromBinaryStream(ifstream& in);
};
private:
int startNode;
FastEraseVector<Node> nodes;
void rightLanguage(int node, vector<string>& strs, string& myString) const;
public:
friend class AutomatonBuilder;
explicit Automaton(int size = 0): nodes(size) {
startNode = nodes.add(Node(false));
}
void allSuffixes(const string& word, vector<string>* res) const;
void writeToBinaryStream(ofstream& out) const;
void readFromBinaryStream(ifstream& in);
};