module Data.Vector.MVector (
MVectorPure(..), MVector(..),
- slice, new, newWith, read, write, copy, grow, unstream, map, update
+ slice, new, newWith, read, write, copy, grow, unstream, update, reverse, map
) where
import qualified Data.Vector.Stream as Stream
double2Int, int2Double
)
-import Prelude hiding ( length, map, read )
+import Prelude hiding ( length, reverse, map, read )
gROWTH_FACTOR :: Double
gROWTH_FACTOR = 1.5
. double2Int
$ int2Double (length v) * gROWTH_FACTOR
+update :: MVector v m a => v a -> Stream (Int, a) -> m ()
+{-# INLINE update #-}
+update v s = Stream.mapM_ put s
+ where
+ {-# INLINE put #-}
+ put (i, x) = write v i x
+
+reverse :: MVector v m a => v a -> m ()
+{-# INLINE reverse #-}
+reverse v = reverse_loop 0 (length v - 1)
+ where
+ reverse_loop i j | i < j = do
+ x <- unsafeRead v i
+ y <- unsafeRead v j
+ unsafeWrite v i y
+ unsafeWrite v j x
+ reverse_loop _ _ = return ()
+
+
map :: MVector v m a => (a -> a) -> v a -> m ()
{-# INLINE map #-}
map f v = map_loop 0
write v i (f x)
| otherwise = return ()
-update :: MVector v m a => v a -> Stream (Int, a) -> m ()
-{-# INLINE update #-}
-update v s = Stream.mapM_ put s
- where
- {-# INLINE put #-}
- put (i, x) = write v i x
#include "phases.h"
module Data.Vector.MVector.Mut (
- Mut(..), run, unstream, map, update
+ Mut(..), run, unstream, update, reverse, map
) where
import qualified Data.Vector.MVector as MVector
import Data.Vector.Stream ( Stream )
-import Prelude hiding ( map )
+import Prelude hiding ( reverse, map )
data Mut a = Mut (forall m mv. MVector mv m a => m (mv a))
{-# INLINE run #-}
run (Mut p) = p
+trans :: Mut a -> (forall m mv. MVector mv m a => mv a -> m ()) -> Mut a
+{-# INLINE trans #-}
+trans (Mut p) q = Mut (do { v <- p; q v; return v })
+
unstream :: Stream a -> Mut a
{-# INLINE_STREAM unstream #-}
unstream s = Mut (MVector.unstream s)
-map :: (a -> a) -> Mut a -> Mut a
-{-# INLINE_STREAM map #-}
-map f (Mut p) = Mut (do { v <- p; MVector.map f v; return v })
-
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 })
+update m s = trans m (\v -> MVector.update v s)
+
+reverse :: Mut a -> Mut a
+{-# INLINE_STREAM reverse #-}
+reverse m = trans m (MVector.reverse)
+
+map :: (a -> a) -> Mut a -> Mut a
+{-# INLINE_STREAM map #-}
+map f m = trans m (MVector.map f)