Module: Algorithm::Diff
- Defined in:
- lib/algorithm/diff.rb
Class Method Summary collapse
- .diff(list_a, list_b, path) ⇒ Object
- .ond(list_a, list_b) ⇒ Object
- .sdiff(list_a, list_b, path) ⇒ Object
- .solve(a, b, d, snake, nn) ⇒ Object
- .traverse(list_a, list_b, path, obj) ⇒ Object
Class Method Details
.diff(list_a, list_b, path) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/algorithm/diff.rb', line 104 def self.diff(list_a,list_b,path) hunk_list = [] hunk = [] (0..path.size-2).each {|i| x,y = path[i] mx,my = path[i+1] if x == mx while y < my hunk << [ '+', y, list_b[y]] y += 1 end elsif y == my while (x < mx) hunk << [ '-', x, list_a[x]] x += 1 end else hunk_list << hunk hunk = [] end } unless hunk.empty? hunk_list << hunk end return hunk_list end |
.ond(list_a, list_b) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/algorithm/diff.rb', line 3 def self.ond(list_a,list_b) m = list_a.size n = list_b.size delta = m - n ofs = max = m + n v = Array.new(max * 2 + 2, 0) snake = Hash.new() nn = Array.new(max+1) d = 0 nextbest = 0 while (d <= max) work = Array.new(n+1) nn[d] = work k = -d best = nextbest while (k <= d) if ((k + delta).abs > max - best) k+=2 next end a = v[k - 1 + ofs] b = v[k + 1 + ofs] y = ( (k == -d) || (k != d) && (a < b)) ? b : a + 1 x = y - k count = 0 while ( x < m && y < n && list_a[x] == list_b[y]) x += 1 y += 1 count+= 1 end p = work[y] if (p == nil) work[y] = p = [x] else work[y] = (p << x) end if (count > 0) then s = snake[idx = x + y * m] s = 0 if s == nil if (x >= 0 && s < count) snake[idx] = count end end v[k + ofs] = y if (x == m && y == n) return [d,snake,nn] end k += 2 nextbest = ((pos = x+y) > nextbest) ? pos : nextbest end d += 1 end return nil end |
.sdiff(list_a, list_b, path) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/algorithm/diff.rb', line 131 def self.sdiff(list_a,list_b,path) hunk_list = [] hunk = [] (0..path.size-2).each {|i| x,y = path[i] mx,my = path[i+1] if x == mx while y < my hunk << [ '+', nil, list_b[y] ] y += 1 end elsif y == my while (x < mx) hunk << [ '-', list_a[x], nil ] x += 1 end else hunk_list << hunk while (x < mx && y < my) hunk_list << ['u', list_a[x], list_b[y]] x += 1 y += 1 end hunk = [] end } unless hunk.empty? hunk_list << hunk end list = [] hunk_list.each {|hunk| if hunk.first=='u' list << hunk elsif hunk.size==1 list << hunk.first else del_idx = 0 while del_idx < hunk.size break if hunk[del_idx].first=='-' del_idx += 1 end ins_idx = 0 while ins_idx < hunk.size break if hunk[ins_idx].first=='+' ins_idx += 1 end while del_idx < hunk.size || ins_idx < hunk.size del = nil ins = nil if del_idx < hunk.size del = hunk[del_idx][1] end if ins_idx < hunk.size ins = hunk[ins_idx][2] end if del && ins list << ['c',del,ins] elsif del list << ['-',del,nil] elsif ins list << ['+',nil,ins] end del_idx += 1 ins_idx += 1 end end } list end |
.solve(a, b, d, snake, nn) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/algorithm/diff.rb', line 58 def self.solve(a,b,d,snake,nn) path = [] m = a.size n = b.size d = d - 1 x = m y = n path << [x,y] mx = -1 my = -1 p = nil s = nil while d >= 0 p = nn[d] s = snake[x + y * m] s = 0 if s == nil px = x - s py = y - s mx = -1 my = -1 if (((q = p[py-1]) != nil) && q.index(px) != nil) mx = px my = py - 1 path << [px, py] elsif (((q = p[py]) != nil) && q.index(px - 1) != nil) mx = px - 1 my = py path << [px, py] elsif (((q = p[py]) != nil) && q.index(px) != nil) mx = px my = py; end path << [mx, my] x = mx y = my break if (x == 0 && y == 0) d -= 1 end path << [0, 0] path = path.reverse if (path[1] == [0,0]) path.shift end path end |
.traverse(list_a, list_b, path, obj) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/algorithm/diff.rb', line 202 def self.traverse(list_a,list_b,path,obj) (0..path.size-2).each {|i| x,y = path[i] mx,my = path[i+1] if x == mx while y < my obj.discard_b(mx, y, list_b[y]) y += 1 end elsif y == my while (x < mx) obj.discard_a(x, my, list_a[x]) x += 1 end else while (x < mx && y < my) obj.match(x, y, list_b[y]) x += 1 y += 1 end end } return end |