#include "fusion-phases.h"
module Data.Array.Parallel.Unlifted.Sequential.Flat.Basics (
lengthU, nullU, emptyU, singletonU, consU, unitsU, replicateU,
replicateEachU,
(!:), (+:+), repeatU,
indexedU,
toU, fromU
) where
import Data.Array.Parallel.Base (
(:*:)(..))
import Data.Array.Parallel.Stream (
Step(..), Stream(..),
consS, singletonS, replicateS, (+++), indexedS,
replicateEachS, zipS, 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)
replicateEachU :: UA e => Int -> UArr Int -> UArr e -> UArr e
replicateEachU n ns es = unstreamU
. replicateEachS n
$ zipS (streamU ns) (streamU es)
(!:) :: 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 (rep n xs)
rep :: UA e => Int -> UArr e -> Stream e
rep 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]]