compare.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* This file is part of the software similarity tester SIM.
  2. Written by Dick Grune, Vrije Universiteit, Amsterdam.
  3. $Id: compare.c,v 2.5 2001/09/28 09:03:47 dick Exp $
  4. */
  5. #include "sim.h"
  6. #include "tokenarray.h"
  7. #include "hash.h"
  8. #include "language.h"
  9. #include "options.h"
  10. #include "add_run.h"
  11. #include "compare.h"
  12. static void compare1text(int, int, int);
  13. static unsigned int lcs(
  14. struct text *, unsigned int, struct text **, unsigned int *,
  15. unsigned int, unsigned int
  16. );
  17. /* The overall structure of the routine Compare() is:
  18. for all new files
  19. for all texts it must be compared to
  20. for all positions in the new file
  21. for all positions in the text
  22. for ever increasing sizes
  23. try to match and keep the best
  24. */
  25. void
  26. Compare(void) {
  27. register int n;
  28. for (n = 0; n < NumberOfNewTexts; n++) {
  29. register int first =
  30. ( option_set('S') ? NumberOfNewTexts + 1
  31. : option_set('s') ? n + 1
  32. : n
  33. );
  34. if (option_set('e')) {
  35. /* from first to NumberOfTexts in steps */
  36. register int m;
  37. for (m = first; m < NumberOfTexts; m++) {
  38. compare1text(n, m, m+1);
  39. }
  40. }
  41. else {
  42. /* from first to NumberOfTexts in one action */
  43. if (first < NumberOfTexts) {
  44. compare1text(n, first, NumberOfTexts);
  45. }
  46. }
  47. }
  48. }
  49. static void
  50. compare1text(
  51. int n, /* text to be compared */
  52. int first, /* first text to be compared to */
  53. int limit /* limit text in comparison */
  54. ) {
  55. register unsigned int i_first = Text[first].tx_start;
  56. register unsigned int i_limit = Text[limit-1].tx_limit;
  57. register struct text *txt0 = &Text[n];
  58. register unsigned int i0 = txt0->tx_start;
  59. while ( /* there may still be a useful substring */
  60. i0 + MinRunSize - 1 < txt0->tx_limit
  61. ) {
  62. /* see if there really is one */
  63. struct text *txt_best;
  64. unsigned int i_best;
  65. register unsigned int size_best =
  66. lcs(txt0, i0, &txt_best, &i_best, i_first, i_limit);
  67. if (size_best) {
  68. /* good run found; enter it */
  69. add_run(txt0, i0, txt_best, i_best, size_best);
  70. /* and skip it */
  71. i0 += size_best;
  72. }
  73. else {
  74. /* we try our luck at the next token */
  75. i0++;
  76. }
  77. }
  78. }
  79. static unsigned int
  80. lcs( struct text *txt0, /* input: starting position */
  81. unsigned int i0,
  82. struct text **tbp, /* output: position of best run */
  83. unsigned int *ibp,
  84. unsigned int i_first, /* no comparison before this pos. */
  85. unsigned int i_limit /* no comparison after this pos. */
  86. ) {
  87. /* Finds the longest common substring (not -sequence) in:
  88. txt0, starting precisely at i0 and
  89. the text between i_first and i_limit.
  90. Writes the position in tbp and ibp and returns the size.
  91. Returns 0 if no common substring is found.
  92. */
  93. register struct text *txt1 = txt0;
  94. register unsigned int i1 = i0;
  95. register unsigned int size_best = 0;
  96. register unsigned int txt0limit = txt0->tx_limit;
  97. register unsigned int txt1limit = txt1->tx_limit;
  98. while ( /* there is a next opportunity */
  99. (i1 = ForwardReference(i1))
  100. && /* it is still in range */
  101. i1 < i_limit
  102. ) {
  103. register unsigned int min_size;
  104. register unsigned int new_size;
  105. register unsigned int j0;
  106. register unsigned int j1;
  107. if (i1 < i_first) { /* not in range */
  108. continue;
  109. }
  110. /* bump txt1; we may have skipped a text or two */
  111. while (i1 >= txt1->tx_limit) {
  112. txt1++;
  113. }
  114. txt1limit = txt1->tx_limit;
  115. min_size = (size_best ? size_best+1 : MinRunSize);
  116. /* are we looking at something better than we have got? */
  117. {
  118. j0 = i0 + min_size - 1;
  119. j1 = i1 + min_size - 1;
  120. if ( /* j0 still inside txt0 */
  121. j0 < txt0limit
  122. && /* j1 still inside txt1 */
  123. j1 < txt1limit
  124. && /* j0 and j1 don't overlap */
  125. j0 < j1 - min_size + 1
  126. ) {
  127. /* there would be room enough */
  128. register int cnt = min_size;
  129. /* does the text match? */
  130. while ( cnt
  131. && TOKEN_EQ(TokenArray[j0], TokenArray[j1])
  132. ) {
  133. cnt--, j0--, j1--;
  134. }
  135. if (cnt) continue; /* forget it */
  136. }
  137. else continue; /* forget it */
  138. }
  139. /* yes, we are; how long can we make it? */
  140. {
  141. register unsigned int size = min_size;
  142. j0 = i0 + min_size;
  143. j1 = i1 + min_size;
  144. while ( /* j0 still inside txt0 */
  145. j0 < txt0limit
  146. && /* j1 still inside txt1 */
  147. j1 < txt1limit
  148. && /* j0 and j1 don't overlap */
  149. j0 + size < j1
  150. && /* tokens are the same */
  151. TOKEN_EQ(TokenArray[j0], TokenArray[j1])
  152. ) {
  153. j0++, j1++, size++;
  154. }
  155. new_size = size;
  156. }
  157. /* offer the run to the Language Department which may
  158. reject it or may cut its tail
  159. */
  160. new_size = ( MayBeStartOfRun(TokenArray[i0])
  161. ? CheckRun(&TokenArray[i0], new_size)
  162. : 0
  163. );
  164. if ( /* we still have something acceptable */
  165. new_size >= MinRunSize
  166. && /* it is better still than what we had */
  167. new_size > size_best
  168. ) {
  169. /* record it */
  170. *tbp = txt1;
  171. *ibp = i1;
  172. size_best = new_size;
  173. }
  174. }
  175. return size_best;
  176. }