Class: Diff::SequenceMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/diff.rb

Instance Method Summary collapse

Constructor Details

#initialize(a = [''], b = [''], isjunk = nil, byline = false) ⇒ SequenceMatcher

Returns a new instance of SequenceMatcher.



38
39
40
41
42
43
# File 'lib/diff.rb', line 38

def initialize(a=[''], b=[''], isjunk=nil, =false)
  a = (! and a.kind_of? String) ? a.split(//) : a
  b = (! and b.kind_of? String) ? b.split(//) : b
  @isjunk = isjunk || proc {}
  set_seqs a, b
end

Instance Method Details

#find_longest_match(alo, ahi, blo, bhi) ⇒ Object



100
101
102
103
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/diff.rb', line 100

def find_longest_match(alo, ahi, blo, bhi)
  besti, bestj, bestsize = alo, blo, 0
  
  j2len = {}
  
  (alo..ahi).step do |i|
    newj2len = {}
    (@b2j[@a[i]] || []).each do |j|
      if j < blo
        next
      end
      if j >= bhi
        break
      end
      
      k = newj2len[j] = (j2len[j - 1] || 0) + 1
      if k > bestsize
        besti, bestj, bestsize = i - k + 1, j - k + 1, k
      end
    end
    j2len = newj2len
  end
  
  while besti > alo and bestj > blo and
      not @isbjunk.call(@b[bestj-1]) and
      @a[besti-1] == @b[bestj-1]
    besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
  end
  
  while besti+bestsize < ahi and bestj+bestsize < bhi and
      not @isbjunk.call(@b[bestj+bestsize]) and
      @a[besti+bestsize] == @b[bestj+bestsize]
    bestsize += 1
  end
  
  while besti > alo and bestj > blo and
      @isbjunk.call(@b[bestj-1]) and
      @a[besti-1] == @b[bestj-1]
    besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
  end
  
  while besti+bestsize < ahi and bestj+bestsize < bhi and
      @isbjunk.call(@b[bestj+bestsize]) and
      @a[besti+bestsize] == @b[bestj+bestsize]
    bestsize += 1
  end
  
  [besti, bestj, bestsize]
end

#get_grouped_opcodes(n = 3) ⇒ Object

XXX: untested



200
201
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/diff.rb', line 200

def get_grouped_opcodes(n=3)
  codes = get_opcodes
  if codes[0][0] == :equal
    tag, i1, i2, j1, j2 = codes[0]
    codes[0] = tag, [i1, i2 - n].max, i2, [j1, j2-n].max, j2
  end
  
  if codes[-1][0] == :equal
    tag, i1, i2, j1, j2 = codes[-1]
    codes[-1] = tag, i1, min(i2, i1+n), j1, min(j2, j1+n)
  end
  nn = n + n
  group = []
  codes.each do |tag, i1, i2, j1, j2|
    if tag == :equal and i2-i1 > nn
      group.push [tag, i1, [i2, i1 + n].min, j1, [j2, j1 + n].min]
      yield group
      group = []
      i1, j1 = [i1, i2-n].max, [j1, j2-n].max
      group.push [tag, i1, i2, j1 ,j2]
    end
  end
  if group and group.length != 1 and group[0][0] == :equal
    yield group
  end
end

#get_matching_blocksObject



150
151
152
153
154
155
156
157
158
# File 'lib/diff.rb', line 150

def get_matching_blocks
  return @matching_blocks unless @matching_blocks.nil? or 
    @matching_blocks.empty?
  
  @matching_blocks = []
  la, lb = @a.length, @b.length
  match_block_helper(0, la, 0, lb, @matching_blocks)
  @matching_blocks.push [la, lb, 0]
end

#get_opcodesObject



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
# File 'lib/diff.rb', line 173

def get_opcodes
  unless @opcodes.nil? or @opcodes.empty?
    return @opcodes
  end

  i = j = 0
  @opcodes = answer = []
  get_matching_blocks.each do |ai, bj, size|
    tag = if i < ai and j < bj
            :replace
          elsif i < ai
            :delete
          elsif j < bj 
            :insert
          end

    answer.push [tag, i, ai, j, bj] if tag
    
    i, j = ai + size, bj + size
    
    answer.push [:equal, ai, i, bj, j] unless size.zero?
    
  end
  return answer
end

#quick_ratioObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/diff.rb', line 234

def quick_ratio
  if @fullbcount.nil? or @fullbcount.empty?
    @fullbcount = {}
    @b.each do |elt|
      @fullbcount[elt] = (@fullbcount[elt] || 0) + 1
    end
  end
  
  avail = {}
  matches = 0
  @a.each do |elt|
    if avail.has_key? elt
      numb = avail[elt]
    else
      numb = @fullbcount[elt] || 0
    end
    avail[elt] = numb - 1
    if numb > 0
      matches += 1
    end
  end
  Diff.calculate_ratio matches, @a.length + @b.length
end

#ratioObject



227
228
229
230
231
232
# File 'lib/diff.rb', line 227

def ratio
  matches = get_matching_blocks.reduce(0) do |sum, triple|
    sum + triple[-1]
  end
  Diff.calculate_ratio(matches, @a.length + @b.length)
end

#real_quick_ratioObject



258
259
260
261
# File 'lib/diff.rb', line 258

def real_quick_ratio
  la, lb = @a.length, @b.length
  Diff.calculate_ratio([la, lb].min, la + lb)
end

#set_seq_a(a) ⇒ Object



50
51
52
53
# File 'lib/diff.rb', line 50

def set_seq_a(a)
  @a = a
  @matching_blocks = @opcodes = nil
end

#set_seq_b(b) ⇒ Object



55
56
57
58
59
# File 'lib/diff.rb', line 55

def set_seq_b(b)
  @b = b
  @matching_blocks = @opcodes = nil
  chain_b
end

#set_seqs(a, b) ⇒ Object



45
46
47
48
# File 'lib/diff.rb', line 45

def set_seqs(a, b)
  set_seq_a a
  set_seq_b b
end