diff options
author | tiendo1011 <tiendo1011@gmail.com> | 2021-12-19 17:15:43 +0700 |
---|---|---|
committer | tiendo1011 <tiendo1011@gmail.com> | 2021-12-19 17:17:47 +0700 |
commit | 334b12eb1b2f8671c66b416b4ce37c3c6add18be (patch) | |
tree | 5ccddb2ed62a42fa419a95d3df7c9d9ef0148833 | |
parent | cd4b76d53d77fa596d72584d7531fdb811cd38bf (diff) | |
download | diff-lcs-334b12eb1b2f8671c66b416b4ce37c3c6add18be.tar.gz |
Ax unnecessary calls
-rw-r--r-- | lib/diff/lcs/internals.rb | 10 |
1 files changed, 3 insertions, 7 deletions
diff --git a/lib/diff/lcs/internals.rb b/lib/diff/lcs/internals.rb index 4977288..6e5a962 100644 --- a/lib/diff/lcs/internals.rb +++ b/lib/diff/lcs/internals.rb @@ -44,13 +44,12 @@ class << Diff::LCS::Internals b_finish = b.size - 1 vector = [] - # Prune off any common elements at the beginning... + # Collect any common elements at the beginning... while (a_start <= a_finish) and (b_start <= b_finish) and (a[a_start] == b[b_start]) vector[a_start] = b_start a_start += 1 b_start += 1 end - b_start = a_start # Now the end... while (a_start <= a_finish) and (b_start <= b_finish) and (a[a_finish] == b[b_finish]) @@ -60,6 +59,7 @@ class << Diff::LCS::Internals end # Now, compute the equivalence classes of positions of elements. + # An explanation for how this works: https://codeforces.com/topic/92191 b_matches = position_hash(b, b_start..b_finish) thresh = [] @@ -71,11 +71,7 @@ class << Diff::LCS::Internals bm = b_matches[ai] k = nil bm.reverse_each do |j| - if k and (thresh[k] > j) and (thresh[k - 1] < j) - thresh[k] = j - else - k = replace_next_larger(thresh, j, k) - end + k = replace_next_larger(thresh, j) links[k] = [k.positive? ? links[k - 1] : nil, i, j] unless k.nil? end end |