2 * Copyright (C) 2010, Google Inc.
3 * and other copyright owners as documented in JGit's IP log.
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 #define MAX_PTR UINT_MAX
47 #define MAX_CNT UINT_MAX
49 #define LINE_END(n) (line##n + count##n - 1)
50 #define LINE_END_PTR(n) (*line##n + *count##n - 1)
54 unsigned int ptr
, cnt
;
56 } **records
, /* an occurrence */
57 **line_map
; /* map of line to record chain */
59 unsigned int *next_ptrs
;
60 unsigned int table_bits
,
64 unsigned int max_chain_length
,
76 unsigned int begin1
, end1
;
77 unsigned int begin2
, end2
;
80 #define LINE_MAP(i, a) (i->line_map[(a) - i->ptr_shift])
82 #define NEXT_PTR(index, ptr) \
83 (index->next_ptrs[(ptr) - index->ptr_shift])
85 #define CNT(index, ptr) \
86 ((LINE_MAP(index, ptr))->cnt)
88 #define REC(env, s, l) \
89 (env->xdf##s.recs[l - 1])
91 static int cmp_recs(xrecord_t
*r1
, xrecord_t
*r2
)
93 return r1
->ha
== r2
->ha
;
97 #define CMP(i, s1, l1, s2, l2) \
98 (cmp_recs(REC(i->env, s1, l1), REC(i->env, s2, l2)))
100 #define TABLE_HASH(index, side, line) \
101 XDL_HASHLONG((REC(index->env, side, line))->ha, index->table_bits)
103 static int scanA(struct histindex
*index
, int line1
, int count1
)
105 unsigned int ptr
, tbl_idx
;
106 unsigned int chain_len
;
107 struct record
**rec_chain
, *rec
;
109 for (ptr
= LINE_END(1); line1
<= ptr
; ptr
--) {
110 tbl_idx
= TABLE_HASH(index
, 1, ptr
);
111 rec_chain
= index
->records
+ tbl_idx
;
116 if (CMP(index
, 1, rec
->ptr
, 1, ptr
)) {
118 * ptr is identical to another element. Insert
119 * it onto the front of the existing element
122 NEXT_PTR(index
, ptr
) = rec
->ptr
;
124 /* cap rec->cnt at MAX_CNT */
125 rec
->cnt
= XDL_MIN(MAX_CNT
, rec
->cnt
+ 1);
126 LINE_MAP(index
, ptr
) = rec
;
134 if (chain_len
== index
->max_chain_length
)
138 * This is the first time we have ever seen this particular
139 * element in the sequence. Construct a new chain for it.
141 if (!(rec
= xdl_cha_alloc(&index
->rcha
)))
145 rec
->next
= *rec_chain
;
147 LINE_MAP(index
, ptr
) = rec
;
156 static int try_lcs(struct histindex
*index
, struct region
*lcs
, int b_ptr
,
157 int line1
, int count1
, int line2
, int count2
)
159 unsigned int b_next
= b_ptr
+ 1;
160 struct record
*rec
= index
->records
[TABLE_HASH(index
, 2, b_ptr
)];
161 unsigned int as
, ae
, bs
, be
, np
, rc
;
164 for (; rec
; rec
= rec
->next
) {
165 if (rec
->cnt
> index
->cnt
) {
166 if (!index
->has_common
)
167 index
->has_common
= CMP(index
, 1, rec
->ptr
, 2, b_ptr
);
172 if (!CMP(index
, 1, as
, 2, b_ptr
))
175 index
->has_common
= 1;
178 np
= NEXT_PTR(index
, as
);
184 while (line1
< as
&& line2
< bs
185 && CMP(index
, 1, as
- 1, 2, bs
- 1)) {
189 rc
= XDL_MIN(rc
, CNT(index
, as
));
191 while (ae
< LINE_END(1) && be
< LINE_END(2)
192 && CMP(index
, 1, ae
+ 1, 2, be
+ 1)) {
196 rc
= XDL_MIN(rc
, CNT(index
, ae
));
201 if (lcs
->end1
- lcs
->begin1
< ae
- as
|| rc
< index
->cnt
) {
213 np
= NEXT_PTR(index
, np
);
229 static int fall_back_to_classic_diff(xpparam_t
const *xpp
, xdfenv_t
*env
,
230 int line1
, int count1
, int line2
, int count2
)
234 memset(&xpparam
, 0, sizeof(xpparam
));
235 xpparam
.flags
= xpp
->flags
& ~XDF_DIFF_ALGORITHM_MASK
;
237 return xdl_fall_back_diff(env
, &xpparam
,
238 line1
, count1
, line2
, count2
);
241 static inline void free_index(struct histindex
*index
)
243 xdl_free(index
->records
);
244 xdl_free(index
->line_map
);
245 xdl_free(index
->next_ptrs
);
246 xdl_cha_free(&index
->rcha
);
249 static int find_lcs(xpparam_t
const *xpp
, xdfenv_t
*env
,
251 int line1
, int count1
, int line2
, int count2
)
255 struct histindex index
;
257 memset(&index
, 0, sizeof(index
));
262 index
.records
= NULL
;
263 index
.line_map
= NULL
;
264 /* in case of early xdl_cha_free() */
265 index
.rcha
.head
= NULL
;
267 index
.table_bits
= xdl_hashbits(count1
);
268 index
.records_size
= 1 << index
.table_bits
;
269 if (!XDL_CALLOC_ARRAY(index
.records
, index
.records_size
))
272 index
.line_map_size
= count1
;
273 if (!XDL_CALLOC_ARRAY(index
.line_map
, index
.line_map_size
))
276 if (!XDL_CALLOC_ARRAY(index
.next_ptrs
, index
.line_map_size
))
279 /* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */
280 if (xdl_cha_init(&index
.rcha
, sizeof(struct record
), count1
/ 4 + 1) < 0)
283 index
.ptr_shift
= line1
;
284 index
.max_chain_length
= 64;
286 if (scanA(&index
, line1
, count1
))
289 index
.cnt
= index
.max_chain_length
+ 1;
291 for (b_ptr
= line2
; b_ptr
<= LINE_END(2); )
292 b_ptr
= try_lcs(&index
, lcs
, b_ptr
, line1
, count1
, line2
, count2
);
294 if (index
.has_common
&& index
.max_chain_length
< index
.cnt
)
304 static int histogram_diff(xpparam_t
const *xpp
, xdfenv_t
*env
,
305 int line1
, int count1
, int line2
, int count2
)
313 if (count1
<= 0 && count2
<= 0)
316 if (LINE_END(1) >= MAX_PTR
)
321 env
->xdf2
.rchg
[line2
++ - 1] = 1;
323 } else if (!count2
) {
325 env
->xdf1
.rchg
[line1
++ - 1] = 1;
329 memset(&lcs
, 0, sizeof(lcs
));
330 lcs_found
= find_lcs(xpp
, env
, &lcs
, line1
, count1
, line2
, count2
);
334 result
= fall_back_to_classic_diff(xpp
, env
, line1
, count1
, line2
, count2
);
336 if (lcs
.begin1
== 0 && lcs
.begin2
== 0) {
338 env
->xdf1
.rchg
[line1
++ - 1] = 1;
340 env
->xdf2
.rchg
[line2
++ - 1] = 1;
343 result
= histogram_diff(xpp
, env
,
344 line1
, lcs
.begin1
- line1
,
345 line2
, lcs
.begin2
- line2
);
349 * result = histogram_diff(xpp, env,
350 * lcs.end1 + 1, LINE_END(1) - lcs.end1,
351 * lcs.end2 + 1, LINE_END(2) - lcs.end2);
352 * but let's optimize tail recursion ourself:
354 count1
= LINE_END(1) - lcs
.end1
;
355 line1
= lcs
.end1
+ 1;
356 count2
= LINE_END(2) - lcs
.end2
;
357 line2
= lcs
.end2
+ 1;
365 int xdl_do_histogram_diff(xpparam_t
const *xpp
, xdfenv_t
*env
)
367 return histogram_diff(xpp
, env
,
368 env
->xdf1
.dstart
+ 1, env
->xdf1
.dend
- env
->xdf1
.dstart
+ 1,
369 env
->xdf2
.dstart
+ 1, env
->xdf2
.dend
- env
->xdf2
.dstart
+ 1);