#include "fusion-phases.h"
module Data.Array.Parallel.Unlifted.Sequential.Flat.Basics (
lengthU, nullU, emptyU, singletonU, consU, unitsU, replicateU,
(!:), (+:+), repeatU, repeatUS,
indexedU,
toU, fromU
) where
import Data.Array.Parallel.Base (
(:*:)(..))
import Data.Array.Parallel.Stream (
Step(..), Stream(..),
consS, singletonS, replicateS, (+++), indexedS,
toStream)
import Data.Array.Parallel.Unlifted.Sequential.Flat.UArr (
UA, UArr, unitsU, lengthU, indexU, newU)
import Data.Array.Parallel.Unlifted.Sequential.Flat.Stream (
streamU, unstreamU)
infixl 9 !:
infixr 5 +:+
nullU :: UA e => UArr e -> Bool
nullU = (== 0) . lengthU
emptyU :: UA e => UArr e
emptyU = newU 0 (const $ return ())
singletonU :: UA e => e -> UArr e
singletonU = unstreamU . singletonS
consU :: UA e => e -> UArr e -> UArr e
consU x = unstreamU . consS x . streamU
replicateU :: UA e => Int -> e -> UArr e
replicateU n e = unstreamU (replicateS n e)
(!:) :: UA e => UArr e -> Int -> e
(!:) = indexU
(+:+) :: UA e => UArr e -> UArr e -> UArr e
a1 +:+ a2 = unstreamU (streamU a1 +++ streamU a2)
repeatU :: UA e => Int -> UArr e -> UArr e
repeatU n xs = unstreamU (repeatUS n xs)
repeatUS :: UA e => Int -> UArr e -> Stream e
repeatUS k !xs = Stream next (0 :*: k) (k*n)
where
n = lengthU xs
next (i :*: 0) = Done
next (i :*: k) | i == n = Skip (0 :*: k1)
| otherwise = Yield (xs !: i) (i+1 :*: k)
indexedU :: UA e => UArr e -> UArr (Int :*: e)
indexedU = unstreamU . indexedS . streamU
toU :: UA e => [e] -> UArr e
toU = unstreamU . toStream
fromU :: UA e => UArr e -> [e]
fromU a = [a!:i | i <- [0..lengthU a 1]]