module Mask where import Data.Int newtype Mask = Mask [(Int32,Int32)] deriving (Eq,Ord,Show) {- merge ((x0,x):xs) ((y0,y):ys) = if x0 <= y0 then case compare x y0 of LT -> (x0 x) (y0 y) EQ -> (x0 x=y0 y) GT -> if x <= y then -> (x0 y0 x y) else -> (x0 y0 y x) else case compare x0 y of LT -> if x <= y then (y0 x0 x y) else (y0 x0 y x) EQ -> (y0 y=x0 x) GT -> (y0 y) (x0 x) -} subtr [] ys = [] subtr xs [] = xs subtr ((x0,x):xs) ((y0,y):ys) = if x0 <= y0 then case compare x y0 of LT -> subtr ((x0,x):xs) ys EQ -> (x0,x) : subtr xs ((y0,y):ys) GT -> if x <= y then (if x0 if x <= y then subtr xs ((y0,y):ys) else subtr ((y,x):xs) ys EQ -> subtr ((x0,x):xs) ys GT -> subtr ((x0,x):xs) ys union [] ys = ys union xs [] = xs union ((x0,x):xs) ((y0,y):ys) = if x0 <= y0 then case compare x y0 of LT -> (x0,x) : union xs ((y0,y):ys) EQ -> (x0,y) : union xs ys GT -> if x <= y then union xs ((x0,y):ys) else union ((x0,x):xs) ys else case compare x0 y of LT -> if x <= y then union xs ((y0,y):ys) else union ((y0,x):xs) ys EQ -> (y0,x) : union xs ys GT -> (y0,y) : union ((x0,x):xs) ys endpts :: (Int32,Int32) -> (Int32,Int32) endpts (x0,sz) = (x0,x0 + sz) sized :: (Int32,Int32) -> (Int32,Int32) sized (x0,x) = (x0, x - x0) maskPlus :: Mask -> Mask -> Mask maskPlus (Mask xs) (Mask ys) = Mask $ sized <$> union (endpts <$> xs) (endpts <$> ys) maskSubtract :: Mask -> Mask -> Mask maskSubtract (Mask xs) (Mask ys) = Mask $ sized <$> subtr (endpts <$> xs) (endpts <$> ys)