diffreg.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "diff.h"
  5. /* diff - differential file comparison
  6. *
  7. * Uses an algorithm due to Harold Stone, which finds
  8. * a pair of longest identical subsequences in the two
  9. * files.
  10. *
  11. * The major goal is to generate the match vector J.
  12. * J[i] is the index of the line in file1 corresponding
  13. * to line i file0. J[i] = 0 if there is no
  14. * such line in file1.
  15. *
  16. * Lines are hashed so as to work in core. All potential
  17. * matches are located by sorting the lines of each file
  18. * on the hash (called value). In particular, this
  19. * collects the equivalence classes in file1 together.
  20. * Subroutine equiv replaces the value of each line in
  21. * file0 by the index of the first element of its
  22. * matching equivalence in (the reordered) file1.
  23. * To save space equiv squeezes file1 into a single
  24. * array member in which the equivalence classes
  25. * are simply concatenated, except that their first
  26. * members are flagged by changing sign.
  27. *
  28. * Next the indices that point into member are unsorted into
  29. * array class according to the original order of file0.
  30. *
  31. * The cleverness lies in routine stone. This marches
  32. * through the lines of file0, developing a vector klist
  33. * of "k-candidates". At step i a k-candidate is a matched
  34. * pair of lines x,y (x in file0 y in file1) such that
  35. * there is a common subsequence of lenght k
  36. * between the first i lines of file0 and the first y
  37. * lines of file1, but there is no such subsequence for
  38. * any smaller y. x is the earliest possible mate to y
  39. * that occurs in such a subsequence.
  40. *
  41. * Whenever any of the members of the equivalence class of
  42. * lines in file1 matable to a line in file0 has serial number
  43. * less than the y of some k-candidate, that k-candidate
  44. * with the smallest such y is replaced. The new
  45. * k-candidate is chained (via pred) to the current
  46. * k-1 candidate so that the actual subsequence can
  47. * be recovered. When a member has serial number greater
  48. * that the y of all k-candidates, the klist is extended.
  49. * At the end, the longest subsequence is pulled out
  50. * and placed in the array J by unravel.
  51. *
  52. * With J in hand, the matches there recorded are
  53. * check'ed against reality to assure that no spurious
  54. * matches have crept in due to hashing. If they have,
  55. * they are broken, and "jackpot " is recorded--a harmless
  56. * matter except that a true match for a spuriously
  57. * mated line may now be unnecessarily reported as a change.
  58. *
  59. * Much of the complexity of the program comes simply
  60. * from trying to minimize core utilization and
  61. * maximize the range of doable problems by dynamically
  62. * allocating what is needed and reusing what is not.
  63. * The core requirements for problems larger than somewhat
  64. * are (in words) 2*length(file0) + length(file1) +
  65. * 3*(number of k-candidates installed), typically about
  66. * 6n words for files of length n.
  67. */
  68. /* TIDY THIS UP */
  69. struct cand {
  70. int x;
  71. int y;
  72. int pred;
  73. } cand;
  74. struct line {
  75. int serial;
  76. int value;
  77. } *file[2], line;
  78. int len[2];
  79. int binary;
  80. struct line *sfile[2]; /*shortened by pruning common prefix and suffix*/
  81. int slen[2];
  82. int pref, suff; /*length of prefix and suffix*/
  83. int *class; /*will be overlaid on file[0]*/
  84. int *member; /*will be overlaid on file[1]*/
  85. int *klist; /*will be overlaid on file[0] after class*/
  86. struct cand *clist; /* merely a free storage pot for candidates */
  87. int clen;
  88. int *J; /*will be overlaid on class*/
  89. long *ixold; /*will be overlaid on klist*/
  90. long *ixnew; /*will be overlaid on file[1]*/
  91. /* END OF SOME TIDYING */
  92. static void
  93. sort(struct line *a, int n) /*shellsort CACM #201*/
  94. {
  95. int m;
  96. struct line *ai, *aim, *j, *k;
  97. struct line w;
  98. int i;
  99. m = 0;
  100. for (i = 1; i <= n; i *= 2)
  101. m = 2*i - 1;
  102. for (m /= 2; m != 0; m /= 2) {
  103. k = a+(n-m);
  104. for (j = a+1; j <= k; j++) {
  105. ai = j;
  106. aim = ai+m;
  107. do {
  108. if (aim->value > ai->value ||
  109. aim->value == ai->value &&
  110. aim->serial > ai->serial)
  111. break;
  112. w = *ai;
  113. *ai = *aim;
  114. *aim = w;
  115. aim = ai;
  116. ai -= m;
  117. } while (ai > a && aim >= ai);
  118. }
  119. }
  120. }
  121. static void
  122. unsort(struct line *f, int l, int *b)
  123. {
  124. int *a;
  125. int i;
  126. a = MALLOC(int, (l+1));
  127. for(i=1;i<=l;i++)
  128. a[f[i].serial] = f[i].value;
  129. for(i=1;i<=l;i++)
  130. b[i] = a[i];
  131. FREE(a);
  132. }
  133. static void
  134. prune(void)
  135. {
  136. int i,j;
  137. for(pref=0;pref<len[0]&&pref<len[1]&&
  138. file[0][pref+1].value==file[1][pref+1].value;
  139. pref++ ) ;
  140. for(suff=0;suff<len[0]-pref&&suff<len[1]-pref&&
  141. file[0][len[0]-suff].value==file[1][len[1]-suff].value;
  142. suff++) ;
  143. for(j=0;j<2;j++) {
  144. sfile[j] = file[j]+pref;
  145. slen[j] = len[j]-pref-suff;
  146. for(i=0;i<=slen[j];i++)
  147. sfile[j][i].serial = i;
  148. }
  149. }
  150. static void
  151. equiv(struct line *a, int n, struct line *b, int m, int *c)
  152. {
  153. int i, j;
  154. i = j = 1;
  155. while(i<=n && j<=m) {
  156. if(a[i].value < b[j].value)
  157. a[i++].value = 0;
  158. else if(a[i].value == b[j].value)
  159. a[i++].value = j;
  160. else
  161. j++;
  162. }
  163. while(i <= n)
  164. a[i++].value = 0;
  165. b[m+1].value = 0;
  166. j = 0;
  167. while(++j <= m) {
  168. c[j] = -b[j].serial;
  169. while(b[j+1].value == b[j].value) {
  170. j++;
  171. c[j] = b[j].serial;
  172. }
  173. }
  174. c[j] = -1;
  175. }
  176. static int
  177. newcand(int x, int y, int pred)
  178. {
  179. struct cand *q;
  180. clist = REALLOC(clist, struct cand, (clen+1));
  181. q = clist + clen;
  182. q->x = x;
  183. q->y = y;
  184. q->pred = pred;
  185. return clen++;
  186. }
  187. static int
  188. search(int *c, int k, int y)
  189. {
  190. int i, j, l;
  191. int t;
  192. if(clist[c[k]].y < y) /*quick look for typical case*/
  193. return k+1;
  194. i = 0;
  195. j = k+1;
  196. while((l=(i+j)/2) > i) {
  197. t = clist[c[l]].y;
  198. if(t > y)
  199. j = l;
  200. else if(t < y)
  201. i = l;
  202. else
  203. return l;
  204. }
  205. return l+1;
  206. }
  207. static int
  208. stone(int *a, int n, int *b, int *c)
  209. {
  210. int i, k,y;
  211. int j, l;
  212. int oldc, tc;
  213. int oldl;
  214. k = 0;
  215. c[0] = newcand(0,0,0);
  216. for(i=1; i<=n; i++) {
  217. j = a[i];
  218. if(j==0)
  219. continue;
  220. y = -b[j];
  221. oldl = 0;
  222. oldc = c[0];
  223. do {
  224. if(y <= clist[oldc].y)
  225. continue;
  226. l = search(c, k, y);
  227. if(l!=oldl+1)
  228. oldc = c[l-1];
  229. if(l<=k) {
  230. if(clist[c[l]].y <= y)
  231. continue;
  232. tc = c[l];
  233. c[l] = newcand(i,y,oldc);
  234. oldc = tc;
  235. oldl = l;
  236. } else {
  237. c[l] = newcand(i,y,oldc);
  238. k++;
  239. break;
  240. }
  241. } while((y=b[++j]) > 0);
  242. }
  243. return k;
  244. }
  245. static void
  246. unravel(int p)
  247. {
  248. int i;
  249. struct cand *q;
  250. for(i=0; i<=len[0]; i++) {
  251. if (i <= pref)
  252. J[i] = i;
  253. else if (i > len[0]-suff)
  254. J[i] = i+len[1]-len[0];
  255. else
  256. J[i] = 0;
  257. }
  258. for(q=clist+p;q->y!=0;q=clist+q->pred)
  259. J[q->x+pref] = q->y+pref;
  260. }
  261. static void
  262. output(void)
  263. {
  264. int m, i0, i1, j0, j1;
  265. m = len[0];
  266. J[0] = 0;
  267. J[m+1] = len[1]+1;
  268. if (mode != 'e') {
  269. for (i0 = 1; i0 <= m; i0 = i1+1) {
  270. while (i0 <= m && J[i0] == J[i0-1]+1)
  271. i0++;
  272. j0 = J[i0-1]+1;
  273. i1 = i0-1;
  274. while (i1 < m && J[i1+1] == 0)
  275. i1++;
  276. j1 = J[i1+1]-1;
  277. J[i1] = j1;
  278. change(i0, i1, j0, j1);
  279. }
  280. }
  281. else {
  282. for (i0 = m; i0 >= 1; i0 = i1-1) {
  283. while (i0 >= 1 && J[i0] == J[i0+1]-1 && J[i0])
  284. i0--;
  285. j0 = J[i0+1]-1;
  286. i1 = i0+1;
  287. while (i1 > 1 && J[i1-1] == 0)
  288. i1--;
  289. j1 = J[i1-1]+1;
  290. J[i1] = j1;
  291. change(i1 , i0, j1, j0);
  292. }
  293. }
  294. if (m == 0)
  295. change(1, 0, 1, len[1]);
  296. flushchanges();
  297. }
  298. #define BUF 4096
  299. static int
  300. cmp(Biobuf* b1, Biobuf* b2)
  301. {
  302. int n;
  303. uchar buf1[BUF], buf2[BUF];
  304. int f1, f2;
  305. vlong nc = 1;
  306. uchar *b1s, *b1e, *b2s, *b2e;
  307. f1 = Bfildes(b1);
  308. f2 = Bfildes(b2);
  309. seek(f1, 0, 0);
  310. seek(f2, 0, 0);
  311. b1s = b1e = buf1;
  312. b2s = b2e = buf2;
  313. for(;;){
  314. if(b1s >= b1e){
  315. if(b1s >= &buf1[BUF])
  316. b1s = buf1;
  317. n = read(f1, b1s, &buf1[BUF] - b1s);
  318. b1e = b1s + n;
  319. }
  320. if(b2s >= b2e){
  321. if(b2s >= &buf2[BUF])
  322. b2s = buf2;
  323. n = read(f2, b2s, &buf2[BUF] - b2s);
  324. b2e = b2s + n;
  325. }
  326. n = b2e - b2s;
  327. if(n > b1e - b1s)
  328. n = b1e - b1s;
  329. if(n <= 0)
  330. break;
  331. if(memcmp((void *)b1s, (void *)b2s, n) != 0){
  332. return 1;
  333. }
  334. nc += n;
  335. b1s += n;
  336. b2s += n;
  337. }
  338. if(b1e - b1s == b2e - b2s)
  339. return 0;
  340. return 1;
  341. }
  342. void
  343. diffreg(char *f, char *t)
  344. {
  345. Biobuf *b0, *b1;
  346. int k;
  347. binary = 0;
  348. b0 = prepare(0, f);
  349. if (!b0)
  350. return;
  351. b1 = prepare(1, t);
  352. if (!b1) {
  353. Bterm(b0);
  354. return;
  355. }
  356. if (binary){
  357. // could use b0 and b1 but this is simpler.
  358. if (cmp(b0, b1))
  359. print("binary files %s %s differ\n", f, t);
  360. Bterm(b0);
  361. Bterm(b1);
  362. return;
  363. }
  364. clen = 0;
  365. prune();
  366. sort(sfile[0], slen[0]);
  367. sort(sfile[1], slen[1]);
  368. member = (int *)file[1];
  369. equiv(sfile[0], slen[0], sfile[1], slen[1], member);
  370. member = REALLOC(member, int, slen[1]+2);
  371. class = (int *)file[0];
  372. unsort(sfile[0], slen[0], class);
  373. class = REALLOC(class, int, slen[0]+2);
  374. klist = MALLOC(int, slen[0]+2);
  375. clist = MALLOC(struct cand, 1);
  376. k = stone(class, slen[0], member, klist);
  377. FREE(member);
  378. FREE(class);
  379. J = MALLOC(int, len[0]+2);
  380. unravel(klist[k]);
  381. FREE(clist);
  382. FREE(klist);
  383. ixold = MALLOC(long, len[0]+2);
  384. ixnew = MALLOC(long, len[1]+2);
  385. Bseek(b0, 0, 0); Bseek(b1, 0, 0);
  386. check(b0, b1);
  387. output();
  388. FREE(J); FREE(ixold); FREE(ixnew);
  389. Bterm(b0); Bterm(b1); /* ++++ */
  390. }