From 6d15558f727d8ea47db895e32ae9447685122cd2 Mon Sep 17 00:00:00 2001 From: tiendo1011 Date: Mon, 20 Dec 2021 13:42:48 +0700 Subject: Use element directly instead of accessing it from the index We have ai, bj to keep track of which index we're in for both a and b, so keeping i is not needed Without it, the logic will be simpler --- lib/diff/lcs.rb | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'lib/diff/lcs.rb') diff --git a/lib/diff/lcs.rb b/lib/diff/lcs.rb index 43d3ce1..f102bb1 100644 --- a/lib/diff/lcs.rb +++ b/lib/diff/lcs.rb @@ -292,15 +292,13 @@ class << Diff::LCS b_size = seq2.size ai = bj = 0 - (0...matches.size).each do |i| - b_line = matches[i] - - ax = string ? seq1[i, 1] : seq1[i] + matches.each do |b_line| + ax = string ? seq1[ai, 1] : seq1[ai] bx = string ? seq2[bj, 1] : seq2[bj] if b_line.nil? unless ax.nil? or (string and ax.empty?) - event = Diff::LCS::ContextChange.new('-', i, ax, bj, bx) + event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx) event = yield event if block_given? callbacks.discard_a(event) end @@ -309,20 +307,19 @@ class << Diff::LCS break unless bj < b_line bx = string ? seq2[bj, 1] : seq2[bj] - event = Diff::LCS::ContextChange.new('+', i, ax, bj, bx) + event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx) event = yield event if block_given? callbacks.discard_b(event) bj += 1 end bx = string ? seq2[bj, 1] : seq2[bj] - event = Diff::LCS::ContextChange.new('=', i, ax, bj, bx) + event = Diff::LCS::ContextChange.new('=', ai, ax, bj, bx) event = yield event if block_given? callbacks.match(event) bj += 1 end - ai = i + ai += 1 end - ai += 1 # The last entry (if any) processed was a match. +ai+ and +bj+ point just # past the last matching lines in their sequences. -- cgit v1.2.1 From fbedcd9d1afa7af9a1df55c158dd9baa731e6b20 Mon Sep 17 00:00:00 2001 From: tiendo1011 Date: Mon, 20 Dec 2021 13:25:55 +0700 Subject: The symmetrically makes it easier to understand --- lib/diff/lcs.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'lib/diff/lcs.rb') diff --git a/lib/diff/lcs.rb b/lib/diff/lcs.rb index f102bb1..f3716e1 100644 --- a/lib/diff/lcs.rb +++ b/lib/diff/lcs.rb @@ -377,14 +377,14 @@ class << Diff::LCS ai += 1 end - next unless bj < b_size - - ax = string ? seq1[ai, 1] : seq1[ai] - bx = string ? seq2[bj, 1] : seq2[bj] - event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx) - event = yield event if block_given? - callbacks.discard_b(event) - bj += 1 + if bj < b_size + ax = string ? seq1[ai, 1] : seq1[ai] + bx = string ? seq2[bj, 1] : seq2[bj] + event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx) + event = yield event if block_given? + callbacks.discard_b(event) + bj += 1 + end end end -- cgit v1.2.1 From 47ad358795caf269a47a054d249d017f5eb72ce4 Mon Sep 17 00:00:00 2001 From: tiendo1011 Date: Wed, 22 Dec 2021 13:46:42 +0700 Subject: Apply some performance improvement --- lib/diff/lcs.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'lib/diff/lcs.rb') diff --git a/lib/diff/lcs.rb b/lib/diff/lcs.rb index f3716e1..8425d15 100644 --- a/lib/diff/lcs.rb +++ b/lib/diff/lcs.rb @@ -293,16 +293,18 @@ class << Diff::LCS ai = bj = 0 matches.each do |b_line| - ax = string ? seq1[ai, 1] : seq1[ai] - bx = string ? seq2[bj, 1] : seq2[bj] - if b_line.nil? - unless ax.nil? or (string and ax.empty?) + unless seq1[ai].nil? + ax = string ? seq1[ai, 1] : seq1[ai] + bx = string ? seq2[bj, 1] : seq2[bj] + event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx) event = yield event if block_given? callbacks.discard_a(event) end else + ax = string ? seq1[ai, 1] : seq1[ai] + loop do break unless bj < b_line -- cgit v1.2.1 From 7019d8df41c16d1c72097450c0243229229b230a Mon Sep 17 00:00:00 2001 From: tiendo1011 Date: Wed, 22 Dec 2021 14:04:09 +0700 Subject: Updating the comment to reflect the new found logic More info can be found here: https://github.com/halostatue/diff-lcs/issues/77 --- lib/diff/lcs.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/diff/lcs.rb') diff --git a/lib/diff/lcs.rb b/lib/diff/lcs.rb index 8425d15..38dff6e 100644 --- a/lib/diff/lcs.rb +++ b/lib/diff/lcs.rb @@ -250,8 +250,9 @@ class << Diff::LCS # advance that arrow and will call callbacks#discard_a or # callbacks#discard_b, depending on which arrow it advanced. If both # arrows point to elements that are not part of the longest common - # subsequence, then #traverse_sequences will advance one of them and call the - # appropriate callback, but it is not specified which it will call. + # subsequence, then #traverse_sequences will advance arrow +a+ and call the + # appropriate callback, then it will advance arrow +b+ and call the appropriate + # callback. # # The methods for callbacks#match, callbacks#discard_a, and # callbacks#discard_b are invoked with an event comprising the -- cgit v1.2.1