Skip to content

Commit

Permalink
Fix getReachableStates() method (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi authored Oct 5, 2023
1 parent 394b25d commit 0c8e1e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/StateSetIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,15 @@ private function getReachableStates(int $startState, int $editDistance, int $cur
// A state is always able to reach itself
$reachable->add($startState, $currentDistance);

for ($i = 0; $i <= $editDistance; $i++) {
for ($c = 0; $c < $this->config->getAlphabetSize(); $c++) {
$state = $startState + $c * $i;
if ($this->stateSet->has($state)) {
$reachable->add($startState, $currentDistance);
}
if ($currentDistance === $editDistance) {
return $reachable;
}

for ($c = 1; $c <= $this->config->getAlphabetSize(); $c++) {
$state = $startState * $this->config->getAlphabetSize() + $c;
if ($this->stateSet->has($state)) {
$reachable->add($state, $currentDistance + 1);
$reachable = $reachable->mergeWith($this->getReachableStates($state, $editDistance, $currentDistance + 1));
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/StateSetIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ public function testAssassinCanBeFound(): void

$this->assertSame(['assassin'], $stateSetIndex->find('assasin', 2));
}
}
}

0 comments on commit 0c8e1e0

Please sign in to comment.