-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-diff.h
177 lines (138 loc) · 3.45 KB
/
generic-diff.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (c) 2015-2016 SUSE Linux GmbH
*
* Licensed under the GNU General Public License Version 2
* as published by the Free Software Foundation.
*
* See http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* for details.
*
* Author: Joerg Roedel <jroedel@suse.de>
*/
#ifndef __GENERICDIFF_H
#define __GENERICDIFF_H
#include <memory>
namespace diff {
using size_type = unsigned;
enum class diff_type {
EQUAL,
ADDED,
REMOVED,
};
struct diff_element {
enum diff_type type;
size_type idx_a;
size_type idx_b;
};
// Interface for diffable content,
// T must provide '==' and '!=' operators
template<typename T>
class diffable {
public:
// Get total number of elements to diff
virtual size_type elements() const = 0;
// Get specific element
virtual const T& element(size_type) const = 0;
};
class lcs_matrix {
private:
size_type m_x;
size_type m_y;
std::unique_ptr<int[]> m_matrix;
std::unique_ptr<bool[]> m_bool;
public:
lcs_matrix(size_type x, size_type y)
: m_x(x + 1), m_y(y + 1),
m_matrix(new int[m_x * m_y]), m_bool(new bool[m_x * m_y])
{
}
void set(size_type x, size_type y, size_type value)
{
m_matrix[(y * m_x) + x] = value;
}
size_type get(size_type x, size_type y) const
{
return m_matrix[(y * m_x) + x];
}
void set_bool(size_type x, size_type y, bool value)
{
m_bool[(y * m_x) + x] = value;
}
bool get_bool(size_type x, size_type y) const
{
return m_bool[(y * m_x) + x];
}
};
template<typename T>
class diff {
private:
lcs_matrix m_lcs;
const diffable<T> &m_a;
const diffable<T> &m_b;
void create()
{
auto size_a = m_a.elements() + 1;
auto size_b = m_b.elements() + 1;
decltype(size_a) a;
decltype(size_b) b;
for (a = 0; a < size_a; ++a) {
for (b = 0; b < size_b; ++b) {
if (a == 0 || b == 0) {
m_lcs.set(a, b, 0);
continue;
}
if (m_a.element(a - 1) == m_b.element(b - 1)) {
m_lcs.set(a, b, m_lcs.get(a - 1, b - 1) + 1);
m_lcs.set_bool(a, b, true);
} else {
int ai = m_lcs.get(a - 1, b);
int bi = m_lcs.get(a, b - 1);
m_lcs.set(a, b, std::max(ai, bi));
m_lcs.set_bool(a, b, false);
}
}
}
}
void create_diff(std::vector<diff_element> &output,
size_type a, size_type b) const
{
struct diff_element element;
if (a > 0 && b > 0 && m_lcs.get_bool(a, b)) {
create_diff(output, a - 1, b - 1);
element.type = diff_type::EQUAL;
element.idx_a = a - 1;
element.idx_b = b - 1;
output.push_back(element);
} else if (b > 0 && (a == 0 || m_lcs.get(a, b - 1) >= m_lcs.get(a - 1, b))) {
create_diff(output, a, b - 1);
element.type = diff_type::ADDED;
element.idx_b = b - 1;
output.push_back(element);
} else if (a > 0 && (b == 0 || m_lcs.get(a, b - 1) < m_lcs.get(a - 1, b))) {
create_diff(output, a - 1, b);
element.type = diff_type::REMOVED;
element.idx_a = a - 1;
output.push_back(element);
}
}
public:
diff(const diffable<T> &a, const diffable<T> &b)
: m_lcs(a.elements(), b.elements()), m_a(a), m_b(b)
{
create();
}
bool is_different() const
{
auto size_a = m_a.elements();
auto size_b = m_b.elements();
return ((size_a != size_b) || (m_lcs.get(size_a, size_b) != size_a));
}
std::vector<diff_element> get_diff() const
{
std::vector<diff_element> ret;
create_diff(ret, m_a.elements(), m_b.elements());
return ret;
}
};
} // Namespace diff
#endif