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

'combine' function pulled out and named and exported, with documentation #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 21 additions & 9 deletions src/Data/Algorithm/Diff3.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-| An implementation of a 3-way merge algorithm. -}
module Data.Algorithm.Diff3 (Hunk(..), diff3, merge) where
module Data.Algorithm.Diff3 (Hunk(..), diff3, combine, merge) where

import Data.Algorithm.Diff
import Data.Monoid (Monoid, mempty, mappend)
Expand Down Expand Up @@ -29,15 +29,27 @@ instance Functor Hunk where
-- a \'low level\' interface to the 3-way diff algorithm - you may be more
-- interested in 'merge'.
diff3 :: Eq a => [a] -> [a] -> [a] -> [Hunk a]
diff3 a o b = step (getDiff o a) (getDiff o b)
diff3 a o b = combine (getDiff o a) (getDiff o b)

--------------------------------------------------------------------------------
-- | Combine two list of 'Diff's representing two diverging changes from
-- the __same__ original document into a list of 'Hunk's representing
-- a three-way merge.
--
-- Behavior is undefined if the two inputs do not come from diffs from the
-- same original document.
--
-- @
-- 'diff3' a o b = 'combine' ('getDiff' o a) ('getDiff' o b)
-- @
combine :: [Diff a] -> [Diff a] -> [Hunk a]
combine [] [] = []
combine [] ob = toHunk [] ob
combine oa [] = toHunk oa []
combine oa ob = conflictHunk ++ matchHunk ++ combine ra' rb'
where
step [] [] = []
step [] ob = toHunk [] ob
step oa [] = toHunk oa []
step oa ob =
let (conflictHunk, ra, rb) = shortestConflict oa ob
(matchHunk, ra', rb') = shortestMatch ra rb
in conflictHunk ++ matchHunk ++ step ra' rb'
(conflictHunk, ra, rb) = shortestConflict oa ob
(matchHunk, ra', rb') = shortestMatch ra rb


--------------------------------------------------------------------------------
Expand Down