@@ -1,184 +0,0 @@
1−-- |
2−-- Module : Streamly.Benchmark.Data.ArrayOps
3−-- Copyright : (c) 2020 Composewell Technologies
4−--
5−-- License : BSD-3-Clause
6−-- Maintainer : streamly@composewell.com
7−
8−{-# LANGUAGE CPP #-}
9−{-# LANGUAGE FlexibleContexts #-}
10−{-# LANGUAGE ScopedTypeVariables #-}
11−
12−-- CPP:
13−-- DATA_ARRAY
14−
15−module Streamly.Benchmark.Data.ArrayOps where
16−
17−import Control.Monad.IO.Class (MonadIO)
18−import Prelude (Bool, Int, Maybe(..), ($), (+), (.), (==), (>), undefined)
19−
20−import qualified Prelude as P
21−import qualified Streamly.Prelude as S
22−
23−#ifdef DEVBUILD
24−{-
25−import qualified Data.Foldable as F
26−-}
27−#endif
28−
29−#if defined(DATA_ARRAY)
30−import qualified Streamly.Internal.Data.Array as A
31−type Stream = A.Array
32−#endif
33−
34−-------------------------------------------------------------------------------
35−-- Benchmark ops
36−-------------------------------------------------------------------------------
37−
38−-------------------------------------------------------------------------------
39−-- Stream generation and elimination
40−-------------------------------------------------------------------------------
41−
42−-------------------------------------------------------------------------------
43−-- CPP Common to:
44−-- DATA_ARRAY
45−-------------------------------------------------------------------------------
46−
47−{-# INLINE sourceUnfoldr #-}
48−sourceUnfoldr :: MonadIO m => Int -> Int -> m (Stream Int)
49−sourceUnfoldr value n = S.fold (A.writeN value) $ S.unfoldr step n
50− where
51− step cnt =
52− if cnt > n + value
53− then Nothing
54− else (Just (cnt, cnt + 1))
55−
56−{-# INLINE sourceIntFromTo #-}
57−sourceIntFromTo :: MonadIO m => Int -> Int -> m (Stream Int)
58−sourceIntFromTo value n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
59−
60−{-# INLINE sourceFromList #-}
61−sourceFromList :: MonadIO m => Int -> Int -> m (Stream Int)
62−sourceFromList value n = S.fold (A.writeN value) $ S.fromList [n..n+value]
63−
64−-- CPP:
65−{-# INLINE sourceIntFromToFromList #-}
66−sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
67−sourceIntFromToFromList value n = P.return $ A.fromList $ [n..n + value]
68−
69−-------------------------------------------------------------------------------
70−-- CPP Common to:
71−-- DATA_ARRAY
72−-------------------------------------------------------------------------------
73−
74−-- CPP:
75−{-# INLINE sourceIntFromToFromStream #-}
76−sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
77−sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
78−
79−-------------------------------------------------------------------------------
80−-- Transformation
81−-------------------------------------------------------------------------------
82−
83−-------------------------------------------------------------------------------
84−-- CPP Common to:
85−-- DATA_ARRAY
86−-------------------------------------------------------------------------------
87−
88−{-# INLINE composeN #-}
89−composeN :: P.Monad m
90− => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
91−composeN n f x =
92− case n of
93− 1 -> f x
94− 2 -> f x P.>>= f
95− 3 -> f x P.>>= f P.>>= f
96− 4 -> f x P.>>= f P.>>= f P.>>= f
97− _ -> undefined
98−
99−{-# INLINE scanl' #-}
100−{-# INLINE scanl1' #-}
101−{-# INLINE map #-}
102−
103−scanl' , scanl1', map
104− :: MonadIO m => Int -> Int -> Stream Int -> m (Stream Int)
105−
106−
107−{-# INLINE onArray #-}
108−onArray
109− :: MonadIO m => Int -> (S.SerialT m Int -> S.SerialT m Int)
110− -> Stream Int
111− -> m (Stream Int)
112−onArray value f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
113−
114−scanl' value n = composeN n $ onArray value $ S.scanl' (+) 0
115−scanl1' value n = composeN n $ onArray value $ S.scanl1' (+)
116−map value n = composeN n $ onArray value $ S.map (+1)
117−-- map n = composeN n $ A.map (+1)
118−
119−{-# INLINE eqInstance #-}
120−eqInstance :: Stream Int -> Bool
121−eqInstance src = src == src
122−
123−{-# INLINE eqInstanceNotEq #-}
124−eqInstanceNotEq :: Stream Int -> Bool
125−eqInstanceNotEq src = src P./= src
126−
127−{-# INLINE ordInstance #-}
128−ordInstance :: Stream Int -> Bool
129−ordInstance src = src P.< src
130−
131−{-# INLINE ordInstanceMin #-}
132−ordInstanceMin :: Stream Int -> Stream Int
133−ordInstanceMin src = P.min src src
134−
135−{-# INLINE showInstance #-}
136−showInstance :: Stream Int -> P.String
137−showInstance src = P.show src
138−
139−{-# INLINE pureFoldl' #-}
140−pureFoldl' :: MonadIO m => Stream Int -> m Int
141−pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
142−
143−-------------------------------------------------------------------------------
144−-- CPP Common to:
145−-- DATA_ARRAY
146−-------------------------------------------------------------------------------
147−
148−-- CPP:
149−{-# INLINE readInstance #-}
150−readInstance :: P.String -> Stream Int
151−readInstance str =
152− let r = P.reads str
153− in case r of
154− [(x,"")] -> x
155− _ -> P.error "readInstance: no parse"
156−
157−#ifdef DEVBUILD
158−{-
159−{-# INLINE foldableFoldl' #-}
160−foldableFoldl' :: Stream Int -> Int
161−foldableFoldl' = F.foldl' (+) 0
162−
163−{-# INLINE foldableSum #-}
164−foldableSum :: Stream Int -> Int
165−foldableSum = P.sum
166−-}
167−#endif
168−
169−-------------------------------------------------------------------------------
170−-- Elimination
171−-------------------------------------------------------------------------------
172−
173−-------------------------------------------------------------------------------
174−-- CPP Common to:
175−-- DATA_ARRAY
176−-------------------------------------------------------------------------------
177−
178−{-# INLINE unfoldReadDrain #-}
179−unfoldReadDrain :: MonadIO m => Stream Int -> m ()
180−unfoldReadDrain = S.drain . S.unfold A.read
181−
182−{-# INLINE toStreamRevDrain #-}
183−toStreamRevDrain :: MonadIO m => Stream Int -> m ()
184−toStreamRevDrain = S.drain . A.toStreamRev