| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Unison.Runtime.Array
Description
This module wraps the operations in the primitive package so that bounds checks can be toggled on during the build for debugging purposes. It exports the entire API for the three array types needed, and adds wrappers for the operations that are unchecked in the base library.
Checking is toggled using the arraychecks flag.
Synopsis
- data ByteArray# :: UnliftedType
- data MutableByteArray# a :: UnliftedType
- data Array a = Array {}
- emptyArray :: Array a
- newArray :: PrimMonad m => Int -> a -> m (MutableArray (PrimState m) a)
- data MutableByteArray s = MutableByteArray (MutableByteArray# s)
- data ByteArray = ByteArray ByteArray#
- 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
- isByteArrayPinned :: ByteArray -> Bool
- foldrByteArray :: Prim a => (a -> b -> b) -> b -> ByteArray -> b
- copyPtrToMutableByteArray :: (PrimMonad m, Prim a) => MutableByteArray (PrimState m) -> Int -> Ptr a -> Int -> m ()
- copyByteArrayToPtr :: (PrimMonad m, Prim a) => Ptr a -> ByteArray -> Int -> Int -> m ()
- cloneArray :: Array a -> Int -> Int -> Array a
- newAlignedPinnedByteArray :: PrimMonad m => Int -> Int -> m (MutableByteArray (PrimState m))
- data MutableArray s a = MutableArray {
- marray# :: MutableArray# s a
- createArray :: Int -> a -> (forall s. MutableArray s a -> ST s ()) -> Array a
- shrinkMutableByteArray :: PrimMonad m => MutableByteArray (PrimState m) -> 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 :: Applicative 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
- runArray :: (forall s. ST s (MutableArray s a)) -> 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
- byteArrayAsForeignPtr :: ByteArray -> ForeignPtr Word8
- mutableByteArrayAsForeignPtr :: MutableByteArray RealWorld -> ForeignPtr Word8
- byteArrayContents :: ByteArray -> Ptr Word8
- withByteArrayContents :: PrimBase m => ByteArray -> (Ptr Word8 -> m a) -> m a
- mutableByteArrayContents :: MutableByteArray s -> Ptr Word8
- withMutableByteArrayContents :: PrimBase m => MutableByteArray (PrimState m) -> (Ptr Word8 -> m a) -> m a
- 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
- isMutableByteArrayPinned :: MutableByteArray s -> Bool
- byteArrayFromList :: Prim a => [a] -> ByteArray
- 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
- createByteArray :: Int -> (forall s. MutableByteArray s -> ST s ()) -> ByteArray
- readCharArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m Char
- writeCharArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Char -> m ()
- indexCharArray :: ByteArray -> Int -> Char
- data MutablePrimArray s a = MutablePrimArray (MutableByteArray# s)
- data PrimArray a = PrimArray ByteArray#
- 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
- createPrimArray :: Prim a => Int -> (forall s. MutablePrimArray s a -> ST s ()) -> PrimArray a
- withPrimArrayContents :: PrimBase m => PrimArray a -> (Ptr a -> m a) -> m a
- withMutablePrimArrayContents :: PrimBase m => MutablePrimArray (PrimState m) a -> (Ptr a -> m a) -> m 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 ()
- traverseArrayIO :: (a -> IO b) -> Array a -> IO (Array b)
- 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 :: (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
- byteArrayToShortByteString :: ByteArray -> ShortByteString
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
| MonadZip Array | |
| 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 |
| NFData1 Array | |
Defined in Data.Primitive.Array | |
| Alternative Array | |
| Applicative Array | |
| Functor Array | |
| Monad Array | |
| MonadPlus Array | |
| MonadFail Array | |
Defined in Data.Primitive.Array | |
| MonadFix Array | |
Defined in Data.Primitive.Array | |
| Foldable Array | |
Defined in Data.Primitive.Array Methods 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 # | |
| Traversable Array | |
| Lift a => Lift (Array a :: Type) | |
| NFData a => NFData (Array a) | |
Defined in Data.Primitive.Array | |
| Monoid (Array a) | |
| Semigroup (Array a) | Since: primitive-0.6.3.0 |
| Data a => Data (Array a) | |
Defined in Data.Primitive.Array Methods 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) # | |
| IsList (Array a) | |
| Read a => Read (Array a) | |
| Show a => Show (Array a) | |
| Eq a => Eq (Array a) | |
| Ord a => Ord (Array a) | Lexicographic ordering. Subject to change between major versions. |
Defined in Data.Primitive.Array | |
| BuiltinForeign (Array Val) Source # | |
| type Mutable Array | |
Defined in Data.Vector.Generic.Base | |
| type Item (Array a) | |
Defined in Data.Primitive.Array | |
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.
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
Constructors
| MutableByteArray (MutableByteArray# s) |
Instances
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
Constructors
| ByteArray ByteArray# |
Instances
| Binary ByteArray | Since: binary-orphans-1.0.3 |
| NFData ByteArray | Since: deepseq-1.4.7.0 |
Defined in Control.DeepSeq | |
| Monoid ByteArray | Since: base-4.17.0.0 |
| Semigroup ByteArray | Since: base-4.17.0.0 |
| Data ByteArray | Since: base-4.17.0.0 |
Defined in Data.Array.Byte Methods 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 # | |
| IsList ByteArray | Since: base-4.17.0.0 |
| Show ByteArray | Since: base-4.17.0.0 |
| 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 |
| 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 | |
| BuiltinForeign ByteArray Source # | |
Defined in Unison.Runtime.Stack | |
| Lift ByteArray | Since: template-haskell-2.19.0.0 |
| type Item ByteArray | |
Defined in Data.Array.Byte | |
newByteArray :: PrimMonad m => Int -> m (MutableByteArray (PrimState m)) #
Create a new mutable byte array of the specified size in bytes. The underlying memory is left uninitialized.
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.
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
foldrByteArray :: Prim a => (a -> b -> b) -> b -> ByteArray -> b #
Right-fold over the elements of a ByteArray.
Arguments
| :: (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.
Arguments
| :: (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
Arguments
| :: 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. The underlying memory is left uninitialized.
Note: this function does not check if the input is non-negative.
data MutableArray s a #
Mutable boxed arrays associated with a primitive state token.
Constructors
| MutableArray | |
Fields
| |
Instances
| (Typeable s, Typeable a) => Data (MutableArray s a) | |
Defined in Data.Primitive.Array Methods 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 Methods (==) :: MutableArray s a -> MutableArray s a -> Bool # (/=) :: MutableArray s a -> MutableArray s a -> Bool # | |
| BuiltinForeign (MutableArray RealWorld Val) Source # | |
Defined in Unison.Runtime.Stack Methods builtinName :: Tagged (MutableArray RealWorld Val) String Source # wrapBuiltin :: MutableArray RealWorld Val -> Foreign Source # maybeUnwrapBuiltin :: Foreign -> Maybe (MutableArray RealWorld Val) Source # | |
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
Arguments
| :: 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
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. The underlying memory is left uninitialized.
Note: this function does not check if the input is non-negative.
Arguments
| :: (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 :: Applicative m => Array a -> Int -> m a #
Read a value from the immutable array at the given index using an applicative. 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
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.
byteArrayAsForeignPtr :: ByteArray -> ForeignPtr Word8 #
Create a foreign pointer that points to the array's data. This operation is only safe on pinned byte arrays. The array's data is not garbage collected while references to the foreign pointer exist. Writing to the array through the foreign pointer results in undefined behavior.
mutableByteArrayAsForeignPtr :: MutableByteArray RealWorld -> ForeignPtr Word8 #
Variant of byteArrayAsForeignPtr for mutable byte arrays. Similarly, this
is only safe on pinned mutable byte arrays. This function differs from the
variant for immutable arrays in that it is safe to write to the array though
the foreign pointer.
byteArrayContents :: ByteArray -> Ptr Word8 #
Yield a pointer to the array's data. This operation is only safe on
pinned byte arrays. Byte arrays allocated by newPinnedByteArray and
newAlignedPinnedByteArray are guaranteed to be pinned. Byte arrays
allocated by newByteArray may or may not be pinned (use
isByteArrayPinned to figure out).
Prefer withByteArrayContents, which ensures that the array is not
garbage collected while the pointer is being used.
withByteArrayContents :: PrimBase m => ByteArray -> (Ptr Word8 -> m a) -> m a #
A composition of byteArrayContents and keepAliveUnlifted.
The callback function must not return the pointer. The argument byte
array must be pinned. See byteArrayContents for an explanation
of which byte arrays are pinned.
Note: This could be implemented with keepAlive instead of
keepAliveUnlifted, but keepAlive here would cause GHC to materialize
the wrapper data constructor on the heap.
mutableByteArrayContents :: MutableByteArray s -> Ptr Word8 #
Yield a pointer to the array's data. This operation is only safe on
pinned byte arrays. See byteArrayContents for an explanation
of which byte arrays are pinned.
Prefer withByteArrayContents, which ensures that the array is not
garbage collected while the pointer is being used.
withMutableByteArrayContents :: PrimBase m => MutableByteArray (PrimState m) -> (Ptr Word8 -> m a) -> m a #
A composition of mutableByteArrayContents and keepAliveUnlifted.
The callback function must not return the pointer. The argument byte
array must be pinned. See byteArrayContents for an explanation
of which byte arrays are pinned.
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.
Arguments
| :: 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.
Arguments
| :: 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 is deprecated and will be removed. Its behavior
is undefined if resizeMutableByteArray is ever called on the mutable
byte array given as the argument. Prefer getSizeofMutableByteArray,
which ensures correct sequencing in the presence of resizing.
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
byteArrayFromList :: Prim a => [a] -> ByteArray #
Create a ByteArray from a list.
byteArrayFromList xs = byteArrayFromListN (length xs) xsArguments
| :: (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
Arguments
| :: 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
Arguments
| :: 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
Arguments
| :: 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.
Arguments
| :: 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.
Arguments
| :: 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.
Arguments
| :: 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
createByteArray :: Int -> (forall s. MutableByteArray s -> ST s ()) -> ByteArray #
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.
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.
Constructors
| MutablePrimArray (MutableByteArray# s) |
Instances
| NFData (MutablePrimArray s a) | |
Defined in Data.Primitive.PrimArray Methods rnf :: MutablePrimArray s a -> () # | |
| Eq (MutablePrimArray s a) | |
Defined in Data.Primitive.PrimArray Methods (==) :: 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.
Constructors
| PrimArray ByteArray# |
Instances
| Lift (PrimArray a :: Type) | |||||
| NFData (PrimArray a) | |||||
Defined in Data.Primitive.PrimArray | |||||
| 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 | ||||
Defined in Data.Primitive.PrimArray Associated Types
| |||||
| (Show a, Prim a) => Show (PrimArray a) | Since: primitive-0.6.4.0 | ||||
| (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 Mutable PrimArray | |||||
Defined in Data.Vector.Generic.Base | |||||
| type Item (PrimArray a) | |||||
Defined in Data.Primitive.PrimArray | |||||
primArrayFromList :: Prim a => [a] -> PrimArray a #
Create a PrimArray from a list.
primArrayFromList vs = primArrayFromListN (length vs) vsprimArrayFromListN :: 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.
Arguments
| :: (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.
Arguments
| :: (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.
Arguments
| :: (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.
Arguments
| :: (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.
Arguments
| :: (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.
Arguments
| :: (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.
This function is deprecated and will be removed.
sameMutablePrimArray :: MutablePrimArray s a -> MutablePrimArray s a -> Bool #
Check if the two arrays refer to the same memory block.
Arguments
| :: (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.
Arguments
| :: (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.
Arguments
| :: (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.
Arguments
| :: (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.
Arguments
| :: (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.
Arguments
| :: (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.
Arguments
| :: (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. The underlying memory is left uninitialized.
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. The underlying memory is left uninitialized.
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
Arguments
| :: 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.
Arguments
| :: (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
createPrimArray :: Prim a => Int -> (forall s. MutablePrimArray s a -> ST s ()) -> PrimArray a #
Create an uninitialized array of the given length, apply the function to it, and freeze the result.
Note: this function does not check if the input is non-negative.
@since FIXME
withPrimArrayContents :: PrimBase m => PrimArray a -> (Ptr a -> m a) -> m a #
A composition of primArrayContents and keepAliveUnlifted.
The callback function must not return the pointer. The argument
array must be pinned. See primArrayContents for an explanation
of which primitive arrays are pinned.
Note: This could be implemented with keepAlive instead of
keepAliveUnlifted, but keepAlive here would cause GHC to materialize
the wrapper data constructor on the heap.
withMutablePrimArrayContents :: PrimBase m => MutablePrimArray (PrimState m) a -> (Ptr a -> m a) -> m a #
A composition of mutablePrimArrayContents and keepAliveUnlifted.
The callback function must not return the pointer. The argument
array must be pinned. See primArrayContents for an explanation
of which primitive arrays are pinned.
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 #