Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data ByteArray# :: UnliftedType
- data MutableByteArray# a :: UnliftedType
- data Array a = Array {}
- data MutableByteArray s = MutableByteArray (MutableByteArray# s)
- data ByteArray = ByteArray ByteArray#
- data MutableArray s a = MutableArray {
- marray# :: MutableArray# s a
- data MutablePrimArray s a = MutablePrimArray (MutableByteArray# s)
- data PrimArray a = PrimArray ByteArray#
- fromList :: IsList l => [Item l] -> l
- fromListN :: IsList l => Int -> [Item l] -> l
- emptyArray :: Array a
- newArray :: PrimMonad m => Int -> a -> m (MutableArray (PrimState m) a)
- newByteArray :: PrimMonad m => Int -> m (MutableByteArray (PrimState m))
- unsafeFreezeByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> m ByteArray
- sizeofByteArray :: ByteArray -> Int
- byteArrayFromListN :: Prim a => Int -> [a] -> ByteArray
- emptyByteArray :: ByteArray
- copyByteArrayToPtr :: (PrimMonad m, Prim a) => Ptr a -> ByteArray -> Int -> Int -> m ()
- newPinnedByteArray :: PrimMonad m => Int -> m (MutableByteArray (PrimState m))
- setByteArray :: (Prim a, PrimMonad m) => MutableByteArray (PrimState m) -> Int -> Int -> a -> m ()
- sizeofArray :: Array a -> Int
- sizeofMutableArray :: MutableArray s a -> Int
- indexArray :: Array a -> Int -> a
- indexArray## :: Array a -> Int -> (# a #)
- indexArrayM :: Monad m => Array a -> Int -> m a
- freezeArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> Int -> m (Array a)
- unsafeFreezeArray :: PrimMonad m => MutableArray (PrimState m) a -> m (Array a)
- thawArray :: PrimMonad m => Array a -> Int -> Int -> m (MutableArray (PrimState m) a)
- unsafeThawArray :: PrimMonad m => Array a -> m (MutableArray (PrimState m) a)
- sameMutableArray :: MutableArray s a -> MutableArray s a -> Bool
- cloneArray :: Array a -> Int -> Int -> Array a
- runArray :: (forall s. ST s (MutableArray s a)) -> Array a
- createArray :: Int -> a -> (forall s. MutableArray s a -> ST s ()) -> Array a
- traverseArrayP :: PrimMonad m => (a -> m b) -> Array a -> m (Array b)
- mapArray' :: (a -> b) -> Array a -> Array b
- arrayFromListN :: Int -> [a] -> Array a
- arrayFromList :: [a] -> Array a
- newAlignedPinnedByteArray :: PrimMonad m => Int -> Int -> m (MutableByteArray (PrimState m))
- byteArrayContents :: ByteArray -> Ptr Word8
- mutableByteArrayContents :: MutableByteArray s -> Ptr Word8
- sameMutableByteArray :: MutableByteArray s -> MutableByteArray s -> Bool
- resizeMutableByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m (MutableByteArray (PrimState m))
- getSizeofMutableByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> m Int
- freezeByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> m ByteArray
- thawByteArray :: PrimMonad m => ByteArray -> Int -> Int -> m (MutableByteArray (PrimState m))
- unsafeThawByteArray :: PrimMonad m => ByteArray -> m (MutableByteArray (PrimState m))
- sizeofMutableByteArray :: MutableByteArray s -> Int
- shrinkMutableByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m ()
- isByteArrayPinned :: ByteArray -> Bool
- isMutableByteArrayPinned :: MutableByteArray s -> Bool
- foldrByteArray :: Prim a => (a -> b -> b) -> b -> ByteArray -> b
- byteArrayFromList :: Prim a => [a] -> ByteArray
- copyPtrToMutableByteArray :: (PrimMonad m, Prim a) => MutableByteArray (PrimState m) -> Int -> Ptr a -> Int -> m ()
- copyMutableByteArrayToPtr :: (PrimMonad m, Prim a) => Ptr a -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
- copyByteArrayToAddr :: PrimMonad m => Ptr Word8 -> ByteArray -> Int -> Int -> m ()
- copyMutableByteArrayToAddr :: PrimMonad m => Ptr Word8 -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
- fillByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> Word8 -> m ()
- compareByteArrays :: ByteArray -> Int -> ByteArray -> Int -> Int -> Ordering
- cloneByteArray :: ByteArray -> Int -> Int -> ByteArray
- cloneMutableByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Int -> m (MutableByteArray (PrimState m))
- runByteArray :: (forall s. ST s (MutableByteArray s)) -> ByteArray
- readCharArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m Char
- writeCharArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Char -> m ()
- indexCharArray :: ByteArray -> Int -> Char
- primArrayFromList :: Prim a => [a] -> PrimArray a
- primArrayFromListN :: Prim a => Int -> [a] -> PrimArray a
- primArrayToList :: Prim a => PrimArray a -> [a]
- emptyPrimArray :: PrimArray a
- newPrimArray :: (PrimMonad m, Prim a) => Int -> m (MutablePrimArray (PrimState m) a)
- resizeMutablePrimArray :: (PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> Int -> m (MutablePrimArray (PrimState m) a)
- shrinkMutablePrimArray :: (PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> Int -> m ()
- copyMutablePrimArray :: (PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> Int -> MutablePrimArray (PrimState m) a -> Int -> Int -> m ()
- copyPrimArray :: (PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> Int -> PrimArray a -> Int -> Int -> m ()
- copyPrimArrayToPtr :: (PrimMonad m, Prim a) => Ptr a -> PrimArray a -> Int -> Int -> m ()
- copyMutablePrimArrayToPtr :: (PrimMonad m, Prim a) => Ptr a -> MutablePrimArray (PrimState m) a -> Int -> Int -> m ()
- copyPtrToMutablePrimArray :: (PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> Int -> Ptr a -> Int -> m ()
- setPrimArray :: (Prim a, PrimMonad m) => MutablePrimArray (PrimState m) a -> Int -> Int -> a -> m ()
- getSizeofMutablePrimArray :: (PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> m Int
- sizeofMutablePrimArray :: forall s a. Prim a => MutablePrimArray s a -> Int
- sameMutablePrimArray :: MutablePrimArray s a -> MutablePrimArray s a -> Bool
- freezePrimArray :: (PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> Int -> Int -> m (PrimArray a)
- thawPrimArray :: (PrimMonad m, Prim a) => PrimArray a -> Int -> Int -> m (MutablePrimArray (PrimState m) a)
- unsafeFreezePrimArray :: PrimMonad m => MutablePrimArray (PrimState m) a -> m (PrimArray a)
- unsafeThawPrimArray :: PrimMonad m => PrimArray a -> m (MutablePrimArray (PrimState m) a)
- sizeofPrimArray :: Prim a => PrimArray a -> Int
- isPrimArrayPinned :: PrimArray a -> Bool
- isMutablePrimArrayPinned :: MutablePrimArray s a -> Bool
- foldrPrimArray :: Prim a => (a -> b -> b) -> b -> PrimArray a -> b
- foldrPrimArray' :: Prim a => (a -> b -> b) -> b -> PrimArray a -> b
- foldlPrimArray :: Prim a => (b -> a -> b) -> b -> PrimArray a -> b
- foldlPrimArray' :: Prim a => (b -> a -> b) -> b -> PrimArray a -> b
- foldlPrimArrayM' :: (Prim a, Monad m) => (b -> a -> m b) -> b -> PrimArray a -> m b
- traversePrimArrayP :: (PrimMonad m, Prim a, Prim b) => (a -> m b) -> PrimArray a -> m (PrimArray b)
- filterPrimArrayP :: (PrimMonad m, Prim a) => (a -> m Bool) -> PrimArray a -> m (PrimArray a)
- mapMaybePrimArrayP :: (PrimMonad m, Prim a, Prim b) => (a -> m (Maybe b)) -> PrimArray a -> m (PrimArray b)
- generatePrimArrayP :: (PrimMonad m, Prim a) => Int -> (Int -> m a) -> m (PrimArray a)
- replicatePrimArrayP :: (PrimMonad m, Prim a) => Int -> m a -> m (PrimArray a)
- mapPrimArray :: (Prim a, Prim b) => (a -> b) -> PrimArray a -> PrimArray b
- imapPrimArray :: (Prim a, Prim b) => (Int -> a -> b) -> PrimArray a -> PrimArray b
- filterPrimArray :: Prim a => (a -> Bool) -> PrimArray a -> PrimArray a
- filterPrimArrayA :: (Applicative f, Prim a) => (a -> f Bool) -> PrimArray a -> f (PrimArray a)
- mapMaybePrimArrayA :: (Applicative f, Prim a, Prim b) => (a -> f (Maybe b)) -> PrimArray a -> f (PrimArray b)
- mapMaybePrimArray :: (Prim a, Prim b) => (a -> Maybe b) -> PrimArray a -> PrimArray b
- traversePrimArray :: (Applicative f, Prim a, Prim b) => (a -> f b) -> PrimArray a -> f (PrimArray b)
- itraversePrimArray :: (Applicative f, Prim a, Prim b) => (Int -> a -> f b) -> PrimArray a -> f (PrimArray b)
- itraversePrimArrayP :: (Prim a, Prim b, PrimMonad m) => (Int -> a -> m b) -> PrimArray a -> m (PrimArray b)
- generatePrimArray :: Prim a => Int -> (Int -> a) -> PrimArray a
- replicatePrimArray :: Prim a => Int -> a -> PrimArray a
- generatePrimArrayA :: (Applicative f, Prim a) => Int -> (Int -> f a) -> f (PrimArray a)
- replicatePrimArrayA :: (Applicative f, Prim a) => Int -> f a -> f (PrimArray a)
- traversePrimArray_ :: (Applicative f, Prim a) => (a -> f b) -> PrimArray a -> f ()
- itraversePrimArray_ :: (Applicative f, Prim a) => (Int -> a -> f b) -> PrimArray a -> f ()
- newPinnedPrimArray :: (PrimMonad m, Prim a) => Int -> m (MutablePrimArray (PrimState m) a)
- newAlignedPinnedPrimArray :: (PrimMonad m, Prim a) => Int -> m (MutablePrimArray (PrimState m) a)
- primArrayContents :: PrimArray a -> Ptr a
- mutablePrimArrayContents :: MutablePrimArray s a -> Ptr a
- clonePrimArray :: Prim a => PrimArray a -> Int -> Int -> PrimArray a
- cloneMutablePrimArray :: (PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> Int -> Int -> m (MutablePrimArray (PrimState m) a)
- runPrimArray :: (forall s. ST s (MutablePrimArray s a)) -> PrimArray a
- byteArrayToList :: ByteArray -> [Word8]
- readArray :: CheckCtx => PrimMonad m => MutableArray (PrimState m) a -> Int -> m a
- writeArray :: CheckCtx => PrimMonad m => MutableArray (PrimState m) a -> Int -> a -> m ()
- copyArray :: CheckCtx => PrimMonad m => MutableArray (PrimState m) a -> Int -> Array a -> Int -> Int -> m ()
- copyMutableArray :: CheckCtx => PrimMonad m => MutableArray (PrimState m) a -> Int -> MutableArray (PrimState m) a -> Int -> Int -> m ()
- cloneMutableArray :: CheckCtx => PrimMonad m => MutableArray (PrimState m) a -> Int -> Int -> m (MutableArray (PrimState m) a)
- readByteArray :: forall a m. CheckCtx => PrimMonad m => Prim a => MutableByteArray (PrimState m) -> Int -> m a
- writeByteArray :: forall a m. CheckCtx => PrimMonad m => Prim a => MutableByteArray (PrimState m) -> Int -> a -> m ()
- indexByteArray :: forall a. CheckCtx => Prim a => ByteArray -> Int -> a
- copyByteArray :: CheckCtx => PrimMonad m => MutableByteArray (PrimState m) -> Int -> ByteArray -> Int -> Int -> m ()
- copyMutableByteArray :: CheckCtx => PrimMonad m => MutableByteArray (PrimState m) -> Int -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
- moveByteArray :: CheckCtx => PrimMonad m => MutableByteArray (PrimState m) -> Int -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
- readPrimArray :: CheckCtx => PrimMonad m => Prim a => MutablePrimArray (PrimState m) a -> Int -> m a
- writePrimArray :: CheckCtx => PrimMonad m => Prim a => MutablePrimArray (PrimState m) a -> Int -> a -> m ()
- indexPrimArray :: CheckCtx => Prim a => PrimArray a -> Int -> a
Documentation
data ByteArray# :: UnliftedType #
A boxed, unlifted datatype representing a region of raw memory in the garbage-collected heap, which is not scanned for pointers during garbage collection.
It is created by freezing a MutableByteArray#
with unsafeFreezeByteArray#
.
Freezing is essentially a no-op, as MutableByteArray#
and ByteArray#
share the same heap structure under the hood.
The immutable and mutable variants are commonly used for scenarios requiring high-performance data structures,
like Text
, Primitive Vector
, Unboxed Array
, and ShortByteString
.
Another application of fundamental importance is Integer
, which is backed by ByteArray#
.
The representation on the heap of a Byte Array is:
+------------+-----------------+-----------------------+ | | | | | HEADER | SIZE (in bytes) | PAYLOAD | | | | | +------------+-----------------+-----------------------+
To obtain a pointer to actual payload (e.g., for FFI purposes) use byteArrayContents#
or mutableByteArrayContents#
.
Alternatively, enabling the UnliftedFFITypes
extension
allows to mention ByteArray#
and MutableByteArray#
in FFI type signatures directly.
data MutableByteArray# a :: UnliftedType #
A mutable ByteAray#
. It can be created in three ways:
newByteArray#
: Create an unpinned array.newPinnedByteArray#
: This will create a pinned array,newAlignedPinnedByteArray#
: This will create a pinned array, with a custom alignment.
Unpinned arrays can be moved around during garbage collection, so you must not store or pass pointers to these values if there is a chance for the garbage collector to kick in. That said, even unpinned arrays can be passed to unsafe FFI calls, because no garbage collection happens during these unsafe calls (see Guaranteed Call Safety in the GHC Manual). For safe FFI calls, byte arrays must be not only pinned, but also kept alive by means of the keepAlive# function for the duration of a call (that's because garbage collection cannot move a pinned array, but is free to scrap it altogether).
Boxed arrays.
Instances
MonadFail Array | |
Defined in Data.Primitive.Array | |
MonadFix Array | |
Defined in Data.Primitive.Array | |
MonadZip Array | |
Foldable Array | |
Defined in Data.Primitive.Array fold :: Monoid m => Array m -> m # foldMap :: Monoid m => (a -> m) -> Array a -> m # foldMap' :: Monoid m => (a -> m) -> Array a -> m # foldr :: (a -> b -> b) -> b -> Array a -> b # foldr' :: (a -> b -> b) -> b -> Array a -> b # foldl :: (b -> a -> b) -> b -> Array a -> b # foldl' :: (b -> a -> b) -> b -> Array a -> b # foldr1 :: (a -> a -> a) -> Array a -> a # foldl1 :: (a -> a -> a) -> Array a -> a # elem :: Eq a => a -> Array a -> Bool # maximum :: Ord a => Array a -> a # minimum :: Ord a => Array a -> a # | |
Eq1 Array | Since: primitive-0.6.4.0 |
Ord1 Array | Since: primitive-0.6.4.0 |
Defined in Data.Primitive.Array | |
Read1 Array | Since: primitive-0.6.4.0 |
Defined in Data.Primitive.Array | |
Show1 Array | Since: primitive-0.6.4.0 |
Traversable Array | |
Alternative Array | |
Applicative Array | |
Functor Array | |
Monad Array | |
MonadPlus Array | |
NFData1 Array | |
Defined in Data.Primitive.Array | |
Lift a => Lift (Array a :: Type) | |
Data a => Data (Array a) | |
Defined in Data.Primitive.Array gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Array a -> c (Array a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Array a) # toConstr :: Array a -> Constr # dataTypeOf :: Array a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Array a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Array a)) # gmapT :: (forall b. Data b => b -> b) -> Array a -> Array a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Array a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Array a -> r # gmapQ :: (forall d. Data d => d -> u) -> Array a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Array a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Array a -> m (Array a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Array a -> m (Array a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Array a -> m (Array a) # | |
Monoid (Array a) | |
Semigroup (Array a) | Since: primitive-0.6.3.0 |
IsList (Array a) | |
Read a => Read (Array a) | |
Show a => Show (Array a) | |
NFData a => NFData (Array a) | |
Defined in Data.Primitive.Array | |
Eq a => Eq (Array a) | |
Ord a => Ord (Array a) | Lexicographic ordering. Subject to change between major versions. |
type Item (Array a) | |
Defined in Data.Primitive.Array |
data MutableByteArray s #
Lifted wrapper for MutableByteArray#
.
Since MutableByteArray#
is an unlifted type and not a member of kind Type
,
things like [MutableByteArray#]
or IO MutableByteArray#
are ill-typed. To work around this
inconvenience this module provides a standard lifted wrapper, inhabiting Type
.
Clients are expected to use MutableByteArray
in higher-level APIs,
but wrap and unwrap MutableByteArray
internally as they please
and use functions from GHC.Exts.
Since: base-4.17.0.0
Instances
Typeable s => Data (MutableByteArray s) | Since: base-4.17.0.0 |
Defined in Data.Array.Byte gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> MutableByteArray s -> c (MutableByteArray s) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (MutableByteArray s) # toConstr :: MutableByteArray s -> Constr # dataTypeOf :: MutableByteArray s -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (MutableByteArray s)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (MutableByteArray s)) # gmapT :: (forall b. Data b => b -> b) -> MutableByteArray s -> MutableByteArray s # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> MutableByteArray s -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> MutableByteArray s -> r # gmapQ :: (forall d. Data d => d -> u) -> MutableByteArray s -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> MutableByteArray s -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> MutableByteArray s -> m (MutableByteArray s) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> MutableByteArray s -> m (MutableByteArray s) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> MutableByteArray s -> m (MutableByteArray s) # | |
NFData (MutableByteArray s) | Since: deepseq-1.4.8.0 |
Defined in Control.DeepSeq rnf :: MutableByteArray s -> () # | |
Eq (MutableByteArray s) | Since: base-4.17.0.0 |
Defined in Data.Array.Byte (==) :: MutableByteArray s -> MutableByteArray s -> Bool # (/=) :: MutableByteArray s -> MutableByteArray s -> Bool # |
Lifted wrapper for ByteArray#
.
Since ByteArray#
is an unlifted type and not a member of kind Type
,
things like [ByteArray#]
or IO ByteArray#
are ill-typed. To work around this
inconvenience this module provides a standard lifted wrapper, inhabiting Type
.
Clients are expected to use ByteArray
in higher-level APIs,
but wrap and unwrap ByteArray
internally as they please
and use functions from GHC.Exts.
Since: base-4.17.0.0
Instances
Data ByteArray | Since: base-4.17.0.0 |
Defined in Data.Array.Byte gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ByteArray -> c ByteArray # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c ByteArray # toConstr :: ByteArray -> Constr # dataTypeOf :: ByteArray -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c ByteArray) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ByteArray) # gmapT :: (forall b. Data b => b -> b) -> ByteArray -> ByteArray # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ByteArray -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ByteArray -> r # gmapQ :: (forall d. Data d => d -> u) -> ByteArray -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> ByteArray -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> ByteArray -> m ByteArray # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ByteArray -> m ByteArray # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ByteArray -> m ByteArray # | |
Monoid ByteArray | Since: base-4.17.0.0 |
Semigroup ByteArray | Since: base-4.17.0.0 |
IsList ByteArray | Since: base-4.17.0.0 |
Show ByteArray | Since: base-4.17.0.0 |
NFData ByteArray | Since: deepseq-1.4.7.0 |
Defined in Control.DeepSeq | |
Eq ByteArray | Since: base-4.17.0.0 |
Ord ByteArray | Non-lexicographic ordering. This compares the lengths of the byte arrays first and uses a lexicographic ordering if the lengths are equal. Subject to change between major versions. Since: base-4.17.0.0 |
Defined in Data.Array.Byte | |
Hashable ByteArray | This instance was available since 1.4.1.0 only for GHC-9.4+ Since: hashable-1.4.2.0 |
Defined in Data.Hashable.Class | |
Lift ByteArray | Since: template-haskell-2.19.0.0 |
type Item ByteArray | |
Defined in Data.Array.Byte |
data MutableArray s a #
Mutable boxed arrays associated with a primitive state token.
MutableArray | |
|
Instances
(Typeable s, Typeable a) => Data (MutableArray s a) | |
Defined in Data.Primitive.Array gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> MutableArray s a -> c (MutableArray s a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (MutableArray s a) # toConstr :: MutableArray s a -> Constr # dataTypeOf :: MutableArray s a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (MutableArray s a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (MutableArray s a)) # gmapT :: (forall b. Data b => b -> b) -> MutableArray s a -> MutableArray s a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> MutableArray s a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> MutableArray s a -> r # gmapQ :: (forall d. Data d => d -> u) -> MutableArray s a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> MutableArray s a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> MutableArray s a -> m (MutableArray s a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> MutableArray s a -> m (MutableArray s a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> MutableArray s a -> m (MutableArray s a) # | |
Eq (MutableArray s a) | |
Defined in Data.Primitive.Array (==) :: MutableArray s a -> MutableArray s a -> Bool # (/=) :: MutableArray s a -> MutableArray s a -> Bool # |
data MutablePrimArray s a #
Mutable primitive arrays associated with a primitive state token.
These can be written to and read from in a monadic context that supports
sequencing, such as IO
or ST
. Typically, a mutable primitive array will
be built and then converted to an immutable primitive array using
unsafeFreezePrimArray
. However, it is also acceptable to simply discard
a mutable primitive array since it lives in managed memory and will be
garbage collected when no longer referenced.
Instances
NFData (MutablePrimArray s a) | |
Defined in Data.Primitive.PrimArray rnf :: MutablePrimArray s a -> () # | |
Eq (MutablePrimArray s a) | |
Defined in Data.Primitive.PrimArray (==) :: MutablePrimArray s a -> MutablePrimArray s a -> Bool # (/=) :: MutablePrimArray s a -> MutablePrimArray s a -> Bool # |
Arrays of unboxed elements. This accepts types like Double
, Char
,
Int
and Word
, as well as their fixed-length variants (Word8
,
Word16
, etc.). Since the elements are unboxed, a PrimArray
is strict
in its elements. This differs from the behavior of Array
,
which is lazy in its elements.
Instances
Lift (PrimArray a :: Type) | |
Monoid (PrimArray a) | Since: primitive-0.6.4.0 |
Semigroup (PrimArray a) | Since: primitive-0.6.4.0 |
Prim a => IsList (PrimArray a) | Since: primitive-0.6.4.0 |
(Show a, Prim a) => Show (PrimArray a) | Since: primitive-0.6.4.0 |
NFData (PrimArray a) | |
Defined in Data.Primitive.PrimArray | |
(Eq a, Prim a) => Eq (PrimArray a) | Since: primitive-0.6.4.0 |
(Ord a, Prim a) => Ord (PrimArray a) | Lexicographic ordering. Subject to change between major versions. Since: primitive-0.6.4.0 |
Defined in Data.Primitive.PrimArray | |
type Item (PrimArray a) | |
Defined in Data.Primitive.PrimArray |
fromList :: IsList l => [Item l] -> l #
The fromList
function constructs the structure l
from the given
list of Item l
emptyArray :: Array a #
The empty Array
.
newArray :: PrimMonad m => Int -> a -> m (MutableArray (PrimState m) a) #
Create a new mutable array of the specified size and initialise all elements with the given value.
Note: this function does not check if the input is non-negative.
newByteArray :: PrimMonad m => Int -> m (MutableByteArray (PrimState m)) #
Create a new mutable byte array of the specified size in bytes.
Note: this function does not check if the input is non-negative.
unsafeFreezeByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> m ByteArray #
Convert a mutable byte array to an immutable one without copying. The array should not be modified after the conversion.
sizeofByteArray :: ByteArray -> Int #
Size of the byte array in bytes.
byteArrayFromListN :: Prim a => Int -> [a] -> ByteArray #
Create a ByteArray
from a list of a known length. If the length
of the list does not match the given length, this throws an exception.
The empty ByteArray
.
:: (PrimMonad m, Prim a) | |
=> Ptr a | destination |
-> ByteArray | source array |
-> Int | offset into source array, interpreted as elements of type |
-> Int | number of elements to copy |
-> m () |
Copy a slice of a byte array to an unmanaged pointer address. These must not overlap. The offset and length are given in elements, not in bytes.
Note: this function does not do bounds or overlap checking.
Since: primitive-0.7.1.0
newPinnedByteArray :: PrimMonad m => Int -> m (MutableByteArray (PrimState m)) #
Create a pinned byte array of the specified size in bytes. The garbage collector is guaranteed not to move it.
Note: this function does not check if the input is non-negative.
:: (Prim a, PrimMonad m) | |
=> MutableByteArray (PrimState m) | array to fill |
-> Int | offset into array |
-> Int | number of values to fill |
-> a | value to fill with |
-> m () |
Fill a slice of a mutable byte array with a value. The offset and length
are given in elements of type a
rather than in bytes.
Note: this function does not do bounds checking.
sizeofArray :: Array a -> Int #
The number of elements in an immutable array.
sizeofMutableArray :: MutableArray s a -> Int #
The number of elements in a mutable array.
indexArray :: Array a -> Int -> a #
Read a value from the immutable array at the given index.
Note: this function does not do bounds checking.
indexArray## :: Array a -> Int -> (# a #) #
Read a value from the immutable array at the given index, returning the result in an unboxed unary tuple. This is currently used to implement folds.
Note: this function does not do bounds checking.
indexArrayM :: Monad m => Array a -> Int -> m a #
Monadically read a value from the immutable array at the given index. This allows us to be strict in the array while remaining lazy in the read element which is very useful for collective operations. Suppose we want to copy an array. We could do something like this:
copy marr arr ... = do ... writeArray marr i (indexArray arr i) ... ...
But since the arrays are lazy, the calls to indexArray
will not be
evaluated. Rather, marr
will be filled with thunks each of which would
retain a reference to arr
. This is definitely not what we want!
With indexArrayM
, we can instead write
copy marr arr ... = do ... x <- indexArrayM arr i writeArray marr i x ...
Now, indexing is executed immediately although the returned element is still not evaluated.
Note: this function does not do bounds checking.
Create an immutable copy of a slice of an array.
This operation makes a copy of the specified section, so it is safe to continue using the mutable array afterward.
Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.
unsafeFreezeArray :: PrimMonad m => MutableArray (PrimState m) a -> m (Array a) #
Convert a mutable array to an immutable one without copying. The array should not be modified after the conversion.
Create a mutable array from a slice of an immutable array.
This operation makes a copy of the specified slice, so it is safe to use the immutable array afterward.
Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.
unsafeThawArray :: PrimMonad m => Array a -> m (MutableArray (PrimState m) a) #
Convert an immutable array to an mutable one without copying. The immutable array should not be used after the conversion.
sameMutableArray :: MutableArray s a -> MutableArray s a -> Bool #
Check whether the two arrays refer to the same memory block.
runArray :: (forall s. ST s (MutableArray s a)) -> Array a #
Execute the monadic action and freeze the resulting array.
runArray m = runST $ m >>= unsafeFreezeArray
createArray :: Int -> a -> (forall s. MutableArray s a -> ST s ()) -> Array a #
Create an array of the given size with a default value,
apply the monadic function and freeze the result. If the
size is 0, return emptyArray
(rather than a new copy thereof).
createArray 0 _ _ = emptyArray createArray n x f = runArray $ do mary <- newArray n x f mary pure mary
traverseArrayP :: PrimMonad m => (a -> m b) -> Array a -> m (Array b) #
arrayFromListN :: Int -> [a] -> Array a #
Create an array from a list of a known length. If the length of the list does not match the given length, this throws an exception.
arrayFromList :: [a] -> Array a #
Create an array from a list.
:: PrimMonad m | |
=> Int | size |
-> Int | alignment |
-> m (MutableByteArray (PrimState m)) |
Create a pinned byte array of the specified size in bytes and with the given alignment. The garbage collector is guaranteed not to move it.
Note: this function does not check if the input is non-negative.
byteArrayContents :: ByteArray -> Ptr Word8 #
Yield a pointer to the array's data. This operation is only safe on
pinned byte arrays allocated by newPinnedByteArray
or
newAlignedPinnedByteArray
.
mutableByteArrayContents :: MutableByteArray s -> Ptr Word8 #
Yield a pointer to the array's data. This operation is only safe on
pinned byte arrays allocated by newPinnedByteArray
or
newAlignedPinnedByteArray
.
sameMutableByteArray :: MutableByteArray s -> MutableByteArray s -> Bool #
Check if the two arrays refer to the same memory block.
resizeMutableByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m (MutableByteArray (PrimState m)) #
Resize a mutable byte array. The new size is given in bytes.
This will either resize the array in-place or, if not possible, allocate the contents into a new, unpinned array and copy the original array's contents.
To avoid undefined behaviour, the original MutableByteArray
shall not be
accessed anymore after a resizeMutableByteArray
has been performed.
Moreover, no reference to the old one should be kept in order to allow
garbage collection of the original MutableByteArray
in case a new
MutableByteArray
had to be allocated.
Since: primitive-0.6.4.0
getSizeofMutableByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> m Int #
Get the size of a byte array in bytes. Unlike sizeofMutableByteArray
,
this function ensures sequencing in the presence of resizing.
:: PrimMonad m | |
=> MutableByteArray (PrimState m) | source |
-> Int | offset in bytes |
-> Int | length in bytes |
-> m ByteArray |
Create an immutable copy of a slice of a byte array. The offset and length are given in bytes.
This operation makes a copy of the specified section, so it is safe to continue using the mutable array afterward.
Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.
:: PrimMonad m | |
=> ByteArray | source |
-> Int | offset in bytes |
-> Int | length in bytes |
-> m (MutableByteArray (PrimState m)) |
Create a mutable byte array from a slice of an immutable byte array. The offset and length are given in bytes.
This operation makes a copy of the specified slice, so it is safe to use the immutable array afterward.
Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.
Since: primitive-0.7.2.0
unsafeThawByteArray :: PrimMonad m => ByteArray -> m (MutableByteArray (PrimState m)) #
Convert an immutable byte array to a mutable one without copying. The original array should not be used after the conversion.
sizeofMutableByteArray :: MutableByteArray s -> Int #
Size of the mutable byte array in bytes. This function's behavior
is undefined if resizeMutableByteArray
is ever called on the mutable
byte array given as the argument. Consequently, use of this function
is discouraged. Prefer getSizeofMutableByteArray
, which ensures correct
sequencing in the presence of resizing.
:: PrimMonad m | |
=> MutableByteArray (PrimState m) | |
-> Int | new size |
-> m () |
Shrink a mutable byte array. The new size is given in bytes. It must be smaller than the old size. The array will be resized in place.
Since: primitive-0.7.1.0
isByteArrayPinned :: ByteArray -> Bool #
Check whether or not the byte array is pinned. Pinned byte arrays cannot
be moved by the garbage collector. It is safe to use byteArrayContents
on
such byte arrays.
Caution: This function is only available when compiling with GHC 8.2 or newer.
Since: primitive-0.6.4.0
isMutableByteArrayPinned :: MutableByteArray s -> Bool #
Check whether or not the mutable byte array is pinned.
Caution: This function is only available when compiling with GHC 8.2 or newer.
Since: primitive-0.6.4.0
foldrByteArray :: Prim a => (a -> b -> b) -> b -> ByteArray -> b #
Right-fold over the elements of a ByteArray
.
byteArrayFromList :: Prim a => [a] -> ByteArray #
Create a ByteArray
from a list.
byteArrayFromList xs = byteArrayFromListN
(length xs) xs
:: (PrimMonad m, Prim a) | |
=> MutableByteArray (PrimState m) | destination array |
-> Int | destination offset given in elements of type |
-> Ptr a | source pointer |
-> Int | number of elements |
-> m () |
Copy from an unmanaged pointer address to a byte array. These must not overlap. The offset and length are given in elements, not in bytes.
Note: this function does not do bounds or overlap checking.
:: (PrimMonad m, Prim a) | |
=> Ptr a | destination |
-> MutableByteArray (PrimState m) | source array |
-> Int | offset into source array, interpreted as elements of type |
-> Int | number of elements to copy |
-> m () |
Copy a slice of a mutable byte array to an unmanaged pointer address. These must not overlap. The offset and length are given in elements, not in bytes.
Note: this function does not do bounds or overlap checking.
Since: primitive-0.7.1.0
:: PrimMonad m | |
=> Ptr Word8 | destination |
-> ByteArray | source array |
-> Int | offset into source array |
-> Int | number of bytes to copy |
-> m () |
Copy a slice of a byte array to an unmanaged address. These must not overlap.
Note: This function is just copyByteArrayToPtr
where a
is Word8
.
Since: primitive-0.6.4.0
:: PrimMonad m | |
=> Ptr Word8 | destination |
-> MutableByteArray (PrimState m) | source array |
-> Int | offset into source array |
-> Int | number of bytes to copy |
-> m () |
Copy a slice of a mutable byte array to an unmanaged address. These must not overlap.
Note: This function is just copyMutableByteArrayToPtr
where a
is Word8
.
Since: primitive-0.6.4.0
:: PrimMonad m | |
=> MutableByteArray (PrimState m) | array to fill |
-> Int | offset into array |
-> Int | number of bytes to fill |
-> Word8 | byte to fill with |
-> m () |
Fill a slice of a mutable byte array with a byte.
Note: this function does not do bounds checking.
:: ByteArray | array A |
-> Int | offset A, given in bytes |
-> ByteArray | array B |
-> Int | offset B, given in bytes |
-> Int | length of the slice, given in bytes |
-> Ordering |
Lexicographic comparison of equal-length slices into two byte arrays.
This wraps the compareByteArrays#
primop, which wraps memcmp
.
:: ByteArray | source array |
-> Int | offset into destination array |
-> Int | number of bytes to copy |
-> ByteArray |
Return a newly allocated array with the specified subrange of the provided array. The provided array should contain the full subrange specified by the two Ints, but this is not checked.
:: PrimMonad m | |
=> MutableByteArray (PrimState m) | source array |
-> Int | offset into destination array |
-> Int | number of bytes to copy |
-> m (MutableByteArray (PrimState m)) |
Return a newly allocated mutable array with the specified subrange of the provided mutable array. The provided mutable array should contain the full subrange specified by the two Ints, but this is not checked.
runByteArray :: (forall s. ST s (MutableByteArray s)) -> ByteArray #
Execute the monadic action and freeze the resulting array.
runByteArray m = runST $ m >>= unsafeFreezeByteArray
readCharArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m Char #
Read an 8-bit element from the byte array, interpreting it as a Latin-1-encoded character. The offset is given in bytes.
Note: this function does not do bounds checking.
writeCharArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Char -> m () #
Write a character to the byte array, encoding it with Latin-1 as a single byte. Behavior is undefined for codepoints outside of the ASCII and Latin-1 blocks. The offset is given in bytes.
Note: this function does not do bounds checking.
indexCharArray :: ByteArray -> Int -> Char #
Read an 8-bit element from the byte array, interpreting it as a Latin-1-encoded character. The offset is given in bytes.
Note: this function does not do bounds checking.
primArrayFromList :: Prim a => [a] -> PrimArray a #
Create a PrimArray
from a list.
primArrayFromList vs = primArrayFromListN
(length vs) vs
primArrayFromListN :: Prim a => Int -> [a] -> PrimArray a #
Create a PrimArray
from a list of a known length. If the length
of the list does not match the given length, this throws an exception.
primArrayToList :: Prim a => PrimArray a -> [a] #
Convert a PrimArray
to a list.
emptyPrimArray :: PrimArray a #
The empty PrimArray
.
newPrimArray :: (PrimMonad m, Prim a) => Int -> m (MutablePrimArray (PrimState m) a) #
Create a new mutable primitive array of the given length. The underlying memory is left uninitialized.
Note: this function does not check if the input is non-negative.
:: (PrimMonad m, Prim a) | |
=> MutablePrimArray (PrimState m) a | |
-> Int | new size |
-> m (MutablePrimArray (PrimState m) a) |
Resize a mutable primitive array. The new size is given in elements.
This will either resize the array in-place or, if not possible, allocate the contents into a new, unpinned array and copy the original array's contents.
To avoid undefined behaviour, the original MutablePrimArray
shall not be
accessed anymore after a resizeMutablePrimArray
has been performed.
Moreover, no reference to the old one should be kept in order to allow
garbage collection of the original MutablePrimArray
in case a new
MutablePrimArray
had to be allocated.
:: (PrimMonad m, Prim a) | |
=> MutablePrimArray (PrimState m) a | |
-> Int | new size |
-> m () |
Shrink a mutable primitive array. The new size is given in elements. It must be smaller than the old size. The array will be resized in place.
:: (PrimMonad m, Prim a) | |
=> MutablePrimArray (PrimState m) a | destination array |
-> Int | offset into destination array |
-> MutablePrimArray (PrimState m) a | source array |
-> Int | offset into source array |
-> Int | number of elements to copy |
-> m () |
Copy part of a mutable array into another mutable array. In the case that the destination and source arrays are the same, the regions may overlap.
Note: this function does not do bounds or overlap checking.
:: (PrimMonad m, Prim a) | |
=> MutablePrimArray (PrimState m) a | destination array |
-> Int | offset into destination array |
-> PrimArray a | source array |
-> Int | offset into source array |
-> Int | number of elements to copy |
-> m () |
Copy part of an array into another mutable array.
Note: this function does not do bounds or overlap checking.
:: (PrimMonad m, Prim a) | |
=> Ptr a | destination pointer |
-> PrimArray a | source array |
-> Int | offset into source array |
-> Int | number of elements to copy |
-> m () |
Copy a slice of an immutable primitive array to a pointer.
The offset and length are given in elements of type a
.
This function assumes that the Prim
instance of a
agrees with the Storable
instance.
Note: this function does not do bounds or overlap checking.
:: (PrimMonad m, Prim a) | |
=> Ptr a | destination pointer |
-> MutablePrimArray (PrimState m) a | source array |
-> Int | offset into source array |
-> Int | number of elements to copy |
-> m () |
Copy a slice of a mutable primitive array to a pointer.
The offset and length are given in elements of type a
.
This function assumes that the Prim
instance of a
agrees with the Storable
instance.
Note: this function does not do bounds or overlap checking.
:: (PrimMonad m, Prim a) | |
=> MutablePrimArray (PrimState m) a | destination array |
-> Int | destination offset |
-> Ptr a | source pointer |
-> Int | number of elements |
-> m () |
Copy from a pointer to a mutable primitive array.
The offset and length are given in elements of type a
.
This function assumes that the Prim
instance of a
agrees with the Storable
instance.
Note: this function does not do bounds or overlap checking.
:: (Prim a, PrimMonad m) | |
=> MutablePrimArray (PrimState m) a | array to fill |
-> Int | offset into array |
-> Int | number of values to fill |
-> a | value to fill with |
-> m () |
Fill a slice of a mutable primitive array with a value.
Note: this function does not do bounds checking.
:: (PrimMonad m, Prim a) | |
=> MutablePrimArray (PrimState m) a | array |
-> m Int |
Get the size of a mutable primitive array in elements. Unlike sizeofMutablePrimArray
,
this function ensures sequencing in the presence of resizing.
sizeofMutablePrimArray :: forall s a. Prim a => MutablePrimArray s a -> Int #
Size of the mutable primitive array in elements. This function shall not
be used on primitive arrays that are an argument to or a result of
resizeMutablePrimArray
or shrinkMutablePrimArray
.
sameMutablePrimArray :: MutablePrimArray s a -> MutablePrimArray s a -> Bool #
Check if the two arrays refer to the same memory block.
:: (PrimMonad m, Prim a) | |
=> MutablePrimArray (PrimState m) a | source |
-> Int | offset in elements |
-> Int | length in elements |
-> m (PrimArray a) |
Create an immutable copy of a slice of a primitive array. The offset and length are given in elements.
This operation makes a copy of the specified section, so it is safe to continue using the mutable array afterward.
Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.
:: (PrimMonad m, Prim a) | |
=> PrimArray a | source |
-> Int | offset in elements |
-> Int | length in elements |
-> m (MutablePrimArray (PrimState m) a) |
Create a mutable primitive array from a slice of an immutable primitive array. The offset and length are given in elements.
This operation makes a copy of the specified slice, so it is safe to use the immutable array afterward.
Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.
Since: primitive-0.7.2.0
unsafeFreezePrimArray :: PrimMonad m => MutablePrimArray (PrimState m) a -> m (PrimArray a) #
Convert a mutable primitive array to an immutable one without copying. The array should not be modified after the conversion.
unsafeThawPrimArray :: PrimMonad m => PrimArray a -> m (MutablePrimArray (PrimState m) a) #
Convert an immutable array to a mutable one without copying. The original array should not be used after the conversion.
sizeofPrimArray :: Prim a => PrimArray a -> Int #
Get the size, in elements, of the primitive array.
isPrimArrayPinned :: PrimArray a -> Bool #
Check whether or not the primitive array is pinned. Pinned primitive arrays cannot
be moved by the garbage collector. It is safe to use primArrayContents
on such arrays. This function is only available when compiling with
GHC 8.2 or newer.
Since: primitive-0.7.1.0
isMutablePrimArrayPinned :: MutablePrimArray s a -> Bool #
Check whether or not the mutable primitive array is pinned. This function is only available when compiling with GHC 8.2 or newer.
Since: primitive-0.7.1.0
foldrPrimArray :: Prim a => (a -> b -> b) -> b -> PrimArray a -> b #
Lazy right-associated fold over the elements of a PrimArray
.
foldrPrimArray' :: Prim a => (a -> b -> b) -> b -> PrimArray a -> b #
Strict right-associated fold over the elements of a PrimArray
.
foldlPrimArray :: Prim a => (b -> a -> b) -> b -> PrimArray a -> b #
Lazy left-associated fold over the elements of a PrimArray
.
foldlPrimArray' :: Prim a => (b -> a -> b) -> b -> PrimArray a -> b #
Strict left-associated fold over the elements of a PrimArray
.
foldlPrimArrayM' :: (Prim a, Monad m) => (b -> a -> m b) -> b -> PrimArray a -> m b #
Strict left-associated fold over the elements of a PrimArray
.
traversePrimArrayP :: (PrimMonad m, Prim a, Prim b) => (a -> m b) -> PrimArray a -> m (PrimArray b) #
Traverse a primitive array. The traversal forces the resulting values and writes them to the new primitive array as it performs the monadic effects. Consequently:
>>>
traversePrimArrayP (\x -> print x $> bool x undefined (x == 2)) (fromList [1, 2, 3 :: Int])
1 2 *** Exception: Prelude.undefined
In many situations, traversePrimArrayP
can replace traversePrimArray
,
changing the strictness characteristics of the traversal but typically improving
the performance. Consider the following short-circuiting traversal:
incrPositiveA :: PrimArray Int -> Maybe (PrimArray Int) incrPositiveA xs = traversePrimArray (\x -> bool Nothing (Just (x + 1)) (x > 0)) xs
This can be rewritten using traversePrimArrayP
. To do this, we must
change the traversal context to MaybeT (ST s)
, which has a PrimMonad
instance:
incrPositiveB :: PrimArray Int -> Maybe (PrimArray Int) incrPositiveB xs = runST $ runMaybeT $ traversePrimArrayP (\x -> bool (MaybeT (return Nothing)) (MaybeT (return (Just (x + 1)))) (x > 0)) xs
Benchmarks demonstrate that the second implementation runs 150 times faster than the first. It also results in fewer allocations.
filterPrimArrayP :: (PrimMonad m, Prim a) => (a -> m Bool) -> PrimArray a -> m (PrimArray a) #
Filter the primitive array, keeping the elements for which the monadic predicate evaluates to true.
mapMaybePrimArrayP :: (PrimMonad m, Prim a, Prim b) => (a -> m (Maybe b)) -> PrimArray a -> m (PrimArray b) #
Map over the primitive array, keeping the elements for which the monadic
predicate provides a Just
.
Generate a primitive array by evaluating the monadic generator function at each index.
replicatePrimArrayP :: (PrimMonad m, Prim a) => Int -> m a -> m (PrimArray a) #
Execute the monadic action the given number of times and store the results in a primitive array.
mapPrimArray :: (Prim a, Prim b) => (a -> b) -> PrimArray a -> PrimArray b #
Map over the elements of a primitive array.
imapPrimArray :: (Prim a, Prim b) => (Int -> a -> b) -> PrimArray a -> PrimArray b #
Indexed map over the elements of a primitive array.
filterPrimArray :: Prim a => (a -> Bool) -> PrimArray a -> PrimArray a #
Filter elements of a primitive array according to a predicate.
:: (Applicative f, Prim a) | |
=> (a -> f Bool) | mapping function |
-> PrimArray a | primitive array |
-> f (PrimArray a) |
Filter the primitive array, keeping the elements for which the monadic predicate evaluates true.
:: (Applicative f, Prim a, Prim b) | |
=> (a -> f (Maybe b)) | mapping function |
-> PrimArray a | primitive array |
-> f (PrimArray b) |
Map over the primitive array, keeping the elements for which the applicative
predicate provides a Just
.
mapMaybePrimArray :: (Prim a, Prim b) => (a -> Maybe b) -> PrimArray a -> PrimArray b #
Map over a primitive array, optionally discarding some elements. This
has the same behavior as Data.Maybe.mapMaybe
.
:: (Applicative f, Prim a, Prim b) | |
=> (a -> f b) | mapping function |
-> PrimArray a | primitive array |
-> f (PrimArray b) |
Traverse a primitive array. The traversal performs all of the applicative effects before forcing the resulting values and writing them to the new primitive array. Consequently:
>>>
traversePrimArray (\x -> print x $> bool x undefined (x == 2)) (fromList [1, 2, 3 :: Int])
1 2 3 *** Exception: Prelude.undefined
The function traversePrimArrayP
always outperforms this function, but it
requires a PrimMonad
constraint, and it forces the values as
it performs the effects.
itraversePrimArray :: (Applicative f, Prim a, Prim b) => (Int -> a -> f b) -> PrimArray a -> f (PrimArray b) #
Traverse a primitive array with the index of each element.
itraversePrimArrayP :: (Prim a, Prim b, PrimMonad m) => (Int -> a -> m b) -> PrimArray a -> m (PrimArray b) #
Traverse a primitive array with the indices. The traversal forces the resulting values and writes them to the new primitive array as it performs the monadic effects.
Generate a primitive array.
Create a primitive array by copying the element the given number of times.
:: (Applicative f, Prim a) | |
=> Int | length |
-> (Int -> f a) | element from index |
-> f (PrimArray a) |
Generate a primitive array by evaluating the applicative generator function at each index.
:: (Applicative f, Prim a) | |
=> Int | length |
-> f a | applicative element producer |
-> f (PrimArray a) |
Execute the applicative action the given number of times and store the
results in a PrimArray
.
traversePrimArray_ :: (Applicative f, Prim a) => (a -> f b) -> PrimArray a -> f () #
Traverse the primitive array, discarding the results. There
is no PrimMonad
variant of this function, since it would not provide
any performance benefit.
itraversePrimArray_ :: (Applicative f, Prim a) => (Int -> a -> f b) -> PrimArray a -> f () #
Traverse the primitive array with the indices, discarding the results.
There is no PrimMonad
variant of this function, since it would not
provide any performance benefit.
newPinnedPrimArray :: (PrimMonad m, Prim a) => Int -> m (MutablePrimArray (PrimState m) a) #
Create a pinned primitive array of the specified size (in elements). The garbage collector is guaranteed not to move it.
Since: primitive-0.7.1.0
newAlignedPinnedPrimArray :: (PrimMonad m, Prim a) => Int -> m (MutablePrimArray (PrimState m) a) #
Create a pinned primitive array of the specified size (in elements) and
with the alignment given by its Prim
instance. The garbage collector is
guaranteed not to move it.
Since: primitive-0.7.0.0
primArrayContents :: PrimArray a -> Ptr a #
Yield a pointer to the array's data. This operation is only safe on
pinned prim arrays allocated by newPinnedByteArray
or
newAlignedPinnedByteArray
.
Since: primitive-0.7.1.0
mutablePrimArrayContents :: MutablePrimArray s a -> Ptr a #
Yield a pointer to the array's data. This operation is only safe on
pinned byte arrays allocated by newPinnedByteArray
or
newAlignedPinnedByteArray
.
Since: primitive-0.7.1.0
:: Prim a | |
=> PrimArray a | source array |
-> Int | offset into destination array |
-> Int | number of elements to copy |
-> PrimArray a |
Return a newly allocated array with the specified subrange of the provided array. The provided array should contain the full subrange specified by the two Ints, but this is not checked.
:: (PrimMonad m, Prim a) | |
=> MutablePrimArray (PrimState m) a | source array |
-> Int | offset into destination array |
-> Int | number of elements to copy |
-> m (MutablePrimArray (PrimState m) a) |
Return a newly allocated mutable array with the specified subrange of the provided mutable array. The provided mutable array should contain the full subrange specified by the two Ints, but this is not checked.
runPrimArray :: (forall s. ST s (MutablePrimArray s a)) -> PrimArray a #
Execute the monadic action and freeze the resulting array.
runPrimArray m = runST $ m >>= unsafeFreezePrimArray
byteArrayToList :: ByteArray -> [Word8] Source #
writeArray :: CheckCtx => PrimMonad m => MutableArray (PrimState m) a -> Int -> a -> m () Source #
copyArray :: CheckCtx => PrimMonad m => MutableArray (PrimState m) a -> Int -> Array a -> Int -> Int -> m () Source #
copyMutableArray :: CheckCtx => PrimMonad m => MutableArray (PrimState m) a -> Int -> MutableArray (PrimState m) a -> Int -> Int -> m () Source #
cloneMutableArray :: CheckCtx => PrimMonad m => MutableArray (PrimState m) a -> Int -> Int -> m (MutableArray (PrimState m) a) Source #
readByteArray :: forall a m. CheckCtx => PrimMonad m => Prim a => MutableByteArray (PrimState m) -> Int -> m a Source #
writeByteArray :: forall a m. CheckCtx => PrimMonad m => Prim a => MutableByteArray (PrimState m) -> Int -> a -> m () Source #
copyByteArray :: CheckCtx => PrimMonad m => MutableByteArray (PrimState m) -> Int -> ByteArray -> Int -> Int -> m () Source #
copyMutableByteArray :: CheckCtx => PrimMonad m => MutableByteArray (PrimState m) -> Int -> MutableByteArray (PrimState m) -> Int -> Int -> m () Source #
moveByteArray :: CheckCtx => PrimMonad m => MutableByteArray (PrimState m) -> Int -> MutableByteArray (PrimState m) -> Int -> Int -> m () Source #
readPrimArray :: CheckCtx => PrimMonad m => Prim a => MutablePrimArray (PrimState m) a -> Int -> m a Source #
writePrimArray :: CheckCtx => PrimMonad m => Prim a => MutablePrimArray (PrimState m) a -> Int -> a -> m () Source #