summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2011-07-10 17:31:24 +0000
committerAlberto Ruiz <aruiz@um.es>2011-07-10 17:31:24 +0000
commit2cb2293eeb617baa404f444944bb4613c645133a (patch)
tree8506bf7fe33d640bdb859b80bae5bc30d254f5d6 /lib/Data/Packed/Internal
parentb48c876b4b9ba37e45104fc7b54d8024c6bd1eb5 (diff)
pure mapVectorWithIndex
Diffstat (limited to 'lib/Data/Packed/Internal')
-rw-r--r--lib/Data/Packed/Internal/Vector.hs17
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index 078cb36..8f403f4 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -17,7 +17,7 @@ module Data.Packed.Internal.Vector (
17 Vector, dim, 17 Vector, dim,
18 fromList, toList, (|>), 18 fromList, toList, (|>),
19 join, (@>), safe, at, at', subVector, takesV, 19 join, (@>), safe, at, at', subVector, takesV,
20 mapVector, zipVectorWith, unzipVectorWith, 20 mapVector, mapVectorWithIndex, zipVectorWith, unzipVectorWith,
21 mapVectorM, mapVectorM_, mapVectorWithIndexM, mapVectorWithIndexM_, 21 mapVectorM, mapVectorM_, mapVectorWithIndexM, mapVectorWithIndexM_,
22 foldVector, foldVectorG, foldLoop, foldVectorWithIndex, 22 foldVector, foldVectorG, foldLoop, foldVectorWithIndex,
23 createVector, vec, 23 createVector, vec,
@@ -497,6 +497,21 @@ mapVectorWithIndexM_ f v = do
497 mapVectorM' (k+1) t 497 mapVectorM' (k+1) t
498{-# INLINE mapVectorWithIndexM_ #-} 498{-# INLINE mapVectorWithIndexM_ #-}
499 499
500
501mapVectorWithIndex :: (Storable a, Storable b) => (Int -> a -> b) -> Vector a -> Vector b
502--mapVectorWithIndex g = head . mapVectorWithIndexM (\a b -> [g a b])
503mapVectorWithIndex f v = unsafePerformIO $ do
504 w <- createVector (dim v)
505 unsafeWith v $ \p ->
506 unsafeWith w $ \q -> do
507 let go (-1) = return ()
508 go !k = do x <- peekElemOff p k
509 pokeElemOff q k (f k x)
510 go (k-1)
511 go (dim v -1)
512 return w
513{-# INLINE mapVectorWithIndex #-}
514
500------------------------------------------------------------------- 515-------------------------------------------------------------------
501 516
502 517