Module: Diffable

Included in:
Array, String
Defined in:
lib/diff.rb

Instance Method Summary collapse

Instance Method Details

#diff(b) ⇒ Object



151
152
153
# File 'lib/diff.rb', line 151

def diff(b)
  RedmineDiff::Diff.new(self, b)
end

#patch(diff) ⇒ Object



198
199
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
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/diff.rb', line 198

def patch(diff)
  newary = nil
  if diff.difftype == String
    newary = diff.difftype.new('')
  else
    newary = diff.difftype.new
  end
  ai = 0
  bi = 0
  diff.diffs.each { |d|
    d.each { |mod|
case mod[0]
when '-'
  while ai < mod[1]
    newary << self[ai]
    ai += 1
    bi += 1
  end
  ai += 1
when '+'
  while bi < mod[1]
    newary << self[ai]
    ai += 1
    bi += 1
  end
  newary << mod[2]
  bi += 1
else
  raise "Unknown diff action"
end
    }
  }
  while ai < self.length
    newary << self[ai]
    ai += 1
    bi += 1
  end
  return newary
end

#replacenextlarger(value, high = nil) ⇒ Object



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

def replacenextlarger(value, high = nil)
  high ||= self.length
  if self.empty? || value > self[-1]
    push value
    return high
  end
  # binary search for replacement point
  low = 0
  while low < high
    index = (high+low)/2
    found = self[index]
    return nil if value == found
    if value > found
low = index + 1
    else
high = index
    end
  end

  self[low] = value
  # $stderr << "replace #{value} : 0/#{low}/#{init_high} (#{steps} steps) (#{init_high-low} off )\n"
  # $stderr.puts self.inspect
  #gets
  #p length - low
  return low
end

#reverse_hash(range = (0...self.length)) ⇒ Object

Create a hash that maps elements of the array to arrays of indices where the elements are found.



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/diff.rb', line 158

def reverse_hash(range = (0...self.length))
  revmap = {}
  range.each { |i|
    elem = self[i]
    if revmap.has_key? elem
revmap[elem].push i
    else
revmap[elem] = [i]
    end
  }
  return revmap
end