module Data.Vector.Generic.New (
New(..), create, run, runPrim, apply, modify, modifyWithStream,
unstream, transform, unstreamR, transformR,
slice, init, tail, take, drop,
unsafeSlice, unsafeInit, unsafeTail
) where
import qualified Data.Vector.Generic.Mutable as MVector
import Data.Vector.Generic.Mutable ( MVector )
import Data.Vector.Generic.Base ( Vector, Mutable )
import Data.Vector.Fusion.Stream ( Stream, MStream )
import qualified Data.Vector.Fusion.Stream as Stream
import Control.Monad.Primitive
import Control.Monad.ST ( ST )
import Control.Monad ( liftM )
import Prelude hiding ( init, tail, take, drop, reverse, map, filter )
#include "vector.h"
data New v a = New (forall s. ST s (Mutable v s a))
create :: (forall s. ST s (Mutable v s a)) -> New v a
create p = New p
run :: New v a -> ST s (Mutable v s a)
run (New p) = p
runPrim :: PrimMonad m => New v a -> m (Mutable v (PrimState m) a)
runPrim (New p) = primToPrim p
apply :: (forall s. Mutable v s a -> Mutable v s a) -> New v a -> New v a
apply f (New p) = New (liftM f p)
modify :: (forall s. Mutable v s a -> ST s ()) -> New v a -> New v a
modify f (New p) = New (do { v <- p; f v; return v })
modifyWithStream :: (forall s. Mutable v s a -> Stream b -> ST s ())
-> New v a -> Stream b -> New v a
modifyWithStream f (New p) s = s `seq` New (do { v <- p; f v s; return v })
unstream :: Vector v a => Stream a -> New v a
unstream s = s `seq` New (MVector.unstream s)
transform :: Vector v a =>
(forall m. Monad m => MStream m a -> MStream m a) -> New v a -> New v a
transform f (New p) = New (MVector.transform f =<< p)
unstreamR :: Vector v a => Stream a -> New v a
unstreamR s = s `seq` New (MVector.unstreamR s)
transformR :: Vector v a =>
(forall m. Monad m => MStream m a -> MStream m a) -> New v a -> New v a
transformR f (New p) = New (MVector.transformR f =<< p)
slice :: Vector v a => Int -> Int -> New v a -> New v a
slice i n m = apply (MVector.slice i n) m
init :: Vector v a => New v a -> New v a
init m = apply MVector.init m
tail :: Vector v a => New v a -> New v a
tail m = apply MVector.tail m
take :: Vector v a => Int -> New v a -> New v a
take n m = apply (MVector.take n) m
drop :: Vector v a => Int -> New v a -> New v a
drop n m = apply (MVector.drop n) m
unsafeSlice :: Vector v a => Int -> Int -> New v a -> New v a
unsafeSlice i n m = apply (MVector.unsafeSlice i n) m
unsafeInit :: Vector v a => New v a -> New v a
unsafeInit m = apply MVector.unsafeInit m
unsafeTail :: Vector v a => New v a -> New v a
unsafeTail m = apply MVector.unsafeTail m