-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added an Utf8Alphabet implementation * Completely revamped implementation. Dropped hierarchical logic to reduce storage requirements * Added tests for the Levenshtein implementation * Fixed Utf8Alphabet * Added support for Damerau-Levenshtein * Fixed algorithm * Removed Damerau-Levenshtein attempt * CS
- Loading branch information
Showing
16 changed files
with
309 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Toflar\StateSetIndex\Alphabet; | ||
|
||
class Utf8Alphabet implements AlphabetInterface | ||
{ | ||
/** | ||
* @var array<int, array<string, int>> | ||
*/ | ||
private array $cache = []; | ||
|
||
public function map(string $char, int $alphabetSize): int | ||
{ | ||
if (!isset($this->cache[$alphabetSize][$char])) { | ||
// +1 in order to never assign 0 | ||
$this->cache[$alphabetSize][$char] = (mb_ord($char, 'UTF-8') % $alphabetSize) + 1; | ||
} | ||
|
||
return $this->cache[$alphabetSize][$char]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Toflar\StateSetIndex\DataStore; | ||
|
||
interface DataStoreInterface | ||
{ | ||
public function add(int $state, string $string): void; | ||
|
||
/** | ||
* Returns the matching strings per state. Key is the state and the value is an array of matching strings | ||
* for that state. | ||
* | ||
* @return array<int,array<string>> | ||
*/ | ||
public function getForStates(array $states = []): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Toflar\StateSetIndex\DataStore; | ||
|
||
class InMemoryDataStore implements DataStoreInterface | ||
{ | ||
/** | ||
* @var array<int, array<string>> | ||
*/ | ||
private array $data = []; | ||
|
||
public function add(int $state, string $string): void | ||
{ | ||
$this->data[$state][] = $string; | ||
} | ||
|
||
public function all(): array | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function getForStates(array $states = []): array | ||
{ | ||
if ([] === $states) { | ||
return $this->data; | ||
} | ||
|
||
return array_intersect_key($this->data, array_flip($states)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Toflar\StateSetIndex\DataStore; | ||
|
||
class NullDataStore implements DataStoreInterface | ||
{ | ||
public function add(int $state, string $string): void | ||
{ | ||
// noop | ||
} | ||
|
||
public function getForStates(array $states = []): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.