-
Notifications
You must be signed in to change notification settings - Fork 16
/
diff.rb
524 lines (444 loc) · 13.7 KB
/
diff.rb
1
2
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
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
94
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
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
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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
require './util'
module Diff
Insert = Struct.new(:location, :element) do
def apply(array)
raise "Index mismatch on insert: #{self.location} not between 0 and #{array.length}." if self.location < 0 or self.location > array.length
rv = array.clone
rv.insert(self.location, self.element)
return rv
end
def to_s
"Insert[#{self.location},#{self.element}]"
end
def inspect
to_s
end
def move(off)
Insert.new(self.location + off, self.element)
end
end
Delete = Struct.new(:location, :element) do
def apply(array)
raise "Index mismatch on delete: #{self.location} not between 0 and #{array.length-1}." if self.location < 0 or self.location >= array.length
raise "Element mismatch on delete: element at location (#{self.location}) #{array[self.location]} != expected #{self.element}." if array[self.location] != self.element
rv = array.clone
rv.delete_at(self.location)
return rv
end
def to_s
"Delete[#{self.location},#{self.element}]"
end
def inspect
to_s
end
def move(off)
Delete.new(self.location + off, self.element)
end
end
Alter = Struct.new(:location, :from_elt, :to_elt) do
def apply(array)
raise "Index mismatch on alter: #{self.location} not between 0 and #{array.length-1}." if self.location < 0 or self.location >= array.length
raise "Element mismatch on alter: element at location (#{self.location}) #{array[self.location]} != expected #{self.from_elt}." if array[self.location] != self.from_elt
rv = array.clone
rv[self.location] = self.to_elt
return rv
end
def to_s
"Alter[#{self.location},#{self.from_elt}->#{self.to_elt}]"
end
def inspect
to_s
end
def move(off)
Alter.new(self.location + off, self.from_elt, self.to_elt)
end
end
Move = Struct.new(:from_loc, :to_loc, :element) do
def apply(array)
del = Delete.new(self.from_loc, self.element)
ins = Insert.new(self.to_loc, self.element)
begin
ins.apply(del.apply(array))
rescue Exception => ex
raise "While processing #{self.to_s}: #{ex}"
end
end
def to_s
"Move[#{self.from_loc}->#{self.to_loc},#{self.element}]"
end
def inspect
to_s
end
def move(off)
Move.new(self.from_loc + off, self.to_loc + off, self.element)
end
# don't want to create self-moves: these are null operations
# and should be treated as such!
def self.create(from_loc, to_loc, element)
if from_loc == to_loc
nil
else
Move.new(from_loc, to_loc, element)
end
end
end
class Swap
def self.swap(a, b)
a_name = a.class.name.downcase.gsub(/.*::/, "")
b_name = b.class.name.downcase.gsub(/.*::/, "")
method = "swap_#{a_name}_#{b_name}"
self.send(method.to_sym, a, b)
end
def self.swap_insert_insert(a, b)
if a.location < b.location
[b.move(-1), a]
else
[b, a.move(1)]
end
end
def self.swap_insert_alter(a, b)
if a.location < b.location
[b.move(-1), a]
elsif a.location == b.location
[nil, Insert.new(b.location, b.to_elt)]
else
[b, a]
end
end
def self.swap_insert_delete(a, b)
if a.location < b.location
[b.move(-1), a]
elsif a.location == b.location
[nil, nil]
else
[b, a.move(-1)]
end
end
def self.swap_insert_move(a, b)
if a.location == b.from_loc
# the insert is immediately moved, so will end up
# as an insert in the moved-to location. note that
# the insert must be second in this list so that
# they are kept back if they are tainted operations.
[nil, Insert.new(b.to_loc, a.element)]
else
dist = 0
new_from_loc = b.from_loc
new_to_loc = b.to_loc
if a.location < b.from_loc
new_from_loc -= 1
dist += 1
end
if (a.location < b.to_loc) || ((a.location == b.to_loc) && (b.from_loc < b.to_loc))
new_to_loc -= 1
dist -= 1
end
[Move.create(new_from_loc, new_to_loc, b.element), a.move(dist)]
end
end
def self.swap_alter_insert(a, b)
if a.location < b.location
[b, a]
else
[b, a.move(1)]
end
end
def self.swap_alter_alter(a, b)
if a.location != b.location
[b, a]
else
[Alter.new(a.location, a.from_elt, b.to_elt), nil]
end
end
def self.swap_alter_delete(a, b)
if a.location < b.location
[b, a]
elsif a.location == b.location
[Delete.new(a.location, a.from_elt), nil]
else
[b, a.move(-1)]
end
end
def self.swap_alter_move(a, b)
if b.from_loc == a.location
[Move.create(b.from_loc, b.to_loc, a.from_elt), a.move(b.to_loc - b.from_loc)]
else
dist = 0
dist -= 1 if a.location > b.from_loc
dist += 1 if (a.location > b.to_loc) || ((a.location == b.to_loc) && (b.from_loc > b.to_loc))
[b, a.move(dist)]
end
end
def self.swap_delete_insert(a, b)
if a.location <= b.location
[b.move(1), a]
else
[b, a.move(1)]
end
end
def self.swap_delete_alter(a, b)
if a.location <= b.location
[b.move(1), a]
else
[b, a]
end
end
def self.swap_delete_delete(a, b)
if a.location <= b.location
[b.move(1), a]
else
[b, a.move(-1)]
end
end
def self.swap_delete_move(a, b)
dist = 0
new_from_loc = b.from_loc
new_to_loc = b.to_loc
if a.location <= b.from_loc
new_from_loc += 1
dist += 1
end
if a.location <= b.to_loc
new_to_loc += 1
dist -= 1
end
[Move.create(new_from_loc, new_to_loc, b.element), a.move(dist)]
end
def self.swap_move_insert(a, b)
dist = 0
new_from_loc = a.from_loc
new_to_loc = a.to_loc
if b.location <= a.from_loc
new_from_loc += 1
else
dist += 1
end
if b.location <= a.to_loc
new_to_loc += 1
else
dist -= 1
end
[b.move(dist), Move.create(new_from_loc, new_to_loc, a.element)]
end
def self.swap_move_alter(a, b)
if b.location == a.to_loc
[b.move(a.from_loc - a.to_loc), Move.create(a.from_loc, a.to_loc, b.to_elt)]
else
dist = 0
dist += 1 if (b.location > a.from_loc) || ((b.location == a.from_loc) && (a.from_loc < a.to_loc))
dist -= 1 if b.location > a.to_loc
[b.move(dist), a]
end
end
def self.swap_move_delete(a, b)
if a.to_loc == b.location
[Delete.new(a.from_loc, b.element), nil]
else
dist = 0
new_from_loc = a.from_loc
new_to_loc = a.to_loc
if (b.location <= a.from_loc) && !((b.location == a.from_loc) && (a.from_loc < a.to_loc))
new_from_loc -= 1
else
dist += 1
end
if b.location < a.to_loc
new_to_loc -= 1
else
dist -= 1
end
[b.move(dist), Move.create(new_from_loc, new_to_loc, a.element)]
end
end
def self.swap_move_move(a, b)
foo = lambda do |af, at, bf, bt|
[Move.create(b.from_loc + bf, b.to_loc + bt, b.element), Move.create(a.from_loc + af, a.to_loc + at, a.element)]
end
if (b.from_loc == a.to_loc) && (a.element == b.element)
if a.from_loc != b.to_loc
# we have a chain
[Move.create(a.from_loc, b.to_loc, a.element), nil]
else
# we have a revert
[nil, nil]
end
else
# if the moves are sufficiently far from eachother not to
# affect each others' indices, then we can simply swap them.
if (([a.from_loc, a.to_loc].max < [b.from_loc, b.to_loc].min) ||
([a.from_loc, a.to_loc].min > [b.from_loc, b.to_loc].max))
[b, a]
else
rv = if ((a.from_loc < a.to_loc) && (b.from_loc < a.to_loc) && (b.to_loc < a.to_loc))
if b.to_loc < a.from_loc
foo.call(1, 0, 1, 0)
else
if b.from_loc < a.from_loc
foo.call(-1, 0, 0, 1)
else
foo.call(0, 0, 1, 1)
end
end
elsif ((a.from_loc < a.to_loc) && (b.from_loc > a.to_loc) && (b.to_loc <= a.to_loc))
if b.to_loc < a.from_loc
foo.call(1, 1, 0, 0)
else
foo.call(0, 1, 0, 1)
end
elsif ((a.from_loc < a.to_loc) && (b.from_loc < a.to_loc) && (b.to_loc >= a.to_loc))
if b.from_loc < a.from_loc
foo.call(-1, -1, 0, 0)
else
foo.call(0, -1, 1, 0)
end
elsif ((a.from_loc > a.to_loc) && (b.from_loc > a.from_loc) && (b.to_loc <= a.to_loc))
foo.call(1, 1, 0, 0)
elsif ((a.from_loc > a.to_loc) && (b.from_loc > a.from_loc) && (b.to_loc <= a.from_loc))
foo.call(1, 0, 0, -1)
elsif ((a.from_loc > a.to_loc) && (b.from_loc <= a.from_loc) && (b.to_loc >= a.from_loc))
if b.from_loc < a.to_loc
foo.call(-1, -1, 0, 0)
else
foo.call(-1, 0, -1, 0)
end
elsif ((a.from_loc > a.to_loc) && (b.from_loc <= a.from_loc) && (b.to_loc <= a.to_loc))
if b.from_loc > b.to_loc
foo.call(0, 1, -1, 0)
else
foo.call(0, -1, 0, -1)
end
elsif ((a.from_loc > a.to_loc) && (b.from_loc <= a.from_loc) && (b.to_loc > a.to_loc))
if b.from_loc > a.to_loc
foo.call(0, 0, -1, -1)
else
foo.call(0, -1, 0, -1)
end
else
raise "Unhandled move-move case! [#{a} <=> #{b}]"
end
# return answer
rv
end
end
end
end
def self.first_contraction(a, i)
foo = a.each_cons(2).each_with_index.select do |arr,ix|
x, y = arr
((((x.class == Insert) and (y.class == Delete)) or
((x.class == Delete) and (y.class == Insert))) and
(x.location == y.location) and
(ix > i))
end
foo.empty? ? nil : foo.first[1]
end
def self.first_relocation(a, i)
foo = a.each_with_index.select do |x, ix|
if ((ix > i) and ((x.class == Insert) or (x.class == Delete)))
pairclass = (x.class == Insert) ? Delete : Insert
iy = a.index {|y| (y.class == pairclass) and (y.element == x.element) }
return [ix, iy] unless iy.nil?
end
end
return [nil, nil]
end
def self.diff(a, b, options = {})
a_idx = 0
rv = Array.new
Util.diff(a, b).each do |src,elt|
case src
when :a
rv << Diff::Delete.new(a_idx, elt)
when :b
rv << Diff::Insert.new(a_idx, elt)
a_idx += 1
when :c
a_idx += 1
end
end
if options.has_key?(:detect_alter)
# now try and detect alterations to the role
# which we treat separately from other changes.
fc = -1
eql_proc = options[:detect_alter]
while (fc = first_contraction(rv, fc))
from = rv[fc].class == Delete ? rv[fc] : rv[fc+1]
to = rv[fc+1].class == Insert ? rv[fc+1] : rv[fc]
if eql_proc.call(from.element, to.element)
rv[fc] = Alter.new(from.location, from.element, to.element)
rv.delete_at(fc+1)
end
end
end
if options[:detect_move] == true
fidx = -1
loop do
fidx, sidx = first_relocation(rv, fidx)
break if fidx.nil?
fidx, sidx = [[fidx, sidx].min, [fidx, sidx].max]
delidx, insidx = (rv[fidx].class == Delete) ? [fidx, sidx] : [sidx, fidx]
del_loc = rv[delidx].location
ins_loc = rv[insidx].location
movement = rv[(fidx+1)...sidx].map do |a|
case a
when Insert
1
when Delete
-1
else
0
end
end.reduce(:+)
movement = 0 if movement.nil?
if del_loc > ins_loc
del_loc -= (movement + 1)
else
ins_loc -= movement
end
rv[fidx] = Move.create(del_loc, ins_loc, rv[insidx].element)
rv.delete_at(sidx)
end
end
return rv
end
def self.compose(a, b)
new_a = Array.new
new_b = b.clone
a.reverse.each do |a_act|
new_a_act = a_act.clone
new_b.map! do |b_act|
unless new_a_act.nil? or b_act.nil?
new_b_act, new_a_act = Swap.swap(new_a_act, b_act)
new_b_act
else
b_act
end
end
new_a.insert(0, new_a_act) unless new_a_act.nil?
end
return [new_a, new_b.compact]
end
def self.split_deletes(a)
deletes = Array.new
other = Array.new
a.each do |act|
if act.class == Delete
# deletes are moved before other elements,
# so we will need to compose them with the
# others before putting them onto the list.
other, new_act = Diff::compose(other, [act])
deletes += new_act
else
# non-deletes can be added straight onto the
# list of such elements.
other << act.clone
end
end
return [deletes, other]
end
def self.apply(diff, arr)
diff.inject(arr) do |ax,di|
di.apply(ax)
end
end
end