module Data.Array.Repa.Eval.Target
( Target (..)
, fromList)
where
import Data.Array.Repa.Base
import Data.Array.Repa.Shape
import Control.Monad
import System.IO.Unsafe
class Target r e where
data MVec r e
newMVec :: Int -> IO (MVec r e)
unsafeWriteMVec :: MVec r e -> Int -> e -> IO ()
unsafeFreezeMVec :: sh -> MVec r e -> IO (Array r sh e)
deepSeqMVec :: MVec r e -> a -> a
touchMVec :: MVec r e -> IO ()
fromList :: (Shape sh, Target r e)
=> sh -> [e] -> Array r sh e
fromList :: sh -> [e] -> Array r sh e
fromList sh
sh [e]
xx
= IO (Array r sh e) -> Array r sh e
forall a. IO a -> a
unsafePerformIO
(IO (Array r sh e) -> Array r sh e)
-> IO (Array r sh e) -> Array r sh e
forall a b. (a -> b) -> a -> b
$ do let len :: Int
len = [e] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [e]
xx
if Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= sh -> Int
forall sh. Shape sh => sh -> Int
size sh
sh
then [Char] -> IO (Array r sh e)
forall a. HasCallStack => [Char] -> a
error [Char]
"Data.Array.Repa.Eval.Fill.fromList: provide array shape does not match list length"
else do
MVec r e
mvec <- Int -> IO (MVec r e)
forall r e. Target r e => Int -> IO (MVec r e)
newMVec Int
len
(Int -> e -> IO ()) -> [Int] -> [e] -> IO ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ (MVec r e -> Int -> e -> IO ()
forall r e. Target r e => MVec r e -> Int -> e -> IO ()
unsafeWriteMVec MVec r e
mvec) [Int
0..] [e]
xx
sh -> MVec r e -> IO (Array r sh e)
forall r e sh. Target r e => sh -> MVec r e -> IO (Array r sh e)
unsafeFreezeMVec sh
sh MVec r e
mvec