aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMirek Kratochvil <exa.exa@gmail.com>2025-07-13 09:52:14 +0200
committerMirek Kratochvil <exa.exa@gmail.com>2025-07-13 09:52:14 +0200
commit96a623ac0766df9d6427419c1c8a8e799798d825 (patch)
tree71493baf85e4325ad0f150fcbeda2fb7d620e13a
parent3ce3c1d8936b122fd9777db77bbbc289b127eeec (diff)
downloadwerge-96a623ac0766df9d6427419c1c8a8e799798d825.tar.gz
werge-96a623ac0766df9d6427419c1c8a8e799798d825.tar.bz2
prevent slow list appending in regroup&expand
-rw-r--r--Main.hs21
1 files changed, 14 insertions, 7 deletions
diff --git a/Main.hs b/Main.hs
index 3918795..dd2f35f 100644
--- a/Main.hs
+++ b/Main.hs
@@ -68,23 +68,30 @@ align ((Add, m):ms) ys = Conflict [m] [] [] : align ms ys
align ms ((Add, y):ys) = Conflict [] [] [y] : align ms ys
align _ _ = error "diffs do not align"
--- TODO this is quadratic, call regroup first and case it
regroup :: [Merged] -> [Merged]
regroup [] = []
regroup (Ok []:xs) = regroup xs
-regroup (Ok a:Ok b:xs) = regroup (Ok (a ++ b) : xs)
+regroup (x@(Ok a):xs) =
+ case regroup xs of
+ (Ok b:xs') -> Ok (a ++ b) : xs'
+ xs' -> x : xs'
regroup (Conflict [] [] []:xs) = regroup xs
-regroup (Conflict m1 o1 y1:Conflict m2 o2 y2:xs) =
- regroup (Conflict (m1 ++ m2) (o1 ++ o2) (y1 ++ y2) : xs)
+regroup (x@(Conflict m1 o1 y1):xs) =
+ case regroup xs of
+ (Conflict m2 o2 y2:xs') -> Conflict (m1 ++ m2) (o1 ++ o2) (y1 ++ y2) : xs'
+ xs' -> x : xs'
regroup (x:xs) = x : regroup xs
expand :: Int -> [Merged] -> [Merged]
expand n = go
where
go [] = []
- go (Conflict m1 o1 y1:Ok a:Conflict m2 o2 y2:xs)
- | length a <= n =
- go $ Conflict (m1 ++ a ++ m2) (o1 ++ a ++ o2) (y1 ++ a ++ y2) : xs
+ go (x@(Conflict m1 o1 y1):xs) =
+ case go xs of
+ (Ok a:Conflict m2 o2 y2:xs')
+ | length a <= n ->
+ Conflict (m1 ++ a ++ m2) (o1 ++ a ++ o2) (y1 ++ a ++ y2) : xs'
+ xs' -> x : xs'
go (x:xs) = x : go xs
zeal (Conflict m o y) =