forked from greenantdotcom/diffpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence_matcher.cpp
203 lines (175 loc) · 5.46 KB
/
sequence_matcher.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Copyright © 2011-13 Qtrac Ltd. All rights reserved.
This program or module is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 2 of
the License, or (at your option) any later version. This program is
distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
*/
#include "sequence_matcher.hpp"
#include <QSet>
RangesPair computeRanges(SequenceMatcher *matcher)
{
Ranges ranges1;
Ranges ranges2;
QList<Match> matches = matcher->get_matching_blocks();
foreach (const Match &match, matches) {
if (match.size == 0)
continue;
ranges1 |= unorderedRange(match.i + match.size, match.i);
ranges2 |= unorderedRange(match.j + match.size, match.j);
}
return qMakePair(ranges1, ranges2);
}
RangesPair invertRanges(const Ranges &ranges1, int length1,
const Ranges &ranges2, int length2)
{
const Ranges newRanges1 = unorderedRange(length1) - ranges1;
const Ranges newRanges2 = unorderedRange(length2) - ranges2;
return qMakePair(newRanges1, newRanges2);
}
bool matchLessThan(const Match &a, const Match &b)
{
if (a.i != b.i)
return a.i < b.i;
if (a.j != b.j)
return a.j < b.j;
return a.size < b.size;
}
struct Offsets
{
Offsets(int a_low_=0, int a_high_=0, int b_low_=0, int b_high_=0)
: a_low(a_low_), a_high(a_high_), b_low(b_low_), b_high(b_high_) {}
int a_low;
int a_high;
int b_low;
int b_high;
};
SequenceMatcher::SequenceMatcher(const Sequence &a_, const Sequence &b_)
: a(a_), b(b_)
{
set_sequences(a, b);
}
void SequenceMatcher::set_sequence1(const Sequence &sequence)
{
a = sequence;
matching_blocks.clear();
}
void SequenceMatcher::set_sequence2(const Sequence &sequence)
{
b = sequence;
matching_blocks.clear();
chain_b();
}
void SequenceMatcher::chain_b()
{
const int N = b.count();
b2j.clear();
QSet<Element> popular;
for (int i = 0; i < N; ++i) {
const Element &element = b.at(i);
if (b2j.contains(element)) {
QList<int> &indexes = b2j[element];
if (N >= 200 && indexes.count() * 100 > N) {
popular.insert(element);
indexes.clear();
}
else
indexes.append(i);
}
else
b2j[element].append(i);
}
foreach (const Element &element, popular)
b2j.remove(element);
}
QList<Match> SequenceMatcher::get_matching_blocks()
{
if (!matching_blocks.isEmpty())
return matching_blocks;
const int LengthA = a.count();
const int LengthB = b.count();
QList<Offsets> offsets;
offsets << Offsets(0, LengthA, 0, LengthB);
while (!offsets.isEmpty()) {
const Offsets &offset = offsets.takeLast();
const int a_low = offset.a_low;
const int a_high = offset.a_high;
const int b_low = offset.b_low;
const int b_high = offset.b_high;
const Match match = find_longest_match(a_low, a_high, b_low,
b_high);
const int i = match.i;
const int j = match.j;
const int k = match.size;
if (k) {
matching_blocks.append(match);
if (a_low < i && b_low < j)
offsets.append(Offsets(a_low, i, b_low, j));
if (i + k < a_high && j + k < b_high)
offsets.append(Offsets(i + k, a_high, j + k, b_high));
}
}
qSort(matching_blocks.begin(), matching_blocks.end(), matchLessThan);
int i1 = 0;
int j1 = 0;
int k1 = 0;
QList<Match> non_adjacent;
foreach (const Match match, matching_blocks) {
const int i2 = match.i;
const int j2 = match.j;
const int k2 = match.size;
if (i1 + k1 == i2 && j1 + k1 == j2)
k1 += k2;
else {
if (k1)
non_adjacent.append(Match(i1, j1, k1));
i1 = i2;
j1 = j2;
k1 = k2;
}
}
if (k1)
non_adjacent.append(Match(i1, j1, k1));
non_adjacent.append(Match(LengthA, LengthB, 0));
matching_blocks = non_adjacent;
return matching_blocks;
}
Match SequenceMatcher::find_longest_match(int a_low, int a_high,
int b_low, int b_high)
{
int best_i = a_low;
int best_j = b_low;
int best_size = 0;
QHash<int, int> j2len;
for (int i = a_low; i < a_high; ++i) {
QHash<int, int> newj2len;
foreach (int j, b2j.value(a[i])) {
if (j < b_low)
continue;
if (j >= b_high)
break;
const int k = j2len.value(j - 1, 0) + 1;
newj2len[j] = k;
if (k > best_size) {
best_i = i - k + 1;
best_j = j - k + 1;
best_size = k;
}
}
j2len = newj2len;
}
while (best_i > a_low && best_j > b_low &&
a[best_i - 1] == b[best_j - 1]) {
--best_i;
--best_j;
++best_size;
}
while (best_i + best_size < a_high && best_j + best_size < b_high &&
a[best_i + best_size] == b[best_j + best_size])
++best_size;
return Match(best_i, best_j, best_size);
}