forked from lfkdsk/PracticeCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDfaState.java
More file actions
116 lines (98 loc) · 2.56 KB
/
Copy pathDfaState.java
File metadata and controls
116 lines (98 loc) · 2.56 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package sample;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* Dfa 状态
*
* @author liufengkai
* Created by liufengkai on 16/7/10.
*/
public class DfaState implements Comparable<DfaState> {
private static int DFA_ID_COUNT = 0;
/**
* state id
*/
private int stateId;
/**
* transition set
* char / set of dfaState
*/
private Map<Integer, DfaState> transitionSet;
private DfaState parentState;
private Integer parentInput;
/**
* 构造方法
*
* @param input 输入串
* @param parentState 父节点
*/
public DfaState(Integer input, DfaState parentState) {
this.parentInput = input;
this.parentState = parentState;
this.stateId = DFA_ID_COUNT++;
this.transitionSet = new HashMap<>();
}
/**
* 添加一条转移语句
*
* @param input 输入字符
* @param state 下一个状态
* @return 返回添加状态
*/
public DfaState addTransition(int input, DfaState state) {
if (!transitionSet.containsKey(input)) {
transitionSet.put(input, state);
}
return state;
}
public DfaState getTransitionInput(int input) {
return getTransitionSet().get(input);
}
public int getStateId() {
return stateId;
}
public static int getTotalNumber() {
return DFA_ID_COUNT;
}
public Map<Integer, DfaState> getTransitionSet() {
return transitionSet;
}
public DfaState getParentState() {
return parentState;
}
@Override
public int compareTo(DfaState o) {
return 0;
}
public int getParentInput() {
return parentInput;
}
/**
* 打印状态
*/
public void printState() {
System.out.println("state : " + getStateId());
for (Integer integer : transitionSet.keySet()) {
System.out.println("symbol: " +
(char) integer.intValue() + " to :" +
transitionSet.get(integer).getStateId());
transitionSet.get(integer).printState();
}
}
/**
* 返回结束状态
*
* @param list 传入结束状态
*/
public void returnEndList(ArrayList<DfaState> list) {
for (Integer key : transitionSet.keySet()) {
DfaState cur = transitionSet.get(key);
if (cur.getTransitionSet().isEmpty()) {
list.add(cur);
} else {
cur.returnEndList(list);
}
}
}
}