Limit diff algorithm complexity

Return a non-optimal, but valid, diff when we detect too many
iterations

Fixes #1509
This commit is contained in:
Maxime Coste 2017-07-23 21:33:12 +02:00
parent d43e2ac843
commit d1c005dd8c

View File

@ -43,15 +43,15 @@ Snake find_end_snake_of_further_reaching_dpath(Iterator a, int N, Iterator b, in
template<typename Iterator, typename Equal> template<typename Iterator, typename Equal>
Snake find_middle_snake(Iterator a, int N, Iterator b, int M, Snake find_middle_snake(Iterator a, int N, Iterator b, int M,
int* V1, int* V2, Equal eq) int* V1, int* V2, int cost_limit, Equal eq)
{ {
const int delta = N - M; const int delta = N - M;
V1[1] = 0; V1[1] = 0;
V2[1] = 0; V2[1] = 0;
std::reverse_iterator<Iterator> ra{a + N}, rb{b + M}; std::reverse_iterator<Iterator> ra{a + N}, rb{b + M};
const int max_D = std::min((M + N + 1) / 2 + 1, cost_limit);
for (int D = 0; D <= (M + N + 1) / 2; ++D) for (int D = 0; D < max_D; ++D)
{ {
for (int k1 = -D; k1 <= D; k1 += 2) for (int k1 = -D; k1 <= D; k1 += 2)
{ {
@ -59,11 +59,8 @@ Snake find_middle_snake(Iterator a, int N, Iterator b, int M,
V1[k1] = p.u; V1[k1] = p.u;
const int k2 = -(k1 - delta); const int k2 = -(k1 - delta);
if ((delta % 2 != 0) and -(D-1) <= k2 and k2 <= (D-1)) if ((delta % 2 != 0) and -(D-1) <= k2 and k2 <= (D-1) and V1[k1] + V2[k2] >= N)
{ return p;// return last snake on forward path, len = (2 * D - 1)
if (V1[k1] + V2[k2] >= N)
return p;// return last snake on forward path, len = (2 * D - 1)
}
} }
for (int k2 = -D; k2 <= D; k2 += 2) for (int k2 = -D; k2 <= D; k2 += 2)
@ -72,17 +69,32 @@ Snake find_middle_snake(Iterator a, int N, Iterator b, int M,
V2[k2] = p.u; V2[k2] = p.u;
const int k1 = -(k2 - delta); const int k1 = -(k2 - delta);
if ((delta % 2 == 0) and -D <= k1 and k1 <= D) if ((delta % 2 == 0) and -D <= k1 and k1 <= D and V1[k1] + V2[k2] >= N)
{ return { N - p.u, M - p.v, N - p.x , M - p.y,
if (V1[k1] + V2[k2] >= N) (Snake::Op)(p.op + Snake::RevAdd) };// return last snake on reverse path, len = 2 * D
return { N - p.u, M - p.v, N - p.x , M - p.y,
(Snake::Op)(p.op + Snake::RevAdd) };// return last snake on reverse path, len = 2 * D
}
} }
} }
kak_assert(false); // We did not find a minimal path in less than max_D iterations, iterate one more time finding the best
return {}; Snake best{};
for (int k1 = -max_D; k1 <= max_D; k1 += 2)
{
auto p = find_end_snake_of_further_reaching_dpath(a, N, b, M, V1, max_D, k1, eq);
V1[k1] = p.u;
if ((delta % 2 != 0) and p.u + p.v >= best.u + best.v and p.u <= N and p.v <= M)
best = p;
}
for (int k2 = -max_D; k2 <= max_D; k2 += 2)
{
auto p = find_end_snake_of_further_reaching_dpath(ra, N, rb, M, V2, max_D, k2, eq);
V2[k2] = p.u;
if ((delta % 2 == 0) and p.u + p.v >= best.u + best.v and p.u <= N and p.v <= M)
best = {p.x, p.y, p.u, p.v, (Snake::Op)(p.op + Snake::RevAdd)};
}
if (best.op >= Snake::RevAdd)
best = { N - best.u, M - best.v, N - best.x , M - best.y, best.op };
return best;
} }
struct Diff struct Diff
@ -108,7 +120,8 @@ inline void append_diff(Vector<Diff>& diffs, Diff diff)
template<typename Iterator, typename Equal> template<typename Iterator, typename Equal>
void find_diff_rec(Iterator a, int begA, int endA, void find_diff_rec(Iterator a, int begA, int endA,
Iterator b, int begB, int endB, Iterator b, int begB, int endB,
int* V1, int* V2, Equal eq, Vector<Diff>& diffs) int* V1, int* V2, int cost_limit,
Equal eq, Vector<Diff>& diffs)
{ {
int prefix_len = 0; int prefix_len = 0;
while (begA != endA and begB != endB and eq(a[begA], b[begB])) while (begA != endA and begB != endB and eq(a[begA], b[begB]))
@ -128,12 +141,12 @@ void find_diff_rec(Iterator a, int begA, int endA,
append_diff(diffs, {Diff::Remove, lenA, 0}); append_diff(diffs, {Diff::Remove, lenA, 0});
else else
{ {
auto snake = find_middle_snake(a + begA, lenA, b + begB, lenB, V1, V2, eq); auto snake = find_middle_snake(a + begA, lenA, b + begB, lenB, V1, V2, cost_limit, eq);
kak_assert(snake.u <= lenA and snake.v <= lenB); kak_assert(snake.u <= lenA and snake.v <= lenB);
find_diff_rec(a, begA, begA + snake.x - (int)(snake.op == Snake::Del), find_diff_rec(a, begA, begA + snake.x - (int)(snake.op == Snake::Del),
b, begB, begB + snake.y - (int)(snake.op == Snake::Add), b, begB, begB + snake.y - (int)(snake.op == Snake::Add),
V1, V2, eq, diffs); V1, V2, cost_limit, eq, diffs);
if (snake.op == Snake::Add) if (snake.op == Snake::Add)
append_diff(diffs, {Diff::Add, 1, begB + snake.y - 1}); append_diff(diffs, {Diff::Add, 1, begB + snake.y - 1});
@ -149,7 +162,7 @@ void find_diff_rec(Iterator a, int begA, int endA,
find_diff_rec(a, begA + snake.u + (int)(snake.op == Snake::RevDel), endA, find_diff_rec(a, begA + snake.u + (int)(snake.op == Snake::RevDel), endA,
b, begB + snake.v + (int)(snake.op == Snake::RevAdd), endB, b, begB + snake.v + (int)(snake.op == Snake::RevAdd), endB,
V1, V2, eq, diffs); V1, V2, cost_limit, eq, diffs);
} }
append_diff(diffs, {Diff::Keep, suffix_len, 0}); append_diff(diffs, {Diff::Keep, suffix_len, 0});
@ -161,7 +174,8 @@ Vector<Diff> find_diff(Iterator a, int N, Iterator b, int M, Equal eq = Equal{})
const int max = 2 * (N + M) + 1; const int max = 2 * (N + M) + 1;
Vector<int> data(2*max); Vector<int> data(2*max);
Vector<Diff> diffs; Vector<Diff> diffs;
find_diff_rec(a, 0, N, b, 0, M, &data[N+M], &data[max + N+M], eq, diffs); constexpr int cost_limit = 1000;
find_diff_rec(a, 0, N, b, 0, M, &data[N+M], &data[max + N+M], cost_limit, eq, diffs);
return diffs; return diffs;
} }