forked from leea/diff-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcs.h
493 lines (398 loc) · 14.5 KB
/
lcs.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#ifndef LCH_H
#define LCS_H
#include <algorithm>
#include "Matrix.h"
#include "RandomAccessSequence.h"
#include "DiffErr.h"
#include <cassert>
#include <climits>
#include <functional>
#include <sstream>
#include <list>
typedef NegIndexVector<uint32_t> Vector;
/**
This class encapsulates operations used on (col, row) positions
in the matrix that's used to trace the D-Path
*/
class Position {
public:
u_int x;
u_int y;
Position(){}
Position(u_int col, u_int row):
x(col), y(row){}
inline bool operator==(Position RHS)
{ return y==RHS.y && x == RHS.x; }
inline bool operator!=(Position RHS)
{ return !(*this == RHS); }
inline Position operator--(){
--y; --x;
return *this;
}
inline Position operator++(){
++y; ++x;
return *this;
}
friend std::ostream &operator << (std::ostream &out, Position &p) {
return out << "(" << p.x << "," << p.y << ")";
}
};
/* Hirshberg's linear space refinement relies on being able to run
* the same algorithm in the forward and reverse direction. When the
* direction is FORWARD, the algorithm starts at (0, 0) and searches
* forward. When the direction is REVERSE, the algorithm stars at
* (Orig.size(), New.size()) and searches backward. */
typedef enum {FORWARD, REVERSE} Direction;
template < Direction dir,
typename _RandomAccessSequenceTy,
typename _Equivalent>
class MyersAlgorithm {
typedef std::list<typename _RandomAccessSequenceTy::ElemTy> LCSList;
_RandomAccessSequenceTy Orig;
_RandomAccessSequenceTy New;
int size_delta;
unsigned D;
Vector V;
_Equivalent Cmp;
/** Takes a position that would be an offset from the beginning of the
seqence in the forward direction and mirrors it so that it's an
offset from the end of the sequence.
*/
inline Position normalize(Position p){
if(dir == REVERSE)
return Position(New.size() - p.x, Orig.size() - p.y);
else
return p;
}
/** Extends the longest possible snake from position front,
returning the last position of the snake
@param Front the first position
*/
inline Position snake(Position front) {
Position norm = normalize(front);
debugOut << " snake: front=" << front << " normalized=" << norm << std::endl;
assert(front.y <= Orig.size() && front.x <= New.size());
while (front.y < Orig.size() && front.x < New.size() &&
(dir==FORWARD ?
Cmp(Orig[norm.y], New[norm.x]) :
Cmp(Orig[norm.y-1], New[norm.x-1]))) {
++front;
norm = normalize(front);
}
return front;
}
/**
* Computes the starting diagonal for Myer's algorithm
*
*/
int k_begin() {
// if D has grown larger than Orig.size(), set the k to the first
// starting diagonal within the matrix
if (D >= Orig.size()) {
// Since diagonals are increased with steps of 2, set the
// starting diagonal depending on whether the delta of D and
// Orig.size is even or odd.
const int delta = D - Orig.size();
if (delta % 2 == 0){
return -(Orig.size()-2);
} else {
return -(Orig.size()-1);
}
}
else return -D;
}
/**
* Computes the stopping diagonal for Myer's algorithm
*
*
*/
int k_end(){
// if D has grown larger than New.size(), set the end to the last
// diagonal within the matrix
if (D >= New.size()){
// Since diagonals are increased with steps of 2, set the
// ending diagonal depending on whether the delta of D and
// New.size is even or odd.
const int delta = D - New.size();
if (delta % 2 == 0){
return (New.size()-2);
} else {
return (New.size()-1);
}
}
else return D;
}
public:
/**
* Computes the furthest reaching D-paths.
*
* This function makes one "step", computing the furthest reaching D
* path for all diagonals from k_begin() to k_end()
*/
bool trace_D_path() {
if(dir==FORWARD) {debugOut<< "Forward: \n";} else debugOut<< "Reverse: \n";
debugOut << " trace_D_path: ";
int kBegin = k_begin();
int kEnd = k_end();
debugOut << "D=" << D << " k=" << kBegin << " to " << kEnd << "\n";
assert(D < INT_MAX); //TODO make this an error case?
// For each diagonal k
for (int k = kBegin; k <= kEnd; k+=2) {
unsigned row, col;
if ((k == -(int)D) ||
(k != (int)D && V[k-1] < V[k+1]))
col = V[k+1];
else
col = V[k-1] + 1;
row = col - k;
debugOut << " x=" << col << " y=" << row << std::endl;
if (row > Orig.size() || col > New.size()) {
debugOut << " Outside Matrix col=" << col << " row=" <<row <<"\n";
continue;
}
Position furthest = snake(Position(col, row));
debugOut << " end=" << furthest << std::endl;
V[k] = furthest.x;
if ((furthest.y == Orig.size()) && (furthest.x == New.size())){
debugOut << " Reached End";
return true;
}
}
return false;
}
/**
* Checks if the forward algorithm has collided with the reverse algorithm.
* If it has, return the point of the overlap, where the two sequences can be bisected
*
* @param reverse IN the vector of furthest reaching paths in the reverse direction
* @param bisect OUT the position where the overlap occurred.
*/
bool is_overlapped(Vector &forward, Position &bisect) {
debugOut << " is_overlapped: \n";
assert(dir==REVERSE);
int32_t kBegin = k_begin();
int32_t kEnd = k_end();
//Only check the diagonals that have been walked in the other direction
int32_t kb = std::max(size_delta - kEnd, kBegin);
int32_t ke = std::min(size_delta - kBegin, kEnd);
for (int k = kb; k <= ke; k++) {
int k_r = size_delta - k;
//TODO add function (V,k) -> Position
Position reversePos = Position(V[k], V[k] - k);
Position forwardPos = Position(forward[k_r], forward[k_r] - k_r);
reversePos = normalize(reversePos);
debugOut << " k=" << k << " forwardPos=" << forwardPos
<< " reversePos=" << reversePos << std::endl;
if (forwardPos.x >= reversePos.x){
bisect = forwardPos;
return true;
}
}
return false;
}
MyersAlgorithm(_RandomAccessSequenceTy O,
_RandomAccessSequenceTy N, _Equivalent Cmp = _Equivalent())
:Orig(O), New(N), size_delta(New.size() - Orig.size()),
D(0), V(Orig.size(), New.size()), Cmp(Cmp)
{
if(dir==FORWARD) { debugOut<< "Forward: \n"; } else debugOut<< "Reverse: \n";
}
Vector & getV() {return V;}
void incr_D() { ++D; }
};
template <typename _RandomAccessSequenceTy,
typename _Equivalent = std::equal_to<
typename _RandomAccessSequenceTy::ElemTy>>
class Diff {
public:
typedef std::list<typename std::reference_wrapper<
const typename _RandomAccessSequenceTy::ElemTy>> LCSList;
typedef std::list<unsigned> IndexList;
protected:
//The Longest Common Subsequence for the two sequences
LCSList _LCS;
IndexList _OrigLCSIndices;
IndexList _NewLCSIndices;
IndexList _OrigOnlyIndices;
IndexList _NewOnlyIndices;
//Eat up common elements at the beginning of both sequences
inline void eatPrefix(_RandomAccessSequenceTy &Orig,
_RandomAccessSequenceTy &New,
IndexList &origPrefix,
IndexList &newPrefix,
unsigned origOffset,
unsigned newOffset) {
while ((Orig.size() != 0 && New.size() != 0) &&
_Cmp(*Orig.begin(), *New.begin())) {
debugOut << "Added " << *Orig.begin() <<"\n";
//Append the common element to the LCS
origPrefix.push_back(origOffset);
newPrefix.push_back(newOffset);
origOffset++;
newOffset++;
//Remove it from both sequences
Orig.pop_front();
New.pop_front();
}
}
//Eat up common elements at the end of both sequences
inline void eatSuffix(_RandomAccessSequenceTy &Orig,
_RandomAccessSequenceTy &New,
IndexList &origSuffix,
IndexList &newSuffix,
unsigned origOffset,
unsigned newOffset) {
unsigned origIndex = origOffset + Orig.size() - 1;
unsigned newIndex = newOffset + New.size() - 1;
while ((Orig.size() != 0 && New.size() != 0) &&
_Cmp(*(Orig.end()-1), *(New.end()-1))) {
debugOut << "Added " << *(Orig.end()-1)<< "\n";
//Append the common element to the LCS
origSuffix.push_front(origIndex);
newSuffix.push_front(newIndex);
//Remove it from both sequences
Orig.pop_back();
--origIndex;
New.pop_back();
--newIndex;
}
}
void do_diff(_RandomAccessSequenceTy Orig,
_RandomAccessSequenceTy New,
IndexList &OrigLCSIndices,
IndexList &NewLCSIndices,
unsigned origOffset,
unsigned newOffset) {
debugOut << "do_diff Orig.size=" << Orig.size()
<< " New.size=" << New.size() << std::endl;
dprintMatrix(Orig, New);
IndexList origPrefix, origSuffix, newPrefix, newSuffix;
//Eat up common elements at the beginning and end of the sequence
eatPrefix(Orig, New, origPrefix, newPrefix, origOffset, newOffset);
origOffset += origPrefix.size();
newOffset += newPrefix.size();
eatSuffix(Orig, New, origSuffix, newSuffix, origOffset, newOffset);
//If the problem is trivial, solve it
if (Orig.size() == 0 || New.size() == 0){
//lcs is empty; do nothing
}
else if (Orig.size() == 1) {
unsigned index = New.find(Orig[0], _Cmp);
if (index != static_cast<unsigned>(-1)) {
OrigLCSIndices.push_front(origOffset);
NewLCSIndices.push_front(newOffset + index);
}
}
else if (New.size() == 1) {
unsigned index = Orig.find(New[0], _Cmp);
if (index != static_cast<unsigned>(-1)) {
OrigLCSIndices.push_front(origOffset + index);
NewLCSIndices.push_front(newOffset);
}
//Otherwise find the bisection point, and compute the diff of the left and right part
} else {
_RandomAccessSequenceTy origLeft, origRight, newLeft, newRight;
// Get the bisection point
Position bisection = bisect(Orig, New);
Orig.split(bisection.y, origLeft, origRight);
New.split(bisection.x, newLeft, newRight);
// Compute the diffs of the left and right part
IndexList newLeftIndices, origLeftIndices, newRightIndices,
origRightIndices;
do_diff(origLeft, newLeft, origLeftIndices, newLeftIndices, origOffset,
newOffset);
do_diff(origRight, newRight, origRightIndices, newRightIndices,
origOffset + bisection.y, newOffset + bisection.x);
// Join the results
OrigLCSIndices.splice(OrigLCSIndices.begin(), origRightIndices);
OrigLCSIndices.splice(OrigLCSIndices.begin(), origLeftIndices);
NewLCSIndices.splice(NewLCSIndices.begin(), newRightIndices);
NewLCSIndices.splice(NewLCSIndices.begin(), newLeftIndices);
}
//Add the prefix and suffix back;
if (!origPrefix.empty()) {
OrigLCSIndices.splice(OrigLCSIndices.begin(), origPrefix);
NewLCSIndices.splice(NewLCSIndices.begin(), newPrefix);
}
if (!origSuffix.empty()) {
OrigLCSIndices.splice(OrigLCSIndices.end(), origSuffix);
NewLCSIndices.splice(NewLCSIndices.end(), newSuffix);
}
}
Position bisect( _RandomAccessSequenceTy Orig,
_RandomAccessSequenceTy New ) {
MyersAlgorithm<FORWARD,_RandomAccessSequenceTy, _Equivalent> forward(
Orig, New, _Cmp);
MyersAlgorithm<REVERSE,_RandomAccessSequenceTy, _Equivalent> reverse(
Orig, New, _Cmp);
bool overlap = false;
Position bisection;
// D is the length of the Shortest Edit Script.
// Search D-paths until the end of each string is reached
while (!overlap) {
forward.trace_D_path();
reverse.trace_D_path();
overlap = reverse.is_overlapped(forward.getV(), bisection);
forward.incr_D(); reverse.incr_D();
}
return bisection;
}
public:
Diff(_RandomAccessSequenceTy Orig, _RandomAccessSequenceTy New,
_Equivalent cmp = _Equivalent())
: _Cmp(cmp) {
Init(Orig, New);
}
template <class RandomAccessIterable>
Diff(const RandomAccessIterable& Orig, const RandomAccessIterable& New,
_Equivalent cmp = _Equivalent()) : _Cmp(cmp) {
_RandomAccessSequenceTy origSeq(Orig.begin(), Orig.end());
_RandomAccessSequenceTy newSeq(New.begin(), New.end());
Init(origSeq, newSeq);
}
inline const LCSList & LCS() const { return _LCS; }
inline const IndexList & OrigLCSIndices() const { return _OrigLCSIndices; }
inline const IndexList & NewLCSIndices() const { return _NewLCSIndices; }
inline const IndexList & OrigOnlyIndices() const { return _OrigOnlyIndices; }
inline const IndexList & NewOnlyIndices() const { return _NewOnlyIndices; }
private:
void Init(_RandomAccessSequenceTy& Orig,
_RandomAccessSequenceTy& New) {
do_diff(Orig, New, _OrigLCSIndices, _NewLCSIndices, 0, 0);
// Doesn't matter which one we populate _LCS from.
PopulateUniqueIndices(Orig, _OrigLCSIndices, _OrigOnlyIndices, true);
PopulateUniqueIndices(New, _NewLCSIndices, _NewOnlyIndices, false);
}
void PopulateUniqueIndices(_RandomAccessSequenceTy& input_seq,
IndexList& lcs_index_list,
IndexList& unique_indices, bool record_lcs) {
auto lcs_iter = lcs_index_list.begin();
auto input_iter = input_seq.begin();
for (unsigned index = 0; index < input_seq.size(); ++index, ++input_iter) {
if (lcs_iter != lcs_index_list.end() && index == *lcs_iter) {
if (record_lcs) {
_LCS.emplace_back(*input_iter);
}
++lcs_iter;
} else {
unique_indices.push_back(index);
}
}
assert(_LCS.size() + unique_indices.size() == input_seq.size());
}
_Equivalent _Cmp;
};
template < typename RandomAccessIterator,
typename OutputIterator,
typename Equivalent = std::equal_to<void>>
OutputIterator
lcs (RandomAccessIterator begin1, RandomAccessIterator end1,
RandomAccessIterator begin2, RandomAccessIterator end2,
OutputIterator output) {
typedef RandomAccessSequence<RandomAccessIterator> RandAccSeqTy;
RandAccSeqTy Orig(begin1, end1);
RandAccSeqTy New(begin2, end2);
Diff<RandAccSeqTy, Equivalent> Instance(Orig, New);
return std::copy(Instance.LCS().begin(), Instance.LCS().end(), output);
}
#endif