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 #-}
18module Streamly.Benchmark.Data.ArrayOps where
20import Control.Monad.IO.Class (MonadIO)
21import Prelude (Bool, Int, Maybe(..), ($), (+), (.), (==), (>), undefined)
23import qualified Prelude as P
24import qualified Streamly.Prelude as S
26#ifndef DATA_PRIM_ARRAY
28import qualified Data.Foldable as F
33import qualified Streamly.Internal.Data.SmallArray as A
34type Stream = A.SmallArray
35#elif defined(MEMORY_ARRAY)
36import qualified GHC.Exts as GHC
37import qualified Streamly.Data.Array.Storable.Foreign as A
38import qualified Streamly.Internal.Data.Array.Storable.Foreign as A
40#elif defined(DATA_PRIM_ARRAY)
41import qualified Streamly.Internal.Data.Prim.Array as A
42type Stream = A.PrimArray
44import qualified Streamly.Internal.Data.Array as A
48-------------------------------------------------------------------------------
50-------------------------------------------------------------------------------
52-------------------------------------------------------------------------------
53-- Stream generation and elimination
54-------------------------------------------------------------------------------
56-------------------------------------------------------------------------------
62-------------------------------------------------------------------------------
64{-# INLINE sourceUnfoldr #-}
65sourceUnfoldr :: MonadIO m => Int -> Int -> m (Stream Int)
66sourceUnfoldr value n = S.fold (A.writeN value) $ S.unfoldr step n
71 else (Just (cnt, cnt + 1))
73{-# INLINE sourceIntFromTo #-}
74sourceIntFromTo :: MonadIO m => Int -> Int -> m (Stream Int)
75sourceIntFromTo value n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
77{-# INLINE sourceFromList #-}
78sourceFromList :: MonadIO m => Int -> Int -> m (Stream Int)
79sourceFromList value n = S.fold (A.writeN value) $ S.fromList [n..n+value]
81-- Different defination of sourceIntFromToFromList for DATA_SMALLARRAY
83{-# INLINE sourceIntFromToFromList #-}
84sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
85#ifndef DATA_SMALLARRAY
86sourceIntFromToFromList value n = P.return $ A.fromList $ [n..n + value]
88sourceIntFromToFromList value n = P.return $ A.fromListN value $ [n..n + value]
91-------------------------------------------------------------------------------
96-------------------------------------------------------------------------------
99#ifndef DATA_SMALLARRAY
100{-# INLINE sourceIntFromToFromStream #-}
101sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
102sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
105-------------------------------------------------------------------------------
108-------------------------------------------------------------------------------
112{-# INLINE sourceIsList #-}
113sourceIsList :: Int -> Int -> Stream Int
114sourceIsList value n = GHC.fromList [n..n+value]
116{-# INLINE sourceIsString #-}
117sourceIsString :: Int -> Int -> Stream P.Char
118sourceIsString value n = GHC.fromString (P.replicate (n + value) 'a')
121-------------------------------------------------------------------------------
123-------------------------------------------------------------------------------
125-------------------------------------------------------------------------------
131-------------------------------------------------------------------------------
133{-# INLINE composeN #-}
135 => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
140 3 -> f x P.>>= f P.>>= f
141 4 -> f x P.>>= f P.>>= f P.>>= f
145{-# INLINE scanl1' #-}
149 :: MonadIO m => Int -> Int -> Stream Int -> m (Stream Int)
152{-# INLINE onArray #-}
154 :: MonadIO m => Int -> (S.SerialT m Int -> S.SerialT m Int)
157onArray value f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
159scanl' value n = composeN n $ onArray value $ S.scanl' (+) 0
160scanl1' value n = composeN n $ onArray value $ S.scanl1' (+)
161map value n = composeN n $ onArray value $ S.map (+1)
162-- map n = composeN n $ A.map (+1)
164{-# INLINE eqInstance #-}
165eqInstance :: Stream Int -> Bool
166eqInstance src = src == src
168{-# INLINE eqInstanceNotEq #-}
169eqInstanceNotEq :: Stream Int -> Bool
170eqInstanceNotEq src = src P./= src
172{-# INLINE ordInstance #-}
173ordInstance :: Stream Int -> Bool
174ordInstance src = src P.< src
176{-# INLINE ordInstanceMin #-}
177ordInstanceMin :: Stream Int -> Stream Int
178ordInstanceMin src = P.min src src
180{-# INLINE showInstance #-}
181showInstance :: Stream Int -> P.String
182showInstance src = P.show src
184{-# INLINE pureFoldl' #-}
185pureFoldl' :: MonadIO m => Stream Int -> m Int
186pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
188-------------------------------------------------------------------------------
193-------------------------------------------------------------------------------
196#ifndef DATA_PRIM_ARRAY
197{-# INLINE readInstance #-}
198readInstance :: P.String -> Stream Int
203 _ -> P.error "readInstance: no parse"
206{-# INLINE foldableFoldl' #-}
207foldableFoldl' :: Stream Int -> Int
208foldableFoldl' = F.foldl' (+) 0
210{-# INLINE foldableSum #-}
211foldableSum :: Stream Int -> Int
216-------------------------------------------------------------------------------
218-------------------------------------------------------------------------------
220-------------------------------------------------------------------------------
226-------------------------------------------------------------------------------
228{-# INLINE unfoldReadDrain #-}
229unfoldReadDrain :: MonadIO m => Stream Int -> m ()
230unfoldReadDrain = S.drain . S.unfold A.read
232{-# INLINE toStreamRevDrain #-}
233toStreamRevDrain :: MonadIO m => Stream Int -> m ()
234toStreamRevDrain = 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