Class: Redmine::StringArrayDiff::Diff
- Inherits:
-
Object
- Object
- Redmine::StringArrayDiff::Diff
- Defined in:
- lib/redmine/string_array_diff/diff.rb
Constant Summary collapse
- VERSION =
0.3
Instance Attribute Summary collapse
-
#diffs ⇒ Object
readonly
Returns the value of attribute diffs.
-
#difftype ⇒ Object
readonly
Returns the value of attribute difftype.
Class Method Summary collapse
Instance Method Summary collapse
- #compact ⇒ Object
- #compact! ⇒ Object
- #compactdiffs ⇒ Object
- #discarda(i, elem) ⇒ Object
- #discardb(i, elem) ⇒ Object
-
#initialize(diffs_or_a, b = nil, isstring = nil) ⇒ Diff
constructor
A new instance of Diff.
- #inspect ⇒ Object
- #makediff(a, b) ⇒ Object
- #match(ai, bi) ⇒ Object
Constructor Details
#initialize(diffs_or_a, b = nil, isstring = nil) ⇒ Diff
Returns a new instance of Diff.
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/redmine/string_array_diff/diff.rb', line 149 def initialize(diffs_or_a, b = nil, isstring = nil) if b.nil? @diffs = diffs_or_a @isstring = isstring else @diffs = [] @curdiffs = [] makediff(diffs_or_a, b) @difftype = diffs_or_a.class end end |
Instance Attribute Details
#diffs ⇒ Object (readonly)
Returns the value of attribute diffs.
147 148 149 |
# File 'lib/redmine/string_array_diff/diff.rb', line 147 def diffs @diffs end |
#difftype ⇒ Object (readonly)
Returns the value of attribute difftype.
147 148 149 |
# File 'lib/redmine/string_array_diff/diff.rb', line 147 def difftype @difftype end |
Class Method Details
.lcs(a, b) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 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 |
# File 'lib/redmine/string_array_diff/diff.rb', line 45 def Diff.lcs(a, b) astart = 0 bstart = 0 afinish = a.length-1 bfinish = b.length-1 mvector = [] # First we prune off any common elements at the beginning while (astart <= afinish) && (bstart <= afinish) && (a[astart] == b[bstart]) mvector[astart] = bstart astart += 1 bstart += 1 end # now the end while (astart <= afinish) && (bstart <= bfinish) && (a[afinish] == b[bfinish]) mvector[afinish] = bfinish afinish -= 1 bfinish -= 1 end bmatches = b.reverse_hash(bstart..bfinish) thresh = [] links = [] (astart..afinish).each { |aindex| aelem = a[aindex] next unless bmatches.has_key? aelem k = nil bmatches[aelem].reverse_each { |bindex| if k && (thresh[k] > bindex) && (thresh[k-1] < bindex) thresh[k] = bindex else k = thresh.replacenextlarger(bindex, k) end links[k] = [ (k==0) ? nil : links[k-1], aindex, bindex ] if k } } unless thresh.empty? link = links[thresh.length-1] while link mvector[link[1]] = link[2] link = link[0] end end return mvector end |
Instance Method Details
#compact ⇒ Object
174 175 176 |
# File 'lib/redmine/string_array_diff/diff.rb', line 174 def compact return Diff.new(compactdiffs) end |
#compact! ⇒ Object
178 179 180 |
# File 'lib/redmine/string_array_diff/diff.rb', line 178 def compact! @diffs = compactdiffs end |
#compactdiffs ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/redmine/string_array_diff/diff.rb', line 124 def compactdiffs diffs = [] @diffs.each { |df| i = 0 curdiff = [] while i < df.length whot = df[i][0] s = @isstring ? df[i][2].chr : [df[i][2]] p = df[i][1] last = df[i][1] i += 1 while df[i] && df[i][0] == whot && df[i][1] == last+1 s << df[i][2] last = df[i][1] i += 1 end curdiff.push [whot, p, s] end diffs.push curdiff } return diffs end |
#discarda(i, elem) ⇒ Object
166 167 168 |
# File 'lib/redmine/string_array_diff/diff.rb', line 166 def discarda(i, elem) @curdiffs.push ['-', i, elem] end |
#discardb(i, elem) ⇒ Object
170 171 172 |
# File 'lib/redmine/string_array_diff/diff.rb', line 170 def discardb(i, elem) @curdiffs.push ['+', i, elem] end |
#inspect ⇒ Object
182 183 184 |
# File 'lib/redmine/string_array_diff/diff.rb', line 182 def inspect @diffs.inspect end |
#makediff(a, b) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/redmine/string_array_diff/diff.rb', line 95 def makediff(a, b) mvector = Diff.lcs(a, b) ai = bi = 0 while ai < mvector.length bline = mvector[ai] if bline while bi < bline discardb(bi, b[bi]) bi += 1 end match(ai, bi) bi += 1 else discarda(ai, a[ai]) end ai += 1 end while ai < a.length discarda(ai, a[ai]) ai += 1 end while bi < b.length discardb(bi, b[bi]) bi += 1 end match(ai, bi) 1 end |
#match(ai, bi) ⇒ Object
161 162 163 164 |
# File 'lib/redmine/string_array_diff/diff.rb', line 161 def match(ai, bi) @diffs.push @curdiffs unless @curdiffs.empty? @curdiffs = [] end |