13 # First we prune off any common elements at the beginning
14 while (astart <= afinish && bstart <= afinish && a[astart] == b[bstart])
15 mvector[astart] = bstart
21 while (astart <= afinish && bstart <= bfinish && a[afinish] == b[bfinish])
22 mvector[afinish] = bfinish
27 bmatches = b.reverse_hash(bstart..bfinish)
31 (astart..afinish).each { |aindex|
33 next unless bmatches.has_key? aelem
35 bmatches[aelem].reverse.each { |bindex|
36 if k && (thresh[k] > bindex) && (thresh[k-1] < bindex)
39 k = thresh.replacenextlarger(bindex, k)
41 links[k] = [ (k==0) ? nil : links[k-1], aindex, bindex ] if k
46 link = links[thresh.length-1]
48 mvector[link[1]] = link[2]
57 mvector = Diff.lcs(a, b)
59 while ai < mvector.length
92 s = @isstring ? df[i][2].chr : [df[i][2]]
96 while df[i] && df[i][0] == whot && df[i][1] == last+1
101 curdiff.push [whot, p, s]
108 attr_reader :diffs, :difftype
110 def initialize(diffs_or_a, b = nil, isstring = nil)
117 makediff(diffs_or_a, b)
118 @difftype = diffs_or_a.class
123 @diffs.push @curdiffs unless @curdiffs.empty?
127 def discarda(i, elem)
128 @curdiffs.push ['-', i, elem]
131 def discardb(i, elem)
132 @curdiffs.push ['+', i, elem]
136 return Diff.new(compactdiffs)
140 @diffs = compactdiffs
152 RedmineDiff::Diff.new(self, b)
155 # Create a hash that maps elements of the array to arrays of indices
156 # where the elements are found.
158 def reverse_hash(range = (0...self.length))
162 if revmap.has_key? elem
171 def replacenextlarger(value, high = nil)
173 if self.empty? || value > self[-1]
177 # binary search for replacement point
182 return nil if value == found
191 # $stderr << "replace #{value} : 0/#{low}/#{init_high} (#{steps} steps) (#{init_high-low} off )\n"
192 # $stderr.puts self.inspect
200 if diff.difftype == String
201 newary = diff.difftype.new('')
203 newary = diff.difftype.new
207 diff.diffs.each { |d|
226 raise "Unknown diff action"
230 while ai < self.length
249 (({diff.rb})) - computes the differences between two arrays or
250 strings. Copyright (C) 2001 Lars Christensen
254 diff = Diff.new(a, b)
261 Creates a Diff object which represent the differences between
262 ((|a|)) and ((|b|)). ((|a|)) and ((|b|)) can be either be arrays
263 of any objects, strings, or object of any class that include
264 module ((|Diffable|))
267 The module ((|Diffable|)) is intended to be included in any class for
268 which differences are to be computed. Diffable is included into String
269 and Array when (({diff.rb})) is (({require}))'d.
271 Classes including Diffable should implement (({[]})) to get element at
272 integer indices, (({<<})) to append elements to the object and
273 (({ClassName#new})) should accept 0 arguments to create a new empty
277 --- Diffable#patch(diff)
278 Applies the differences from ((|diff|)) to the object ((|obj|))
279 and return the result. ((|obj|)) is not changed. ((|obj|)) and
280 can be either an array or a string, but must match the object
281 from which the ((|diff|)) was created.