-
Notifications
You must be signed in to change notification settings - Fork 0
/
automaton_builder.h
47 lines (45 loc) · 1.3 KB
/
automaton_builder.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
#pragma once
#include <iostream>
#include "automaton.h"
#include "hash_table.h"
#include <set>
static int maxOneBit(int num) {
int res = 0;
while (num != 0) {
num >>= 1;
++res;
}
return res - 1;
}
// A wrap class of Automaton, that allows to add words to it keeping it minimal.
class AutomatonBuilder {
Automaton* const aut;
HashTable<Automaton::Node, int> registr;
vector<bool> registered;
vector<int> incoming;
vector<unsigned> hash;
public:
AutomatonBuilder(Automaton* const automaton, unsigned int size):
aut(automaton), registr(maxOneBit(size)), registered(size, false), incoming(size, 0), hash(size, 0) {
registered[aut->startNode] = true;
registr.add(hash[aut->startNode], aut->startNode);
}
private:
int cloneNode(int node);
void link(int node1, int node2, char c);
void relink(int node1, int node2, char c);
int addNode(const Automaton::Node& node, unsigned nodeHash);
int findInRegistr(int node) const;
void removeNode(int node);
void addBranch(int node, const string& str);
void replaceOrRegister(int node);
bool replaceOrRegisterWithAnswer(int node);
void makeTerminal(int node);
public:
void addWord(const string& word);
void printStats() {
std::cout << registr.collisions() << " / " << aut->nodes.size() << "\n";
}
// Slow. For debuging.
bool checkMinimality() const;
};