on master help sign in
Export some pre-release array APIs
03cf686Harendra Kumar
benchmark/Streamly/Benchmark/Data/Array.hs+3−3
@@ -120,8 +120,8 @@ o_1_space_elimination value =
120 , benchIOSink value "foldl'" Ops.pureFoldl'
120 , benchIOSink value "foldl'" Ops.pureFoldl'
121 , benchIOSink value "read" Ops.unfoldReadDrain
121 , benchIOSink value "read" Ops.unfoldReadDrain
122 , benchIOSink value "toStreamRev" Ops.toStreamRevDrain
122 , benchIOSink value "toStreamRev" Ops.toStreamRevDrain
123− , benchFold "lastN.1" (S.fold (IA.lastN 1)) (P.sourceUnfoldrM value)
123+ , benchFold "writeLastN.1" (S.fold (IA.writeLastN 1)) (P.sourceUnfoldrM value)
124− , benchFold "lastN.10" (S.fold (IA.lastN 10)) (P.sourceUnfoldrM value)
124+ , benchFold "writeLastN.10" (S.fold (IA.writeLastN 10)) (P.sourceUnfoldrM value)
125#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
125#if !defined(DATA_ARRAY_PRIM) && !defined(DATA_ARRAY_PRIM_PINNED)
126#ifdef DEVBUILD
126#ifdef DEVBUILD
127 , benchPureSink value "foldable/foldl'" Ops.foldableFoldl'
127 , benchPureSink value "foldable/foldl'" Ops.foldableFoldl'
@@ -136,7 +136,7 @@ o_n_heap_serial value =
136 [ bgroup "elimination"
136 [ bgroup "elimination"
137 [
137 [
138 -- Converting the stream to an array
138 -- Converting the stream to an array
139− benchFold "lastN.Max" (S.fold (IA.lastN (value + 1)))
139+ benchFold "writeLastN.Max" (S.fold (IA.writeLastN (value + 1)))
140 (P.sourceUnfoldrM value)
140 (P.sourceUnfoldrM value)
141 , benchFold "writeN" (S.fold (A.writeN value))
141 , benchFold "writeN" (S.fold (A.writeN value))
142 (P.sourceUnfoldrM value)
142 (P.sourceUnfoldrM value)
benchmark/Streamly/Benchmark/Data/Array/Stream/Foreign.hs+1−1
@@ -55,7 +55,7 @@ toChunksLast inh = do
55 larr <- Stream.last s
55 larr <- Stream.last s
56 return $ case larr of
56 return $ case larr of
57 Nothing -> Nothing
57 Nothing -> Nothing
58− Just arr -> Array.readIndex arr (Array.length arr - 1)
58+ Just arr -> Array.getIndex arr (Array.length arr - 1)
59
59
60#ifdef INSPECTION
60#ifdef INSPECTION
61inspect $ hasNoTypeClasses 'toChunksLast
61inspect $ hasNoTypeClasses 'toChunksLast
src/Streamly/Data/Array/Foreign.hs+7−2
@@ -54,7 +54,7 @@ module Streamly.Data.Array.Foreign
54 -- Monadic APIs
54 -- Monadic APIs
55 , A.writeN -- drop new
55 , A.writeN -- drop new
56 , A.write -- full buffer
56 , A.write -- full buffer
57− -- , writeLastN -- drop old (ring buffer)
57+ , writeLastN -- drop old (ring buffer)
58
58
59 -- ** Elimination
59 -- ** Elimination
60 -- 'GHC.Exts.toList' from "GHC.Exts" can be used to convert an array to a
60 -- 'GHC.Exts.toList' from "GHC.Exts" can be used to convert an array to a
@@ -62,11 +62,16 @@ module Streamly.Data.Array.Foreign
62
62
63 , A.toList
63 , A.toList
64 , A.read
64 , A.read
65+ , A.readRev
66+
67+ -- ** Casting
68+ , cast
69+ , asBytes
65
70
66 -- ** Random Access
71 -- ** Random Access
67 , A.length
72 , A.length
68 -- , (!!)
73 -- , (!!)
69− -- , A.readIndex
74+ , A.getIndex
70 )
75 )
71where
76where
72
77
src/Streamly/Internal/Data/Array/Foreign.hs+29−23
@@ -60,7 +60,7 @@ module Streamly.Internal.Data.Array.Foreign
60 , A.writeN -- drop new
60 , A.writeN -- drop new
61 , A.writeNAligned
61 , A.writeNAligned
62 , A.write -- full buffer
62 , A.write -- full buffer
63− -- , writeLastN -- drop old (ring buffer)
63+ , writeLastN
64
64
65 -- * Elimination
65 -- * Elimination
66
66
@@ -78,7 +78,7 @@ module Streamly.Internal.Data.Array.Foreign
78 , null
78 , null
79 , last
79 , last
80 -- , (!!)
80 -- , (!!)
81− , readIndex
81+ , getIndex
82 , A.unsafeIndex
82 , A.unsafeIndex
83 -- , readIndices
83 -- , readIndices
84 -- , readRanges
84 -- , readRanges
@@ -130,15 +130,12 @@ module Streamly.Internal.Data.Array.Foreign
130 , cast
130 , cast
131 , unsafeCast
131 , unsafeCast
132 , asPtr
132 , asPtr
133− , asByteArray
133+ , asBytes
134 , asCString
134 , asCString
135
135
136 -- * Folding Arrays
136 -- * Folding Arrays
137 , streamFold
137 , streamFold
138 , fold
138 , fold
139−
140− -- * Folds with Array as the container
141− , lastN
142 )
139 )
143where
140where
144
141
@@ -273,34 +270,43 @@ unsafeRead = Unfold step inject
273null :: Storable a => Array a -> Bool
270null :: Storable a => Array a -> Bool
274null arr = length arr == 0
271null arr = length arr == 0
275
272
276−-- | > last arr = readIndex arr (length arr - 1)
273+-- | > last arr = getIndex arr (length arr - 1)
277--
274--
278-- /Pre-release/
275-- /Pre-release/
279{-# INLINE last #-}
276{-# INLINE last #-}
280last :: Storable a => Array a -> Maybe a
277last :: Storable a => Array a -> Maybe a
281−last arr = readIndex arr (length arr - 1)
278+last arr = getIndex arr (length arr - 1)
282
279
283-------------------------------------------------------------------------------
280-------------------------------------------------------------------------------
284-- Folds with Array as the container
281-- Folds with Array as the container
285-------------------------------------------------------------------------------
282-------------------------------------------------------------------------------
286
283
287−-- | Take last 'n' elements from the stream and discard the rest.
284+-- | @writeLastN n@ folds a maximum of @n@ elements from the end of the input
285+-- stream to an 'Array'.
286+--
287+-- @since 0.8.0
288−{-# INLINE lastN #-}
288+{-# INLINE writeLastN #-}
289−lastN :: (Storable a, MonadIO m) => Int -> Fold m a (Array a)
289+writeLastN :: (Storable a, MonadIO m) => Int -> Fold m a (Array a)
290−lastN n
290+writeLastN n
291 | n <= 0 = fmap (const mempty) FL.drain
291 | n <= 0 = fmap (const mempty) FL.drain
292 | otherwise = A.unsafeFreeze <$> Fold step initial done
292 | otherwise = A.unsafeFreeze <$> Fold step initial done
293+
293− where
294+ where
295+
294 step (Tuple3' rb rh i) a = do
296 step (Tuple3' rb rh i) a = do
295 rh1 <- liftIO $ RB.unsafeInsert rb rh a
297 rh1 <- liftIO $ RB.unsafeInsert rb rh a
296 return $ FL.Partial $ Tuple3' rb rh1 (i + 1)
298 return $ FL.Partial $ Tuple3' rb rh1 (i + 1)
299+
297 initial =
300 initial =
298 let f (a, b) = FL.Partial $ Tuple3' a b (0 :: Int)
301 let f (a, b) = FL.Partial $ Tuple3' a b (0 :: Int)
299 in fmap f $ liftIO $ RB.new n
302 in fmap f $ liftIO $ RB.new n
303+
300 done (Tuple3' rb rh i) = do
304 done (Tuple3' rb rh i) = do
301 arr <- liftIO $ MA.newArray n
305 arr <- liftIO $ MA.newArray n
302 foldFunc i rh snoc' arr rb
306 foldFunc i rh snoc' arr rb
307+
303 snoc' b a = liftIO $ MA.unsafeSnoc b a
308 snoc' b a = liftIO $ MA.unsafeSnoc b a
309+
304 foldFunc i
310 foldFunc i
305 | i < n = RB.unsafeFoldRingM
311 | i < n = RB.unsafeFoldRingM
306 | otherwise = RB.unsafeFoldRingFullM
312 | otherwise = RB.unsafeFoldRingFullM
@@ -425,10 +431,10 @@ foldbWith level f = undefined
425
431
426-- | /O(1)/ Lookup the element at the given index, starting from 0.
432-- | /O(1)/ Lookup the element at the given index, starting from 0.
427--
433--
428−-- /Pre-release/
434+-- @since 0.8.0
429−{-# INLINE readIndex #-}
435+{-# INLINE getIndex #-}
430−readIndex :: Storable a => Array a -> Int -> Maybe a
436+getIndex :: Storable a => Array a -> Int -> Maybe a
431−readIndex arr i =
437+getIndex arr i =
432 if i < 0 || i > length arr - 1
438 if i < 0 || i > length arr - 1
433 then Nothing
439 then Nothing
434 else A.unsafeInlineIO $
440 else A.unsafeInlineIO $
@@ -543,18 +549,18 @@ unsafeCast ::
543 Array a -> Array b
549 Array a -> Array b
544unsafeCast (Array start end) = Array (castForeignPtr start) (castPtr end)
550unsafeCast (Array start end) = Array (castForeignPtr start) (castPtr end)
545
551
546−-- | Cast an array into a Word8 array
552+-- | Cast an @Array a@ into an @Array Word8@.
547--
553--
548−-- /Pre-release/
554+-- @since 0.8.0
549--
555--
550−asByteArray :: Array a -> Array Word8
556+asBytes :: Array a -> Array Word8
551−asByteArray = unsafeCast
557+asBytes = unsafeCast
552
558
553-- | Cast an array having elements of type @a@ into an array having elements of
559-- | Cast an array having elements of type @a@ into an array having elements of
554-- type @b@. The length of the array should be a multiple of the size of the
560-- type @b@. The length of the array should be a multiple of the size of the
555-- target element otherwise 'Nothing' is returned.
561-- target element otherwise 'Nothing' is returned.
556--
562--
557−-- /Pre-release/
563+-- @since 0.8.0
558--
564--
559cast :: forall a b. (Storable b) => Array a -> Maybe (Array b)
565cast :: forall a b. (Storable b) => Array a -> Maybe (Array b)
560cast arr =
566cast arr =
@@ -584,7 +590,7 @@ asPtr Array{..} act = do
584--
590--
585asCString :: Array a -> (CString -> IO b) -> IO b
591asCString :: Array a -> (CString -> IO b) -> IO b
586asCString arr act = do
592asCString arr act = do
587− let Array{..} = asByteArray arr <> A.fromList [0]
593+ let Array{..} = asBytes arr <> A.fromList [0]
588 withForeignPtr aStart $ \ptr -> act (castPtr ptr)
594 withForeignPtr aStart $ \ptr -> act (castPtr ptr)
589
595
590-------------------------------------------------------------------------------
596-------------------------------------------------------------------------------
src/Streamly/Internal/FileSystem/Event/Linux.hs+1−1
@@ -621,7 +621,7 @@ ensureTrailingSlash :: Array Word8 -> Array Word8
621ensureTrailingSlash path =
621ensureTrailingSlash path =
622 if A.length path /= 0
622 if A.length path /= 0
623 then
623 then
624− let mx = A.readIndex path (A.length path - 1)
624+ let mx = A.getIndex path (A.length path - 1)
625 in case mx of
625 in case mx of
626 Nothing -> error "ensureTrailingSlash: Bug: Invalid index"
626 Nothing -> error "ensureTrailingSlash: Bug: Invalid index"
627 Just x ->
627 Just x ->
test/Streamly/Test/Common/Array.hs+8−8
@@ -206,14 +206,14 @@ testLastN =
206 monadicIO $ do
206 monadicIO $ do
207 xs <- run
207 xs <- run
208 $ fmap A.toList
208 $ fmap A.toList
209− $ S.fold (A.lastN n)
209+ $ S.fold (A.writeLastN n)
210 $ S.fromList list
210 $ S.fromList list
211 assert (xs == lastN n list)
211 assert (xs == lastN n list)
212
212
213testLastN_LN :: Int -> Int -> IO Bool
213testLastN_LN :: Int -> Int -> IO Bool
214testLastN_LN len n = do
214testLastN_LN len n = do
215 let list = [1..len]
215 let list = [1..len]
216− l1 <- fmap A.toList $ S.fold (A.lastN n) $ S.fromList list
216+ l1 <- fmap A.toList $ S.fold (A.writeLastN n) $ S.fromList list
217 let l2 = lastN n list
217 let l2 = lastN n list
218 return $ l1 == l2
218 return $ l1 == l2
219
219
@@ -275,10 +275,10 @@ main =
275 it "middle" (unsafeWriteIndex [1..10] 5 0 `shouldReturn` True)
275 it "middle" (unsafeWriteIndex [1..10] 5 0 `shouldReturn` True)
276 it "last" (unsafeWriteIndex [1..10] 9 0 `shouldReturn` True)
276 it "last" (unsafeWriteIndex [1..10] 9 0 `shouldReturn` True)
277 describe "Fold" $ do
277 describe "Fold" $ do
278− prop "lastN : 0 <= n <= len" $ testLastN
278+ prop "writeLastN : 0 <= n <= len" $ testLastN
279− describe "lastN boundary conditions" $ do
279+ describe "writeLastN boundary conditions" $ do
280− it "lastN -1" (testLastN_LN 10 (-1) `shouldReturn` True)
280+ it "writeLastN -1" (testLastN_LN 10 (-1) `shouldReturn` True)
281− it "lastN 0" (testLastN_LN 10 0 `shouldReturn` True)
281+ it "writeLastN 0" (testLastN_LN 10 0 `shouldReturn` True)
282− it "lastN length" (testLastN_LN 10 10 `shouldReturn` True)
282+ it "writeLastN length" (testLastN_LN 10 10 `shouldReturn` True)
283− it "lastN (length + 1)" (testLastN_LN 10 11 `shouldReturn` True)
283+ it "writeLastN (length + 1)" (testLastN_LN 10 11 `shouldReturn` True)
284#endif
284#endif
test/Streamly/Test/FileSystem/Event.hs+1−1
@@ -93,7 +93,7 @@ removeTrailingSlash path =
93 if Array.length path == 0
93 if Array.length path == 0
94 then path
94 then path
95 else
95 else
96− let mx = Array.readIndex path (Array.length path - 1)
96+ let mx = Array.getIndex path (Array.length path - 1)
97 in case mx of
97 in case mx of
98 Nothing -> error "removeTrailingSlash: Bug: Invalid index"
98 Nothing -> error "removeTrailingSlash: Bug: Invalid index"
99 Just x ->
99 Just x ->