Compare commits

..

No commits in common. "92e99a5874c93bee072280867021d27c6a78527a" and "a4f830f9175f970492d0d01d62e2e682bea11f74" have entirely different histories.

2 changed files with 16 additions and 21 deletions

View File

@ -11,11 +11,11 @@ memory, to serve as a buffer for storing results.
1. Make benchmarks.
2. Introduce more tests.
2.1 Tests with pre, post and infix strings shared between the strings
2.2 Tests where the length of the strings are combinations of odd and
even.
3. ~~Reduce the size of the buffer. When this was done with the old version,
performance was increased 100%.~~
2.1. Tests with pre, post and infix strings shared between the strings
2.2 Tests where the length of the strings are combinations of odd and
even.
3. Reduce the size of the buffer. When this was done with the old version,
performance was increased 100%.
4. Look into SIMD instructions
5. Look into parallelism.
6. Templating the code
@ -36,9 +36,3 @@ longer string in the middle and then calculating the two parts sperately.
If that can be done, it should be easy to turn on the threads and make run this
on all the cores.
## Templating the code
There's nothing in the code that's specific to `std::string`, and there's no
real reason for keeping it restricted to `std::string`. Making this a template
should be trivial.

View File

@ -16,25 +16,26 @@ auto levenshtein_distance(std::string_view const& a, std::string_view const& b)
if (i != 0) return levenshtein_distance(a.substr(0, a.size() - i), b.substr(0, b.size() - i));
auto const buffer_length = a.size() + 1;
auto buffer = new unsigned int[buffer_length];
auto buffers = new unsigned int[buffer_length * 2];
std::iota(buffer, buffer + buffer_length, 0);
unsigned int * buffer[2] = {buffers, buffers + buffer_length};
std::iota(buffer[0], buffer[1], 0);
std::fill(buffer[1], buffer[1] + buffer_length, 0);
for (auto i = 0u; i < b.size(); ++i)
{
buffer[0] = i;
auto temp = i;
buffer[1][0] = i + 1;
for (auto j = 0u; j < a.size(); ++j)
{
temp = std::min(
temp + (a[j] == b[i] ? 0u : 1u),
std::min(buffer[j + 1], buffer[j]) + 1u
buffer[1][j + 1] = std::min(
buffer[0][j] + (a[j] == b[i] ? 0u : 1u),
std::min(buffer[0][j + 1], buffer[1][j]) + 1u
);
std::swap(buffer[j + 1], temp);
}
std::swap(buffer[0], buffer[1]);
}
auto cost = buffer[buffer_length - 1];
delete [] buffer;
auto cost = buffer[0][buffer_length - 1];
delete [] buffers;
return cost;
}