@@ 4,6 4,8 @@ module Data.Text.ParagraphLayout.Internal.ResolvedBox
, boxLeftSpacing
, boxRightSpacing
, boxStartSpacing
+ , diff
+ , union
)
where
@@ 24,6 26,29 @@ data ResolvedBox d = ResolvedBox
instance Eq (ResolvedBox d) where
a == b = boxIndex a == boxIndex b
+instance Ord (ResolvedBox d) where
+ a `compare` b = boxIndex a `compare` boxIndex b
+
+-- | Calculate the union of two lists of boxes that are ordered
+-- from highest index to lowest and do not contain duplicates.
+union :: Ord a => [a] -> [a] -> [a]
+union xs [] = xs
+union [] ys = ys
+union (x : xs) (y : ys) = case x `compare` y of
+ EQ -> x : union xs ys
+ GT -> x : union xs (y : ys)
+ LT -> y : union (x : xs) ys
+
+-- | Calculate the difference of two lists of boxes that are ordered
+-- from highest index to lowest and do not contain duplicates.
+diff :: Ord a => [a] -> [a] -> [a]
+diff xs [] = xs
+diff [] _ = []
+diff (x : xs) (y : ys) = case x `compare` y of
+ EQ -> diff xs ys
+ GT -> x : diff xs (y : ys)
+ LT -> diff (x : xs) ys
+
boxLeftSpacing :: ResolvedBox d -> Int32
boxLeftSpacing rb = case boxSpacing $ boxOptions rb of
BoxSpacingLeftRight s _ -> s