Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract min_diff from GoBoundless/www #53

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dyph.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "pry-rescue"
spec.add_development_dependency "pry-stack_explorer"
spec.add_development_dependency 'rspec', '~> 3.3.0'
spec.add_development_dependency 'rspec-its', '~> 1.2.0'
spec.add_development_dependency "awesome_print"
spec.add_development_dependency "factory_girl"
spec.add_development_dependency "codeclimate-test-reporter"
Expand Down
2 changes: 2 additions & 0 deletions lib/dyph.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "dyph/version"
require "dyph/differ"

require "dyph/min_diff"

require "dyph/merge_result"

require "dyph/outcome"
Expand Down
76 changes: 76 additions & 0 deletions lib/dyph/min_diff.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module Dyph

# Perform a two-way diff of the given arrays twice, the second time with left and right reversed, and return the smaller result.
# @param left [Object]
# @param base [Object]
# @return [MergeResult]
def self.min_diff(left, right)
a = Differ.two_way_diff(left, right)
b = Differ.two_way_diff(right, left)
if a.size <= b.size
a
else
sort_actions(invert_actions(b))
end
end

# Invert the meanings of deleted and added chunks.
# @param actions [object]
# @return actions [object]
def self.invert_actions(actions)
actions.map do |action|
case action
when Action::Add then Action::Delete.new(value: action.value, old_index: action.old_index, new_index: action.new_index)
when Action::Delete then Action::Add.new(value: action.value, old_index: action.old_index, new_index: action.new_index)
else action
end
end
end

# Reorder a diff result so that deletes are always before adds.
# @param actions [object]
# @return actions [object]
def self.sort_actions(actions)
actions = actions.reduce([]) do |result, action|
case action
when Action::Delete, Action::Add
ChangeGroup.begin(result)
result.last.add action
when Action::NoChange
ChangeGroup.end(result)
result << action
end
result
end
ChangeGroup.end(actions)
return actions
end

class ChangeGroup

def initialize
@adds = []
@deletes = []
end

def add(change)
case change
when Action::Delete then @deletes << change
when Action::Add then @adds << change
end
end

def values
[*@deletes, *@adds]
end

def self.begin(action)
action << new unless action.last.is_a? self
end

def self.end(action)
action.concat(action.pop.values) if action.last.is_a? self
end
end

end
2 changes: 1 addition & 1 deletion lib/dyph/two_way_differs/output_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,4 @@ def partition_into_actions(array)
end
end
end
end
end
13 changes: 13 additions & 0 deletions spec/lib/dyph/min_diff_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe Dyph do
describe ".min_diff" do
let(:left) { %w[a b c d e f g] }
let(:right) { %w[d e f g a b c] }
let(:regular_diff) { Dyph::Differ.two_way_diff(left, right) }
let(:subject) { Dyph.min_diff(left, right) }

its(:length) { is_expected.to be < regular_diff.length }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, what if instead of just checking that the length is smaller, we check to ensure it matches Dyph::Differ.two_way_diff(right, left)? Optionally also checking that Dyph::Differ.two_way_diff(right, left) < Dyph::Differ.two_way_diff(right, left) so if the test fails, we know it's because of min_diff and not that two_way_diff perhaps got better?


end
end
7 changes: 4 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require "codeclimate-test-reporter"
CodeClimate::TestReporter.start

require 'dyph'
require "dyph"

require "pry"
require "awesome_print"
require 'codeclimate-test-reporter'
require 'faker'
require "codeclimate-test-reporter"
require "faker"
require "rspec/its"

Dir[File.dirname(__FILE__) + '/fixtures/*.rb'].each {|file| require file }

Expand Down