Created
September 18, 2015 21:27
-
-
Save Tiny-Giant/9b880afb912be77c156d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| App.funcs.diff = function() { | |
| var table = $('<table/>')[0]; | |
| function maakRij(x, y, type, rij){ | |
| var tr = document.createElement('tr'); | |
| if(type==='+'){ | |
| tr.className='add'; | |
| } else if(type==='-'){ | |
| tr.className='del'; | |
| } else return false; | |
| var td1 = document.createElement('td'); | |
| var td2 = document.createElement('td'); | |
| var td3 = document.createElement('td'); | |
| td1.className = 'codekolom'; | |
| td2.className = 'codekolom'; | |
| td3.className = 'bredecode'; | |
| var txt1 = document.createTextNode(y); | |
| var txt2 = document.createTextNode(x); | |
| var txt3 = document.createTextNode(type + ' ' + rij); | |
| td1.appendChild(txt1); | |
| td2.appendChild(txt2); | |
| td3.appendChild(txt3); | |
| tr.appendChild(td1); | |
| tr.appendChild(td2); | |
| tr.appendChild(td3); | |
| table.appendChild(tr); | |
| } | |
| function getDiff(matrix, a1, a2, x, y){ | |
| if(x>0 && y>0 && a1[y-1]===a2[x-1]){ | |
| getDiff(matrix, a1, a2, x-1, y-1); | |
| maakRij(x, y, ' ', a1[y-1]); | |
| } else { | |
| if(x>0 && (y===0 || matrix[y][x-1] >= matrix[y-1][x])){ | |
| getDiff(matrix, a1, a2, x-1, y); | |
| maakRij(x, '', '+', a2[x-1]); | |
| } else if(y>0 && (x===0 || matrix[y][x-1] < matrix[y-1][x])){ | |
| getDiff(matrix, a1, a2, x, y-1); | |
| maakRij('', y, '-', a1[y-1], ''); | |
| } else { | |
| return; | |
| } | |
| } | |
| } | |
| a1 = App.originals.body; | |
| a2 = App.items.body; | |
| var matrix = new Array(a1.length+1); | |
| for(var y=0; y<matrix.length; y++){ | |
| matrix[y] = new Array(a2.length+1); | |
| for(var x=0; x<matrix[y].length; x++){ | |
| matrix[y][x] = 0; | |
| } | |
| } | |
| for(var y=1; y<matrix.length; y++){ | |
| for(var x=1; x<matrix[y].length; x++){ | |
| if(a1[y-1]===a2[x-1]){ | |
| matrix[y][x] = 1 + matrix[y-1][x-1]; | |
| } else { | |
| matrix[y][x] = Math.max(matrix[y-1][x], matrix[y][x-1]); | |
| } | |
| } | |
| } | |
| try { | |
| getDiff(matrix, a1, a2, x-1, y-1); | |
| return table; | |
| } catch(e){ | |
| alert(e); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment