2-- Module : Streamly.Benchmark.Data.ArrayOps
3-- Copyright : (c) 2020 Composewell Technologies
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
16-- DATA_ARRAY_PRIM_PINNED
19module Streamly.Benchmark.Data.ArrayOps where
21import Control.Monad.IO.Class (MonadIO)
22import Prelude (Bool, Int, Maybe(..), ($), (+), (.), (==), (>), undefined)
24import qualified Prelude as P
25import qualified Streamly.Prelude as S
27#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
29import qualified Data.Foldable as F
34import qualified Streamly.Internal.Data.SmallArray as A
35type Stream = A.SmallArray
36#elif defined(MEMORY_ARRAY)
37import qualified GHC.Exts as GHC
38import qualified Streamly.Data.Array.Foreign as A
39import qualified Streamly.Internal.Data.Array.Foreign as A
41#elif defined(DATA_ARRAY_PRIM)
42import qualified Streamly.Internal.Data.Array.Prim as A
44#elif defined(DATA_ARRAY_PRIM_PINNED)
45import qualified Streamly.Internal.Data.Array.Prim.Pinned as A
47#elif defined(DATA_ARRAY)
48import qualified Streamly.Internal.Data.Array as A
52-------------------------------------------------------------------------------
54-------------------------------------------------------------------------------
56-------------------------------------------------------------------------------
57-- Stream generation and elimination
58-------------------------------------------------------------------------------
60-------------------------------------------------------------------------------
65-- DATA_ARRAY_PRIM_PINNED
67-------------------------------------------------------------------------------
69{-# INLINE sourceUnfoldr #-}
70sourceUnfoldr :: MonadIO m => Int -> Int -> m (Stream Int)
71sourceUnfoldr value n = S.fold (A.writeN value) $ S.unfoldr step n
76 else (Just (cnt, cnt + 1))
78{-# INLINE sourceIntFromTo #-}
79sourceIntFromTo :: MonadIO m => Int -> Int -> m (Stream Int)
80sourceIntFromTo value n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
82{-# INLINE sourceFromList #-}
83sourceFromList :: MonadIO m => Int -> Int -> m (Stream Int)
84sourceFromList value n = S.fold (A.writeN value) $ S.fromList [n..n+value]
86-- Different defination of sourceIntFromToFromList for DATA_SMALLARRAY
88{-# INLINE sourceIntFromToFromList #-}
89sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
90#ifndef DATA_SMALLARRAY
91sourceIntFromToFromList value n = P.return $ A.fromList $ [n..n + value]
93sourceIntFromToFromList value n = P.return $ A.fromListN value $ [n..n + value]
96-------------------------------------------------------------------------------
101-- DATA_ARRAY_PRIM_PINNED
102-------------------------------------------------------------------------------
105#ifndef DATA_SMALLARRAY
106{-# INLINE sourceIntFromToFromStream #-}
107sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
108sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
111-------------------------------------------------------------------------------
114-------------------------------------------------------------------------------
118{-# INLINE sourceIsList #-}
119sourceIsList :: Int -> Int -> Stream Int
120sourceIsList value n = GHC.fromList [n..n+value]
122{-# INLINE sourceIsString #-}
123sourceIsString :: Int -> Int -> Stream P.Char
124sourceIsString value n = GHC.fromString (P.replicate (n + value) 'a')
127-------------------------------------------------------------------------------
129-------------------------------------------------------------------------------
131-------------------------------------------------------------------------------
136-- DATA_ARRAY_PRIM_PINNED
138-------------------------------------------------------------------------------
140{-# INLINE composeN #-}
142 => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
147 3 -> f x P.>>= f P.>>= f
148 4 -> f x P.>>= f P.>>= f P.>>= f
152{-# INLINE scanl1' #-}
156 :: MonadIO m => Int -> Int -> Stream Int -> m (Stream Int)
159{-# INLINE onArray #-}
161 :: MonadIO m => Int -> (S.SerialT m Int -> S.SerialT m Int)
164onArray value f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
166scanl' value n = composeN n $ onArray value $ S.scanl' (+) 0
167scanl1' value n = composeN n $ onArray value $ S.scanl1' (+)
168map value n = composeN n $ onArray value $ S.map (+1)
169-- map n = composeN n $ A.map (+1)
171{-# INLINE eqInstance #-}
172eqInstance :: Stream Int -> Bool
173eqInstance src = src == src
175{-# INLINE eqInstanceNotEq #-}
176eqInstanceNotEq :: Stream Int -> Bool
177eqInstanceNotEq src = src P./= src
179{-# INLINE ordInstance #-}
180ordInstance :: Stream Int -> Bool
181ordInstance src = src P.< src
183{-# INLINE ordInstanceMin #-}
184ordInstanceMin :: Stream Int -> Stream Int
185ordInstanceMin src = P.min src src
187{-# INLINE showInstance #-}
188showInstance :: Stream Int -> P.String
189showInstance src = P.show src
191{-# INLINE pureFoldl' #-}
192pureFoldl' :: MonadIO m => Stream Int -> m Int
193pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
195-------------------------------------------------------------------------------
200-------------------------------------------------------------------------------
203#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
204{-# INLINE readInstance #-}
205readInstance :: P.String -> Stream Int
210 _ -> P.error "readInstance: no parse"
213{-# INLINE foldableFoldl' #-}
214foldableFoldl' :: Stream Int -> Int
215foldableFoldl' = F.foldl' (+) 0
217{-# INLINE foldableSum #-}
218foldableSum :: Stream Int -> Int
223-------------------------------------------------------------------------------
225-------------------------------------------------------------------------------
227-------------------------------------------------------------------------------
232-- DATA_ARRAY_PRIM_PINNED
234-------------------------------------------------------------------------------
236{-# INLINE unfoldReadDrain #-}
237unfoldReadDrain :: MonadIO m => Stream Int -> m ()
238unfoldReadDrain = S.drain . S.unfold A.read
240{-# INLINE toStreamRevDrain #-}
241toStreamRevDrain :: MonadIO m => Stream Int -> m ()
242toStreamRevDrain = S.drain . A.toStreamRev
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list