module Data.Vector.Fusion.Bundle.Monadic (
Bundle(..), Chunk(..),
size, sized,
length, null,
empty, singleton, cons, snoc, replicate, replicateM, generate, generateM, (++),
head, last, (!!), (!?),
slice, init, tail, take, drop,
map, mapM, mapM_, trans, unbox, concatMap, flatten,
indexed, indexedR, zipWithM_,
zipWithM, zipWith3M, zipWith4M, zipWith5M, zipWith6M,
zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
zip, zip3, zip4, zip5, zip6,
eq, cmp,
filter, filterM, takeWhile, takeWhileM, dropWhile, dropWhileM,
elem, notElem, find, findM, findIndex, findIndexM,
foldl, foldlM, foldl1, foldl1M, foldM, fold1M,
foldl', foldlM', foldl1', foldl1M', foldM', fold1M',
foldr, foldrM, foldr1, foldr1M,
and, or, concatMapM,
unfoldr, unfoldrM,
unfoldrN, unfoldrNM,
iterateN, iterateNM,
prescanl, prescanlM, prescanl', prescanlM',
postscanl, postscanlM, postscanl', postscanlM',
scanl, scanlM, scanl', scanlM',
scanl1, scanl1M, scanl1', scanl1M',
enumFromStepN, enumFromTo, enumFromThenTo,
toList, fromList, fromListN, unsafeFromList,
fromVector, reVector, fromVectors, concatVectors,
fromStream, chunks, elements
) where
import Data.Vector.Generic.Base
import qualified Data.Vector.Generic.Mutable.Base as M
import Data.Vector.Fusion.Bundle.Size
import Data.Vector.Fusion.Util ( Box(..), delay_inline )
import Data.Vector.Fusion.Stream.Monadic ( Stream(..), Step(..) )
import qualified Data.Vector.Fusion.Stream.Monadic as S
import Control.Monad.Primitive
import qualified Data.List as List
import Data.Char ( ord )
import GHC.Base ( unsafeChr )
import Control.Monad ( liftM )
import Prelude hiding ( length, null,
replicate, (++),
head, last, (!!),
init, tail, take, drop,
map, mapM, mapM_, concatMap,
zipWith, zipWith3, zip, zip3,
filter, takeWhile, dropWhile,
elem, notElem,
foldl, foldl1, foldr, foldr1,
and, or,
scanl, scanl1,
enumFromTo, enumFromThenTo )
import Data.Int ( Int8, Int16, Int32, Int64 )
import Data.Word ( Word8, Word16, Word32, Word, Word64 )
#include "vector.h"
#include "MachDeps.h"
data Chunk v a = Chunk Int (forall m. (PrimMonad m, Vector v a) => Mutable v (PrimState m) a -> m ())
data Bundle m v a = Bundle { sElems :: Stream m a
, sChunks :: Stream m (Chunk v a)
, sVector :: Maybe (v a)
, sSize :: Size
}
fromStream :: Monad m => Stream m a -> Size -> Bundle m v a
fromStream (Stream step t) sz = Bundle (Stream step t) (Stream step' t) Nothing sz
where
step' s = do r <- step s
return $ fmap (\x -> Chunk 1 (\v -> M.basicUnsafeWrite v 0 x)) r
chunks :: Bundle m v a -> Stream m (Chunk v a)
chunks = sChunks
elements :: Bundle m v a -> Stream m a
elements = sElems
size :: Bundle m v a -> Size
size = sSize
sized :: Bundle m v a -> Size -> Bundle m v a
sized s sz = s { sSize = sz }
length :: Monad m => Bundle m v a -> m Int
length Bundle{sSize = Exact n} = return n
length Bundle{sChunks = s} = S.foldl' (\n (Chunk k _) -> n+k) 0 s
null :: Monad m => Bundle m v a -> m Bool
null Bundle{sSize = Exact n} = return (n == 0)
null Bundle{sChunks = s} = S.foldr (\(Chunk n _) z -> n == 0 && z) True s
empty :: Monad m => Bundle m v a
empty = fromStream S.empty (Exact 0)
singleton :: Monad m => a -> Bundle m v a
singleton x = fromStream (S.singleton x) (Exact 1)
replicate :: Monad m => Int -> a -> Bundle m v a
replicate n x = Bundle (S.replicate n x)
(S.singleton $ Chunk len (\v -> M.basicSet v x))
Nothing
(Exact len)
where
len = delay_inline max n 0
replicateM :: Monad m => Int -> m a -> Bundle m v a
replicateM n p = fromStream (S.replicateM n p) (Exact (delay_inline max n 0))
generate :: Monad m => Int -> (Int -> a) -> Bundle m v a
generate n f = generateM n (return . f)
generateM :: Monad m => Int -> (Int -> m a) -> Bundle m v a
generateM n f = fromStream (S.generateM n f) (Exact (delay_inline max n 0))
cons :: Monad m => a -> Bundle m v a -> Bundle m v a
cons x s = singleton x ++ s
snoc :: Monad m => Bundle m v a -> a -> Bundle m v a
snoc s x = s ++ singleton x
infixr 5 ++
(++) :: Monad m => Bundle m v a -> Bundle m v a -> Bundle m v a
Bundle sa ta _ na ++ Bundle sb tb _ nb = Bundle (sa S.++ sb) (ta S.++ tb) Nothing (na + nb)
head :: Monad m => Bundle m v a -> m a
head = S.head . sElems
last :: Monad m => Bundle m v a -> m a
last = S.last . sElems
infixl 9 !!
(!!) :: Monad m => Bundle m v a -> Int -> m a
b !! i = sElems b S.!! i
infixl 9 !?
(!?) :: Monad m => Bundle m v a -> Int -> m (Maybe a)
b !? i = sElems b S.!? i
slice :: Monad m => Int
-> Int
-> Bundle m v a
-> Bundle m v a
slice i n s = take n (drop i s)
init :: Monad m => Bundle m v a -> Bundle m v a
init Bundle{sElems = s, sSize = sz} = fromStream (S.init s) (sz1)
tail :: Monad m => Bundle m v a -> Bundle m v a
tail Bundle{sElems = s, sSize = sz} = fromStream (S.tail s) (sz1)
take :: Monad m => Int -> Bundle m v a -> Bundle m v a
take n Bundle{sElems = s, sSize = sz} = fromStream (S.take n s) (smaller (Exact n) sz)
drop :: Monad m => Int -> Bundle m v a -> Bundle m v a
drop n Bundle{sElems = s, sSize = sz} =
fromStream (S.drop n s) (clampedSubtract sz (Exact n))
instance Monad m => Functor (Bundle m v) where
fmap = map
map :: Monad m => (a -> b) -> Bundle m v a -> Bundle m v b
map f = mapM (return . f)
mapM :: Monad m => (a -> m b) -> Bundle m v a -> Bundle m v b
mapM f Bundle{sElems = s, sSize = n} = fromStream (S.mapM f s) n
mapM_ :: Monad m => (a -> m b) -> Bundle m v a -> m ()
mapM_ m = S.mapM_ m . sElems
trans :: (Monad m, Monad m') => (forall z. m z -> m' z)
-> Bundle m v a -> Bundle m' v a
trans f Bundle{sElems = s, sChunks = cs, sVector = v, sSize = n}
= Bundle { sElems = S.trans f s, sChunks = S.trans f cs, sVector = v, sSize = n }
unbox :: Monad m => Bundle m v (Box a) -> Bundle m v a
unbox Bundle{sElems = s, sSize = n} = fromStream (S.unbox s) n
indexed :: Monad m => Bundle m v a -> Bundle m v (Int,a)
indexed Bundle{sElems = s, sSize = n} = fromStream (S.indexed s) n
indexedR :: Monad m => Int -> Bundle m v a -> Bundle m v (Int,a)
indexedR m Bundle{sElems = s, sSize = n} = fromStream (S.indexedR m s) n
zipWithM :: Monad m => (a -> b -> m c) -> Bundle m v a -> Bundle m v b -> Bundle m v c
zipWithM f Bundle{sElems = sa, sSize = na}
Bundle{sElems = sb, sSize = nb} = fromStream (S.zipWithM f sa sb) (smaller na nb)
zipWithM_ :: Monad m => (a -> b -> m c) -> Bundle m v a -> Bundle m v b -> m ()
zipWithM_ f sa sb = S.zipWithM_ f (sElems sa) (sElems sb)
zipWith3M :: Monad m => (a -> b -> c -> m d) -> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
zipWith3M f Bundle{sElems = sa, sSize = na}
Bundle{sElems = sb, sSize = nb}
Bundle{sElems = sc, sSize = nc}
= fromStream (S.zipWith3M f sa sb sc) (smaller na (smaller nb nc))
zipWith4M :: Monad m => (a -> b -> c -> d -> m e)
-> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
-> Bundle m v e
zipWith4M f sa sb sc sd
= zipWithM (\(a,b) (c,d) -> f a b c d) (zip sa sb) (zip sc sd)
zipWith5M :: Monad m => (a -> b -> c -> d -> e -> m f)
-> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
-> Bundle m v e -> Bundle m v f
zipWith5M f sa sb sc sd se
= zipWithM (\(a,b,c) (d,e) -> f a b c d e) (zip3 sa sb sc) (zip sd se)
zipWith6M :: Monad m => (a -> b -> c -> d -> e -> f -> m g)
-> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
-> Bundle m v e -> Bundle m v f -> Bundle m v g
zipWith6M fn sa sb sc sd se sf
= zipWithM (\(a,b,c) (d,e,f) -> fn a b c d e f) (zip3 sa sb sc)
(zip3 sd se sf)
zipWith :: Monad m => (a -> b -> c) -> Bundle m v a -> Bundle m v b -> Bundle m v c
zipWith f = zipWithM (\a b -> return (f a b))
zipWith3 :: Monad m => (a -> b -> c -> d)
-> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
zipWith3 f = zipWith3M (\a b c -> return (f a b c))
zipWith4 :: Monad m => (a -> b -> c -> d -> e)
-> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
-> Bundle m v e
zipWith4 f = zipWith4M (\a b c d -> return (f a b c d))
zipWith5 :: Monad m => (a -> b -> c -> d -> e -> f)
-> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
-> Bundle m v e -> Bundle m v f
zipWith5 f = zipWith5M (\a b c d e -> return (f a b c d e))
zipWith6 :: Monad m => (a -> b -> c -> d -> e -> f -> g)
-> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
-> Bundle m v e -> Bundle m v f -> Bundle m v g
zipWith6 fn = zipWith6M (\a b c d e f -> return (fn a b c d e f))
zip :: Monad m => Bundle m v a -> Bundle m v b -> Bundle m v (a,b)
zip = zipWith (,)
zip3 :: Monad m => Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v (a,b,c)
zip3 = zipWith3 (,,)
zip4 :: Monad m => Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
-> Bundle m v (a,b,c,d)
zip4 = zipWith4 (,,,)
zip5 :: Monad m => Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
-> Bundle m v e -> Bundle m v (a,b,c,d,e)
zip5 = zipWith5 (,,,,)
zip6 :: Monad m => Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
-> Bundle m v e -> Bundle m v f -> Bundle m v (a,b,c,d,e,f)
zip6 = zipWith6 (,,,,,)
eq :: (Monad m, Eq a) => Bundle m v a -> Bundle m v a -> m Bool
eq x y = sElems x `S.eq` sElems y
cmp :: (Monad m, Ord a) => Bundle m v a -> Bundle m v a -> m Ordering
cmp x y = sElems x `S.cmp` sElems y
filter :: Monad m => (a -> Bool) -> Bundle m v a -> Bundle m v a
filter f = filterM (return . f)
filterM :: Monad m => (a -> m Bool) -> Bundle m v a -> Bundle m v a
filterM f Bundle{sElems = s, sSize = n} = fromStream (S.filterM f s) (toMax n)
takeWhile :: Monad m => (a -> Bool) -> Bundle m v a -> Bundle m v a
takeWhile f = takeWhileM (return . f)
takeWhileM :: Monad m => (a -> m Bool) -> Bundle m v a -> Bundle m v a
takeWhileM f Bundle{sElems = s, sSize = n} = fromStream (S.takeWhileM f s) (toMax n)
dropWhile :: Monad m => (a -> Bool) -> Bundle m v a -> Bundle m v a
dropWhile f = dropWhileM (return . f)
dropWhileM :: Monad m => (a -> m Bool) -> Bundle m v a -> Bundle m v a
dropWhileM f Bundle{sElems = s, sSize = n} = fromStream (S.dropWhileM f s) (toMax n)
infix 4 `elem`
elem :: (Monad m, Eq a) => a -> Bundle m v a -> m Bool
elem x = S.elem x . sElems
infix 4 `notElem`
notElem :: (Monad m, Eq a) => a -> Bundle m v a -> m Bool
notElem x = S.notElem x . sElems
find :: Monad m => (a -> Bool) -> Bundle m v a -> m (Maybe a)
find f = findM (return . f)
findM :: Monad m => (a -> m Bool) -> Bundle m v a -> m (Maybe a)
findM f = S.findM f . sElems
findIndex :: Monad m => (a -> Bool) -> Bundle m v a -> m (Maybe Int)
findIndex f = findIndexM (return . f)
findIndexM :: Monad m => (a -> m Bool) -> Bundle m v a -> m (Maybe Int)
findIndexM f = S.findIndexM f . sElems
foldl :: Monad m => (a -> b -> a) -> a -> Bundle m v b -> m a
foldl f = foldlM (\a b -> return (f a b))
foldlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> m a
foldlM m z = S.foldlM m z . sElems
foldM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> m a
foldM = foldlM
foldl1 :: Monad m => (a -> a -> a) -> Bundle m v a -> m a
foldl1 f = foldl1M (\a b -> return (f a b))
foldl1M :: Monad m => (a -> a -> m a) -> Bundle m v a -> m a
foldl1M f = S.foldl1M f . sElems
fold1M :: Monad m => (a -> a -> m a) -> Bundle m v a -> m a
fold1M = foldl1M
foldl' :: Monad m => (a -> b -> a) -> a -> Bundle m v b -> m a
foldl' f = foldlM' (\a b -> return (f a b))
foldlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> m a
foldlM' m z = S.foldlM' m z . sElems
foldM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> m a
foldM' = foldlM'
foldl1' :: Monad m => (a -> a -> a) -> Bundle m v a -> m a
foldl1' f = foldl1M' (\a b -> return (f a b))
foldl1M' :: Monad m => (a -> a -> m a) -> Bundle m v a -> m a
foldl1M' f = S.foldl1M' f . sElems
fold1M' :: Monad m => (a -> a -> m a) -> Bundle m v a -> m a
fold1M' = foldl1M'
foldr :: Monad m => (a -> b -> b) -> b -> Bundle m v a -> m b
foldr f = foldrM (\a b -> return (f a b))
foldrM :: Monad m => (a -> b -> m b) -> b -> Bundle m v a -> m b
foldrM f z = S.foldrM f z . sElems
foldr1 :: Monad m => (a -> a -> a) -> Bundle m v a -> m a
foldr1 f = foldr1M (\a b -> return (f a b))
foldr1M :: Monad m => (a -> a -> m a) -> Bundle m v a -> m a
foldr1M f = S.foldr1M f . sElems
and :: Monad m => Bundle m v Bool -> m Bool
and = S.and . sElems
or :: Monad m => Bundle m v Bool -> m Bool
or = S.or . sElems
concatMap :: Monad m => (a -> Bundle m v b) -> Bundle m v a -> Bundle m v b
concatMap f = concatMapM (return . f)
concatMapM :: Monad m => (a -> m (Bundle m v b)) -> Bundle m v a -> Bundle m v b
concatMapM f Bundle{sElems = s} = fromStream (S.concatMapM (liftM sElems . f) s) Unknown
flatten :: Monad m => (a -> m s) -> (s -> m (Step s b)) -> Size
-> Bundle m v a -> Bundle m v b
flatten mk istep sz Bundle{sElems = s} = fromStream (S.flatten mk istep s) sz
unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Bundle m u a
unfoldr f = unfoldrM (return . f)
unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Bundle m u a
unfoldrM f s = fromStream (S.unfoldrM f s) Unknown
unfoldrN :: Monad m => Int -> (s -> Maybe (a, s)) -> s -> Bundle m u a
unfoldrN n f = unfoldrNM n (return . f)
unfoldrNM :: Monad m => Int -> (s -> m (Maybe (a, s))) -> s -> Bundle m u a
unfoldrNM n f s = fromStream (S.unfoldrNM n f s) (Max (delay_inline max n 0))
iterateNM :: Monad m => Int -> (a -> m a) -> a -> Bundle m u a
iterateNM n f x0 = fromStream (S.iterateNM n f x0) (Exact (delay_inline max n 0))
iterateN :: Monad m => Int -> (a -> a) -> a -> Bundle m u a
iterateN n f x0 = iterateNM n (return . f) x0
prescanl :: Monad m => (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
prescanl f = prescanlM (\a b -> return (f a b))
prescanlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
prescanlM f z Bundle{sElems = s, sSize = sz} = fromStream (S.prescanlM f z s) sz
prescanl' :: Monad m => (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
prescanl' f = prescanlM' (\a b -> return (f a b))
prescanlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
prescanlM' f z Bundle{sElems = s, sSize = sz} = fromStream (S.prescanlM' f z s) sz
postscanl :: Monad m => (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
postscanl f = postscanlM (\a b -> return (f a b))
postscanlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
postscanlM f z Bundle{sElems = s, sSize = sz} = fromStream (S.postscanlM f z s) sz
postscanl' :: Monad m => (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
postscanl' f = postscanlM' (\a b -> return (f a b))
postscanlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
postscanlM' f z Bundle{sElems = s, sSize = sz} = fromStream (S.postscanlM' f z s) sz
scanl :: Monad m => (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
scanl f = scanlM (\a b -> return (f a b))
scanlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
scanlM f z s = z `cons` postscanlM f z s
scanl' :: Monad m => (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
scanl' f = scanlM' (\a b -> return (f a b))
scanlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
scanlM' f z s = z `seq` (z `cons` postscanlM f z s)
scanl1 :: Monad m => (a -> a -> a) -> Bundle m v a -> Bundle m v a
scanl1 f = scanl1M (\x y -> return (f x y))
scanl1M :: Monad m => (a -> a -> m a) -> Bundle m v a -> Bundle m v a
scanl1M f Bundle{sElems = s, sSize = sz} = fromStream (S.scanl1M f s) sz
scanl1' :: Monad m => (a -> a -> a) -> Bundle m v a -> Bundle m v a
scanl1' f = scanl1M' (\x y -> return (f x y))
scanl1M' :: Monad m => (a -> a -> m a) -> Bundle m v a -> Bundle m v a
scanl1M' f Bundle{sElems = s, sSize = sz} = fromStream (S.scanl1M' f s) sz
enumFromStepN :: (Num a, Monad m) => a -> a -> Int -> Bundle m v a
enumFromStepN x y n = fromStream (S.enumFromStepN x y n) (Exact (delay_inline max n 0))
enumFromTo :: (Enum a, Monad m) => a -> a -> Bundle m v a
enumFromTo x y = fromList [x .. y]
enumFromTo_small :: (Integral a, Monad m) => a -> a -> Bundle m v a
enumFromTo_small x y = x `seq` y `seq` fromStream (Stream step x) (Exact n)
where
n = delay_inline max (fromIntegral y fromIntegral x + 1) 0
step z | z <= y = return $ Yield z (z+1)
| otherwise = return $ Done
#if WORD_SIZE_IN_BITS > 32
#endif
enumFromTo_int :: forall m v. Monad m => Int -> Int -> Bundle m v Int
enumFromTo_int x y = x `seq` y `seq` fromStream (Stream step x) (Exact (len x y))
where
len :: Int -> Int -> Int
len u v | u > v = 0
| otherwise = BOUNDS_CHECK(check) "enumFromTo" "vector too large"
(n > 0)
$ n
where
n = vu+1
step z | z <= y = return $ Yield z (z+1)
| otherwise = return $ Done
enumFromTo_intlike :: (Integral a, Monad m) => a -> a -> Bundle m v a
enumFromTo_intlike x y = x `seq` y `seq` fromStream (Stream step x) (Exact (len x y))
where
len u v | u > v = 0
| otherwise = BOUNDS_CHECK(check) "enumFromTo" "vector too large"
(n > 0)
$ fromIntegral n
where
n = vu+1
step z | z <= y = return $ Yield z (z+1)
| otherwise = return $ Done
#else
"enumFromTo<Int32> [Bundle]"
enumFromTo = enumFromTo_intlike :: Monad m => Int32 -> Int32 -> Bundle m v Int32 #-}
#endif
enumFromTo_big_word :: (Integral a, Monad m) => a -> a -> Bundle m v a
enumFromTo_big_word x y = x `seq` y `seq` fromStream (Stream step x) (Exact (len x y))
where
len u v | u > v = 0
| otherwise = BOUNDS_CHECK(check) "enumFromTo" "vector too large"
(n < fromIntegral (maxBound :: Int))
$ fromIntegral (n+1)
where
n = vu
step z | z <= y = return $ Yield z (z+1)
| otherwise = return $ Done
enumFromTo_big_int :: (Integral a, Monad m) => a -> a -> Bundle m v a
enumFromTo_big_int x y = x `seq` y `seq` fromStream (Stream step x) (Exact (len x y))
where
len u v | u > v = 0
| otherwise = BOUNDS_CHECK(check) "enumFromTo" "vector too large"
(n > 0 && n <= fromIntegral (maxBound :: Int))
$ fromIntegral n
where
n = vu+1
step z | z <= y = return $ Yield z (z+1)
| otherwise = return $ Done
#if WORD_SIZE_IN_BITS > 32
#endif
enumFromTo_char :: Monad m => Char -> Char -> Bundle m v Char
enumFromTo_char x y = x `seq` y `seq` fromStream (Stream step xn) (Exact n)
where
xn = ord x
yn = ord y
n = delay_inline max 0 (yn xn + 1)
step zn | zn <= yn = return $ Yield (unsafeChr zn) (zn+1)
| otherwise = return $ Done
enumFromTo_double :: (Monad m, Ord a, RealFrac a) => a -> a -> Bundle m v a
enumFromTo_double n m = n `seq` m `seq` fromStream (Stream step n) (Max (len n lim))
where
lim = m + 1/2
len x y | x > y = 0
| otherwise = BOUNDS_CHECK(check) "enumFromTo" "vector too large"
(l > 0)
$ fromIntegral l
where
l :: Integer
l = truncate (yx)+2
step x | x <= lim = return $ Yield x (x+1)
| otherwise = return $ Done
enumFromThenTo :: (Enum a, Monad m) => a -> a -> a -> Bundle m v a
enumFromThenTo x y z = fromList [x, y .. z]
toList :: Monad m => Bundle m v a -> m [a]
toList = foldr (:) []
fromList :: Monad m => [a] -> Bundle m v a
fromList xs = unsafeFromList Unknown xs
fromListN :: Monad m => Int -> [a] -> Bundle m v a
fromListN n xs = fromStream (S.fromListN n xs) (Max (delay_inline max n 0))
unsafeFromList :: Monad m => Size -> [a] -> Bundle m v a
unsafeFromList sz xs = fromStream (S.fromList xs) sz
fromVector :: (Monad m, Vector v a) => v a -> Bundle m v a
fromVector v = v `seq` n `seq` Bundle (Stream step 0)
(Stream vstep True)
(Just v)
(Exact n)
where
n = basicLength v
step i | i >= n = return Done
| otherwise = case basicUnsafeIndexM v i of
Box x -> return $ Yield x (i+1)
vstep True = return (Yield (Chunk (basicLength v) (\mv -> basicUnsafeCopy mv v)) False)
vstep False = return Done
fromVectors :: forall m v a. (Monad m, Vector v a) => [v a] -> Bundle m v a
fromVectors us = Bundle (Stream pstep (Left us))
(Stream vstep us)
Nothing
(Exact n)
where
n = List.foldl' (\k v -> k + basicLength v) 0 us
pstep (Left []) = return Done
pstep (Left (v:vs)) = basicLength v `seq` return (Skip (Right (v,0,vs)))
pstep (Right (v,i,vs))
| i >= basicLength v = return $ Skip (Left vs)
| otherwise = case basicUnsafeIndexM v i of
Box x -> return $ Yield x (Right (v,i+1,vs))
vstep :: [v a] -> m (Step [v a] (Chunk v a))
vstep [] = return Done
vstep (v:vs) = return $ Yield (Chunk (basicLength v)
(\mv -> INTERNAL_CHECK(check) "concatVectors" "length mismatch"
(M.basicLength mv == basicLength v)
$ basicUnsafeCopy mv v)) vs
concatVectors :: (Monad m, Vector v a) => Bundle m u (v a) -> Bundle m v a
concatVectors Bundle{sElems = Stream step t}
= Bundle (Stream pstep (Left t))
(Stream vstep t)
Nothing
Unknown
where
pstep (Left s) = do
r <- step s
case r of
Yield v s' -> basicLength v `seq` return (Skip (Right (v,0,s')))
Skip s' -> return (Skip (Left s'))
Done -> return Done
pstep (Right (v,i,s))
| i >= basicLength v = return (Skip (Left s))
| otherwise = case basicUnsafeIndexM v i of
Box x -> return (Yield x (Right (v,i+1,s)))
vstep s = do
r <- step s
case r of
Yield v s' -> return (Yield (Chunk (basicLength v)
(\mv -> INTERNAL_CHECK(check) "concatVectors" "length mismatch"
(M.basicLength mv == basicLength v)
$ basicUnsafeCopy mv v)) s')
Skip s' -> return (Skip s')
Done -> return Done
reVector :: Monad m => Bundle m u a -> Bundle m v a
reVector Bundle{sElems = s, sSize = n} = fromStream s n