DoubleTree -> DTree

This commit is contained in:
2025-07-10 11:28:55 +01:00
parent e013bd57f7
commit a9d730a5c1
3 changed files with 25 additions and 25 deletions
+13 -13
View File
@@ -6,13 +6,13 @@ import qualified Data.IntMap.Strict as IM
import Data.Monoid
import Dodge.Data.DoubleTree
singleDT :: a -> DoubleTree a
singleDT :: a -> DTree a
singleDT x = DT x [] []
singleLDT :: a -> LDTree b a
singleLDT x = LDT x [] []
ldtToDT :: LDTree b a -> DoubleTree a
ldtToDT :: LDTree b a -> DTree a
ldtToDT (LDT x l r) = DT x (map (ldtToDT . snd) l) (map (ldtToDT . snd) r)
ldtStartPropagate :: (a -> c) -> (c -> b -> a -> c) -> LDTree b a -> LDTree b c
@@ -92,10 +92,10 @@ ldtPropagateIndices (LDT x l r) = LDT (x, []) (imap (f Left) l) (imap (f Right)
-- conceptually, in a tree growing from left to right,
-- bottom -> top is equated with left -> right.
-- this does not match with thinking of a list as top -> bottom, so take care
doubleTreeToIndentList :: DoubleTree a -> [(a, Int, DoubleTreeNodeType)]
doubleTreeToIndentList :: DTree a -> [(a, Int, DoubleTreeNodeType)]
doubleTreeToIndentList = dtIL DTRootNode
dtIL :: DoubleTreeNodeType -> DoubleTree a -> [(a, Int, DoubleTreeNodeType)]
dtIL :: DoubleTreeNodeType -> DTree a -> [(a, Int, DoubleTreeNodeType)]
dtIL nt (DT x l r) =
map doindent (concat (headMap (dtIL DTBottomNode) (dtIL DTMidBelowNode) l))
++ [(x, 0, nt)]
@@ -103,7 +103,7 @@ dtIL nt (DT x l r) =
where
doindent (a, b, c) = (a, b + 1, c)
dtToAdjacency :: (a -> Int) -> DoubleTree a -> IM.IntMap [Int]
dtToAdjacency :: (a -> Int) -> DTree a -> IM.IntMap [Int]
dtToAdjacency f (DT x l r) =
IM.insert (f x) (map g l <> map g r)
. IM.unions
@@ -111,17 +111,17 @@ dtToAdjacency f (DT x l r) =
where
g = f . _dtValue
dtToIntMapWithRoot :: (a -> Int) -> DoubleTree a -> IM.IntMap (Maybe Int, DoubleTree a)
dtToIntMapWithRoot :: (a -> Int) -> DTree a -> IM.IntMap (Maybe Int, DTree a)
dtToIntMapWithRoot f t@(DT x l r) =
IM.insert (f x) (Nothing, t) $
foldMap (dtToRootIntMap' (f x) f) $ l <> r
dtToRootIntMap' :: Int -> (a -> Int) -> DoubleTree a -> IM.IntMap (Maybe Int, DoubleTree a)
dtToRootIntMap' :: Int -> (a -> Int) -> DTree a -> IM.IntMap (Maybe Int, DTree a)
dtToRootIntMap' root f t@(DT x l r) =
IM.insert (f x) (Just root, t) $
foldMap (dtToRootIntMap' root f) $ l <> r
dtToUpDownAdj :: (a -> Int) -> DoubleTree a -> IM.IntMap ([Int], [Int])
dtToUpDownAdj :: (a -> Int) -> DTree a -> IM.IntMap ([Int], [Int])
dtToUpDownAdj f (DT x l r) =
IM.insert (f x) (map g l, map g r)
. IM.unions
@@ -131,7 +131,7 @@ dtToUpDownAdj f (DT x l r) =
-- returns an adjacency map with oldest ancestor and direct parent if they exist
-- and any left and right children
dtToLRAdj :: (a -> Int) -> DoubleTree a -> IM.IntMap (Maybe (Int, Int), [Int], [Int])
dtToLRAdj :: (a -> Int) -> DTree a -> IM.IntMap (Maybe (Int, Int), [Int], [Int])
dtToLRAdj f (DT x l r) =
IM.insert i (Nothing, map g l, map g r)
. IM.unions
@@ -145,7 +145,7 @@ dtToLRAdj f (DT x l r) =
-- allows to propagate failure in the index discovery
dtToLRAdjEither ::
(a -> Either String Int) ->
DoubleTree a ->
DTree a ->
Either String (IM.IntMap (Maybe (Int, Int), [Int], [Int]))
dtToLRAdjEither f (DT x l r) = do
i <- f x
@@ -158,7 +158,7 @@ dtToLRAdjEither f (DT x l r) = do
where
g = f . _dtValue
dtToAdjRootParent :: Int -> Int -> (a -> Int) -> DoubleTree a -> IM.IntMap (Maybe (Int, Int), [Int], [Int])
dtToAdjRootParent :: Int -> Int -> (a -> Int) -> DTree a -> IM.IntMap (Maybe (Int, Int), [Int], [Int])
dtToAdjRootParent root par f (DT x l r) =
IM.insert (f x) (Just (root, par), map g l, map g r)
. IM.unions
@@ -170,7 +170,7 @@ dtToAdjRootParentEither ::
Int ->
Int ->
(a -> Either String Int) ->
DoubleTree a ->
DTree a ->
Either String (IM.IntMap (Maybe (Int, Int), [Int], [Int]))
dtToAdjRootParentEither root par f (DT x l r) = do
i <- f x
@@ -220,7 +220,7 @@ lastMap _ _ [] = []
lastMap f _ [x] = [f x]
lastMap f g (x : xs) = g x : lastMap f g xs
prettyDT :: (a -> String) -> DoubleTree a -> [String]
prettyDT :: (a -> String) -> DTree a -> [String]
prettyDT f (DT x l r) =
concatMap (map ('/' :) . prettyDT f) r
++ (f x : concatMap (map ('\\' :) . prettyDT f) l)