@@ -82,7 +82,7 @@ public function findMatchingStates(string $string, int $editDistance): array
82
82
// Initial states
83
83
$ states = $ this ->getReachableStates (0 , $ editDistance );
84
84
85
- $ this ->loopOverEveryCharacter ($ string , function (int $ mappedChar, $ char ) use (&$ states , $ editDistance ) {
85
+ $ this ->loopOverEveryCharacter ($ string , function (int $ mappedChar ) use (&$ states , $ editDistance ) {
86
86
$ statesStar = new CostAnnotatedStateSet (); // This is S∗ in the paper
87
87
88
88
foreach ($ states ->all () as $ state => $ cost ) {
@@ -98,7 +98,7 @@ public function findMatchingStates(string $string, int $editDistance): array
98
98
$ newState = (int ) ($ state * $ this ->config ->getAlphabetSize () + $ i );
99
99
100
100
if ($ this ->stateSet ->has ($ newState )) {
101
- if ($ i === $ this -> getAlphabet ()-> map ( $ char , $ this -> config -> getAlphabetSize ()) ) {
101
+ if ($ i === $ mappedChar ) {
102
102
// Match
103
103
$ statesStarC ->add ($ newState , $ cost );
104
104
} elseif ($ cost + 1 <= $ editDistance ) {
@@ -181,12 +181,14 @@ private function getReachableStates(int $startState, int $editDistance, int $cur
181
181
// A state is always able to reach itself
182
182
$ reachable ->add ($ startState , $ currentDistance );
183
183
184
- for ($ i = 0 ; $ i <= $ editDistance ; $ i ++) {
185
- for ($ c = 0 ; $ c < $ this ->config ->getAlphabetSize (); $ c ++) {
186
- $ state = $ startState + $ c * $ i ;
187
- if ($ this ->stateSet ->has ($ state )) {
188
- $ reachable ->add ($ startState , $ currentDistance );
189
- }
184
+ if ($ currentDistance >= $ editDistance ) {
185
+ return $ reachable ;
186
+ }
187
+
188
+ for ($ c = 1 ; $ c <= $ this ->config ->getAlphabetSize (); $ c ++) {
189
+ $ state = $ startState * $ this ->config ->getAlphabetSize () + $ c ;
190
+ if ($ this ->stateSet ->has ($ state )) {
191
+ $ reachable = $ reachable ->mergeWith ($ this ->getReachableStates ($ state , $ editDistance , $ currentDistance + 1 ));
190
192
}
191
193
}
192
194
@@ -203,7 +205,7 @@ private function loopOverEveryCharacter(string $string, \Closure $closure): void
203
205
204
206
foreach (mb_str_split ($ indexedSubstring ) as $ char ) {
205
207
$ mappedChar = $ this ->alphabet ->map ($ char , $ this ->config ->getAlphabetSize ());
206
- $ closure ($ mappedChar, $ char );
208
+ $ closure ($ mappedChar );
207
209
}
208
210
}
209
211
}
0 commit comments