summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal/Vector.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Packed/Internal/Vector.hs')
-rw-r--r--lib/Data/Packed/Internal/Vector.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index 06db806..6d39c6e 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -18,6 +18,7 @@ module Data.Packed.Internal.Vector (
18 fromList, toList, (|>), 18 fromList, toList, (|>),
19 join, (@>), safe, at, at', subVector, takesV, 19 join, (@>), safe, at, at', subVector, takesV,
20 mapVector, zipVector, unzipVectorWith, 20 mapVector, zipVector, unzipVectorWith,
21 mapVectorM, mapVectorM_,
21 foldVector, foldVectorG, foldLoop, 22 foldVector, foldVectorG, foldLoop,
22 createVector, vec, 23 createVector, vec,
23 asComplex, asReal, 24 asComplex, asReal,
@@ -35,6 +36,7 @@ import Foreign.C.String
35import Foreign.C.Types(CInt,CChar) 36import Foreign.C.Types(CInt,CChar)
36import Data.Complex 37import Data.Complex
37import Control.Monad(when) 38import Control.Monad(when)
39import Control.Monad.Trans
38 40
39#if __GLASGOW_HASKELL__ >= 605 41#if __GLASGOW_HASKELL__ >= 605
40import GHC.ForeignPtr (mallocPlainForeignPtrBytes) 42import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
@@ -357,6 +359,39 @@ foldVectorG f s0 v = foldLoop g s0 (dim v)
357 359
358------------------------------------------------------------------- 360-------------------------------------------------------------------
359 361
362-- | monadic map over Vectors
363mapVectorM :: (Storable a, Storable b, MonadIO m) => (a -> m b) -> Vector a -> m (Vector b)
364mapVectorM f v = do
365 w <- liftIO $ createVector (dim v)
366 mapVectorM' f v w (dim v -1)
367 return w
368 where mapVectorM' f' v' w' 0 = do
369 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p 0
370 y <- f' x
371 liftIO $ unsafeWith w' $ \q -> pokeElemOff q 0 y
372 mapVectorM' f' v' w' !k = do
373 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k
374 y <- f' x
375 liftIO $ unsafeWith w' $ \q -> pokeElemOff q k y
376 mapVectorM' f' v' w' (k-1)
377{-# INLINE mapVectorM #-}
378
379-- | monadic map over Vectors
380mapVectorM_ :: (Storable a, MonadIO m) => (a -> m ()) -> Vector a -> m ()
381mapVectorM_ f v = do
382 mapVectorM' f v (dim v -1)
383 where mapVectorM' f' v' 0 = do
384 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p 0
385 f' x
386 mapVectorM' f' v' !k = do
387 x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k
388 _ <- f' x
389 mapVectorM' f' v' (k-1)
390{-# INLINE mapVectorM_ #-}
391
392-------------------------------------------------------------------
393
394
360-- | Loads a vector from an ASCII file (the number of elements must be known in advance). 395-- | Loads a vector from an ASCII file (the number of elements must be known in advance).
361fscanfVector :: FilePath -> Int -> IO (Vector Double) 396fscanfVector :: FilePath -> Int -> IO (Vector Double)
362fscanfVector filename n = do 397fscanfVector filename n = do
@@ -398,3 +433,4 @@ fwriteVector filename v = do
398 free charname 433 free charname
399 434
400foreign import ccall "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV 435foreign import ccall "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV
436