2015-05-19 20:46:24 +02:00
|
|
|
#ifndef diff_hh_INCLUDED
|
|
|
|
#define diff_hh_INCLUDED
|
|
|
|
|
|
|
|
// Implementation of the linear space variant of the algorithm described in
|
|
|
|
// "An O(ND) Difference Algorithm and Its Variations"
|
|
|
|
// (http://xmailserver.org/diff2.pdf)
|
|
|
|
|
2019-12-01 21:55:55 +01:00
|
|
|
#include <algorithm>
|
2015-05-13 21:51:10 +02:00
|
|
|
#include <functional>
|
2019-11-30 00:46:42 +01:00
|
|
|
#include <memory>
|
2015-05-13 21:51:10 +02:00
|
|
|
|
2015-05-13 00:41:35 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2018-02-09 11:30:30 +01:00
|
|
|
// A snake is an edit followed by a (possibly empty) diagonal
|
2017-07-20 17:47:50 +02:00
|
|
|
struct Snake
|
|
|
|
{
|
2018-02-09 11:30:30 +01:00
|
|
|
// The end points of the diagonal (x, y) -> (u, v)
|
2017-07-20 17:47:50 +02:00
|
|
|
int x, y, u, v;
|
2018-02-09 11:30:30 +01:00
|
|
|
// the edit op, reverse op happen at the end of the diagonal
|
2017-07-20 17:47:50 +02:00
|
|
|
enum Op { Add, Del, RevAdd, RevDel } op;
|
|
|
|
};
|
2015-05-13 00:41:35 +02:00
|
|
|
|
2020-02-18 10:20:09 +01:00
|
|
|
template<bool forward, typename IteratorA, typename IteratorB, typename Equal>
|
2020-02-18 09:26:01 +01:00
|
|
|
Snake find_end_snake_of_further_reaching_dpath(IteratorA a, int N, IteratorB b, int M,
|
2017-07-18 16:11:24 +02:00
|
|
|
const int* V, const int D, const int k, Equal eq)
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
|
|
|
const bool add = k == -D or (k != D and V[k-1] < V[k+1]);
|
|
|
|
|
|
|
|
// if diagonal on the right goes further along x than diagonal on the left,
|
|
|
|
// then we take a vertical edge from it to this diagonal, hence x = V[k+1]
|
|
|
|
// else, we take an horizontal edge from our left diagonal,x = V[k-1]+1
|
2017-07-15 10:17:27 +02:00
|
|
|
const int x = add ? V[k+1] : V[k-1]+1;
|
|
|
|
// we are by construction on diagonal k, so our position along b (y) is x - k.
|
|
|
|
const int y = x - k;
|
2015-05-13 00:41:35 +02:00
|
|
|
|
2020-02-18 10:20:09 +01:00
|
|
|
auto at = [](auto&& base, int index, int size) -> decltype(auto) {
|
|
|
|
return forward ? base[index] : base[size - 1 - index];
|
|
|
|
};
|
|
|
|
|
2015-05-13 00:41:35 +02:00
|
|
|
int u = x, v = y;
|
|
|
|
// follow end snake along diagonal k
|
2020-02-18 10:20:09 +01:00
|
|
|
while (u < N and v < M and eq(at(a, u, N), at(b, v, M)))
|
2015-05-13 00:41:35 +02:00
|
|
|
++u, ++v;
|
|
|
|
|
2017-07-20 17:47:50 +02:00
|
|
|
return { x, y, u, v, add ? Snake::Add : Snake::Del };
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 09:26:01 +01:00
|
|
|
template<typename IteratorA, typename IteratorB, typename Equal>
|
|
|
|
Snake find_middle_snake(IteratorA a, int N, IteratorB b, int M,
|
2017-07-23 21:33:12 +02:00
|
|
|
int* V1, int* V2, int cost_limit, Equal eq)
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
|
|
|
const int delta = N - M;
|
2017-07-17 11:32:38 +02:00
|
|
|
V1[1] = 0;
|
|
|
|
V2[1] = 0;
|
2015-05-13 00:41:35 +02:00
|
|
|
|
2017-07-23 21:33:12 +02:00
|
|
|
const int max_D = std::min((M + N + 1) / 2 + 1, cost_limit);
|
|
|
|
for (int D = 0; D < max_D; ++D)
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
|
|
|
for (int k1 = -D; k1 <= D; k1 += 2)
|
|
|
|
{
|
2020-02-18 10:20:09 +01:00
|
|
|
auto p = find_end_snake_of_further_reaching_dpath<true>(a, N, b, M, V1, D, k1, eq);
|
2015-05-13 00:41:35 +02:00
|
|
|
V1[k1] = p.u;
|
|
|
|
|
|
|
|
const int k2 = -(k1 - delta);
|
2017-07-23 21:33:12 +02:00
|
|
|
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)
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int k2 = -D; k2 <= D; k2 += 2)
|
|
|
|
{
|
2020-02-18 10:20:09 +01:00
|
|
|
auto p = find_end_snake_of_further_reaching_dpath<false>(a, N, b, M, V2, D, k2, eq);
|
2015-05-13 00:41:35 +02:00
|
|
|
V2[k2] = p.u;
|
|
|
|
|
|
|
|
const int k1 = -(k2 - delta);
|
2017-07-23 21:33:12 +02:00
|
|
|
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,
|
|
|
|
(Snake::Op)(p.op + Snake::RevAdd) };// return last snake on reverse path, len = 2 * D
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 21:33:12 +02:00
|
|
|
// We did not find a minimal path in less than max_D iterations, iterate one more time finding the best
|
|
|
|
Snake best{};
|
2018-02-09 11:30:30 +01:00
|
|
|
auto score = [](const Snake& s) { return s.u + s.v; };
|
2017-07-23 21:33:12 +02:00
|
|
|
for (int k1 = -max_D; k1 <= max_D; k1 += 2)
|
|
|
|
{
|
2020-02-18 10:20:09 +01:00
|
|
|
auto p = find_end_snake_of_further_reaching_dpath<true>(a, N, b, M, V1, max_D, k1, eq);
|
2017-07-23 21:33:12 +02:00
|
|
|
V1[k1] = p.u;
|
2018-02-09 11:30:30 +01:00
|
|
|
if ((delta % 2 != 0) and p.u <= N and p.v <= M and score(p) >= score(best))
|
2017-07-23 21:33:12 +02:00
|
|
|
best = p;
|
|
|
|
}
|
|
|
|
for (int k2 = -max_D; k2 <= max_D; k2 += 2)
|
|
|
|
{
|
2020-02-18 10:20:09 +01:00
|
|
|
auto p = find_end_snake_of_further_reaching_dpath<false>(a, N, b, M, V2, max_D, k2, eq);
|
2017-07-23 21:33:12 +02:00
|
|
|
V2[k2] = p.u;
|
2018-02-09 11:30:30 +01:00
|
|
|
if ((delta % 2 == 0) and p.u <= N and p.v <= M and score(p) >= score(best))
|
2017-07-23 21:33:12 +02:00
|
|
|
best = {p.x, p.y, p.u, p.v, (Snake::Op)(p.op + Snake::RevAdd)};
|
|
|
|
}
|
|
|
|
|
2018-02-09 11:30:30 +01:00
|
|
|
if (best.op >= Snake::RevAdd) // reverse the snake now, as we were comparing snake length
|
2017-07-23 21:33:12 +02:00
|
|
|
best = { N - best.u, M - best.v, N - best.x , M - best.y, best.op };
|
|
|
|
return best;
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-30 00:46:42 +01:00
|
|
|
enum class DiffOp
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2019-11-30 00:46:42 +01:00
|
|
|
Keep,
|
|
|
|
Add,
|
|
|
|
Remove
|
2015-05-13 00:41:35 +02:00
|
|
|
};
|
|
|
|
|
2020-02-18 09:26:01 +01:00
|
|
|
template<typename IteratorA, typename IteratorB, typename Equal, typename OnDiff>
|
|
|
|
void find_diff_rec(IteratorA a, int begA, int endA,
|
|
|
|
IteratorB b, int begB, int endB,
|
2017-07-23 21:33:12 +02:00
|
|
|
int* V1, int* V2, int cost_limit,
|
2019-11-30 00:46:42 +01:00
|
|
|
Equal eq, OnDiff&& on_diff)
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2020-03-20 10:26:05 +01:00
|
|
|
auto on_diff_ifn = [&](DiffOp op, int len) {
|
2019-11-30 00:46:42 +01:00
|
|
|
if (len != 0)
|
2020-03-20 10:26:05 +01:00
|
|
|
on_diff(op, len);
|
2019-11-30 00:46:42 +01:00
|
|
|
};
|
|
|
|
|
2017-07-17 13:30:11 +02:00
|
|
|
int prefix_len = 0;
|
2017-07-18 17:53:30 +02:00
|
|
|
while (begA != endA and begB != endB and eq(a[begA], b[begB]))
|
2017-07-18 12:14:52 +02:00
|
|
|
++begA, ++begB, ++prefix_len;
|
2017-07-17 13:30:11 +02:00
|
|
|
|
|
|
|
int suffix_len = 0;
|
2017-07-18 17:53:30 +02:00
|
|
|
while (begA != endA and begB != endB and eq(a[endA-1], b[endB-1]))
|
2017-07-18 12:14:52 +02:00
|
|
|
--endA, --endB, ++suffix_len;
|
2017-07-17 13:30:11 +02:00
|
|
|
|
2020-03-20 10:26:05 +01:00
|
|
|
on_diff_ifn(DiffOp::Keep, prefix_len);
|
2017-07-17 13:30:11 +02:00
|
|
|
|
2017-07-18 12:14:52 +02:00
|
|
|
const auto lenA = endA - begA, lenB = endB - begB;
|
|
|
|
|
2017-07-17 13:30:11 +02:00
|
|
|
if (lenA == 0)
|
2020-03-20 10:26:05 +01:00
|
|
|
on_diff_ifn(DiffOp::Add, lenB);
|
2017-07-17 13:30:11 +02:00
|
|
|
else if (lenB == 0)
|
2020-03-20 10:26:05 +01:00
|
|
|
on_diff_ifn(DiffOp::Remove, lenA);
|
2017-07-17 13:30:11 +02:00
|
|
|
else
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2017-07-23 21:33:12 +02:00
|
|
|
auto snake = find_middle_snake(a + begA, lenA, b + begB, lenB, V1, V2, cost_limit, eq);
|
2017-07-20 17:47:50 +02:00
|
|
|
kak_assert(snake.u <= lenA and snake.v <= lenB);
|
2017-07-18 16:11:24 +02:00
|
|
|
|
2017-07-20 17:47:50 +02:00
|
|
|
find_diff_rec(a, begA, begA + snake.x - (int)(snake.op == Snake::Del),
|
|
|
|
b, begB, begB + snake.y - (int)(snake.op == Snake::Add),
|
2019-11-30 00:46:42 +01:00
|
|
|
V1, V2, cost_limit, eq, on_diff);
|
2015-05-13 21:51:10 +02:00
|
|
|
|
2017-07-20 17:47:50 +02:00
|
|
|
if (snake.op == Snake::Add)
|
2020-03-20 10:26:05 +01:00
|
|
|
on_diff_ifn(DiffOp::Add, 1);
|
2017-07-20 17:47:50 +02:00
|
|
|
if (snake.op == Snake::Del)
|
2020-03-20 10:26:05 +01:00
|
|
|
on_diff_ifn(DiffOp::Remove, 1);
|
2015-05-13 00:41:35 +02:00
|
|
|
|
2020-03-20 10:26:05 +01:00
|
|
|
on_diff_ifn(DiffOp::Keep, snake.u - snake.x);
|
2015-05-18 23:59:59 +02:00
|
|
|
|
2017-07-20 17:47:50 +02:00
|
|
|
if (snake.op == Snake::RevAdd)
|
2020-03-20 10:26:05 +01:00
|
|
|
on_diff_ifn(DiffOp::Add, 1);
|
2017-07-20 17:47:50 +02:00
|
|
|
if (snake.op == Snake::RevDel)
|
2020-03-20 10:26:05 +01:00
|
|
|
on_diff_ifn(DiffOp::Remove, 1);
|
2017-07-18 16:11:24 +02:00
|
|
|
|
2017-07-20 17:47:50 +02:00
|
|
|
find_diff_rec(a, begA + snake.u + (int)(snake.op == Snake::RevDel), endA,
|
|
|
|
b, begB + snake.v + (int)(snake.op == Snake::RevAdd), endB,
|
2019-11-30 00:46:42 +01:00
|
|
|
V1, V2, cost_limit, eq, on_diff);
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
2017-07-17 13:30:11 +02:00
|
|
|
|
2020-03-20 10:26:05 +01:00
|
|
|
on_diff_ifn(DiffOp::Keep, suffix_len);
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-30 00:46:42 +01:00
|
|
|
struct Diff
|
|
|
|
{
|
|
|
|
DiffOp op;
|
|
|
|
int len;
|
|
|
|
};
|
|
|
|
|
2020-02-18 09:26:01 +01:00
|
|
|
template<typename IteratorA, typename IteratorB, typename OnDiff, typename Equal = std::equal_to<>>
|
|
|
|
void for_each_diff(IteratorA a, int N, IteratorB b, int M, OnDiff&& on_diff, Equal eq = Equal{})
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2015-05-13 21:51:10 +02:00
|
|
|
const int max = 2 * (N + M) + 1;
|
2019-11-30 00:46:42 +01:00
|
|
|
std::unique_ptr<int[]> data(new int[2*max]);
|
2017-07-23 21:33:12 +02:00
|
|
|
constexpr int cost_limit = 1000;
|
2015-05-13 21:51:10 +02:00
|
|
|
|
2019-11-30 00:46:42 +01:00
|
|
|
Diff last{};
|
|
|
|
find_diff_rec(a, 0, N, b, 0, M, &data[N+M], &data[max + N+M], cost_limit, eq,
|
2020-03-20 10:26:05 +01:00
|
|
|
[&last, &on_diff](DiffOp op, int len) {
|
|
|
|
if (last.op == op)
|
2019-11-30 00:46:42 +01:00
|
|
|
last.len += len;
|
|
|
|
else
|
|
|
|
{
|
2020-03-20 10:26:05 +01:00
|
|
|
if (last.len != 0)
|
|
|
|
on_diff(last.op, last.len);
|
|
|
|
last = Diff{op, len};
|
2019-11-30 00:46:42 +01:00
|
|
|
}
|
|
|
|
});
|
2020-03-20 10:26:05 +01:00
|
|
|
if (last.op != DiffOp{} or last.len != 0)
|
|
|
|
on_diff(last.op, last.len);
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-05-19 20:46:24 +02:00
|
|
|
|
|
|
|
#endif // diff_hh_INCLUDED
|