Skip to content

Commit 6f1ffd0

Browse files
committed
Use Array instead of Vector
1 parent 62a12f8 commit 6f1ffd0

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/Data/TreeDiff/Class.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ import qualified Data.Strict as Strict
101101
-- these
102102
import Data.These (These (..))
103103

104+
-- primitive
105+
-- import qualified Data.Primitive as Prim
106+
104107
-- $setup
105108
-- >>> :set -XDeriveGeneric
106109
-- >>> import Data.Foldable (traverse_)
@@ -571,3 +574,9 @@ instance (ToExpr a, ToExpr b) => ToExpr (These a b) where
571574
toExpr (This x) = App "This" [toExpr x]
572575
toExpr (That y) = App "That" [toExpr y]
573576
toExpr (These x y) = App "These " [toExpr x, toExpr y]
577+
578+
-------------------------------------------------------------------------------
579+
-- primitive
580+
-------------------------------------------------------------------------------
581+
582+
-- TODO: add instances

src/Data/TreeDiff/List.hs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Data.TreeDiff.List (diffBy, Edit (..)) where
44

55
import 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
4040
diffBy :: forall a. (a -> a -> Bool) -> [a] -> [a] -> [Edit a]
4141
diffBy 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

7878
data Cell a = Cell !Int !a
7979

tree-diff.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ library
100100
, bytestring-builder ^>=0.10.8.2.0
101101
, hashable ^>=1.2.7.0 || ^>=1.3.0.0
102102
, parsers ^>=0.12.10
103+
, primitive ^>=0.7.1.0
103104
, QuickCheck ^>=2.14.2
104105
, scientific ^>=0.3.6.2
105106
, semialign >=1.1 && <1.3

0 commit comments

Comments
 (0)