on master help sign in
Use some flatter types
5fd4216Bryan O'Sullivan
Data/Text/Internal/Fusion.hs+7−7
@@ -166,15 +166,15 @@ reverse (Stream next s len0)
166-- | /O(n)/ Perform the equivalent of 'scanr' over a list, only with
166-- | /O(n)/ Perform the equivalent of 'scanr' over a list, only with
167-- the input and result reversed.
167-- the input and result reversed.
168reverseScanr :: (Char -> Char -> Char) -> Char -> Stream Char -> Stream Char
168reverseScanr :: (Char -> Char -> Char) -> Char -> Stream Char -> Stream Char
169−reverseScanr f z0 (Stream next0 s0 len) = Stream next (S1 :*: z0 :*: s0) (len+1) -- HINT maybe too low
169+reverseScanr f z0 (Stream next0 s0 len) = Stream next (Scan1 z0 s0) (len+1) -- HINT maybe too low
170 where
170 where
171 {-# INLINE next #-}
171 {-# INLINE next #-}
172− next (S1 :*: z :*: s) = Yield z (S2 :*: z :*: s)
172+ next (Scan1 z s) = Yield z (Scan2 z s)
173− next (S2 :*: z :*: s) = case next0 s of
173+ next (Scan2 z s) = case next0 s of
174− Yield x s' -> let !x' = f x z
174+ Yield x s' -> let !x' = f x z
175− in Yield x' (S2 :*: x' :*: s')
175+ in Yield x' (Scan2 x' s')
176− Skip s' -> Skip (S2 :*: z :*: s')
176+ Skip s' -> Skip (Scan2 z s')
177− Done -> Done
177+ Done -> Done
178{-# INLINE reverseScanr #-}
178{-# INLINE reverseScanr #-}
179
179
180-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a stream from a seed
180-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a stream from a seed
Data/Text/Internal/Fusion/Common.hs+26−23
@@ -473,17 +473,20 @@ toTitle (Stream next0 s0 len) = Stream next (CC (False :*: s0) '\0' '\0') len
473 next (CC s a b) = Yield a (CC s b '\0')
473 next (CC s a b) = Yield a (CC s b '\0')
474{-# INLINE [0] toTitle #-}
474{-# INLINE [0] toTitle #-}
475
475
476+data Justify i s = Just1 !i !s
477+ | Just2 !i !s
478+
476justifyLeftI :: Integral a => a -> Char -> Stream Char -> Stream Char
479justifyLeftI :: Integral a => a -> Char -> Stream Char -> Stream Char
477justifyLeftI k c (Stream next0 s0 len) =
480justifyLeftI k c (Stream next0 s0 len) =
478− Stream next (s0 :*: S1 :*: 0) (larger (fromIntegral k) len)
481+ Stream next (Just1 0 s0) (larger (fromIntegral k) len)
479 where
482 where
480− next (s :*: S1 :*: n) =
483+ next (Just1 n s) =
481 case next0 s of
484 case next0 s of
482− Done -> next (s :*: S2 :*: n)
485+ Done -> next (Just2 n s)
483− Skip s' -> Skip (s' :*: S1 :*: n)
486+ Skip s' -> Skip (Just1 n s')
484− Yield x s' -> Yield x (s' :*: S1 :*: n+1)
487+ Yield x s' -> Yield x (Just1 (n+1) s')
485− next (s :*: S2 :*: n)
488+ next (Just2 n s)
486− | n < k = Yield c (s :*: S2 :*: n+1)
489+ | n < k = Yield c (Just2 (n+1) s)
487 | otherwise = Done
490 | otherwise = Done
488 {-# INLINE next #-}
491 {-# INLINE next #-}
489{-# INLINE [0] justifyLeftI #-}
492{-# INLINE [0] justifyLeftI #-}
@@ -651,15 +654,15 @@ minimum (Stream next0 s0 _len) = loop0_minimum s0
651-- * Building streams
654-- * Building streams
652
655
653scanl :: (Char -> Char -> Char) -> Char -> Stream Char -> Stream Char
656scanl :: (Char -> Char -> Char) -> Char -> Stream Char -> Stream Char
654−scanl f z0 (Stream next0 s0 len) = Stream next (S1 :*: z0 :*: s0) (len+1) -- HINT maybe too low
657+scanl f z0 (Stream next0 s0 len) = Stream next (Scan1 z0 s0) (len+1) -- HINT maybe too low
655 where
658 where
656 {-# INLINE next #-}
659 {-# INLINE next #-}
657− next (S1 :*: z :*: s) = Yield z (S2 :*: z :*: s)
660+ next (Scan1 z s) = Yield z (Scan2 z s)
658− next (S2 :*: z :*: s) = case next0 s of
661+ next (Scan2 z s) = case next0 s of
659− Yield x s' -> let !x' = f z x
662+ Yield x s' -> let !x' = f z x
660− in Yield x' (S2 :*: x' :*: s')
663+ in Yield x' (Scan2 x' s')
661− Skip s' -> Skip (S2 :*: z :*: s')
664+ Skip s' -> Skip (Scan2 z s')
662− Done -> Done
665+ Done -> Done
663{-# INLINE [0] scanl #-}
666{-# INLINE [0] scanl #-}
664
667
665-- -----------------------------------------------------------------------------
668-- -----------------------------------------------------------------------------
@@ -916,17 +919,17 @@ findIndicesI p (Stream next s0 _len) = loop_findIndex 0 s0
916-- the first argument, instead of a tupling function.
919-- the first argument, instead of a tupling function.
917zipWith :: (a -> a -> b) -> Stream a -> Stream a -> Stream b
920zipWith :: (a -> a -> b) -> Stream a -> Stream a -> Stream b
918zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) =
921zipWith f (Stream next0 sa0 len1) (Stream next1 sb0 len2) =
919− Stream next (sa0 :*: sb0 :*: N) (smaller len1 len2)
922+ Stream next (T3 sa0 sb0 N) (smaller len1 len2)
920 where
923 where
921− next (sa :*: sb :*: N) = case next0 sa of
924+ next (T3 sa sb N) = case next0 sa of
922− Done -> Done
925+ Done -> Done
923− Skip sa' -> Skip (sa' :*: sb :*: N)
926+ Skip sa' -> Skip (T3 sa' sb N)
924− Yield a sa' -> Skip (sa' :*: sb :*: J a)
927+ Yield a sa' -> Skip (T3 sa' sb (J a))
925
928
926− next (sa' :*: sb :*: J a) = case next1 sb of
929+ next (T3 sa' sb (J a)) = case next1 sb of
927− Done -> Done
930+ Done -> Done
928− Skip sb' -> Skip (sa' :*: sb' :*: J a)
931+ Skip sb' -> Skip (T3 sa' sb' (J a))
929− Yield b sb' -> Yield (f a b) (sa' :*: sb' :*: N)
932+ Yield b sb' -> Yield (f a b) (T3 sa' sb' N)
930{-# INLINE [0] zipWith #-}
933{-# INLINE [0] zipWith #-}
931
934
932-- | /O(n)/ The 'countCharI' function returns the number of times the
935-- | /O(n)/ The 'countCharI' function returns the number of times the
Data/Text/Internal/Fusion/Types.hs+13−2
@@ -23,6 +23,8 @@ module Data.Text.Internal.Fusion.Types
23 , M(..)
23 , M(..)
24 , M8
24 , M8
25 , PairS(..)
25 , PairS(..)
26+ , T3(..)
27+ , Scan(..)
26 , RS(..)
28 , RS(..)
27 , Step(..)
29 , Step(..)
28 , Stream(..)
30 , Stream(..)
@@ -42,20 +44,29 @@ data M a = N
42
44
43type M8 = M Word8
45type M8 = M Word8
44
46
45−-- Restreaming state.
47+-- | Restreaming state.
46data RS s
48data RS s
47 = RS0 !s
49 = RS0 !s
48 | RS1 !s {-# UNPACK #-} !Word8
50 | RS1 !s {-# UNPACK #-} !Word8
49 | RS2 !s {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8
51 | RS2 !s {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8
50 | RS3 !s {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8
52 | RS3 !s {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8 {-# UNPACK #-} !Word8
51
53
52−infixl 2 :*:
54+-- | Strict pair.
53data PairS a b = !a :*: !b
55data PairS a b = !a :*: !b
54 -- deriving (Eq, Ord, Show)
56 -- deriving (Eq, Ord, Show)
57+infixl 2 :*:
58+
59+-- | Strict triple.
60+data T3 a b c = T3 !a !b !c
61+
62+-- | An intermediate result in a scan.
63+data Scan s = Scan1 {-# UNPACK #-} !Char !s
64+ | Scan2 {-# UNPACK #-} !Char !s
55
65
56-- | Allow a function over a stream to switch between two states.
66-- | Allow a function over a stream to switch between two states.
57data Switch = S1 | S2
67data Switch = S1 | S2
58
68
69+-- | Intermediate result in a processing pipeline.
59data Step s a = Done
70data Step s a = Done
60 | Skip !s
71 | Skip !s
61 | Yield !a !s
72 | Yield !a !s