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
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct MirroredArray : public ArrayView<T>
|
|
|
|
{
|
|
|
|
MirroredArray(ArrayView<T> data, int size)
|
|
|
|
: ArrayView<T>(data), size(size)
|
|
|
|
{
|
|
|
|
kak_assert(2 * size + 1 <= data.size());
|
2015-05-14 14:57:03 +02:00
|
|
|
for (int i = -size; i <= size; ++i)
|
|
|
|
(*this)[i] = 0;
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
2015-05-14 20:05:41 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-05-13 00:41:35 +02:00
|
|
|
T& operator[](int n) { return ArrayView<T>::operator[](n + size); }
|
2015-05-14 20:05:41 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-05-13 00:41:35 +02:00
|
|
|
const T& operator[](int n) const { return ArrayView<T>::operator[](n + size); }
|
|
|
|
private:
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Snake{ int x, y, u, v; bool add; };
|
|
|
|
|
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,
|
|
|
|
const MirroredArray<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
|
|
|
{
|
|
|
|
int x; // our position along a
|
|
|
|
|
|
|
|
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]
|
|
|
|
if (add)
|
|
|
|
x = V[k+1];
|
|
|
|
// else, we take an horizontal edge from our left diagonal,x = V[k-1]+1
|
|
|
|
else
|
|
|
|
x = V[k-1]+1;
|
|
|
|
|
|
|
|
int y = x - k; // we are by construction on diagonal k, so our position along
|
|
|
|
// b (y) is x - k.
|
|
|
|
|
|
|
|
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,
|
2015-05-13 21:51:10 +02:00
|
|
|
ArrayView<int> data1, ArrayView<int> data2,
|
|
|
|
Equal eq)
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
|
|
|
const int delta = N - M;
|
|
|
|
MirroredArray<int> V1{data1, N + M};
|
|
|
|
MirroredArray<int> V2{data2, N + M};
|
|
|
|
|
|
|
|
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-13 21:51:10 +02:00
|
|
|
template<typename Iterator, typename Equal>
|
|
|
|
void find_diff_rec(Iterator a, int offA, int lenA,
|
|
|
|
Iterator b, int offB, int lenB,
|
2015-05-13 00:41:35 +02:00
|
|
|
ArrayView<int> data1, ArrayView<int> data2,
|
2015-05-13 21:51:10 +02:00
|
|
|
Equal eq, Vector<Diff>& diffs)
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2015-05-13 21:51:10 +02:00
|
|
|
if (lenA > 0 and lenB > 0)
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
2015-05-13 21:51:10 +02:00
|
|
|
auto middle_snake = find_middle_snake(a + offA, lenA, b + offB, lenB, data1, data2, eq);
|
2015-05-14 14:57:03 +02:00
|
|
|
kak_assert(middle_snake.u <= lenA and middle_snake.v <= lenB);
|
2015-05-13 00:41:35 +02:00
|
|
|
if (middle_snake.d > 1)
|
|
|
|
{
|
2015-05-13 21:51:10 +02:00
|
|
|
find_diff_rec(a, offA, middle_snake.x,
|
|
|
|
b, offB, middle_snake.y,
|
|
|
|
data1, data2, eq, diffs);
|
|
|
|
|
|
|
|
if (int len = middle_snake.u - middle_snake.x)
|
|
|
|
diffs.push_back({Diff::Keep, len, 0});
|
2015-05-13 00:41:35 +02:00
|
|
|
|
2015-05-13 21:51:10 +02:00
|
|
|
find_diff_rec(a, offA + middle_snake.u, lenA - middle_snake.u,
|
|
|
|
b, offB + middle_snake.v, lenB - middle_snake.v,
|
|
|
|
data1, data2, eq, diffs);
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
else if (middle_snake.d == 1)
|
|
|
|
{
|
2015-05-15 14:55:39 +02:00
|
|
|
if (int diag = middle_snake.x - (middle_snake.add ? 0 : 1))
|
2015-05-13 21:51:10 +02:00
|
|
|
diffs.push_back({Diff::Keep, diag, 0});
|
|
|
|
|
2015-05-13 00:41:35 +02:00
|
|
|
if (middle_snake.add)
|
2015-05-13 21:51:10 +02:00
|
|
|
diffs.push_back({Diff::Add, 1, offB + middle_snake.y-1});
|
2015-05-13 00:41:35 +02:00
|
|
|
else
|
2015-05-13 21:51:10 +02:00
|
|
|
diffs.push_back({Diff::Remove, 1, 0});
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
2015-05-14 14:57:03 +02:00
|
|
|
else if (int len = middle_snake.u - middle_snake.x)
|
|
|
|
diffs.push_back({Diff::Keep, len, 0});
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
2015-05-13 21:51:10 +02:00
|
|
|
else if (lenB > 0)
|
|
|
|
diffs.push_back({Diff::Add, lenB, offB});
|
|
|
|
else if (lenA > 0)
|
|
|
|
diffs.push_back({Diff::Remove, lenA, 0});
|
2015-05-13 00:41:35 +02:00
|
|
|
}
|
|
|
|
|
2015-05-13 21:51:10 +02:00
|
|
|
inline void compact_diffs(Vector<Diff>& diffs)
|
2015-05-13 00:41:35 +02:00
|
|
|
{
|
|
|
|
if (diffs.size() < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto out_it = diffs.begin();
|
|
|
|
for (auto it = out_it + 1; it != diffs.end(); ++it)
|
|
|
|
{
|
2015-05-13 21:51:10 +02:00
|
|
|
if (it->mode == out_it->mode and
|
|
|
|
(it->mode != Diff::Add or
|
|
|
|
it->posB == out_it->posB + out_it->len))
|
|
|
|
out_it->len += it->len;
|
2015-05-13 00:41:35 +02:00
|
|
|
else if (++out_it != it)
|
|
|
|
*out_it = *it;
|
|
|
|
}
|
2015-05-13 21:51:10 +02:00
|
|
|
diffs.erase(out_it+1, diffs.end());
|
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>>
|
|
|
|
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;
|
|
|
|
find_diff_rec(a, 0, N, b, 0, M,
|
|
|
|
{data.data(), (size_t)max}, {data.data() + max, (size_t)max},
|
|
|
|
eq, diffs);
|
|
|
|
|
|
|
|
compact_diffs(diffs);
|
2015-05-13 00:41:35 +02:00
|
|
|
|
|
|
|
return diffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|