From 01cd9cc6548f5469ec6a252987cdb1d40d403d28 Mon Sep 17 00:00:00 2001 From: Jaro Date: Mon, 8 May 2023 10:48:29 +0200 Subject: [PATCH] Implement set operations on [ResolvedBox]. --- .../ParagraphLayout/Internal/ResolvedBox.hs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Data/Text/ParagraphLayout/Internal/ResolvedBox.hs b/src/Data/Text/ParagraphLayout/Internal/ResolvedBox.hs index 61f679c..552212c 100644 --- a/src/Data/Text/ParagraphLayout/Internal/ResolvedBox.hs +++ b/src/Data/Text/ParagraphLayout/Internal/ResolvedBox.hs @@ -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 -- 2.30.2