robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
1efbd4980f
master
1/* 2* LibXDiff by Davide Libenzi ( File Differential Library ) 3* Copyright (C) 2003-2016 Davide Libenzi, Johannes E. Schindelin 4* 5* This library is free software; you can redistribute it and/or 6* modify it under the terms of the GNU Lesser General Public 7* License as published by the Free Software Foundation; either 8* version 2.1 of the License, or (at your option) any later version. 9* 10* This library is distributed in the hope that it will be useful, 11* but WITHOUT ANY WARRANTY; without even the implied warranty of 12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13* Lesser General Public License for more details. 14* 15* You should have received a copy of the GNU Lesser General Public 16* License along with this library; if not, see 17* <http://www.gnu.org/licenses/>. 18* 19* Davide Libenzi <davidel@xmailserver.org> 20* 21*/ 22#include "xinclude.h" 23 24/* 25* The basic idea of patience diff is to find lines that are unique in 26* both files. These are intuitively the ones that we want to see as 27* common lines. 28* 29* The maximal ordered sequence of such line pairs (where ordered means 30* that the order in the sequence agrees with the order of the lines in 31* both files) naturally defines an initial set of common lines. 32* 33* Now, the algorithm tries to extend the set of common lines by growing 34* the line ranges where the files have identical lines. 35* 36* Between those common lines, the patience diff algorithm is applied 37* recursively, until no unique line pairs can be found; these line ranges 38* are handled by the well-known Myers algorithm. 39*/ 40 41#define NON_UNIQUE ULONG_MAX 42 43/* 44* This is a hash mapping from line hash to line numbers in the first and 45* second file. 46*/ 47struct hashmap { 48int nr ,alloc ; 49struct entry { 50unsigned long hash ; 51/* 52* 0 = unused entry, 1 = first line, 2 = second, etc. 53* line2 is NON_UNIQUE if the line is not unique 54* in either the first or the second file. 55*/ 56unsigned long line1 ,line2 ; 57/* 58* "next" & "previous" are used for the longest common 59* sequence; 60* initially, "next" reflects only the order in file1. 61*/ 62struct entry * next ,* previous ; 63 64/* 65* If 1, this entry can serve as an anchor. See 66* Documentation/diff-options.txt for more information. 67*/ 68unsigned anchor :1 ; 69 }* entries ,* first ,* last ; 70/* were common records found? */ 71unsigned long has_matches ; 72mmfile_t * file1 ,* file2 ; 73xdfenv_t * env ; 74xpparam_t const * xpp ; 75}; 76 77static int is_anchor (xpparam_t const * xpp ,const char * line ) 78{ 79int i ; 80for (i = 0 ;i < (int )xpp -> anchors_nr ;i ++ ) { 81if (!strncmp (line ,xpp -> anchors [i ],strlen (xpp -> anchors [i ]))) 82return 1 ; 83 } 84return 0 ; 85} 86 87/* The argument "pass" is 1 for the first file, 2 for the second. */ 88static void insert_record (xpparam_t const * xpp ,int line ,struct hashmap * map , 89int pass ) 90{ 91xrecord_t ** records = pass == 1 ? 92map -> env -> xdf1 .recs :map -> env -> xdf2 .recs ; 93xrecord_t * record = records [line - 1 ]; 94/* 95* After xdl_prepare_env() (or more precisely, due to 96* xdl_classify_record()), the "ha" member of the records (AKA lines) 97* is _not_ the hash anymore, but a linearized version of it. In 98* other words, the "ha" member is guaranteed to start with 0 and 99* the second record's ha can only be 0 or 1, etc. 100* 101* So we multiply ha by 2 in the hope that the hashing was 102* "unique enough". 103*/ 104int index = (int )((record -> ha <<1 ) %map -> alloc ); 105 106while (map -> entries [index ].line1 ) { 107if (map -> entries [index ].hash != record -> ha ) { 108if (++ index >=map -> alloc ) 109index = 0 ; 110continue ; 111 } 112if (pass == 2 ) 113map -> has_matches = 1 ; 114if (pass == 1 || map -> entries [index ].line2 ) 115map -> entries [index ].line2 = NON_UNIQUE ; 116else 117map -> entries [index ].line2 = line ; 118return ; 119 } 120if (pass == 2 ) 121return ; 122map -> entries [index ].line1 = line ; 123map -> entries [index ].hash = record -> ha ; 124map -> entries [index ].anchor = is_anchor (xpp ,map -> env -> xdf1 .recs [line - 1 ]-> ptr ); 125if (!map -> first ) 126map -> first = map -> entries + index ; 127if (map -> last ) { 128map -> last -> next = map -> entries + index ; 129map -> entries [index ].previous = map -> last ; 130 } 131map -> last = map -> entries + index ; 132map -> nr ++ ; 133} 134 135/* 136* This function has to be called for each recursion into the inter-hunk 137* parts, as previously non-unique lines can become unique when being 138* restricted to a smaller part of the files. 139* 140* It is assumed that env has been prepared using xdl_prepare(). 141*/ 142static int fill_hashmap (mmfile_t * file1 ,mmfile_t * file2 , 143xpparam_t const * xpp ,xdfenv_t * env , 144struct hashmap * result , 145int line1 ,int count1 ,int line2 ,int count2 ) 146{ 147result -> file1 = file1 ; 148result -> file2 = file2 ; 149result -> xpp = xpp ; 150result -> env = env ; 151 152/* We know exactly how large we want the hash map */ 153result -> alloc = count1 * 2 ; 154result -> entries = (struct entry * ) 155xdl_malloc (result -> alloc * sizeof (struct entry )); 156if (!result -> entries ) 157return -1 ; 158memset (result -> entries ,0 ,result -> alloc * sizeof (struct entry )); 159 160/* First, fill with entries from the first file */ 161while (count1 -- ) 162insert_record (xpp ,line1 ++ ,result ,1 ); 163 164/* Then search for matches in the second file */ 165while (count2 -- ) 166insert_record (xpp ,line2 ++ ,result ,2 ); 167 168return 0 ; 169} 170 171/* 172* Find the longest sequence with a smaller last element (meaning a smaller 173* line2, as we construct the sequence with entries ordered by line1). 174*/ 175static int binary_search (struct entry ** sequence ,int longest , 176struct entry * entry ) 177{ 178int left = -1 ,right = longest ; 179 180while (left + 1 < right ) { 181int middle = left + (right - left ) /2 ; 182/* by construction, no two entries can be equal */ 183if (sequence [middle ]-> line2 > entry -> line2 ) 184right = middle ; 185else 186left = middle ; 187 } 188/* return the index in "sequence", _not_ the sequence length */ 189return left ; 190} 191 192/* 193* The idea is to start with the list of common unique lines sorted by 194* the order in file1. For each of these pairs, the longest (partial) 195* sequence whose last element's line2 is smaller is determined. 196* 197* For efficiency, the sequences are kept in a list containing exactly one 198* item per sequence length: the sequence with the smallest last 199* element (in terms of line2). 200*/ 201static struct entry * find_longest_common_sequence (struct hashmap * map ) 202{ 203struct entry ** sequence = xdl_malloc (map -> nr * sizeof (struct entry * )); 204int longest = 0 ,i ; 205struct entry * entry ; 206 207/* 208* If not -1, this entry in sequence must never be overridden. 209* Therefore, overriding entries before this has no effect, so 210* do not do that either. 211*/ 212int anchor_i = -1 ; 213 214// Added to silence Coverity. 215if (sequence == NULL ) 216return map -> first ; 217 218for (entry = map -> first ;entry ;entry = entry -> next ) { 219if (!entry -> line2 || entry -> line2 == NON_UNIQUE ) 220continue ; 221i = binary_search (sequence ,longest ,entry ); 222entry -> previous = i < 0 ?NULL :sequence [i ]; 223++ i ; 224if (i <=anchor_i ) 225continue ; 226sequence [i ]= entry ; 227if (entry -> anchor ) { 228anchor_i = i ; 229longest = anchor_i + 1 ; 230 }else if (i == longest ) { 231longest ++ ; 232 } 233 } 234 235/* No common unique lines were found */ 236if (!longest ) { 237xdl_free (sequence ); 238return NULL ; 239 } 240 241/* Iterate starting at the last element, adjusting the "next" members */ 242entry = sequence [longest - 1 ]; 243entry -> next = NULL ; 244while (entry -> previous ) { 245entry -> previous -> next = entry ; 246entry = entry -> previous ; 247 } 248xdl_free (sequence ); 249return entry ; 250} 251 252static int match (struct hashmap * map ,int line1 ,int line2 ) 253{ 254xrecord_t * record1 = map -> env -> xdf1 .recs [line1 - 1 ]; 255xrecord_t * record2 = map -> env -> xdf2 .recs [line2 - 1 ]; 256return record1 -> ha == record2 -> ha ; 257} 258 259static int patience_diff (mmfile_t * file1 ,mmfile_t * file2 , 260xpparam_t const * xpp ,xdfenv_t * env , 261int line1 ,int count1 ,int line2 ,int count2 ); 262 263static int walk_common_sequence (struct hashmap * map ,struct entry * first , 264int line1 ,int count1 ,int line2 ,int count2 ) 265{ 266int end1 = line1 + count1 ,end2 = line2 + count2 ; 267int next1 ,next2 ; 268 269for (;;) { 270/* Try to grow the line ranges of common lines */ 271if (first ) { 272next1 = first -> line1 ; 273next2 = first -> line2 ; 274while (next1 > line1 && next2 > line2 && 275match (map ,next1 - 1 ,next2 - 1 )) { 276next1 -- ; 277next2 -- ; 278 } 279 }else { 280next1 = end1 ; 281next2 = end2 ; 282 } 283while (line1 < next1 && line2 < next2 && 284match (map ,line1 ,line2 )) { 285line1 ++ ; 286line2 ++ ; 287 } 288 289/* Recurse */ 290if (next1 > line1 || next2 > line2 ) { 291if (patience_diff (map -> file1 ,map -> file2 , 292map -> xpp ,map -> env , 293line1 ,next1 - line1 , 294line2 ,next2 - line2 )) 295return -1 ; 296 } 297 298if (!first ) 299return 0 ; 300 301while (first -> next && 302first -> next -> line1 == first -> line1 + 1 && 303first -> next -> line2 == first -> line2 + 1 ) 304first = first -> next ; 305 306line1 = first -> line1 + 1 ; 307line2 = first -> line2 + 1 ; 308 309first = first -> next ; 310 } 311} 312 313static int fall_back_to_classic_diff (struct hashmap * map , 314int line1 ,int count1 ,int line2 ,int count2 ) 315{ 316xpparam_t xpp ; 317 318memset (& xpp ,0 ,sizeof (xpp )); 319xpp .flags = map -> xpp -> flags & ~XDF_DIFF_ALGORITHM_MASK ; 320 321return xdl_fall_back_diff (map -> env ,& xpp , 322line1 ,count1 ,line2 ,count2 ); 323} 324 325/* 326* Recursively find the longest common sequence of unique lines, 327* and if none was found, ask xdl_do_diff() to do the job. 328* 329* This function assumes that env was prepared with xdl_prepare_env(). 330*/ 331static int patience_diff (mmfile_t * file1 ,mmfile_t * file2 , 332xpparam_t const * xpp ,xdfenv_t * env , 333int line1 ,int count1 ,int line2 ,int count2 ) 334{ 335struct hashmap map ; 336struct entry * first ; 337int result = 0 ; 338 339/* trivial case: one side is empty */ 340if (!count1 ) { 341while (count2 -- ) 342env -> xdf2 .rchg [line2 ++ - 1 ]= 1 ; 343return 0 ; 344 }else if (!count2 ) { 345while (count1 -- ) 346env -> xdf1 .rchg [line1 ++ - 1 ]= 1 ; 347return 0 ; 348 } 349 350memset (& map ,0 ,sizeof (map )); 351if (fill_hashmap (file1 ,file2 ,xpp ,env ,& map , 352line1 ,count1 ,line2 ,count2 )) 353return -1 ; 354 355/* are there any matching lines at all? */ 356if (!map .has_matches ) { 357while (count1 -- ) 358env -> xdf1 .rchg [line1 ++ - 1 ]= 1 ; 359while (count2 -- ) 360env -> xdf2 .rchg [line2 ++ - 1 ]= 1 ; 361xdl_free (map .entries ); 362return 0 ; 363 } 364 365first = find_longest_common_sequence (& map ); 366if (first ) 367result = walk_common_sequence (& map ,first , 368line1 ,count1 ,line2 ,count2 ); 369else 370result = fall_back_to_classic_diff (& map , 371line1 ,count1 ,line2 ,count2 ); 372 373xdl_free (map .entries ); 374return result ; 375} 376 377int xdl_do_patience_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 383/* environment is cleaned up in xdl_diff() */ 384return patience_diff (file1 ,file2 ,xpp ,env , 3851 ,env -> xdf1 .nrec ,1 ,env -> xdf2 .nrec ); 386}