summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
authorVivian McPhail <haskell.vivian.mcphail@gmail.com>2010-09-26 20:50:17 +0000
committerVivian McPhail <haskell.vivian.mcphail@gmail.com>2010-09-26 20:50:17 +0000
commitf2f54ac1d76fc391c0e231b4309ae5c4245cd0f9 (patch)
tree43ffdbe03d68384d77cfb3a7637d8912c1317d9b /lib/Data
parent611ff4782c261a6c6e52fe7ed0c122a0eac22691 (diff)
improve monadic maps performance (go functions)
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Packed/Internal/Vector.hs56
1 files changed, 28 insertions, 28 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index a47c376..7360f93 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -390,64 +390,64 @@ foldVectorG f s0 v = foldLoop g s0 (dim v)
390mapVectorM :: (Storable a, Storable b, Monad m) => (a -> m b) -> Vector a -> m (Vector b) 390mapVectorM :: (Storable a, Storable b, Monad m) => (a -> m b) -> Vector a -> m (Vector b)
391mapVectorM f v = do 391mapVectorM f v = do
392 w <- return $! unsafePerformIO $! createVector (dim v) 392 w <- return $! unsafePerformIO $! createVector (dim v)
393 mapVectorM' f v w 0 (dim v -1) 393 mapVectorM' w 0 (dim v -1)
394 return w 394 return w
395 where mapVectorM' f' v' w' !k !t 395 where mapVectorM' w' !k !t
396 | k == t = do 396 | k == t = do
397 x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k 397 x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
398 y <- f' x 398 y <- f x
399 return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y 399 return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y
400 | otherwise = do 400 | otherwise = do
401 x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k 401 x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
402 y <- f' x 402 y <- f x
403 _ <- return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y 403 _ <- return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y
404 mapVectorM' f' v' w' (k+1) t 404 mapVectorM' w' (k+1) t
405{-# INLINE mapVectorM #-} 405{-# INLINE mapVectorM #-}
406 406
407-- | monadic map over Vectors 407-- | monadic map over Vectors
408mapVectorM_ :: (Storable a, Monad m) => (a -> m ()) -> Vector a -> m () 408mapVectorM_ :: (Storable a, Monad m) => (a -> m ()) -> Vector a -> m ()
409mapVectorM_ f v = do 409mapVectorM_ f v = do
410 mapVectorM' f v 0 (dim v -1) 410 mapVectorM' 0 (dim v -1)
411 where mapVectorM' f' v' !k !t 411 where mapVectorM' !k !t
412 | k == t = do 412 | k == t = do
413 x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k 413 x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
414 f' x 414 f x
415 | otherwise = do 415 | otherwise = do
416 x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k 416 x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
417 _ <- f' x 417 _ <- f x
418 mapVectorM' f' v' (k+1) t 418 mapVectorM' (k+1) t
419{-# INLINE mapVectorM_ #-} 419{-# INLINE mapVectorM_ #-}
420 420
421-- | monadic map over Vectors with the zero-indexed index passed to the mapping function 421-- | monadic map over Vectors with the zero-indexed index passed to the mapping function
422mapVectorWithIndexM :: (Storable a, Storable b, Monad m) => (Int -> a -> m b) -> Vector a -> m (Vector b) 422mapVectorWithIndexM :: (Storable a, Storable b, Monad m) => (Int -> a -> m b) -> Vector a -> m (Vector b)
423mapVectorWithIndexM f v = do 423mapVectorWithIndexM f v = do
424 w <- return $! unsafePerformIO $! createVector (dim v) 424 w <- return $! unsafePerformIO $! createVector (dim v)
425 mapVectorM' f v w 0 (dim v -1) 425 mapVectorM' w 0 (dim v -1)
426 return w 426 return w
427 where mapVectorM' f' v' w' !k !t 427 where mapVectorM' w' !k !t
428 | k == t = do 428 | k == t = do
429 x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k 429 x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
430 y <- f' k x 430 y <- f k x
431 return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y 431 return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y
432 | otherwise = do 432 | otherwise = do
433 x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k 433 x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
434 y <- f' k x 434 y <- f k x
435 _ <- return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y 435 _ <- return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y
436 mapVectorM' f' v' w' (k+1) t 436 mapVectorM' w' (k+1) t
437{-# INLINE mapVectorWithIndexM #-} 437{-# INLINE mapVectorWithIndexM #-}
438 438
439-- | monadic map over Vectors with the zero-indexed index passed to the mapping function 439-- | monadic map over Vectors with the zero-indexed index passed to the mapping function
440mapVectorWithIndexM_ :: (Storable a, Monad m) => (Int -> a -> m ()) -> Vector a -> m () 440mapVectorWithIndexM_ :: (Storable a, Monad m) => (Int -> a -> m ()) -> Vector a -> m ()
441mapVectorWithIndexM_ f v = do 441mapVectorWithIndexM_ f v = do
442 mapVectorM' f v 0 (dim v -1) 442 mapVectorM' 0 (dim v -1)
443 where mapVectorM' f' v' !k !t 443 where mapVectorM' !k !t
444 | k == t = do 444 | k == t = do
445 x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k 445 x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
446 f' k x 446 f k x
447 | otherwise = do 447 | otherwise = do
448 x <- return $! inlinePerformIO $! unsafeWith v' $! \p -> peekElemOff p k 448 x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
449 _ <- f' k x 449 _ <- f k x
450 mapVectorM' f' v' (k+1) t 450 mapVectorM' (k+1) t
451{-# INLINE mapVectorWithIndexM_ #-} 451{-# INLINE mapVectorWithIndexM_ #-}
452 452
453------------------------------------------------------------------- 453-------------------------------------------------------------------