module Data.Vector.Generic.New (
New(..), create, run, runPrim, apply, modify, modifyWithBundle,
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.Base ( Vector, Mutable )
import Data.Vector.Fusion.Bundle ( Bundle )
import qualified Data.Vector.Fusion.Bundle as Bundle
import Data.Vector.Fusion.Stream.Monadic ( Stream )
import Data.Vector.Fusion.Bundle.Size
import Control.Monad.Primitive
import Control.Monad.ST ( ST )
import Control.Monad ( liftM )
import Prelude hiding ( init, tail, take, drop, reverse, map, filter )
#define NOT_VECTOR_MODULE
#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 })
modifyWithBundle :: (forall s. Mutable v s a -> Bundle u b -> ST s ())
-> New v a -> Bundle u b -> New v a
modifyWithBundle f (New p) s = s `seq` New (do { v <- p; f v s; return v })
unstream :: Vector v a => Bundle v a -> New v a
unstream s = s `seq` New (MVector.vunstream s)
transform
:: Vector v a => (forall m. Monad m => Stream m a -> Stream m a)
-> (Size -> Size) -> New v a -> New v a
transform f _ (New p) = New (MVector.transform f =<< p)
unstreamR :: Vector v a => Bundle v a -> New v a
unstreamR s = s `seq` New (MVector.unstreamR s)
transformR
:: Vector v a => (forall m. Monad m => Stream m a -> Stream m a)
-> (Size -> Size) -> 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