Commit 4d68a86e authored by s_kleplj's avatar s_kleplj
Browse files

diagonal algorithm

parent 55bc2cc2
#ifndef levensol_hpp_
#define levensol_hpp_
#include <algorithm>
#include <cstdint>
#include <cstddef>
#include <utility>
namespace levensol {
template< typename policy>
class levenstein {
public:
levenstein(std::size_t a_size, std::size_t b_size)
{}
levenstein(std::size_t a_size, std::size_t b_size) :
a_size_{a_size},
b_size_{b_size}
{
}
// a_size >= b_size
std::uint32_t compute_impl(const std::uint32_t* a, const std::uint32_t* b, const std::size_t a_size, const std::size_t b_size)
{
const std::size_t size = a_size + b_size - 1;
std::uint32_t arr[size + 2];
for (std::size_t i = 0; i < a_size; ++i) {
arr[i] = a_size - i;
}
for (std::size_t i = a_size; i < size + 2; ++i) {
arr[i] = i - a_size;
}
for (std::size_t i = 0; i < b_size; ++i) {
for (std:: size_t j = a_size - i, y = 0; j <= a_size + i; j += 2, ++y) {
std::uint32_t tmp = std::min(
arr[j - 1] + 1,
arr[j + 1] + 1);
arr[j] = std::min(
tmp,
arr[j] + (a[i - y] == b[y] ? 0 : 1));
}
}
for (std::size_t i = b_size; i < a_size; ++i) {
for (std:: size_t j = a_size - i, y = 0; j <= size + b_size - i - 1; j += 2, ++y) {
std::uint32_t tmp = std::min(
arr[j - 1] + 1,
arr[j + 1] + 1);
arr[j] = std::min(
tmp,
arr[j] + (a[i - y] == b[y] ? 0 : 1));
}
}
for (std::size_t i = 0; i < b_size; ++i) {
for (std:: size_t j = i + 1, y = 0; j <= 2 * b_size - i; j += 2, ++y) {
std::uint32_t tmp = std::min(
arr[j - 1] + 1,
arr[j + 1] + 1);
arr[j] = std::min(
tmp,
arr[j] + (a[a_size - y - 1] == b[i + y] ? 0 : 1));
}
}
return arr[b_size];
}
std::uint32_t compute(const std::uint32_t* a, const std::uint32_t* b)
{
return 0;
if (a_size_ < b_size_) {
return compute_impl(b, a, b_size_, a_size_);
} else {
return compute_impl(a, b, a_size_, b_size_);
}
}
const std::size_t a_size_, b_size_;
};
struct policy_sse {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment