Copyright | (c) 2019 Composewell Technologies |
---|---|
License | BSD3 |
Maintainer | streamly@composewell.com |
Stability | released |
Portability | GHC |
Safe Haskell | None |
Language | Haskell2010 |
This module provides immutable arrays in pinned memory (non GC memory) suitable for long lived data storage, random access and for interfacing with the operating system.
Arrays in this module are chunks of pinned memory that hold a sequence of
Storable
values of a given type, they cannot store non-serializable data
like functions. Once created an array cannot be modified. Pinned memory
allows efficient buffering of long lived data without adding any impact to
GC. One array is just one pointer visible to GC and it does not have to be
copied across generations. Moreover, pinned memory allows communication
with foreign consumers and producers (e.g. file or network IO) without
copying the data.
Programmer Notes
To apply a transformation to an array use read
to unfold the array into a
stream, apply a transformation on the stream and then use write
to fold it
back to an array.
This module is designed to be imported qualified:
import qualified Streamly.Data.Array.Foreign as Array
For experimental APIs see Streamly.Internal.Data.Array.Foreign.
Synopsis
- data Array a
- fromListN :: Storable a => Int -> [a] -> Array a
- fromList :: Storable a => [a] -> Array a
- writeN :: forall m a. (MonadIO m, Storable a) => Int -> Fold m a (Array a)
- write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
- writeLastN :: (Storable a, MonadIO m) => Int -> Fold m a (Array a)
- toList :: Storable a => Array a -> [a]
- read :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a
- readRev :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a
- cast :: forall a b. Storable b => Array a -> Maybe (Array b)
- asBytes :: Array a -> Array Word8
- length :: forall a. Storable a => Array a -> Int
- getIndex :: Storable a => Array a -> Int -> Maybe a
Documentation
Instances
Storable a => IsList (Array a) Source # | |
(Storable a, Eq a) => Eq (Array a) Source # | |
(Storable a, Ord a) => Ord (Array a) Source # | |
Defined in Streamly.Internal.Data.Array.Foreign.Type | |
(Storable a, Read a, Show a) => Read (Array a) Source # | |
(Show a, Storable a) => Show (Array a) Source # | |
a ~ Char => IsString (Array a) Source # | |
Defined in Streamly.Internal.Data.Array.Foreign.Type fromString :: String -> Array a # | |
Storable a => Semigroup (Array a) Source # | |
Storable a => Monoid (Array a) Source # | |
(Storable a, NFData a) => NFData (Array a) Source # | |
Defined in Streamly.Internal.Data.Array.Foreign.Type | |
type Item (Array a) Source # | |
Defined in Streamly.Internal.Data.Array.Foreign.Type |
Arrays
Construction
When performance matters, the fastest way to generate an array is
writeN
. IsList
and IsString
instances can be
used to conveniently construct arrays from literal values.
OverloadedLists
extension or fromList
can be used to construct an
array from a list literal. Similarly, OverloadedStrings
extension or
fromList
can be used to construct an array from a string literal.
fromListN :: Storable a => Int -> [a] -> Array a Source #
Create an Array
from the first N elements of a list. The array is
allocated to size N, if the list terminates before N elements then the
array may hold less than N elements.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
fromList :: Storable a => [a] -> Array a Source #
Create an Array
from a list. The list must be of finite size.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
writeN :: forall m a. (MonadIO m, Storable a) => Int -> Fold m a (Array a) Source #
writeN n
folds a maximum of n
elements from the input stream to an
Array
.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a) Source #
Fold the whole input to a single array.
Caution! Do not use this on infinite streams.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
writeLastN :: (Storable a, MonadIO m) => Int -> Fold m a (Array a) Source #
writeLastN n
folds a maximum of n
elements from the end of the input
stream to an Array
.
Since: 0.8.0
Elimination
read :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a Source #
Unfold an array into a stream.
Since 0.7.0 (Streamly.Memory.Array)
Since: 0.8.0
readRev :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a Source #
Unfold an array into a stream in reverse order.
Since: 0.8.0
Casting
cast :: forall a b. Storable b => Array a -> Maybe (Array b) Source #
Cast an array having elements of type a
into an array having elements of
type b
. The length of the array should be a multiple of the size of the
target element otherwise Nothing
is returned.
Since: 0.8.0