#include "fusion-phases.h"
module Data.Array.Parallel.Stream.Flat.Enum (
enumFromToS, enumFromThenToS,
enumFromStepLenS,
enumFromToEachS,
enumFromStepLenEachS
) where
import Data.Array.Parallel.Base (
(:*:)(..), MaybeS(..))
import Data.Array.Parallel.Stream.Flat.Stream
import Data.Array.Parallel.Stream.Flat.Combinators (
mapS)
enumFromToS :: Int -> Int -> Stream Int
enumFromToS start end
= Stream step start (max 0 (end start + 1))
where
step s | s > end = Done
| otherwise = Yield s (s+1)
enumFromThenToS :: Int -> Int -> Int -> Stream Int
enumFromThenToS start next end
= enumFromStepLenS start delta len
where
delta = next start
diff = end start
len | start < next && start <= end = ((endstart) `div` delta) + 1
| start > next && start >= end = ((startend) `div` (startnext)) + 1
| otherwise = 0
enumFromStepLenS :: Int -> Int -> Int -> Stream Int
enumFromStepLenS s !d n = Stream step (s :*: n) n
where
step (s :*: 0) = Done
step (s :*: n) = Yield s ((s+d) :*: (n1))
enumFromToEachS :: Int -> Stream (Int :*: Int) -> Stream Int
enumFromToEachS n (Stream next s _) = Stream next' (NothingS :*: s) n
where
next' (NothingS :*: s)
= case next s of
Yield (k :*: m) s' -> Skip (JustS (k :*: m) :*: s')
Skip s' -> Skip (NothingS :*: s')
Done -> Done
next' (JustS (k :*: m) :*: s)
| k > m = Skip (NothingS :*: s)
| otherwise = Yield k (JustS (k+1 :*: m) :*: s)
enumFromStepLenEachS :: Int -> Stream (Int :*: Int :*: Int) -> Stream Int
enumFromStepLenEachS len (Stream next s n) = Stream next' (NothingS :*: s) len
where
next' (NothingS :*: s)
= case next s of
Yield (from :*: step :*: len) s' -> Skip (JustS (from :*: step :*: len) :*: s')
Skip s' -> Skip (NothingS :*: s')
Done -> Done
next' (JustS (from :*: step :*: 0) :*: s) = Skip (NothingS :*: s)
next' (JustS (from :*: step :*: n) :*: s) = Yield from (JustS (from+step :*: step :*: (n1)) :*: s)