2 * lcs.c : routines for creating an lcs
4 * ====================================================================
5 * Copyright (c) 2000-2004 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
21 #include <apr_pools.h>
22 #include <apr_general.h>
24 #include "svn_error.h"
25 #include "svn_version.h"
32 * Calculate the Longest Common Subsequence between two datasources.
33 * This function is what makes the diff code tick.
35 * The LCS algorithm implemented here is described by Sun Wu,
36 * Udi Manber and Gene Meyers in "An O(NP) Sequence Comparison Algorithm"
40 typedef struct svn_diff__snake_t svn_diff__snake_t
;
42 struct svn_diff__snake_t
46 svn_diff__position_t
*position
[2];
49 static APR_INLINE
void
50 svn_diff__snake(apr_off_t k
,
51 svn_diff__snake_t
*fp
,
53 svn_diff__lcs_t
**freelist
,
56 svn_diff__position_t
*start_position
[2];
57 svn_diff__position_t
*position
[2];
59 svn_diff__lcs_t
*previous_lcs
;
61 /* The previous entry at fp[k] is going to be replaced. See if we
62 * can mark that lcs node for reuse, because the sequence up to this
63 * point was a dead end.
72 previous_lcs
= lcs
->next
;
73 lcs
->next
= *freelist
;
78 if (fp
[k
- 1].y
+ 1 > fp
[k
+ 1].y
)
80 start_position
[0] = fp
[k
- 1].position
[0];
81 start_position
[1] = fp
[k
- 1].position
[1]->next
;
83 previous_lcs
= fp
[k
- 1].lcs
;
87 start_position
[0] = fp
[k
+ 1].position
[0]->next
;
88 start_position
[1] = fp
[k
+ 1].position
[1];
90 previous_lcs
= fp
[k
+ 1].lcs
;
94 /* ### Optimization, skip all positions that don't have matchpoints
95 * ### anyway. Beware of the sentinel, don't skip it!
98 position
[0] = start_position
[0];
99 position
[1] = start_position
[1];
101 while (position
[0]->node
== position
[1]->node
)
103 position
[0] = position
[0]->next
;
104 position
[1] = position
[1]->next
;
107 if (position
[1] != start_position
[1])
112 *freelist
= lcs
->next
;
116 lcs
= apr_palloc(pool
, sizeof(*lcs
));
119 lcs
->position
[idx
] = start_position
[0];
120 lcs
->position
[abs(1 - idx
)] = start_position
[1];
121 lcs
->length
= position
[1]->offset
- start_position
[1]->offset
;
122 lcs
->next
= previous_lcs
;
128 fp
[k
].lcs
= previous_lcs
;
133 previous_lcs
->refcount
++;
136 fp
[k
].position
[0] = position
[0];
137 fp
[k
].position
[1] = position
[1];
139 fp
[k
].y
= position
[1]->offset
;
143 static svn_diff__lcs_t
*
144 svn_diff__lcs_reverse(svn_diff__lcs_t
*lcs
)
146 svn_diff__lcs_t
*next
;
147 svn_diff__lcs_t
*prev
;
163 svn_diff__lcs(svn_diff__position_t
*position_list1
, /* pointer to tail (ring) */
164 svn_diff__position_t
*position_list2
, /* pointer to tail (ring) */
169 svn_diff__snake_t
*fp
;
173 svn_diff__lcs_t
*lcs
, *lcs_freelist
= NULL
;
175 svn_diff__position_t sentinel_position
[2];
177 /* Since EOF is always a sync point we tack on an EOF link
178 * with sentinel positions
180 lcs
= apr_palloc(pool
, sizeof(*lcs
));
181 lcs
->position
[0] = apr_pcalloc(pool
, sizeof(*lcs
->position
[0]));
182 lcs
->position
[0]->offset
= position_list1
? position_list1
->offset
+ 1 : 1;
183 lcs
->position
[1] = apr_pcalloc(pool
, sizeof(*lcs
->position
[1]));
184 lcs
->position
[1]->offset
= position_list2
? position_list2
->offset
+ 1 : 1;
189 if (position_list1
== NULL
|| position_list2
== NULL
)
192 /* Calculate length of both sequences to be compared */
193 length
[0] = position_list1
->offset
- position_list1
->next
->offset
+ 1;
194 length
[1] = position_list2
->offset
- position_list2
->next
->offset
+ 1;
195 idx
= length
[0] > length
[1] ? 1 : 0;
197 /* strikerXXX: here we allocate the furthest point array, which is
198 * strikerXXX: sized M + N + 3 (!)
200 fp
= apr_pcalloc(pool
,
201 sizeof(*fp
) * (apr_size_t
)(length
[0] + length
[1] + 3));
202 fp
+= length
[idx
] + 1;
204 sentinel_position
[idx
].next
= position_list1
->next
;
205 position_list1
->next
= &sentinel_position
[idx
];
206 sentinel_position
[idx
].offset
= position_list1
->offset
+ 1;
208 sentinel_position
[abs(1 - idx
)].next
= position_list2
->next
;
209 position_list2
->next
= &sentinel_position
[abs(1 - idx
)];
210 sentinel_position
[abs(1 - idx
)].offset
= position_list2
->offset
+ 1;
212 /* These are never dereferenced, only compared by value, so we
213 * can safely fake these up and the void* cast is OK.
215 sentinel_position
[0].node
= (void*)&sentinel_position
[0];
216 sentinel_position
[1].node
= (void*)&sentinel_position
[1];
218 d
= length
[abs(1 - idx
)] - length
[idx
];
220 /* k = -1 will be the first to be used to get previous
221 * position information from, make sure it holds sane
224 fp
[-1].position
[0] = sentinel_position
[0].next
;
225 fp
[-1].position
[1] = &sentinel_position
[1];
231 for (k
= -p
; k
< d
; k
++)
233 svn_diff__snake(k
, fp
, idx
, &lcs_freelist
, pool
);
236 for (k
= d
+ p
; k
>= d
; k
--)
238 svn_diff__snake(k
, fp
, idx
, &lcs_freelist
, pool
);
243 while (fp
[d
].position
[1] != &sentinel_position
[1]);
245 lcs
->next
= fp
[d
].lcs
;
246 lcs
= svn_diff__lcs_reverse(lcs
);
248 position_list1
->next
= sentinel_position
[idx
].next
;
249 position_list2
->next
= sentinel_position
[abs(1 - idx
)].next
;