@@ -4,7 +4,7 @@ module Data.TreeDiff.List (diffBy, Edit (..)) where
44
55import Control.DeepSeq (NFData (.. ))
66
7- import qualified Data.Vector as V
7+ import qualified Data.Primitive as P
88
99-- | List edit operations
1010--
@@ -40,40 +40,40 @@ instance NFData a => NFData (Edit a) where
4040diffBy :: forall a . (a -> a -> Bool ) -> [a ] -> [a ] -> [Edit a ]
4141diffBy eq xs' ys' = reverse (getCell (lcs xn yn))
4242 where
43- xn = V. length xs
44- yn = V. length ys
43+ xn = length xs'
44+ yn = length ys'
4545
46- xs = V. fromList xs'
47- ys = V. fromList ys'
46+ xs = P. arrayFromListN xn xs'
47+ ys = P. arrayFromListN yn ys'
4848
49- memo :: V. Vector (Cell [Edit a ])
50- memo = V. fromList
49+ memo :: P. Array (Cell [Edit a ])
50+ memo = P. arrayFromListN ((xn + 1 ) * (yn + 1 ))
5151 [ impl xi yi
5252 | xi <- [0 .. xn]
5353 , yi <- [0 .. yn]
5454 ]
5555
5656 lcs :: Int -> Int -> Cell [Edit a ]
57- lcs xi yi = memo V. ! (yi + xi * (yn + 1 ))
57+ lcs xi yi = P. indexArray memo (yi + xi * (yn + 1 ))
5858
5959 impl :: Int -> Int -> Cell [Edit a ]
6060 impl 0 0 = Cell 0 []
61- impl 0 m = case lcs 0 (m- 1 ) of
62- Cell w edit -> Cell (w + 1 ) (Ins (ys V. ! (m - 1 )) : edit)
63- impl n 0 = case lcs (n - 1 ) 0 of
64- Cell w edit -> Cell (w + 1 ) (Del (xs V. ! (n - 1 )) : edit)
61+ impl 0 m = case lcs 0 (m - 1 ) of
62+ Cell w edit -> Cell (w + 1 ) (Ins (P. indexArray ys (m - 1 )) : edit)
63+ impl n 0 = case lcs (n - 1 ) 0 of
64+ Cell w edit -> Cell (w + 1 ) (Del (P. indexArray xs (n - 1 )) : edit)
6565
6666 impl n m = bestOfThree
6767 edit
6868 (bimap (+ 1 ) (Ins y : ) (lcs n (m - 1 )))
6969 (bimap (+ 1 ) (Del x : ) (lcs (n - 1 ) m))
7070 where
71- x = xs V. ! (n - 1 )
72- y = ys V. ! (m - 1 )
71+ x = P. indexArray xs (n - 1 )
72+ y = P. indexArray ys (m - 1 )
7373
7474 edit
7575 | eq x y = bimap id (Cpy x : ) (lcs (n - 1 ) (m - 1 ))
76- | otherwise = bimap (+ 1 ) (Swp x y : ) (lcs (n - 1 ) (m - 1 ))
76+ | otherwise = bimap (+ 1 ) (Swp x y : ) (lcs (n - 1 ) (m - 1 ))
7777
7878data Cell a = Cell ! Int ! a
7979
0 commit comments