X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FTree.hs;h=5a304705e62fd1a4c203867f9491125facb3ac59;hb=13e58f0482069d84b89d90eeeeca2c172c3e6682;hp=b9d2c54d7226740f5a18db2ea82d5d311600d204;hpb=897ec7c12341f6f2b5f5b34cedf45907f061b2c1;p=haskell-directory.git diff --git a/Data/Tree.hs b/Data/Tree.hs index b9d2c54..5a30470 100644 --- a/Data/Tree.hs +++ b/Data/Tree.hs @@ -29,8 +29,11 @@ import Prelude #endif import Control.Monad -import Data.Maybe -import Data.Queue +import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList, toList, + ViewL(..), ViewR(..), viewl, viewr) +import Data.Typeable + +#include "Typeable.h" -- | Multi-way trees, also known as /rose trees/. data Tree a = Node { @@ -46,6 +49,8 @@ instance Show a => Show (Tree a) #endif type Forest a = [Tree a] +INSTANCE_TYPEABLE1(Tree,treeTc,"Tree") + instance Functor Tree where fmap = mapTree @@ -104,34 +109,33 @@ unfoldForestM f = mapM (unfoldTreeM f) -- | Monadic tree builder, in breadth-first order, -- using an algorithm adapted from --- /Breadth­First Numbering: Lessons from a Small Exercise in Algorithm Design/, +-- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/, -- by Chris Okasaki, /ICFP'00/. unfoldTreeM_BF :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a) -unfoldTreeM_BF f b = liftM (fst . fromJust . deQueue) $ - unfoldForestQ f (listToQueue [b]) +unfoldTreeM_BF f b = liftM getElement $ unfoldForestQ f (singleton b) + where getElement xs = case viewl xs of + x :< _ -> x + EmptyL -> error "unfoldTreeM_BF" -- | Monadic forest builder, in breadth-first order, -- using an algorithm adapted from --- /Breadth­First Numbering: Lessons from a Small Exercise in Algorithm Design/, +-- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/, -- by Chris Okasaki, /ICFP'00/. unfoldForestM_BF :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a) -unfoldForestM_BF f = liftM (reverseOnto []) . unfoldForestQ f . listToQueue - where reverseOnto :: [a] -> Queue a -> [a] - reverseOnto as q = case deQueue q of - Nothing -> as - Just (a, q') -> reverseOnto (a:as) q' - --- takes a queue of seeds --- produces a queue of trees of the same length, but in the reverse order -unfoldForestQ :: Monad m => (b -> m (a, [b])) -> Queue b -> m (Queue (Tree a)) -unfoldForestQ f aQ = case deQueue aQ of - Nothing -> return emptyQueue - Just (a, aQ) -> do +unfoldForestM_BF f = liftM toList . unfoldForestQ f . fromList + +-- takes a sequence (queue) of seeds +-- produces a sequence (reversed queue) of trees of the same length +unfoldForestQ :: Monad m => (b -> m (a, [b])) -> Seq b -> m (Seq (Tree a)) +unfoldForestQ f aQ = case viewl aQ of + EmptyL -> return empty + a :< aQ -> do (b, as) <- f a - tQ <- unfoldForestQ f (foldl addToQueue aQ as) - let (ts, tQ') = splitOnto [] as tQ - return (addToQueue tQ' (Node b ts)) - where splitOnto :: [a] -> [b] -> Queue a -> ([a], Queue a) - splitOnto as [] q = (as, q) - splitOnto as (_:bs) q = case fromJust (deQueue q) of - (a, q') -> splitOnto (a:as) bs q' + tQ <- unfoldForestQ f (foldl (|>) aQ as) + let (tQ', ts) = splitOnto [] as tQ + return (Node b ts <| tQ') + where splitOnto :: [a'] -> [b'] -> Seq a' -> (Seq a', [a']) + splitOnto as [] q = (q, as) + splitOnto as (_:bs) q = case viewr q of + q' :> a -> splitOnto (a:as) bs q' + EmptyR -> error "unfoldForestQ"