~jaro/balkon

01cd9cc6548f5469ec6a252987cdb1d40d403d28 — Jaro 11 months ago 2db7d32
Implement set operations on [ResolvedBox].
1 files changed, 25 insertions(+), 0 deletions(-)

M src/Data/Text/ParagraphLayout/Internal/ResolvedBox.hs
M src/Data/Text/ParagraphLayout/Internal/ResolvedBox.hs => src/Data/Text/ParagraphLayout/Internal/ResolvedBox.hs +25 -0
@@ 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