PageRenderTime 22ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/xdiff/xhistogram.c

https://gitlab.com/storedmirrors/git
C | 378 lines | 260 code | 58 blank | 60 comment | 55 complexity | 1f2962f012a1c2b7782d0923c4645cf0 MD5 | raw file
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in JGit's IP log.
  4. *
  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
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  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.
  23. *
  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
  27. * written permission.
  28. *
  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.
  42. */
  43. #include "xinclude.h"
  44. #define MAX_PTR UINT_MAX
  45. #define MAX_CNT UINT_MAX
  46. #define LINE_END(n) (line##n + count##n - 1)
  47. #define LINE_END_PTR(n) (*line##n + *count##n - 1)
  48. struct histindex {
  49. struct record {
  50. unsigned int ptr, cnt;
  51. struct record *next;
  52. } **records, /* an occurrence */
  53. **line_map; /* map of line to record chain */
  54. chastore_t rcha;
  55. unsigned int *next_ptrs;
  56. unsigned int table_bits,
  57. records_size,
  58. line_map_size;
  59. unsigned int max_chain_length,
  60. key_shift,
  61. ptr_shift;
  62. unsigned int cnt,
  63. has_common;
  64. xdfenv_t *env;
  65. xpparam_t const *xpp;
  66. };
  67. struct region {
  68. unsigned int begin1, end1;
  69. unsigned int begin2, end2;
  70. };
  71. #define LINE_MAP(i, a) (i->line_map[(a) - i->ptr_shift])
  72. #define NEXT_PTR(index, ptr) \
  73. (index->next_ptrs[(ptr) - index->ptr_shift])
  74. #define CNT(index, ptr) \
  75. ((LINE_MAP(index, ptr))->cnt)
  76. #define REC(env, s, l) \
  77. (env->xdf##s.recs[l - 1])
  78. static int cmp_recs(xrecord_t *r1, xrecord_t *r2)
  79. {
  80. return r1->ha == r2->ha;
  81. }
  82. #define CMP(i, s1, l1, s2, l2) \
  83. (cmp_recs(REC(i->env, s1, l1), REC(i->env, s2, l2)))
  84. #define TABLE_HASH(index, side, line) \
  85. XDL_HASHLONG((REC(index->env, side, line))->ha, index->table_bits)
  86. static int scanA(struct histindex *index, int line1, int count1)
  87. {
  88. unsigned int ptr, tbl_idx;
  89. unsigned int chain_len;
  90. struct record **rec_chain, *rec;
  91. for (ptr = LINE_END(1); line1 <= ptr; ptr--) {
  92. tbl_idx = TABLE_HASH(index, 1, ptr);
  93. rec_chain = index->records + tbl_idx;
  94. rec = *rec_chain;
  95. chain_len = 0;
  96. while (rec) {
  97. if (CMP(index, 1, rec->ptr, 1, ptr)) {
  98. /*
  99. * ptr is identical to another element. Insert
  100. * it onto the front of the existing element
  101. * chain.
  102. */
  103. NEXT_PTR(index, ptr) = rec->ptr;
  104. rec->ptr = ptr;
  105. /* cap rec->cnt at MAX_CNT */
  106. rec->cnt = XDL_MIN(MAX_CNT, rec->cnt + 1);
  107. LINE_MAP(index, ptr) = rec;
  108. goto continue_scan;
  109. }
  110. rec = rec->next;
  111. chain_len++;
  112. }
  113. if (chain_len == index->max_chain_length)
  114. return -1;
  115. /*
  116. * This is the first time we have ever seen this particular
  117. * element in the sequence. Construct a new chain for it.
  118. */
  119. if (!(rec = xdl_cha_alloc(&index->rcha)))
  120. return -1;
  121. rec->ptr = ptr;
  122. rec->cnt = 1;
  123. rec->next = *rec_chain;
  124. *rec_chain = rec;
  125. LINE_MAP(index, ptr) = rec;
  126. continue_scan:
  127. ; /* no op */
  128. }
  129. return 0;
  130. }
  131. static int try_lcs(struct histindex *index, struct region *lcs, int b_ptr,
  132. int line1, int count1, int line2, int count2)
  133. {
  134. unsigned int b_next = b_ptr + 1;
  135. struct record *rec = index->records[TABLE_HASH(index, 2, b_ptr)];
  136. unsigned int as, ae, bs, be, np, rc;
  137. int should_break;
  138. for (; rec; rec = rec->next) {
  139. if (rec->cnt > index->cnt) {
  140. if (!index->has_common)
  141. index->has_common = CMP(index, 1, rec->ptr, 2, b_ptr);
  142. continue;
  143. }
  144. as = rec->ptr;
  145. if (!CMP(index, 1, as, 2, b_ptr))
  146. continue;
  147. index->has_common = 1;
  148. for (;;) {
  149. should_break = 0;
  150. np = NEXT_PTR(index, as);
  151. bs = b_ptr;
  152. ae = as;
  153. be = bs;
  154. rc = rec->cnt;
  155. while (line1 < as && line2 < bs
  156. && CMP(index, 1, as - 1, 2, bs - 1)) {
  157. as--;
  158. bs--;
  159. if (1 < rc)
  160. rc = XDL_MIN(rc, CNT(index, as));
  161. }
  162. while (ae < LINE_END(1) && be < LINE_END(2)
  163. && CMP(index, 1, ae + 1, 2, be + 1)) {
  164. ae++;
  165. be++;
  166. if (1 < rc)
  167. rc = XDL_MIN(rc, CNT(index, ae));
  168. }
  169. if (b_next <= be)
  170. b_next = be + 1;
  171. if (lcs->end1 - lcs->begin1 < ae - as || rc < index->cnt) {
  172. lcs->begin1 = as;
  173. lcs->begin2 = bs;
  174. lcs->end1 = ae;
  175. lcs->end2 = be;
  176. index->cnt = rc;
  177. }
  178. if (np == 0)
  179. break;
  180. while (np <= ae) {
  181. np = NEXT_PTR(index, np);
  182. if (np == 0) {
  183. should_break = 1;
  184. break;
  185. }
  186. }
  187. if (should_break)
  188. break;
  189. as = np;
  190. }
  191. }
  192. return b_next;
  193. }
  194. static int fall_back_to_classic_diff(xpparam_t const *xpp, xdfenv_t *env,
  195. int line1, int count1, int line2, int count2)
  196. {
  197. xpparam_t xpparam;
  198. memset(&xpparam, 0, sizeof(xpparam));
  199. xpparam.flags = xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
  200. return xdl_fall_back_diff(env, &xpparam,
  201. line1, count1, line2, count2);
  202. }
  203. static inline void free_index(struct histindex *index)
  204. {
  205. xdl_free(index->records);
  206. xdl_free(index->line_map);
  207. xdl_free(index->next_ptrs);
  208. xdl_cha_free(&index->rcha);
  209. }
  210. static int find_lcs(xpparam_t const *xpp, xdfenv_t *env,
  211. struct region *lcs,
  212. int line1, int count1, int line2, int count2)
  213. {
  214. int b_ptr;
  215. int sz, ret = -1;
  216. struct histindex index;
  217. memset(&index, 0, sizeof(index));
  218. index.env = env;
  219. index.xpp = xpp;
  220. index.records = NULL;
  221. index.line_map = NULL;
  222. /* in case of early xdl_cha_free() */
  223. index.rcha.head = NULL;
  224. index.table_bits = xdl_hashbits(count1);
  225. sz = index.records_size = 1 << index.table_bits;
  226. sz *= sizeof(struct record *);
  227. if (!(index.records = (struct record **) xdl_malloc(sz)))
  228. goto cleanup;
  229. memset(index.records, 0, sz);
  230. sz = index.line_map_size = count1;
  231. sz *= sizeof(struct record *);
  232. if (!(index.line_map = (struct record **) xdl_malloc(sz)))
  233. goto cleanup;
  234. memset(index.line_map, 0, sz);
  235. sz = index.line_map_size;
  236. sz *= sizeof(unsigned int);
  237. if (!(index.next_ptrs = (unsigned int *) xdl_malloc(sz)))
  238. goto cleanup;
  239. memset(index.next_ptrs, 0, sz);
  240. /* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */
  241. if (xdl_cha_init(&index.rcha, sizeof(struct record), count1 / 4 + 1) < 0)
  242. goto cleanup;
  243. index.ptr_shift = line1;
  244. index.max_chain_length = 64;
  245. if (scanA(&index, line1, count1))
  246. goto cleanup;
  247. index.cnt = index.max_chain_length + 1;
  248. for (b_ptr = line2; b_ptr <= LINE_END(2); )
  249. b_ptr = try_lcs(&index, lcs, b_ptr, line1, count1, line2, count2);
  250. if (index.has_common && index.max_chain_length < index.cnt)
  251. ret = 1;
  252. else
  253. ret = 0;
  254. cleanup:
  255. free_index(&index);
  256. return ret;
  257. }
  258. static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env,
  259. int line1, int count1, int line2, int count2)
  260. {
  261. struct region lcs;
  262. int lcs_found;
  263. int result;
  264. redo:
  265. result = -1;
  266. if (count1 <= 0 && count2 <= 0)
  267. return 0;
  268. if (LINE_END(1) >= MAX_PTR)
  269. return -1;
  270. if (!count1) {
  271. while(count2--)
  272. env->xdf2.rchg[line2++ - 1] = 1;
  273. return 0;
  274. } else if (!count2) {
  275. while(count1--)
  276. env->xdf1.rchg[line1++ - 1] = 1;
  277. return 0;
  278. }
  279. memset(&lcs, 0, sizeof(lcs));
  280. lcs_found = find_lcs(xpp, env, &lcs, line1, count1, line2, count2);
  281. if (lcs_found < 0)
  282. goto out;
  283. else if (lcs_found)
  284. result = fall_back_to_classic_diff(xpp, env, line1, count1, line2, count2);
  285. else {
  286. if (lcs.begin1 == 0 && lcs.begin2 == 0) {
  287. while (count1--)
  288. env->xdf1.rchg[line1++ - 1] = 1;
  289. while (count2--)
  290. env->xdf2.rchg[line2++ - 1] = 1;
  291. result = 0;
  292. } else {
  293. result = histogram_diff(xpp, env,
  294. line1, lcs.begin1 - line1,
  295. line2, lcs.begin2 - line2);
  296. if (result)
  297. goto out;
  298. /*
  299. * result = histogram_diff(xpp, env,
  300. * lcs.end1 + 1, LINE_END(1) - lcs.end1,
  301. * lcs.end2 + 1, LINE_END(2) - lcs.end2);
  302. * but let's optimize tail recursion ourself:
  303. */
  304. count1 = LINE_END(1) - lcs.end1;
  305. line1 = lcs.end1 + 1;
  306. count2 = LINE_END(2) - lcs.end2;
  307. line2 = lcs.end2 + 1;
  308. goto redo;
  309. }
  310. }
  311. out:
  312. return result;
  313. }
  314. int xdl_do_histogram_diff(mmfile_t *file1, mmfile_t *file2,
  315. xpparam_t const *xpp, xdfenv_t *env)
  316. {
  317. return histogram_diff(xpp, env,
  318. env->xdf1.dstart + 1, env->xdf1.dend - env->xdf1.dstart + 1,
  319. env->xdf2.dstart + 1, env->xdf2.dend - env->xdf2.dstart + 1);
  320. }