-
Notifications
You must be signed in to change notification settings - Fork 0
/
finite-automata-state.js
76 lines (63 loc) · 2.07 KB
/
finite-automata-state.js
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
class FiniteAutomataState {
constructor(options) {
this._transitionMap = {};
this._transitionMapCheck = {};
this._epsilonTransitions = [];
this._epsilonTransitionsCheck = new Set();
this._finiteAutomata = null;
if (options && options.constructor === Object) {
if (options.hasOwnProperty('finiteAutomata')) {
this._finiteAutomata = options.finiteAutomata;
}
}
this._id = this._finiteAutomata === null ? 0 : this._finiteAutomata.provisionId();
}
addTransition(chars, state) {
const stateId = state.getId();
if (!this._transitionMap.hasOwnProperty(chars)) {
this._transitionMap[chars] = [state];
this._transitionMapCheck[chars] = new Set([stateId]);
} else if (!this._transitionMapCheck[chars].has(stateId)) {
this._transitionMap[chars].push(state);
this._transitionMapCheck[chars].add(stateId);
}
}
hasTransition(chars) {
return this._transitionMap.hasOwnProperty(chars);
}
getTransition(chars) {
if (!this._transitionMap.hasOwnProperty(chars)) {
return [];
}
return this._transitionMap[chars];
}
addEpsilonTransition(state) {
const stateId = state.getId();
if (!this._epsilonTransitionsCheck.has(stateId)) {
this._epsilonTransitions.push(state);
this._epsilonTransitionsCheck.add(stateId);
}
}
hasEpsilonTransition() {
return this._epsilonTransitions.length > 0;
}
getEpsilonTransitions() {
return this._epsilonTransitions;
}
setId(id) {
this._id = id;
}
getId(id) {
return this._id;
}
deprovision() {
if (this._finiteAutomata !== null) {
this._finiteAutomata.deprovisionId(this._id);
}
this._transitionMap = {};
this._transitionMapCheck = {};
this._epsilonTransitions.length = 0;
this._epsilonTransitions.clear();
}
}
module.exports = FiniteAutomataState;