{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Data.Massiv.Array.Ops.Map
( map
, imap
, mapM_
, forM_
, imapM_
, iforM_
, mapP_
, imapP_
, zip
, zip3
, unzip
, unzip3
, zipWith
, zipWith3
, izipWith
, izipWith3
, liftArray2
) where
import Control.Monad (void, when)
import Data.Massiv.Array.Delayed.Internal
import Data.Massiv.Core.Common
import Data.Massiv.Core.Scheduler
import Prelude hiding (map, mapM, mapM_,
unzip, unzip3, zip, zip3,
zipWith, zipWith3)
map :: Source r ix e' => (e' -> e) -> Array r ix e' -> Array D ix e
map f = imap (const f)
{-# INLINE map #-}
imap :: Source r ix e' => (ix -> e' -> e) -> Array r ix e' -> Array D ix e
imap f !arr = DArray (getComp arr) (size arr) (\ !ix -> f ix (unsafeIndex arr ix))
{-# INLINE imap #-}
zip :: (Source r1 ix e1, Source r2 ix e2)
=> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix (e1, e2)
zip = zipWith (,)
{-# INLINE zip #-}
zip3 :: (Source r1 ix e1, Source r2 ix e2, Source r3 ix e3)
=> Array r1 ix e1 -> Array r2 ix e2 -> Array r3 ix e3 -> Array D ix (e1, e2, e3)
zip3 = zipWith3 (,,)
{-# INLINE zip3 #-}
unzip :: Source r ix (e1, e2) => Array r ix (e1, e2) -> (Array D ix e1, Array D ix e2)
unzip arr = (map fst arr, map snd arr)
{-# INLINE unzip #-}
unzip3 :: Source r ix (e1, e2, e3)
=> Array r ix (e1, e2, e3) -> (Array D ix e1, Array D ix e2, Array D ix e3)
unzip3 arr = (map (\ (e, _, _) -> e) arr, map (\ (_, e, _) -> e) arr, map (\ (_, _, e) -> e) arr)
{-# INLINE unzip3 #-}
zipWith :: (Source r1 ix e1, Source r2 ix e2)
=> (e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
zipWith f = izipWith (\ _ e1 e2 -> f e1 e2)
{-# INLINE zipWith #-}
izipWith :: (Source r1 ix e1, Source r2 ix e2)
=> (ix -> e1 -> e2 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array D ix e
izipWith f arr1 arr2 =
DArray (getComp arr1) (liftIndex2 min (size arr1) (size arr1)) $ \ !ix ->
f ix (unsafeIndex arr1 ix) (unsafeIndex arr2 ix)
{-# INLINE izipWith #-}
zipWith3 :: (Source r1 ix e1, Source r2 ix e2, Source r3 ix e3)
=> (e1 -> e2 -> e3 -> e) -> Array r1 ix e1 -> Array r2 ix e2 -> Array r3 ix e3 -> Array D ix e
zipWith3 f = izipWith3 (\ _ e1 e2 e3 -> f e1 e2 e3)
{-# INLINE zipWith3 #-}
izipWith3
:: (Source r1 ix e1, Source r2 ix e2, Source r3 ix e3)
=> (ix -> e1 -> e2 -> e3 -> e)
-> Array r1 ix e1
-> Array r2 ix e2
-> Array r3 ix e3
-> Array D ix e
izipWith3 f arr1 arr2 arr3 =
DArray
(getComp arr1)
(liftIndex2 min (liftIndex2 min (size arr1) (size arr1)) (size arr3)) $ \ !ix ->
f ix (unsafeIndex arr1 ix) (unsafeIndex arr2 ix) (unsafeIndex arr3 ix)
{-# INLINE izipWith3 #-}
mapM_ :: (Source r ix a, Monad m) => (a -> m b) -> Array r ix a -> m ()
mapM_ f !arr = iterM_ zeroIndex (size arr) (pureIndex 1) (<) (f . unsafeIndex arr)
{-# INLINE mapM_ #-}
forM_ :: (Source r ix a, Monad m) => Array r ix a -> (a -> m b) -> m ()
forM_ = flip mapM_
{-# INLINE forM_ #-}
iforM_ :: (Source r ix a, Monad m) => Array r ix a -> (ix -> a -> m b) -> m ()
iforM_ = flip imapM_
{-# INLINE iforM_ #-}
mapP_ :: Source r ix a => (a -> IO b) -> Array r ix a -> IO ()
mapP_ f = imapP_ (const f)
{-# INLINE mapP_ #-}
imapP_ :: Source r ix a => (ix -> a -> IO b) -> Array r ix a -> IO ()
imapP_ f arr = do
let sz = size arr
wIds =
case getComp arr of
ParOn ids -> ids
_ -> []
divideWork_ wIds sz $ \ !scheduler !chunkLength !totalLength !slackStart -> do
loopM_ 0 (< slackStart) (+ chunkLength) $ \ !start ->
scheduleWork scheduler $
iterLinearM_ sz start (start + chunkLength) 1 (<) $ \ !i ix -> do
void $ f ix (unsafeLinearIndex arr i)
when (slackStart < totalLength) $
scheduleWork scheduler $
iterLinearM_ sz slackStart totalLength 1 (<) $ \ !i ix -> do
void $ f ix (unsafeLinearIndex arr i)
{-# INLINE imapP_ #-}