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)
|
|
|
|
|
2015-05-13 00:41:35 +02:00
|
|
|
#include "array_view.hh"
|
|
|
|
#include "vector.hh"
|
|
|
|
|
2015-05-13 21:51:10 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
|
|
|
|
2015-05-13 00:41:35 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-11-19 16:31:00 +01:00
|
|
|
struct Snake{ int x, y, u, v; bool add; };
|
2015-05-13 00:41:35 +02:00
|
|
|
|
2015-05-13 21:51:10 +02:00
|
|
|
template<typename Iterator, typename Equal>
|
2015-05-13 00:41:35 +02:00
|
|
|
Snake find_end_snake_of_further_reaching_dpath(Iterator a, int N, Iterator b, int M,
|
2017-07-17 11:32:38 +02:00
|
|
|
const int* V,
|
2015-05-13 21:51:10 +02:00
|
|
|
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
|
|
|
|
|
|
|
int u = x, v = y;
|
|
|
|
// follow end snake along diagonal k
|
2015-05-13 21:51:10 +02:00
|
|
|
while (u < N and v < M and eq(a[u], b[v]))
|
2015-05-13 00:41:35 +02:00
|
|
|
++u, ++v;
|
|
|
|
|
|
|
|
return { x, y, u, v, add };
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SnakeLen : Snake
|
|
|
|
{
|
|
|
|
SnakeLen(Snake s, int d) : Snake(s), d(d) {}
|
|
|
|
int d;
|
|
|
|
};
|
|
|
|
|
2015-05-13 21:51:10 +02:00
|
|
|
template<typename Iterator, typename Equal>
|
2015-05-13 00:41:35 +02:00
|
|
|
SnakeLen find_middle_snake(Iterator a, int N, Iterator b, int M,
|
2017-07-17 11:32:38 +02:00
|
|
|
int* V1, int* V2, 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
|
|
|
|
|
|
|
std::reverse_iterator<Iterator> ra{a + N}, rb{b + M};
|
|
|
|
|
|
|
|
for (int D = 0; D <= (M + N + 1) / 2; ++D)
|
|
|
|
{
|
|
|
|
for (int k1 = -D; k1 <= D; k1 += 2)
|
|
|
|
{
|
2015-05-13 21:51:10 +02:00
|
|
|
auto p = find_end_snake_of_further_reaching_dpath(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);
|
|
|
|
if ((delta % 2 != 0) and -(D-1) <= k2 and k2 <= (D-1))
|
|
|
|
{
|
|
|
|
if (V1[k1] + V2[k2] >= N)
|
|
|
|
return { p, 2 * D - 1 };// return last snake on forward path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int k2 = -D; k2 <= D; k2 += 2)
|
|
|
|
{
|
2015-05-13 21:51:10 +02:00
|
|
|
auto p = find_end_snake_of_further_reaching_dpath(ra, N, rb, M, V2, D, k2, eq);
|
2015-05-13 00:41:35 +02:00
|
|
|
V2[k2] = p.u;
|
|
|
|
|
|
|
|
const int k1 = -(k2 - delta);
|
|
|
|
if ((delta % 2 == 0) and -D <= k1 and k1 <= D)
|
|
|
|
{
|
|
|
|
if (V1[k1] + V2[k2] >= N)
|
2015-05-15 14:55:39 +02:00
|
|
|
return { { N - p.u, M - p.v, N - p.x , M - p.y, p.add } , 2 * D };// return last snake on reverse path
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kak_assert(false);
|
2015-05-13 21:51:10 +02:00
|
|
|
return { {}, 0 };
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Diff
|
|
|
|
{
|
2015-05-13 21:51:10 +02:00
|
|
|
enum { Keep, Add, Remove } mode;
|
|
|
|
int len;
|
|
|
|
int posB;
|
2015-05-13 00:41:35 +02:00
|
|
|
};
|
|
|
|
|
2015-05-18 23:59:59 +02:00
|
|
|
inline void append_diff(Vector<Diff>& diffs, Diff diff)
|
|
|
|
{
|
2017-07-17 13:30:11 +02:00
|
|
|
if (diff.len == 0)
|
|
|
|
return;
|
|
|
|
|
2015-05-18 23:59:59 +02:00
|
|
|
if (not diffs.empty() and diffs.back().mode == diff.mode
|
|
|
|
and (diff.mode != Diff::Add or
|
|
|
|
diffs.back().posB + diffs.back().len == diff.posB))
|
|
|
|
diffs.back().len += diff.len;
|
|
|
|
else
|
|
|
|
diffs.push_back(diff);
|
|
|
|
}
|
|
|
|
|
2015-05-13 21:51:10 +02:00
|
|
|
template<typename Iterator, typename Equal>
|
2017-07-18 12:14:52 +02:00
|
|
|
void find_diff_rec(Iterator a, int begA, int endA,
|
|
|
|
Iterator b, int begB, int endB,
|
2017-07-17 11:32:38 +02:00
|
|
|
int* V1, int* V2, Equal eq, Vector<Diff>& diffs)
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2017-07-17 13:30:11 +02:00
|
|
|
int prefix_len = 0;
|
2017-07-18 12:14:52 +02:00
|
|
|
while (begA != endA and begB != endB and a[begA] == b[begB])
|
|
|
|
++begA, ++begB, ++prefix_len;
|
2017-07-17 13:30:11 +02:00
|
|
|
|
|
|
|
int suffix_len = 0;
|
2017-07-18 12:14:52 +02:00
|
|
|
while (begA != endA and begB != endB and a[endA-1] == b[endB-1])
|
|
|
|
--endA, --endB, ++suffix_len;
|
2017-07-17 13:30:11 +02:00
|
|
|
|
|
|
|
append_diff(diffs, {Diff::Keep, prefix_len, 0});
|
|
|
|
|
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)
|
2017-07-18 12:14:52 +02:00
|
|
|
append_diff(diffs, {Diff::Add, lenB, begB});
|
2017-07-17 13:30:11 +02:00
|
|
|
else if (lenB == 0)
|
|
|
|
append_diff(diffs, {Diff::Remove, lenA, 0});
|
|
|
|
else
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2017-07-18 12:14:52 +02:00
|
|
|
auto snake = find_middle_snake(a + begA, lenA, b + begB, lenB, V1, V2, eq);
|
|
|
|
kak_assert(snake.u <= lenA and snake.v <= lenB);
|
|
|
|
if (snake.d > 1)
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2017-07-18 12:14:52 +02:00
|
|
|
find_diff_rec(a, begA, begA + snake.x,
|
|
|
|
b, begB, begB + snake.y,
|
2017-07-17 11:32:38 +02:00
|
|
|
V1, V2, eq, diffs);
|
2015-05-13 21:51:10 +02:00
|
|
|
|
2017-07-18 12:14:52 +02:00
|
|
|
append_diff(diffs, {Diff::Keep, snake.u - snake.x, 0});
|
2015-05-13 00:41:35 +02:00
|
|
|
|
2017-07-18 12:14:52 +02:00
|
|
|
find_diff_rec(a, begA + snake.u, endA,
|
|
|
|
b, begB + snake.v, endB,
|
2017-07-17 11:32:38 +02:00
|
|
|
V1, V2, eq, diffs);
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
2015-05-18 23:59:59 +02:00
|
|
|
else
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2017-07-18 12:14:52 +02:00
|
|
|
if (snake.d == 1)
|
2015-05-18 23:59:59 +02:00
|
|
|
{
|
2017-07-18 12:14:52 +02:00
|
|
|
const int diag = snake.x - (snake.add ? 0 : 1);
|
2017-07-17 13:30:11 +02:00
|
|
|
append_diff(diffs, {Diff::Keep, diag, 0});
|
2015-05-18 23:59:59 +02:00
|
|
|
|
2017-07-18 12:14:52 +02:00
|
|
|
if (snake.add)
|
|
|
|
append_diff(diffs, {Diff::Add, 1, begB + diag});
|
2015-05-18 23:59:59 +02:00
|
|
|
else
|
|
|
|
append_diff(diffs, {Diff::Remove, 1, 0});
|
|
|
|
}
|
2017-07-18 12:14:52 +02:00
|
|
|
append_diff(diffs, {Diff::Keep, snake.u - snake.x, 0});
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-17 13:30:11 +02:00
|
|
|
|
|
|
|
append_diff(diffs, {Diff::Keep, suffix_len, 0});
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
2015-05-13 21:51:10 +02:00
|
|
|
template<typename Iterator, typename Equal = std::equal_to<typename std::iterator_traits<Iterator>::value_type>>
|
2015-07-13 14:57:43 +02:00
|
|
|
Vector<Diff> find_diff(Iterator a, int N, Iterator b, int M, 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;
|
|
|
|
Vector<int> data(2*max);
|
|
|
|
Vector<Diff> diffs;
|
2017-07-17 19:29:57 +02:00
|
|
|
find_diff_rec(a, 0, N, b, 0, M, &data[N+M], &data[max + N+M], eq, diffs);
|
2015-05-13 21:51:10 +02:00
|
|
|
|
2015-05-13 00:41:35 +02:00
|
|
|
return diffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-05-19 20:46:24 +02:00
|
|
|
|
|
|
|
#endif // diff_hh_INCLUDED
|