Fix diff implementation and change the Diff struct format

This commit is contained in:
Maxime Coste 2015-05-13 20:51:10 +01:00
parent 51f6c52013
commit 7a8c2d7f56

View File

@ -1,6 +1,9 @@
#include "array_view.hh" #include "array_view.hh"
#include "vector.hh" #include "vector.hh"
#include <functional>
#include <iterator>
namespace Kakoune namespace Kakoune
{ {
@ -21,10 +24,10 @@ private:
struct Snake{ int x, y, u, v; bool add; }; struct Snake{ int x, y, u, v; bool add; };
template<typename Iterator> template<typename Iterator, typename Equal>
Snake find_end_snake_of_further_reaching_dpath(Iterator a, int N, Iterator b, int M, Snake find_end_snake_of_further_reaching_dpath(Iterator a, int N, Iterator b, int M,
const MirroredArray<int>& V, const MirroredArray<int>& V,
const int D, const int k) const int D, const int k, Equal eq)
{ {
int x; // our position along a int x; // our position along a
@ -43,7 +46,7 @@ Snake find_end_snake_of_further_reaching_dpath(Iterator a, int N, Iterator b, in
int u = x, v = y; int u = x, v = y;
// follow end snake along diagonal k // follow end snake along diagonal k
while (u < N and v < M and a[u] == b[v]) while (u < N and v < M and eq(a[u], b[v]))
++u, ++v; ++u, ++v;
return { x, y, u, v, add }; return { x, y, u, v, add };
@ -55,9 +58,10 @@ struct SnakeLen : Snake
int d; int d;
}; };
template<typename Iterator> template<typename Iterator, typename Equal>
SnakeLen find_middle_snake(Iterator a, int N, Iterator b, int M, SnakeLen find_middle_snake(Iterator a, int N, Iterator b, int M,
ArrayView<int> data1, ArrayView<int> data2) ArrayView<int> data1, ArrayView<int> data2,
Equal eq)
{ {
const int delta = N - M; const int delta = N - M;
MirroredArray<int> V1{data1, N + M}; MirroredArray<int> V1{data1, N + M};
@ -69,7 +73,7 @@ SnakeLen find_middle_snake(Iterator a, int N, Iterator b, int M,
{ {
for (int k1 = -D; k1 <= D; k1 += 2) for (int k1 = -D; k1 <= D; k1 += 2)
{ {
auto p = find_end_snake_of_further_reaching_dpath(a, N, b, M, V1, D, k1); auto p = find_end_snake_of_further_reaching_dpath(a, N, b, M, V1, D, k1, eq);
V1[k1] = p.u; V1[k1] = p.u;
const int k2 = -(k1 - delta); const int k2 = -(k1 - delta);
@ -82,7 +86,7 @@ SnakeLen find_middle_snake(Iterator a, int N, Iterator b, int M,
for (int k2 = -D; k2 <= D; k2 += 2) for (int k2 = -D; k2 <= D; k2 += 2)
{ {
auto p = find_end_snake_of_further_reaching_dpath(ra, N, rb, M, V2, D, k2); auto p = find_end_snake_of_further_reaching_dpath(ra, N, rb, M, V2, D, k2, eq);
V2[k2] = p.u; V2[k2] = p.u;
const int k1 = -(k2 - delta); const int k1 = -(k2 - delta);
@ -95,53 +99,60 @@ SnakeLen find_middle_snake(Iterator a, int N, Iterator b, int M,
} }
kak_assert(false); kak_assert(false);
return { {}, 0 };
} }
template<typename Iterator>
struct Diff struct Diff
{ {
bool add; enum { Keep, Add, Remove } mode;
Iterator begin; int len;
Iterator end; int posB;
}; };
template<typename Iterator> template<typename Iterator, typename Equal>
void find_diff_rec(Iterator a, size_t N, Iterator b, size_t M, void find_diff_rec(Iterator a, int offA, int lenA,
Iterator b, int offB, int lenB,
ArrayView<int> data1, ArrayView<int> data2, ArrayView<int> data1, ArrayView<int> data2,
Vector<Diff<Iterator>>& diffs) Equal eq, Vector<Diff>& diffs)
{ {
if (N > 0 and M > 0) if (lenA > 0 and lenB > 0)
{ {
auto middle_snake = find_middle_snake(a, N, b, M, data1, data2); auto middle_snake = find_middle_snake(a + offA, lenA, b + offB, lenB, data1, data2, eq);
if (middle_snake.d > 1) if (middle_snake.d > 1)
{ {
find_diff_rec(a, middle_snake.x, b, middle_snake.y, find_diff_rec(a, offA, middle_snake.x,
data1, data2, diffs); b, offB, middle_snake.y,
data1, data2, eq, diffs);
find_diff_rec(a + middle_snake.u, N - middle_snake.u, if (int len = middle_snake.u - middle_snake.x)
b + middle_snake.v, M - middle_snake.v, diffs.push_back({Diff::Keep, len, 0});
data1, data2, diffs);
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);
} }
else if (middle_snake.d == 1) else if (middle_snake.d == 1)
{ {
int diag = 0; int diag = 0;
while (a[diag] == b[diag]) while (eq(a[offA + diag], b[offB + diag]))
++diag; ++diag;
if (diag != 0)
diffs.push_back({Diff::Keep, diag, 0});
if (middle_snake.add) if (middle_snake.add)
diffs.push_back({true, b + middle_snake.y, b + middle_snake.y + 1}); diffs.push_back({Diff::Add, 1, offB + middle_snake.y-1});
else else
diffs.push_back({false, a + middle_snake.x-1, a + middle_snake.x}); diffs.push_back({Diff::Remove, 1, 0});
} }
} }
else if (M > 0) else if (lenB > 0)
diffs.push_back({true, b, b + M}); diffs.push_back({Diff::Add, lenB, offB});
else if (N > 0) else if (lenA > 0)
diffs.push_back({false, a, a + N}); diffs.push_back({Diff::Remove, lenA, 0});
} }
template<typename Iterator> inline void compact_diffs(Vector<Diff>& diffs)
void compact_diffs(Vector<Diff<Iterator>>& diffs)
{ {
if (diffs.size() < 2) if (diffs.size() < 2)
return; return;
@ -149,25 +160,28 @@ void compact_diffs(Vector<Diff<Iterator>>& diffs)
auto out_it = diffs.begin(); auto out_it = diffs.begin();
for (auto it = out_it + 1; it != diffs.end(); ++it) for (auto it = out_it + 1; it != diffs.end(); ++it)
{ {
if (it->add == out_it->add and it->begin == out_it->end) if (it->mode == out_it->mode and
out_it->end = it->end; (it->mode != Diff::Add or
it->posB == out_it->posB + out_it->len))
out_it->len += it->len;
else if (++out_it != it) else if (++out_it != it)
*out_it = *it; *out_it = *it;
} }
diffs.erase(out_it+1, diffs.end());
} }
template<typename Iterator> template<typename Iterator, typename Equal = std::equal_to<typename std::iterator_traits<Iterator>::value_type>>
Vector<Diff<Iterator>> find_diff(Iterator a, size_t N, Iterator b, size_t M) Vector<Diff> find_diff(Iterator a, int N, Iterator b, int M,
Equal eq = Equal{})
{ {
Vector<int> data(4 * (N+M)); const int max = 2 * (N + M) + 1;
Vector<Diff<Iterator>> diffs; Vector<int> data(2*max);
const size_t max_D_size = 2 * (N + M) + 1; Vector<Diff> diffs;
find_diff_rec(a, N, b, M, find_diff_rec(a, 0, N, b, 0, M,
{data.data(), max_D_size}, {data.data(), (size_t)max}, {data.data() + max, (size_t)max},
{data.data() + max_D_size, max_D_size}, eq, diffs);
diffs);
// compact_diffs(diffs); compact_diffs(diffs);
return diffs; return diffs;
} }