mirror of
https://github.com/Z3Prover/z3
synced 2025-04-12 12:08:18 +00:00
wip - incremental edit distance algorithm
This commit is contained in:
parent
538f74d64c
commit
31ee56c1ca
|
@ -807,8 +807,6 @@ namespace sls {
|
||||||
a += val;
|
a += val;
|
||||||
for (unsigned i = 0; i < len; ++i)
|
for (unsigned i = 0; i < len; ++i)
|
||||||
a_is_value.push_back(is_val);
|
a_is_value.push_back(is_val);
|
||||||
if (!is_val && len == 0 && !a_is_value.empty())
|
|
||||||
a_is_value.back() = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto y : R) {
|
for (auto y : R) {
|
||||||
|
@ -818,8 +816,6 @@ namespace sls {
|
||||||
b += val;
|
b += val;
|
||||||
for (unsigned i = 0; i < len; ++i)
|
for (unsigned i = 0; i < len; ++i)
|
||||||
b_is_value.push_back(is_val);
|
b_is_value.push_back(is_val);
|
||||||
if (!is_val && len == 0 && !b_is_value.empty())
|
|
||||||
b_is_value.back() = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a == b)
|
if (a == b)
|
||||||
|
@ -862,84 +858,60 @@ namespace sls {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
auto delete_char = [&](auto const& es, unsigned i) {
|
||||||
|
for (auto x : es) {
|
||||||
|
auto const& value = strval0(x);
|
||||||
|
if (i >= value.length())
|
||||||
|
i -= value.length();
|
||||||
|
else {
|
||||||
|
if (!is_value(x))
|
||||||
|
m_str_updates.push_back({ x, value.extract(0, i) + value.extract(i + 1, value.length()), 1 });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto add_char = [&](auto const& es, unsigned j, uint32_t ch) {
|
||||||
|
for (auto x : es) {
|
||||||
|
auto const& value = strval0(x);
|
||||||
|
//verbose_stream() << "add " << j << " " << value << " " << value.length() << " " << is_value(x) << "\n";
|
||||||
|
if (j > value.length() || (j == value.length() && j > 0)) {
|
||||||
|
j -= value.length();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!is_value(x))
|
||||||
|
m_str_updates.push_back({ x, value.extract(0, j) + zstring(ch) + value.extract(j, value.length()), 1 });
|
||||||
|
if (j < value.length())
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto copy_char = [&](auto const& es, unsigned j, uint32_t ch) {
|
||||||
|
for (auto x : es) {
|
||||||
|
auto const& value = strval0(x);
|
||||||
|
if (j >= value.length())
|
||||||
|
j -= value.length();
|
||||||
|
else {
|
||||||
|
if (!is_value(x))
|
||||||
|
m_str_updates.push_back({ x, value.extract(0, j) + zstring(ch) + value.extract(j + 1, value.length()), 1 });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for (auto& [side, op, i, j] : m_string_updates) {
|
for (auto& [side, op, i, j] : m_string_updates) {
|
||||||
if (op == op_t::del && side == side_t::left) {
|
if (op == op_t::del && side == side_t::left)
|
||||||
for (auto x : L) {
|
delete_char(L, i);
|
||||||
|
else if (op == op_t::del && side == side_t::right)
|
||||||
auto const& value = strval0(x);
|
delete_char(R, i);
|
||||||
if (i >= value.length())
|
else if (op == op_t::add && side == side_t::left)
|
||||||
i -= value.length();
|
add_char(L, j, b[i]);
|
||||||
else {
|
else if (op == op_t::add && side == side_t::right)
|
||||||
if (!is_value(x))
|
add_char(R, j, a[i]);
|
||||||
m_str_updates.push_back({ x, value.extract(0, i) + value.extract(i + 1, value.length()), 1 });
|
else if (op == op_t::copy && side == side_t::left)
|
||||||
break;
|
copy_char(L, j, b[i]);
|
||||||
}
|
else if (op == op_t::copy && side == side_t::right)
|
||||||
}
|
copy_char(R, j, a[i]);
|
||||||
}
|
|
||||||
else if (op == op_t::del && side == side_t::right) {
|
|
||||||
for (auto x : R) {
|
|
||||||
auto const& value = strval0(x);
|
|
||||||
if (i >= value.length())
|
|
||||||
i -= value.length();
|
|
||||||
else {
|
|
||||||
if (!is_value(x))
|
|
||||||
m_str_updates.push_back({ x, value.extract(0, i) + value.extract(i + 1, value.length()), 1 });
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (op == op_t::add && side == side_t::left) {
|
|
||||||
for (auto x : L) {
|
|
||||||
auto const& value = strval0(x);
|
|
||||||
//verbose_stream() << "add " << j << " " << value << " " << value.length() << " " << is_value(x) << "\n";
|
|
||||||
if (j > value.length() || (j == value.length() && j > 0)) {
|
|
||||||
j -= value.length();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!is_value(x))
|
|
||||||
m_str_updates.push_back({ x, value.extract(0, j) + zstring(b[i]) + value.extract(j, value.length()), 1 });
|
|
||||||
if (j < value.length())
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (op == op_t::add && side == side_t::right) {
|
|
||||||
for (auto x : R) {
|
|
||||||
auto const& value = strval0(x);
|
|
||||||
//verbose_stream() << "add " << j << " " << value << " " << value.length() << " " << is_value(x) << "\n";
|
|
||||||
if (j > value.length() || (j == value.length() && j > 0)) {
|
|
||||||
j -= value.length();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!is_value(x))
|
|
||||||
m_str_updates.push_back({ x, value.extract(0, j) + zstring(a[i]) + value.extract(j, value.length()), 1 });
|
|
||||||
if (j < value.length())
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (op == op_t::copy && side == side_t::left) {
|
|
||||||
for (auto x : L) {
|
|
||||||
auto const& value = strval0(x);
|
|
||||||
if (j >= value.length())
|
|
||||||
j -= value.length();
|
|
||||||
else {
|
|
||||||
if (!is_value(x))
|
|
||||||
m_str_updates.push_back({ x, value.extract(0, j) + zstring(b[i]) + value.extract(j + 1, value.length()), 1 });
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (op == op_t::copy && side == side_t::right) {
|
|
||||||
for (auto x : R) {
|
|
||||||
auto const& value = strval0(x);
|
|
||||||
if (j >= value.length())
|
|
||||||
j -= value.length();
|
|
||||||
else {
|
|
||||||
if (!is_value(x))
|
|
||||||
m_str_updates.push_back({ x, value.extract(0, j) + zstring(a[i]) + value.extract(j + 1, value.length()), 1 });
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
verbose_stream() << "num updates " << m_str_updates.size() << "\n";
|
verbose_stream() << "num updates " << m_str_updates.size() << "\n";
|
||||||
bool r = apply_update();
|
bool r = apply_update();
|
||||||
|
@ -1200,7 +1172,15 @@ namespace sls {
|
||||||
return true;
|
return true;
|
||||||
if (!is_value(x))
|
if (!is_value(x))
|
||||||
m_str_updates.push_back({ x, r, 1 });
|
m_str_updates.push_back({ x, r, 1 });
|
||||||
|
if (!is_value(y))
|
||||||
|
m_str_updates.push_back({ y, zstring(), 1});
|
||||||
|
if (!is_value(z))
|
||||||
|
m_str_updates.push_back({ z, zstring(), 1 });
|
||||||
|
|
||||||
// TODO some more possible ways, also deal with y, z if they are not values.
|
// TODO some more possible ways, also deal with y, z if they are not values.
|
||||||
|
// apply reverse substitution of r to replace z by y, update x to this value
|
||||||
|
// update x using an edit distance reducing update based on the reverse substitution.
|
||||||
|
// reverse substitution isn't unique, so take into account different possibilities (randomly).
|
||||||
return apply_update();
|
return apply_update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue