summaryrefslogtreecommitdiff
path: root/Mask.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Mask.hs')
-rw-r--r--Mask.hs86
1 files changed, 43 insertions, 43 deletions
diff --git a/Mask.hs b/Mask.hs
index 0b91e42..81de4d7 100644
--- a/Mask.hs
+++ b/Mask.hs
@@ -1,54 +1,50 @@
1{-# LANGUAGE CPP #-}
1module Mask where 2module Mask where
2 3
4import Control.Monad
5import Debug.Trace
3import Data.Int 6import Data.Int
7import qualified Data.List as List
8import Data.List.Merge
4 9
10-- Start index , count
5newtype Mask = Mask [(Int32,Int32)] 11newtype Mask = Mask [(Int32,Int32)]
6 deriving (Eq,Ord,Show) 12 deriving (Eq,Ord,Show)
7 13
8{- 14subtr xs ys =
9merge ((x0,x):xs) ((y0,y):ys) = 15 let xxs = concatMap (\(a,b) -> [(a,1),(b,2)]) xs
10 if x0 <= y0 16 yys = concatMap (\(a,b) -> [(a,4),(b,3)]) ys
11 then case compare x y0 of 17 zzs = mergeL xxs yys -- xxx: we really want (comparing fst)
12 LT -> (x0 x) (y0 y) 18 zs = foldr subtr4 (const []) zzs (False,False)
13 EQ -> (x0 x=y0 y) 19 in filter (uncurry (/=)) $ pairs zs
14 GT -> if x <= y then -> (x0 y0 x y) 20
15 else -> (x0 y0 y x) 21subtr4 (x,1) f (False,iny) = (if not iny then (x:) else id) $ f (True,iny)
16 else case compare x0 y of 22subtr4 (x,2) f (True,iny) = (if not iny then (x:) else id) $ f (False,iny)
17 LT -> if x <= y then (y0 x0 x y) 23subtr4 (x,3) f (inx,True) = (if inx then (x:) else id) $ f (inx,False)
18 else (y0 x0 y x) 24subtr4 (x,4) f (inx,False) = (if inx then (x:) else id) $ f (inx,True)
19 EQ -> (y0 y=x0 x) 25subtr4 _ f inxy = f inxy
20 GT -> (y0 y) (x0 x) 26
21-} 27pairs :: [a] -> [(a,a)]
22 28pairs (x:y:ys) = (x,y) : pairs ys
23subtr [] ys = [] 29pairs _ = []
24subtr xs [] = xs 30
25subtr ((x0,x):xs) ((y0,y):ys) = 31
26 if x0 <= y0 32union1 ab [] = (ab,[])
27 then case compare x y0 of 33union1 (a,b) ((x0,x):xs) =
28 LT -> subtr ((x0,x):xs) ys 34 if x0 <= b
29 EQ -> (x0,x) : subtr xs ((y0,y):ys) 35 then union1 (a,max x b) xs
30 GT -> if x <= y then (if x0<y0 then ((x0,y0) :) else id) $ subtr xs ((y0,y):ys) 36 else ((a,b),(x0,x):xs)
31 else (if x0<y0 then ((x0,y0) :) else id) $ subtr ((y,x):xs) ys
32 else case compare x0 y of
33 LT -> if x <= y then subtr xs ((y0,y):ys)
34 else subtr ((y,x):xs) ys
35 EQ -> subtr ((x0,x):xs) ys
36 GT -> subtr ((x0,x):xs) ys
37 37
38union [] ys = ys 38union [] ys = ys
39union xs [] = xs 39union xs [] = xs
40union ((x0,x):xs) ((y0,y):ys) = 40union xs@((x0,_):_) ys@((y0,_):_) | y0<x0 = union ys xs
41 if x0 <= y0 41union (x:xs) ys =
42 then case compare x y0 of 42 let (x',ys') = union1 x ys
43 LT -> (x0,x) : union xs ((y0,y):ys) 43 (z@(_,e),xs') = union1 x' xs
44 EQ -> (x0,y) : union xs ys 44 in if all (\(f,_) -> e < f) (take 1 ys' ++ take 1 xs')
45 GT -> if x <= y then union xs ((x0,y):ys) 45 then z : union xs' ys'
46 else union ((x0,x):xs) ys 46 else union (z:xs') ys'
47 else case compare x0 y of 47
48 LT -> if x <= y then union xs ((y0,y):ys)
49 else union ((y0,x):xs) ys
50 EQ -> (y0,x) : union xs ys
51 GT -> (y0,y) : union ((x0,x):xs) ys
52 48
53endpts :: (Int32,Int32) -> (Int32,Int32) 49endpts :: (Int32,Int32) -> (Int32,Int32)
54endpts (x0,sz) = (x0,x0 + sz) 50endpts (x0,sz) = (x0,x0 + sz)
@@ -57,7 +53,11 @@ sized :: (Int32,Int32) -> (Int32,Int32)
57sized (x0,x) = (x0, x - x0) 53sized (x0,x) = (x0, x - x0)
58 54
59maskPlus :: Mask -> Mask -> Mask 55maskPlus :: Mask -> Mask -> Mask
60maskPlus (Mask xs) (Mask ys) = Mask $ sized <$> union (endpts <$> xs) (endpts <$> ys) 56maskPlus (Mask xs) (Mask ys) =
57 let zs = sized <$> union (endpts <$> xs) (endpts <$> ys)
58 in Mask zs
61 59
62maskSubtract :: Mask -> Mask -> Mask 60maskSubtract :: Mask -> Mask -> Mask
63maskSubtract (Mask xs) (Mask ys) = Mask $ sized <$> subtr (endpts <$> xs) (endpts <$> ys) 61maskSubtract (Mask xs) (Mask ys) =
62 let zs = sized <$> subtr (endpts <$> xs) (endpts <$> ys)
63 in Mask zs