module Data.Algorithm.Diff (DI(..), getDiff, getGroupedDiff) where
import Data.Array.Unboxed
import Data.List
data DI = F | S | B deriving (Show, Eq)
data DL = DL {poi::Int, poj::Int, path::[DI]} deriving (Show, Eq)
instance Ord DL where x <= y = poi x <= poi y
canDiag :: Eq a => [a] -> [a] -> Int -> Int -> (Int, Int) -> Bool
canDiag as bs lena lenb =
safeGet $ array ( (0, 0) , (lena1, lenb1) )
[( (i, j), a'==b' ) | (a',i) <- zip as [0..], (b',j) <- zip bs [0..]]
where
safeGet :: UArray (Int, Int) Bool -> (Int, Int) -> Bool
safeGet ar ind@(i,j) = if i < lena && j < lenb then ar ! ind else False
chunk :: Int -> [a] -> [[a]]
chunk x = unfoldr (\a -> case splitAt x a of ([],[]) -> Nothing; a' -> Just a')
dstep :: ((Int,Int)->Bool) -> [DL] -> [DL]
dstep cd dls = map maximum $ [hd]:(chunk 2 rst)
where (hd:rst) = concatMap extend dls
extend dl = let pdl = path dl
in [addsnake cd $ dl {poi=poi dl + 1, path=(F : pdl)},
addsnake cd $ dl {poj=poj dl + 1, path=(S : pdl)}]
addsnake :: ((Int,Int)->Bool) -> DL -> DL
addsnake cd dl
| cd (pi, pj) = addsnake cd $
dl {poi = pi + 1, poj = pj + 1, path=(B : path dl)}
| otherwise = dl
where pi = poi dl; pj = poj dl
lcs :: (Eq a) => [a] -> [a] -> [DI]
lcs as bs = path . head . dropWhile (\dl -> poi dl /= lena || poj dl /= lenb) .
concat . iterate (dstep cd) . (:[]) . addsnake cd $
DL {poi=0,poj=0,path=[]}
where cd = canDiag as bs lena lenb
lena = length as; lenb = length bs
getDiff :: (Eq t) => [t] -> [t] -> [(DI, t)]
getDiff a b = markup a b . reverse $ lcs a b
where markup (x:xs) ys (F:ds) = (F, x) : markup xs ys ds
markup xs (y:ys) (S:ds) = (S, y) : markup xs ys ds
markup (x:xs) (_:ys) (B:ds) = (B, x) : markup xs ys ds
markup _ _ _ = []
getGroupedDiff :: (Eq t) => [t] -> [t] -> [(DI, [t])]
getGroupedDiff a b = map go . groupBy (\x y -> fst x == fst y) $ getDiff a b
where go ((d,x) : xs) = (d, x : map snd xs)