Show More
@@ -30,6 +30,10 struct line { | |||||
30 | const char *l; |
|
30 | const char *l; | |
31 | }; |
|
31 | }; | |
32 |
|
32 | |||
|
33 | struct pos { | |||
|
34 | int pos, len; | |||
|
35 | }; | |||
|
36 | ||||
33 | struct hunk { |
|
37 | struct hunk { | |
34 | int a1, a2, b1, b2; |
|
38 | int a1, a2, b1, b2; | |
35 | }; |
|
39 | }; | |
@@ -87,36 +91,37 int inline cmp(struct line *a, struct li | |||||
87 |
|
91 | |||
88 | static int equatelines(struct line *a, int an, struct line *b, int bn) |
|
92 | static int equatelines(struct line *a, int an, struct line *b, int bn) | |
89 | { |
|
93 | { | |
90 |
int i, j, buckets = 1, t |
|
94 | int i, j, buckets = 1, t; | |
|
95 | struct pos *h; | |||
91 |
|
96 | |||
92 | /* build a hash table of the next highest power of 2 */ |
|
97 | /* build a hash table of the next highest power of 2 */ | |
93 | while (buckets < bn + 1) |
|
98 | while (buckets < bn + 1) | |
94 | buckets *= 2; |
|
99 | buckets *= 2; | |
95 |
|
100 | |||
96 |
h = malloc(buckets * sizeof( |
|
101 | h = malloc(buckets * sizeof(struct pos)); | |
97 | l = calloc(buckets, sizeof(int)); |
|
|||
98 | buckets = buckets - 1; |
|
102 | buckets = buckets - 1; | |
99 |
if (!h |
|
103 | if (!h) | |
100 | free(h); |
|
|||
101 | return 0; |
|
104 | return 0; | |
102 | } |
|
|||
103 |
|
105 | |||
104 | /* clear the hash table */ |
|
106 | /* clear the hash table */ | |
105 | for (i = 0; i <= buckets; i++) |
|
107 | for (i = 0; i <= buckets; i++) { | |
106 | h[i] = -1; |
|
108 | h[i].pos = -1; | |
|
109 | h[i].len = 0; | |||
|
110 | } | |||
107 |
|
111 | |||
108 | /* add lines to the hash table chains */ |
|
112 | /* add lines to the hash table chains */ | |
109 | for (i = bn - 1; i >= 0; i--) { |
|
113 | for (i = bn - 1; i >= 0; i--) { | |
110 | /* find the equivalence class */ |
|
114 | /* find the equivalence class */ | |
111 |
for (j = b[i].h & buckets; h[j] != -1; |
|
115 | for (j = b[i].h & buckets; h[j].pos != -1; | |
112 | if (!cmp(b + i, b + h[j])) |
|
116 | j = (j + 1) & buckets) | |
|
117 | if (!cmp(b + i, b + h[j].pos)) | |||
113 | break; |
|
118 | break; | |
114 |
|
119 | |||
115 | /* add to the head of the equivalence class */ |
|
120 | /* add to the head of the equivalence class */ | |
116 | b[i].n = h[j]; |
|
121 | b[i].n = h[j].pos; | |
117 | b[i].e = j; |
|
122 | b[i].e = j; | |
118 | h[j] = i; |
|
123 | h[j].pos = i; | |
119 |
|
|
124 | h[j].len++; /* keep track of popularity */ | |
120 | } |
|
125 | } | |
121 |
|
126 | |||
122 | /* compute popularity threshold */ |
|
127 | /* compute popularity threshold */ | |
@@ -125,24 +130,24 static int equatelines(struct line *a, i | |||||
125 | /* match items in a to their equivalence class in b */ |
|
130 | /* match items in a to their equivalence class in b */ | |
126 | for (i = 0; i < an; i++) { |
|
131 | for (i = 0; i < an; i++) { | |
127 | /* find the equivalence class */ |
|
132 | /* find the equivalence class */ | |
128 |
for (j = a[i].h & buckets; h[j] != -1; |
|
133 | for (j = a[i].h & buckets; h[j].pos != -1; | |
129 | if (!cmp(a + i, b + h[j])) |
|
134 | j = (j + 1) & buckets) | |
|
135 | if (!cmp(a + i, b + h[j].pos)) | |||
130 | break; |
|
136 | break; | |
131 |
|
137 | |||
132 | a[i].e = j; /* use equivalence class for quick compare */ |
|
138 | a[i].e = j; /* use equivalence class for quick compare */ | |
133 |
if( |
|
139 | if(h[j].len <= t) | |
134 | a[i].n = h[j]; /* point to head of match list */ |
|
140 | a[i].n = h[j].pos; /* point to head of match list */ | |
135 | else |
|
141 | else | |
136 | a[i].n = -1; /* too popular */ |
|
142 | a[i].n = -1; /* too popular */ | |
137 | } |
|
143 | } | |
138 |
|
144 | |||
139 | /* discard hash tables */ |
|
145 | /* discard hash tables */ | |
140 | free(h); |
|
146 | free(h); | |
141 | free(l); |
|
|||
142 | return 1; |
|
147 | return 1; | |
143 | } |
|
148 | } | |
144 |
|
149 | |||
145 |
static int longest_match(struct line *a, struct line *b, |
|
150 | static int longest_match(struct line *a, struct line *b, struct pos *pos, | |
146 | int a1, int a2, int b1, int b2, int *omi, int *omj) |
|
151 | int a1, int a2, int b1, int b2, int *omi, int *omj) | |
147 | { |
|
152 | { | |
148 | int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k; |
|
153 | int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k; | |
@@ -155,12 +160,12 static int longest_match(struct line *a, | |||||
155 | /* loop through all lines match a[i] in b */ |
|
160 | /* loop through all lines match a[i] in b */ | |
156 | for (; j != -1 && j < b2; j = b[j].n) { |
|
161 | for (; j != -1 && j < b2; j = b[j].n) { | |
157 | /* does this extend an earlier match? */ |
|
162 | /* does this extend an earlier match? */ | |
158 |
if (i > a1 && j > b1 && |
|
163 | if (i > a1 && j > b1 && pos[j - 1].pos == i - 1) | |
159 |
k = |
|
164 | k = pos[j - 1].len + 1; | |
160 | else |
|
165 | else | |
161 | k = 1; |
|
166 | k = 1; | |
162 |
|
|
167 | pos[j].pos = i; | |
163 |
|
|
168 | pos[j].len = k; | |
164 |
|
169 | |||
165 | /* best match so far? */ |
|
170 | /* best match so far? */ | |
166 | if (k > mk) { |
|
171 | if (k > mk) { | |
@@ -189,47 +194,46 static int longest_match(struct line *a, | |||||
189 | return mk + mb; |
|
194 | return mk + mb; | |
190 | } |
|
195 | } | |
191 |
|
196 | |||
192 |
static void recurse(struct line *a, struct line *b, |
|
197 | static void recurse(struct line *a, struct line *b, struct pos *pos, | |
193 | int a1, int a2, int b1, int b2, struct hunklist *l) |
|
198 | int a1, int a2, int b1, int b2, struct hunklist *l) | |
194 | { |
|
199 | { | |
195 | int i, j, k; |
|
200 | int i, j, k; | |
196 |
|
201 | |||
197 | /* find the longest match in this chunk */ |
|
202 | /* find the longest match in this chunk */ | |
198 |
k = longest_match(a, b, |
|
203 | k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j); | |
199 | if (!k) |
|
204 | if (!k) | |
200 | return; |
|
205 | return; | |
201 |
|
206 | |||
202 | /* and recurse on the remaining chunks on either side */ |
|
207 | /* and recurse on the remaining chunks on either side */ | |
203 |
recurse(a, b, |
|
208 | recurse(a, b, pos, a1, i, b1, j, l); | |
204 | l->head->a1 = i; |
|
209 | l->head->a1 = i; | |
205 | l->head->a2 = i + k; |
|
210 | l->head->a2 = i + k; | |
206 | l->head->b1 = j; |
|
211 | l->head->b1 = j; | |
207 | l->head->b2 = j + k; |
|
212 | l->head->b2 = j + k; | |
208 | l->head++; |
|
213 | l->head++; | |
209 |
recurse(a, b, |
|
214 | recurse(a, b, pos, i + k, a2, j + k, b2, l); | |
210 | } |
|
215 | } | |
211 |
|
216 | |||
212 | static struct hunklist diff(struct line *a, int an, struct line *b, int bn) |
|
217 | static struct hunklist diff(struct line *a, int an, struct line *b, int bn) | |
213 | { |
|
218 | { | |
214 | struct hunklist l; |
|
219 | struct hunklist l; | |
215 | int *jpos, *jlen, t; |
|
220 | struct pos *pos; | |
|
221 | int t; | |||
216 |
|
222 | |||
217 | /* allocate and fill arrays */ |
|
223 | /* allocate and fill arrays */ | |
218 | t = equatelines(a, an, b, bn); |
|
224 | t = equatelines(a, an, b, bn); | |
219 |
|
|
225 | pos = calloc(bn, sizeof(struct pos)); | |
220 | jlen = calloc(bn, sizeof(int)); |
|
|||
221 | l.head = l.base = malloc(sizeof(struct hunk) * ((an + bn) / 4 + 2)); |
|
226 | l.head = l.base = malloc(sizeof(struct hunk) * ((an + bn) / 4 + 2)); | |
222 |
|
227 | |||
223 |
if ( |
|
228 | if (pos && l.base && t) { | |
224 | /* generate the matching block list */ |
|
229 | /* generate the matching block list */ | |
225 |
recurse(a, b, |
|
230 | recurse(a, b, pos, 0, an, 0, bn, &l); | |
226 | l.head->a1 = an; |
|
231 | l.head->a1 = an; | |
227 | l.head->b1 = bn; |
|
232 | l.head->b1 = bn; | |
228 | l.head++; |
|
233 | l.head++; | |
229 | } |
|
234 | } | |
230 |
|
235 | |||
231 |
free( |
|
236 | free(pos); | |
232 | free(jlen); |
|
|||
233 | return l; |
|
237 | return l; | |
234 | } |
|
238 | } | |
235 |
|
239 |
General Comments 0
You need to be logged in to leave comments.
Login now