Cleanup LabelDoubleTree -> LDTree

This commit is contained in:
2025-06-27 08:55:00 +01:00
parent 73c708a325
commit dd5a747559
11 changed files with 101 additions and 218 deletions
+13 -13
View File
@@ -9,17 +9,17 @@ import Data.Monoid
singleDT :: a -> DoubleTree a
singleDT x = DT x [] []
singleLDT :: a -> LabelDoubleTree b a
singleLDT :: a -> LDTree b a
singleLDT x = LDT x [] []
ldtToDT :: LabelDoubleTree b a -> DoubleTree a
ldtToDT :: LDTree b a -> DoubleTree a
ldtToDT (LDT x l r) = DT x (map (ldtToDT . snd) l) (map (ldtToDT . snd) r)
-- propagate two functions down the links of an LDT tree
-- which function is chosen depends on whether it is a left or right branch
ldtPropagate :: (c -> b -> c) -> (c -> b -> c)
-> c
-> LabelDoubleTree b a -> LabelDoubleTree c a
-> LDTree b a -> LDTree c a
ldtPropagate lf rf = ildtPropagate (const lf) (const rf)
-- Propgates a value (of type c) down the branches of the LDT.
@@ -30,7 +30,7 @@ ldtPropagateFold :: (c -> a -> b -> a -> c)
-> (c -> a -> b -> a -> c)
-> (c -> a -> d -> d)
-> c
-> LabelDoubleTree b a
-> LDTree b a
-> d
-> d
ldtPropagateFold lf rf up x (LDT v l r) =
@@ -44,9 +44,9 @@ ldtPropagateFold lf rf up x (LDT v l r) =
-- For each node-tree, the updated value is used to update a final value (of type d).
ldtPropagateFoldTree :: (c -> a -> b -> a -> c)
-> (c -> a -> b -> a -> c)
-> (c -> LabelDoubleTree b a -> d -> d)
-> (c -> LDTree b a -> d -> d)
-> c
-> LabelDoubleTree b a
-> LDTree b a
-> d
-> d
ldtPropagateFoldTree lf rf up x t@(LDT v l r) =
@@ -56,12 +56,12 @@ ldtPropagateFoldTree lf rf up x t@(LDT v l r) =
ildtPropagate :: (Int -> c -> b -> c) -> (Int -> c -> b -> c)
-> c
-> LabelDoubleTree b a -> LabelDoubleTree c a
-> LDTree b a -> LDTree c a
ildtPropagate lf rf x (LDT v l r) = LDT v (imap (go lf x) l) (imap (go rf x) r)
where
go f y i (z,t) = (f i y z, ildtPropagate lf rf (f i y z) t)
ldtPropagateIndices :: LabelDoubleTree b a -> LabelDoubleTree b (a, [Either Int Int])
ldtPropagateIndices :: LDTree b a -> LDTree b (a, [Either Int Int])
ldtPropagateIndices (LDT x l r) = LDT (x,[]) (imap (f Left) l) (imap (f Right) r)
where
f e i (y,t) = (y, second (e i:) <$> ldtPropagateIndices t)
@@ -140,13 +140,13 @@ dtToAdjRootParentEither root par f (DT x l r) = do
where
g = f . _dtValue
ldtToIM :: (a -> Int) -> LabelDoubleTree b a -> IM.IntMap (LabelDoubleTree b a)
ldtToIM :: (a -> Int) -> LDTree b a -> IM.IntMap (LDTree b a)
ldtToIM f t@(LDT x l r) = IM.insert (f x) t $ IM.unions $ map (ldtToIM f . snd) $ l <> r
ldtToIndentList :: LabelDoubleTree b a -> [(a,Int,LabelDoubleTreeNodeType b)]
ldtToIndentList :: LDTree b a -> [(a,Int,LabelDoubleTreeNodeType b)]
ldtToIndentList = ldtIL LDTRootNode
ldtIL :: LabelDoubleTreeNodeType b -> LabelDoubleTree b a -> [(a,Int,LabelDoubleTreeNodeType b)]
ldtIL :: LabelDoubleTreeNodeType b -> LDTree b a -> [(a,Int,LabelDoubleTreeNodeType b)]
ldtIL nt (LDT x l r) = map doindent
(concat
(headMap
@@ -180,11 +180,11 @@ prettyDT :: (a -> String) -> DoubleTree a -> [String]
prettyDT f (DT x l r) = concatMap (map ('/':) . prettyDT f) r
++ (f x : concatMap (map ('\\':) . prettyDT f) l)
prettyLDT :: (a -> String) -> LabelDoubleTree b a -> [String]
prettyLDT :: (a -> String) -> LDTree b a -> [String]
prettyLDT f (LDT x l r) = concatMap (map ('/':) . prettyLDT f . snd) r
++ (f x : concatMap (map ('\\':) . prettyLDT f . snd) l)
ldtToLoc :: LabelDoubleTree b a -> LocationLDT b a
ldtToLoc :: LDTree b a -> LocationLDT b a
ldtToLoc = LocLDT TopLDT
-- should probably do tests for these