Conditions | 27 |
Paths | > 20000 |
Total Lines | 141 |
Code Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
36 | public static function fromNfa(Nfa $nfa): Dfa |
||
37 | { |
||
38 | $dfa = new Dfa(); |
||
39 | (new self($dfa, $nfa))->run(); |
||
40 | |||
41 | $stateQuery = []; |
||
42 | $nonEquivalentStates = []; |
||
43 | $dfaStates = $dfa->getStateMap()->getStateList(); |
||
44 | $devilState = max($dfaStates) + 1; |
||
45 | $dfaStates[] = $devilState; |
||
46 | |||
47 | $transitionMap = []; |
||
48 | |||
49 | foreach ($dfaStates as $stateA) { |
||
50 | foreach ($dfaStates as $stateB) { |
||
51 | $marked = $nonEquivalentStates[$stateA][$stateB] ?? false; |
||
52 | if ($marked) { |
||
53 | continue; |
||
54 | } |
||
55 | $isFinishStateA = $stateA == $devilState |
||
56 | ? false |
||
57 | : $dfa->getStateMap()->isFinishState($stateA); |
||
58 | $isFinishStateB = $stateB == $devilState |
||
59 | ? false |
||
60 | : $dfa->getStateMap()->isFinishState($stateB); |
||
61 | if ($isFinishStateA == $isFinishStateB) { |
||
62 | continue; |
||
63 | } |
||
64 | $nonEquivalentStates[$stateA][$stateB] = true; |
||
65 | $nonEquivalentStates[$stateB][$stateA] = true; |
||
66 | $stateQuery[] = [$stateA, $stateB]; |
||
67 | } |
||
68 | $transitionMap[$stateA][$devilState] = $dfa->getSymbolTable()->getSymbolList(); |
||
69 | } |
||
70 | |||
71 | // Making all finish states non-equivalent to distinguish regular expressions |
||
72 | /*foreach ($dfa->getStateMap()->getFinishStateList() as $stateA) { |
||
73 | foreach ($dfa->getStateMap()->getFinishStateList() as $stateB) { |
||
74 | $marked = $nonEquivalentStates[$stateA][$stateB] ?? false; |
||
75 | if ($marked) { |
||
76 | continue; |
||
77 | } |
||
78 | $nonEquivalentStates[$stateA][$stateB] = true; |
||
79 | $nonEquivalentStates[$stateB][$stateA] = true; |
||
80 | $stateQuery[] = [$stateA, $stateB]; |
||
81 | } |
||
82 | }*/ |
||
83 | |||
84 | foreach ($dfa->getTransitionMap()->getTransitionList() as $sourceState => $transitions) { |
||
85 | $devilStateSymbols = $dfa->getSymbolTable()->getSymbolList(); |
||
86 | foreach ($transitions as $targetState => $symbols) { |
||
87 | $devilStateSymbols = array_diff($devilStateSymbols, $symbols); |
||
88 | $transitionMap[$sourceState][$targetState] = $symbols; |
||
89 | } |
||
90 | $transitionMap[$sourceState][$devilState] = $devilStateSymbols; |
||
91 | } |
||
92 | |||
93 | while (!empty($stateQuery)) { |
||
94 | [$firstTargetState, $secondTargetState] = array_pop($stateQuery); |
||
95 | |||
96 | foreach ($dfaStates as $stateA) { |
||
97 | foreach ($dfaStates as $stateB) { |
||
98 | $isMarked = $nonEquivalentStates[$stateA][$stateB] ?? false; |
||
99 | if ($isMarked) { |
||
100 | continue; |
||
101 | } |
||
102 | $symbolsA = $transitionMap[$stateA][$firstTargetState] ?? []; |
||
103 | $symbolsB = $transitionMap[$stateB][$secondTargetState] ?? []; |
||
104 | $symbols = array_intersect($symbolsA, $symbolsB); |
||
105 | if (!empty($symbols)) { |
||
106 | $nonEquivalentStates[$stateA][$stateB] = true; |
||
107 | $nonEquivalentStates[$stateB][$stateA] = true; |
||
108 | $stateQuery[] = [$stateA, $stateB]; |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | $equivalentStates = []; |
||
115 | $nextClass = 1; |
||
116 | $classes = []; |
||
117 | foreach ($dfaStates as $stateA) { |
||
118 | if (!isset($classes[$stateA])) { |
||
119 | $class = $nextClass++; |
||
120 | foreach ($dfaStates as $stateB) { |
||
121 | $isMarked = $nonEquivalentStates[$stateA][$stateB] ?? false; |
||
122 | if (!$isMarked) { |
||
123 | $classes[$stateB] = $class; |
||
124 | if ($stateB != $devilState) { |
||
125 | $equivalentStates[$class][$stateB] = true; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | $minimizedDfa = new Dfa(); |
||
133 | $minimizedDfa->setSymbolTable($dfa->getSymbolTable()); |
||
134 | $nfaStartStates = $nfa->getStateMap()->getStartStateList(); |
||
135 | $nfaFinishStates = $nfa->getStateMap()->getFinishStateList(); |
||
136 | $newDfaStates = []; |
||
137 | foreach ($equivalentStates as $newState => $dfaStates) { |
||
138 | $newValue = []; |
||
139 | foreach (array_keys($dfaStates) as $dfaState) { |
||
140 | $nfaStates = $dfa->getStateMap()->getStateValue($dfaState); |
||
141 | $newValue = array_merge($newValue, $nfaStates); |
||
142 | $newDfaStates[$dfaState] = $newState; |
||
143 | } |
||
144 | $newValue = array_unique($newValue); |
||
145 | sort($newValue); |
||
146 | $minimizedDfa->getStateMap()->createState($newValue); |
||
147 | $isStartState = !empty(array_intersect($nfaStartStates, $newValue)); |
||
148 | if ($isStartState) { |
||
149 | $minimizedDfa->getStateMap()->addStartState($newState); |
||
150 | } |
||
151 | $isFinishState = !empty(array_intersect($nfaFinishStates, $newValue)); |
||
152 | if ($isFinishState) { |
||
153 | $minimizedDfa->getStateMap()->addFinishState($newState); |
||
154 | } |
||
155 | } |
||
156 | $transitionMap = []; |
||
157 | foreach ($dfa->getTransitionMap()->getTransitionList() as $sourceState => $transitions) { |
||
158 | foreach ($transitions as $targetState => $symbols) { |
||
159 | $newSourceState = $newDfaStates[$sourceState]; |
||
160 | $newTargetState = $newDfaStates[$targetState]; |
||
161 | $transitionMap[$newSourceState][$newTargetState] = array_unique( |
||
162 | array_merge( |
||
163 | $transitionMap[$newSourceState][$newTargetState] ?? [], |
||
164 | $symbols |
||
165 | ) |
||
166 | ); |
||
167 | } |
||
168 | } |
||
169 | foreach ($transitionMap as $sourceState => $transitions) { |
||
170 | foreach ($transitions as $targetState => $symbols) { |
||
171 | sort($symbols); |
||
172 | $minimizedDfa->getTransitionMap()->addTransition($sourceState, $targetState, $symbols); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | return $minimizedDfa; |
||
177 | } |
||
313 |