From: Roman Leshchinskiy Date: Sat, 12 Jul 2008 09:29:57 +0000 (+0000) Subject: Declarative combinators for manipulating MVectors with support for fusion X-Git-Tag: 0_1~11 X-Git-Url: http://git.haskell.org/darcs-mirrors/vector.git/commitdiff_plain/fb8c2bc807c114a5e6528cc9322e3b6fa7e580ac Declarative combinators for manipulating MVectors with support for fusion --- diff --git a/Data/Vector/IVector.hs b/Data/Vector/IVector.hs index 89b4e7a..fa668e9 100644 --- a/Data/Vector/IVector.hs +++ b/Data/Vector/IVector.hs @@ -60,6 +60,9 @@ module Data.Vector.IVector ( import qualified Data.Vector.MVector as MVector import Data.Vector.MVector ( MVector ) +import qualified Data.Vector.MVector.Mut as Mut +import Data.Vector.MVector.Mut ( Mut ) + import qualified Data.Vector.Stream as Stream import Data.Vector.Stream ( Stream ) import Data.Vector.Stream.Size @@ -112,9 +115,9 @@ class IVector v a where -- ------ -- | Construct a pure vector from a monadic initialiser -new :: IVector v a => (forall mv m. MVector mv m a => m (mv a)) -> v a +new :: IVector v a => Mut a -> v a {-# INLINE_STREAM new #-} -new init = vnew init +new m = vnew (Mut.run m) -- | Convert a vector to a 'Stream' stream :: IVector v a => v a -> Stream a @@ -130,13 +133,16 @@ stream v = v `seq` (Stream.unfold get 0 `Stream.sized` Exact n) -- | Create a vector from a 'Stream' unstream :: IVector v a => Stream a -> v a {-# INLINE_STREAM unstream #-} -unstream s = new (MVector.unstream s) +unstream s = new (Mut.unstream s) {-# RULES "stream/unstream [IVector]" forall s. stream (unstream s) = s +"Mut.unstream/stream/new [IVector]" forall p. + Mut.unstream (stream (new p)) = p + #-} -- Length diff --git a/Data/Vector/MVector.hs b/Data/Vector/MVector.hs index b26d0c3..ef90355 100644 --- a/Data/Vector/MVector.hs +++ b/Data/Vector/MVector.hs @@ -11,6 +11,8 @@ -- Generic interface to mutable vectors -- +#include "phases.h" + module Data.Vector.MVector ( MVectorPure(..), MVector(..), @@ -164,7 +166,7 @@ grow v by = assert (by >= 0) -- The vector will grow logarithmically if the 'Size' hint of the 'Stream' is -- inexact. unstream :: MVector v m a => Stream a -> m (v a) -{-# INLINE unstream #-} +{-# INLINE_STREAM unstream #-} unstream s = case upperBound (Stream.size s) of Just n -> unstreamMax s n Nothing -> unstreamUnknown s diff --git a/Data/Vector/MVector/Mut.hs b/Data/Vector/MVector/Mut.hs new file mode 100644 index 0000000..0918e3c --- /dev/null +++ b/Data/Vector/MVector/Mut.hs @@ -0,0 +1,27 @@ +{-# LANGUAGE Rank2Types #-} + +#include "phases.h" + +module Data.Vector.MVector.Mut ( + Mut(..), run, unstream, update +) where + +import qualified Data.Vector.MVector as MVector +import Data.Vector.MVector ( MVector ) + +import Data.Vector.Stream ( Stream ) + +data Mut a = Mut (forall m mv. MVector mv m a => m (mv a)) + +run :: MVector mv m a => Mut a -> m (mv a) +{-# INLINE run #-} +run (Mut p) = p + +unstream :: Stream a -> Mut a +{-# INLINE_STREAM unstream #-} +unstream s = Mut (MVector.unstream s) + +update :: Mut a -> Stream (Int, a) -> Mut a +{-# INLINE_STREAM update #-} +update (Mut p) s = Mut (do { v <- p; MVector.update v s; return v }) + diff --git a/vector.cabal b/vector.cabal index 79012c3..5a5bff0 100644 --- a/vector.cabal +++ b/vector.cabal @@ -17,6 +17,7 @@ Library Data.Vector.Stream Data.Vector.MVector + Data.Vector.MVector.Mut Data.Vector.IVector Data.Vector.Unboxed.Unbox