module Grevera8SEDSlow {-# WARNING "Do not use space and speed problems." #-}

where

dX, dXY :: Double
dX = 1
dXY = sqrt 2

minDist :: Double -> Double -> Double -> Double
minDist offset y1 y = min (y1 + offset) y

-- | check(data, x, y, x, y-1, dy )
pass1 :: [Double] -> [Double] -> [Double]
pass1 x y = [minDist dX i j | (i, j) <- zip x y]

-- | check(data, x, y, x-1, y, dx ), check(testData3, x, y, x+1, y, dx );
pass2s1 :: [Double] -> [Double]
pass2s1 [] = error "must not be empty"
pass2s1 (x:xs) = scanl (minDist dX) x xs

-- | check(data, x, y, x-1, y-1, dxy ), check(testData3, x, y, x+1, y-1, dxy );
pass2s2 :: [Double] -> [Double] -> [Double]
pass2s2 [] _ = error "must not be empty"
pass2s2 _ [] = error "must not be empty"
pass2s2 xs (y:ys) = y :  [ minDist dXY i j | (i, j) <- zip xs ys]

-- | pass y-1 y, pass y+1 y
grevera8SEDpass :: [Double] -> [Double] -> [Double]
grevera8SEDpass x = reverse . pass2s2 (reverse x) . pass2s1 . reverse . pass2s2 x . pass2s1 . pass1 x

grevera8SED :: [[Double]] -> [[Double]]
grevera8SED [] = error "must not be empty"
grevera8SED (x:xs) =
  let (y:ys) = reverse $ scanl grevera8SEDpass x xs in
      reverse $ scanl grevera8SEDpass y ys
