robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
1efbd4980f
master
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 44#include "xinclude.h" 45 46#define MAX_PTR INT_MAX 47#define MAX_CNT INT_MAX 48 49#define LINE_END (n ) (line##n + count##n - 1) 50#define LINE_END_PTR (n ) (*line##n + *count##n - 1) 51 52struct histindex { 53struct record { 54unsigned int ptr ,cnt ; 55struct record * next ; 56 }** records ,/* an occurrence */ 57** line_map ;/* map of line to record chain */ 58chastore_t rcha ; 59unsigned int * next_ptrs ; 60unsigned int table_bits , 61records_size , 62line_map_size ; 63 64unsigned int max_chain_length , 65key_shift , 66ptr_shift ; 67 68unsigned int cnt , 69has_common ; 70 71xdfenv_t * env ; 72xpparam_t const * xpp ; 73}; 74 75struct region { 76unsigned int begin1 ,end1 ; 77unsigned int begin2 ,end2 ; 78}; 79 80#define LINE_MAP (i ,a ) (i->line_map[(a) - i->ptr_shift]) 81 82#define NEXT_PTR (index ,ptr ) \ 83 (index->next_ptrs[(ptr) - index->ptr_shift]) 84 85#define CNT (index ,ptr ) \ 86 ((LINE_MAP(index, ptr))->cnt) 87 88#define REC (env ,s ,l ) \ 89 (env->xdf##s.recs[l - 1]) 90 91static int cmp_recs (xpparam_t const * xpp , 92xrecord_t * r1 ,xrecord_t * r2 ) 93{ 94return r1 -> ha == r2 -> ha && 95xdl_recmatch (r1 -> ptr ,r1 -> size ,r2 -> ptr ,r2 -> size , 96xpp -> flags ); 97} 98 99#define CMP_ENV (xpp ,env ,s1 ,l1 ,s2 ,l2 ) \ 100 (cmp_recs(xpp, REC(env, s1, l1), REC(env, s2, l2))) 101 102#define CMP (i ,s1 ,l1 ,s2 ,l2 ) \ 103 (cmp_recs(i->xpp, REC(i->env, s1, l1), REC(i->env, s2, l2))) 104 105#define TABLE_HASH (index ,side ,line ) \ 106 XDL_HASHLONG((REC(index->env, side, line))->ha, index->table_bits) 107 108static int scanA (struct histindex * index ,int line1 ,int count1 ) 109{ 110int ptr ,tbl_idx ; 111unsigned int chain_len ; 112struct record ** rec_chain ,* rec ; 113 114for (ptr = LINE_END (1 );line1 <=ptr ;ptr -- ) { 115tbl_idx = TABLE_HASH (index ,1 ,ptr ); 116rec_chain = index -> records + tbl_idx ; 117rec = * rec_chain ; 118 119chain_len = 0 ; 120while (rec ) { 121if (CMP (index ,1 ,rec -> ptr ,1 ,ptr )) { 122/* 123* ptr is identical to another element. Insert 124* it onto the front of the existing element 125* chain. 126*/ 127NEXT_PTR (index ,ptr )= rec -> ptr ; 128rec -> ptr = ptr ; 129/* cap rec->cnt at MAX_CNT */ 130rec -> cnt = XDL_MIN (MAX_CNT ,rec -> cnt + 1 ); 131LINE_MAP (index ,ptr )= rec ; 132 gotocontinue_scan ; 133 } 134 135rec = rec -> next ; 136chain_len ++ ; 137 } 138 139if (chain_len == index -> max_chain_length ) 140return -1 ; 141 142/* 143* This is the first time we have ever seen this particular 144* element in the sequence. Construct a new chain for it. 145*/ 146if (!(rec = xdl_cha_alloc (& index -> rcha ))) 147return -1 ; 148rec -> ptr = ptr ; 149rec -> cnt = 1 ; 150rec -> next = * rec_chain ; 151* rec_chain = rec ; 152LINE_MAP (index ,ptr )= rec ; 153 154continue_scan : 155 ;/* no op */ 156 } 157 158return 0 ; 159} 160 161static int try_lcs (struct histindex * index ,struct region * lcs ,int b_ptr , 162int line1 ,int count1 ,int line2 ,int count2 ) 163{ 164unsigned int b_next = b_ptr + 1 ; 165struct record * rec = index -> records [TABLE_HASH (index ,2 ,b_ptr )]; 166unsigned int as ,ae ,bs ,be ,np ,rc ; 167int should_break ; 168 169for (;rec ;rec = rec -> next ) { 170if (rec -> cnt > index -> cnt ) { 171if (!index -> has_common ) 172index -> has_common = CMP (index ,1 ,rec -> ptr ,2 ,b_ptr ); 173continue ; 174 } 175 176as = rec -> ptr ; 177if (!CMP (index ,1 ,as ,2 ,b_ptr )) 178continue ; 179 180index -> has_common = 1 ; 181for (;;) { 182should_break = 0 ; 183np = NEXT_PTR (index ,as ); 184bs = b_ptr ; 185ae = as ; 186be = bs ; 187rc = rec -> cnt ; 188 189while (line1 < (int )as && line2 < (int )bs 190&& CMP (index ,1 ,as - 1 ,2 ,bs - 1 )) { 191as -- ; 192bs -- ; 193if (1 < rc ) 194rc = XDL_MIN (rc ,CNT (index ,as )); 195 } 196while ((int )ae < LINE_END (1 )&& (int )be < LINE_END (2 ) 197&& CMP (index ,1 ,ae + 1 ,2 ,be + 1 )) { 198ae ++ ; 199be ++ ; 200if (1 < rc ) 201rc = XDL_MIN (rc ,CNT (index ,ae )); 202 } 203 204if (b_next <=be ) 205b_next = be + 1 ; 206if (lcs -> end1 - lcs -> begin1 < ae - as || rc < index -> cnt ) { 207lcs -> begin1 = as ; 208lcs -> begin2 = bs ; 209lcs -> end1 = ae ; 210lcs -> end2 = be ; 211index -> cnt = rc ; 212 } 213 214if (np == 0 ) 215break ; 216 217while (np <=ae ) { 218np = NEXT_PTR (index ,np ); 219if (np == 0 ) { 220should_break = 1 ; 221break ; 222 } 223 } 224 225if (should_break ) 226break ; 227 228as = np ; 229 } 230 } 231return b_next ; 232} 233 234static int fall_back_to_classic_diff (xpparam_t const * xpp ,xdfenv_t * env , 235int line1 ,int count1 ,int line2 ,int count2 ) 236{ 237xpparam_t xpparam ; 238 239memset (& xpparam ,0 ,sizeof (xpparam )); 240xpparam .flags = xpp -> flags & ~XDF_DIFF_ALGORITHM_MASK ; 241 242return xdl_fall_back_diff (env ,& xpparam , 243line1 ,count1 ,line2 ,count2 ); 244} 245 246static inline void free_index (struct histindex * index ) 247{ 248xdl_free (index -> records ); 249xdl_free (index -> line_map ); 250xdl_free (index -> next_ptrs ); 251xdl_cha_free (& index -> rcha ); 252} 253 254static int find_lcs (xpparam_t const * xpp ,xdfenv_t * env , 255struct region * lcs , 256int line1 ,int count1 ,int line2 ,int count2 ) 257{ 258int b_ptr ; 259int sz ,ret = -1 ; 260struct histindex index ; 261 262memset (& index ,0 ,sizeof (index )); 263 264index .env = env ; 265index .xpp = xpp ; 266 267index .records = NULL ; 268index .line_map = NULL ; 269/* in case of early xdl_cha_free() */ 270index .rcha .head = NULL ; 271 272index .table_bits = xdl_hashbits (count1 ); 273sz = index .records_size = 1 <<index .table_bits ; 274sz *=sizeof (struct record * ); 275if (!(index .records = (struct record ** )xdl_malloc (sz ))) 276 gotocleanup ; 277memset (index .records ,0 ,sz ); 278 279sz = index .line_map_size = count1 ; 280sz *=sizeof (struct record * ); 281if (!(index .line_map = (struct record ** )xdl_malloc (sz ))) 282 gotocleanup ; 283memset (index .line_map ,0 ,sz ); 284 285sz = index .line_map_size ; 286sz *=sizeof (unsigned int ); 287if (!(index .next_ptrs = (unsigned int * )xdl_malloc (sz ))) 288 gotocleanup ; 289memset (index .next_ptrs ,0 ,sz ); 290 291/* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */ 292if (xdl_cha_init (& index .rcha ,sizeof (struct record ),count1 /4 + 1 )< 0 ) 293 gotocleanup ; 294 295index .ptr_shift = line1 ; 296index .max_chain_length = 64 ; 297 298if (scanA (& index ,line1 ,count1 )) 299 gotocleanup ; 300 301index .cnt = index .max_chain_length + 1 ; 302 303for (b_ptr = line2 ;b_ptr <=LINE_END (2 ); ) 304b_ptr = try_lcs (& index ,lcs ,b_ptr ,line1 ,count1 ,line2 ,count2 ); 305 306if (index .has_common && index .max_chain_length < index .cnt ) 307ret = 1 ; 308else 309ret = 0 ; 310 311cleanup : 312free_index (& index ); 313return ret ; 314} 315 316static int histogram_diff (xpparam_t const * xpp ,xdfenv_t * env , 317int line1 ,int count1 ,int line2 ,int count2 ) 318{ 319struct region lcs ; 320int lcs_found ; 321int result ; 322redo : 323result = -1 ; 324 325if (count1 <=0 && count2 <=0 ) 326return 0 ; 327 328if (LINE_END (1 ) >=MAX_PTR ) 329return -1 ; 330 331if (!count1 ) { 332while (count2 -- ) 333env -> xdf2 .rchg [line2 ++ - 1 ]= 1 ; 334return 0 ; 335 }else if (!count2 ) { 336while (count1 -- ) 337env -> xdf1 .rchg [line1 ++ - 1 ]= 1 ; 338return 0 ; 339 } 340 341memset (& lcs ,0 ,sizeof (lcs )); 342lcs_found = find_lcs (xpp ,env ,& lcs ,line1 ,count1 ,line2 ,count2 ); 343if (lcs_found < 0 ) 344 gotoout ; 345else if (lcs_found ) 346result = fall_back_to_classic_diff (xpp ,env ,line1 ,count1 ,line2 ,count2 ); 347else { 348if (lcs .begin1 == 0 && lcs .begin2 == 0 ) { 349while (count1 -- ) 350env -> xdf1 .rchg [line1 ++ - 1 ]= 1 ; 351while (count2 -- ) 352env -> xdf2 .rchg [line2 ++ - 1 ]= 1 ; 353result = 0 ; 354 }else { 355result = histogram_diff (xpp ,env , 356line1 ,lcs .begin1 - line1 , 357line2 ,lcs .begin2 - line2 ); 358if (result ) 359 gotoout ; 360/* 361* result = histogram_diff(xpp, env, 362* lcs.end1 + 1, LINE_END(1) - lcs.end1, 363* lcs.end2 + 1, LINE_END(2) - lcs.end2); 364* but let's optimize tail recursion ourself: 365*/ 366count1 = LINE_END (1 )- lcs .end1 ; 367line1 = lcs .end1 + 1 ; 368count2 = LINE_END (2 )- lcs .end2 ; 369line2 = lcs .end2 + 1 ; 370 gotoredo ; 371 } 372 } 373out : 374return result ; 375} 376 377int xdl_do_histogram_diff (mmfile_t * file1 ,mmfile_t * file2 , 378xpparam_t const * xpp ,xdfenv_t * env ) 379{ 380if (xdl_prepare_env (file1 ,file2 ,xpp ,env )< 0 ) 381return -1 ; 382 383return histogram_diff (xpp ,env , 384env -> xdf1 .dstart + 1 ,env -> xdf1 .dend - env -> xdf1 .dstart + 1 , 385env -> xdf2 .dstart + 1 ,env -> xdf2 .dend - env -> xdf2 .dstart + 1 ); 386}