-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlcs.c
147 lines (124 loc) · 3.57 KB
/
lcs.c
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
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "context.h"
#include "lcs.h"
#define MAX(x,y) (x > y ? x : y)
static size_t lcs (uint8_t *obuf, size_t osize, uint8_t *nbuf, size_t nsize,
uint8_t *lbuf, size_t *scratch)
{
size_t lsize;
for (size_t i = 1; i <= osize; i++) {
for (size_t j = 1; j <= nsize; j++) {
size_t lpos = i * (nsize + 1) + j;
size_t rpos = (i - 1) * (nsize + 1) + j;
if (obuf[i - 1] == nbuf[j - 1])
scratch[lpos] = scratch[rpos - 1] + 1;
else
scratch[lpos] = MAX(scratch[rpos], scratch[rpos + nsize]);
}
}
lsize = scratch[osize * (nsize + 1) + nsize];
{
size_t op = osize;
size_t np = nsize;
for (int i = (int) lsize - 1; i >= 0; i--) {
size_t lpos = op * (nsize + 1) + np - 1;
size_t rpos = (op - 1) * (nsize + 1) + np;
if (obuf[op - 1] == nbuf[np - 1]) {
lbuf[i] = obuf[op - 1];
op--;
np--;
} else if (scratch[lpos] > scratch[rpos]) {
np--;
i++;
} else {
op--;
i++;
}
}
}
return lsize;
}
#undef MAX
static void update_masks(Context *ctx, size_t *podp, size_t *pndp, int subrun, int addrun)
{
uint8_t *odiff = ctx->odiff;
uint8_t *ndiff = ctx->ndiff;
size_t odp = *podp;
size_t ndp = *pndp;
/* for nbuf */
for (int j = 0; j < subrun; j++)
ndiff[ndp + j] = MISSING;
for (int j = 0; j < addrun; j++) {
if (ndiff[ndp + j] == MISSING)
ndiff[ndp + j] = CHANGED;
else if (ndiff[ndp + j] == SAME)
ndiff[ndp + j] = CHANGED;
}
/* For obuf. */
for (int j = 0; j < addrun; j++)
odiff[odp + j] = MISSING;
for (int j = 0; j < subrun; j++) {
if (odiff[odp + j] == MISSING)
odiff[odp + j] = CHANGED;
else if (odiff[odp + j] == SAME)
odiff[odp + j] = CHANGED;
}
if (subrun < addrun) {
*pndp += addrun + 1;
*podp += addrun + 1;
} else {
*pndp += subrun + 1;
*podp += subrun + 1;
}
}
bool calc_lcs_mask(Context *ctx, uint8_t *lbuf, size_t *scratch)
{
uint8_t *obuf = ctx->obuf;
uint8_t *nbuf = ctx->nbuf;
size_t blocksize = ctx->blocksize;
size_t odp = 0;
size_t ndp = 0;
size_t op = 0;
size_t np = 0;
int subrun = 0;
int addrun = 0;
off_t ooff = ctx->of_offset;
off_t noff = ctx->nf_offset;
int ret;
size_t lsz = lcs(&obuf[ooff], blocksize, &nbuf[noff], blocksize, lbuf, scratch);
ret = (lsz == blocksize);
for (size_t i = 0; i < lsz; i++) {
if (obuf[ooff + op] == lbuf[i] && nbuf[noff + np] == lbuf[i]) {
if (subrun || addrun) {
update_masks(ctx, &odp, &ndp, subrun, addrun);
subrun = 0;
addrun = 0;
} else {
odp++;
ndp++;
}
op++;
np++;
continue;
} else if (obuf[ooff + op] != lbuf[i]) {
subrun++;
op++;
i--;
} else if (nbuf[noff + np] != lbuf[i]) {
addrun++;
np++;
i--;
}
}
subrun = blocksize - op;
addrun = blocksize - np;
update_masks(ctx, &odp, &ndp, subrun, addrun);
ctx->odsize = odp - 1;
ctx->ndsize = ndp - 1;
ctx->is_cleared = false;
return ret;
}