Safe Haskell | None |
---|---|
Language | Haskell2010 |
Boxed Vector
unsafe functions. These perform no bounds
checking, and may cause segmentation faults etc.! Import as:
import qualified RIO.Vector.Boxed.Unsafe as VB'
Synopsis
- unsafeIndex :: Vector a -> Int -> a
- unsafeHead :: Vector a -> a
- unsafeLast :: Vector a -> a
- unsafeIndexM :: Monad m => Vector a -> Int -> m a
- unsafeHeadM :: Monad m => Vector a -> m a
- unsafeLastM :: Monad m => Vector a -> m a
- unsafeSlice :: Int -> Int -> Vector a -> Vector a
- unsafeInit :: Vector a -> Vector a
- unsafeTail :: Vector a -> Vector a
- unsafeTake :: Int -> Vector a -> Vector a
- unsafeDrop :: Int -> Vector a -> Vector a
- unsafeUpd :: Vector a -> [(Int, a)] -> Vector a
- unsafeUpdate :: Vector a -> Vector (Int, a) -> Vector a
- unsafeUpdate_ :: Vector a -> Vector Int -> Vector a -> Vector a
- unsafeAccum :: (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a
- unsafeAccumulate :: (a -> b -> a) -> Vector a -> Vector (Int, b) -> Vector a
- unsafeAccumulate_ :: (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
- unsafeBackpermute :: Vector a -> Vector Int -> Vector a
- unsafeFreeze :: PrimMonad m => MVector (PrimState m) a -> m (Vector a)
- unsafeThaw :: PrimMonad m => Vector a -> m (MVector (PrimState m) a)
- unsafeCopy :: PrimMonad m => MVector (PrimState m) a -> Vector a -> m ()
Accessors
Indexing
unsafeIndex :: Vector a -> Int -> a #
O(1) Unsafe indexing without bounds checking
unsafeHead :: Vector a -> a #
O(1) First element without checking if the vector is empty
unsafeLast :: Vector a -> a #
O(1) Last element without checking if the vector is empty
Monadic indexing
unsafeIndexM :: Monad m => Vector a -> Int -> m a #
O(1) Indexing in a monad without bounds checks. See indexM
for an
explanation of why this is useful.
unsafeHeadM :: Monad m => Vector a -> m a #
O(1) First element in a monad without checking for empty vectors.
See indexM
for an explanation of why this is useful.
unsafeLastM :: Monad m => Vector a -> m a #
O(1) Last element in a monad without checking for empty vectors.
See indexM
for an explanation of why this is useful.
Extracting subvectors
O(1) Yield a slice of the vector without copying. The vector must
contain at least i+n
elements but this is not checked.
unsafeInit :: Vector a -> Vector a #
O(1) Yield all but the last element without copying. The vector may not be empty but this is not checked.
unsafeTail :: Vector a -> Vector a #
O(1) Yield all but the first element without copying. The vector may not be empty but this is not checked.
unsafeTake :: Int -> Vector a -> Vector a #
O(1) Yield the first n
elements without copying. The vector must
contain at least n
elements but this is not checked.
unsafeDrop :: Int -> Vector a -> Vector a #
O(1) Yield all but the first n
elements without copying. The vector
must contain at least n
elements but this is not checked.
Modifying vectors
Bulk updates
unsafeUpdate :: Vector a -> Vector (Int, a) -> Vector a #
Same as update
but without bounds checking.
unsafeUpdate_ :: Vector a -> Vector Int -> Vector a -> Vector a #
Same as update_
but without bounds checking.
Accumulations
unsafeAccum :: (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a #
Same as accum
but without bounds checking.
unsafeAccumulate :: (a -> b -> a) -> Vector a -> Vector (Int, b) -> Vector a #
Same as accumulate
but without bounds checking.
unsafeAccumulate_ :: (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a #
Same as accumulate_
but without bounds checking.
Permutations
unsafeBackpermute :: Vector a -> Vector Int -> Vector a #
Same as backpermute
but without bounds checking.
Conversions
Mutable vectors
unsafeFreeze :: PrimMonad m => MVector (PrimState m) a -> m (Vector a) #
O(1) Unsafe convert a mutable vector to an immutable one without copying. The mutable vector may not be used after this operation.
unsafeThaw :: PrimMonad m => Vector a -> m (MVector (PrimState m) a) #
O(1) Unsafely convert an immutable vector to a mutable one without copying. Note that this is very dangerous function and generally it's only safe to read from resulting vector. In which case immutable vector could be used safely as well.
Problem with mutation happens because GHC has a lot of freedom to
introduce sharing. As a result mutable vectors produced by
unsafeThaw
may or may not share same underlying buffer. For
example:
foo = do let vec = V.generate 10 id mvec <- V.unsafeThaw vec do_something mvec
Here GHC could lift vec
outside of foo which means all calls to
do_something
will use same buffer with possibly disastrous
results. Whether such aliasing happens or not depends on program in
question, optimization levels, and GHC flags.
All in all attempts to modify vector after unsafeThaw falls out of domain of software engineering and into realm of black magic, dark rituals, and unspeakable horrors. Only advice that could be given is: "don't attempt to mutate vector after unsafeThaw unless you know how to prevent GHC from aliasing buffers accidentally. We don't"