on d2cde55 help sign in
86 lines 3.1 kB history raw
1#include "inline.hs"
3-- |
4-- Module : Streamly.Internal.Data.Array.Foreign.Mut
5-- Copyright : (c) 2020 Composewell Technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
9-- Portability : GHC
10--
11-- Unboxed pinned mutable array type for 'Storable' types with an option to use
12-- foreign (non-GHC) memory allocators. Fulfils the following goals:
13--
14-- * Random access (array)
15-- * Efficient storage (unboxed)
16-- * Performance (unboxed access)
17-- * Performance - in-place operations (mutable)
18-- * Performance - GC (pinned, mutable)
19-- * interfacing with OS (pinned)
20-- * Fragmentation control (foreign allocators)
21--
22-- Stream and Fold APIs allow easy, efficient and convenient operations on
23-- arrays.
25module Streamly.Internal.Data.Array.Foreign.Mut
26 (
27 module Streamly.Internal.Data.Array.Foreign.Mut.Type
28 , splitOn
29 , genSlicesFromLen
30 , getSlicesFromLen
31 )
32where
34import Prelude hiding (foldr, length, read, splitAt)
36import Streamly.Internal.Data.Array.Foreign.Mut.Type
37import qualified Streamly.Internal.Data.Stream.StreamD as D
38import Foreign.Storable (Storable)
39import Streamly.Internal.Data.Stream.IsStream.Type (SerialT)
40import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
41import qualified Streamly.Internal.Data.Unfold as Unfold
42import Streamly.Internal.Data.Unfold.Type (Unfold(..))
43import Control.Monad.IO.Class (MonadIO(..))
45-- | Split the array into a stream of slices using a predicate. The element
46-- matching the predicate is dropped.
47--
48-- /Pre-release/
49{-# INLINE splitOn #-}
50splitOn :: (MonadIO m, Storable a) =>
51 (a -> Bool) -> Array a -> SerialT m (Array a)
52splitOn predicate arr =
53 IsStream.fromStreamD
54 $ fmap (\(i, len) -> getSliceUnsafe i len arr)
55 $ D.sliceOnSuffix predicate (toStreamD arr)
57-- | Generate a stream of array slice descriptors ((index, len)) of specified
58-- length from an array, starting from the supplied array index. The last slice
59-- may be shorter than the requested length depending on the array length.
60--
61-- /Pre-release/
62{-# INLINE genSlicesFromLen #-}
63genSlicesFromLen :: forall m a. (Monad m, Storable a)
64 => Int -- ^ from index
65 -> Int -- ^ length of the slice
66 -> Unfold m (Array a) (Int, Int)
67genSlicesFromLen from len =
68 let fromThenTo n = (from, from + len, n - 1)
69 mkSlice n i = return (i, min len (n - i))
70 in Unfold.lmap length
71 $ Unfold.mapMWithInput mkSlice
72 $ Unfold.lmap fromThenTo Unfold.enumerateFromThenTo
74-- | Generate a stream of slices of specified length from an array, starting
75-- from the supplied array index. The last slice may be shorter than the
76-- requested length depending on the array length.
77--
78-- /Pre-release/
79{-# INLINE getSlicesFromLen #-}
80getSlicesFromLen :: forall m a. (Monad m, Storable a)
81 => Int -- ^ from index
82 -> Int -- ^ length of the slice
83 -> Unfold m (Array a) (Array a)
84getSlicesFromLen from len =
85 let mkSlice arr (i, n) = return $ getSliceUnsafe i n arr
86 in Unfold.mapMWithInput mkSlice (genSlicesFromLen from len)