rio-0.1.22.0: A standard library for Haskell

Safe Haskell | Safe-Inferred
Language | Haskell2010

RIO.Vector.Unboxed.Unsafe

Contents

- Accessors
  - Indexing
  - Monadic indexing
  - Extracting subvectors
- Modifying vectors
  - Bulk updates
  - Accumulations
  - Permutations
- Conversions
  - Mutable vectors

Description

Unoxed `Vector` unsafe functions. These perform no bounds checking, and may cause segmentation faults etc.! Import as:

    import qualified RIO.Vector.Unoxed.Unsafe as VU'

Synopsis

- unsafeIndex :: Unbox a => Vector a -> Int -> a
- unsafeHead :: Unbox a => Vector a -> a
- unsafeLast :: Unbox a => Vector a -> a
- unsafeIndexM :: (Unbox a, Monad m) => Vector a -> Int -> m a
- unsafeHeadM :: (Unbox a, Monad m) => Vector a -> m a
- unsafeLastM :: (Unbox a, Monad m) => Vector a -> m a
- unsafeSlice :: Unbox a => Int -> Int -> Vector a -> Vector a
- unsafeInit :: Unbox a => Vector a -> Vector a
- unsafeTail :: Unbox a => Vector a -> Vector a
- unsafeTake :: Unbox a => Int -> Vector a -> Vector a
- unsafeDrop :: Unbox a => Int -> Vector a -> Vector a
- unsafeUpd :: Unbox a => Vector a -> [(Int, a)] -> Vector a
- unsafeUpdate :: Unbox a => Vector a -> Vector (Int, a) -> Vector a
- unsafeUpdate_ :: Unbox a => Vector a -> Vector Int -> Vector a -> Vector a
- unsafeAccum :: Unbox a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a
- unsafeAccumulate :: (Unbox a, Unbox b) => (a -> b -> a) -> Vector a -> Vector (Int, b) -> Vector a
- unsafeAccumulate_ :: (Unbox a, Unbox b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
- unsafeBackpermute :: Unbox a => Vector a -> Vector Int -> Vector a
- unsafeFreeze :: (Unbox a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a)
- unsafeThaw :: (Unbox a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a)
- unsafeCopy :: (Unbox a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m ()

# Accessors

## Indexing

unsafeIndex :: Unbox a => Vector a -> Int -> a Source #

O(1) Unsafe indexing without bounds checking.

unsafeHead :: Unbox a => Vector a -> a Source #

O(1) First element, without checking if the vector is empty.

unsafeLast :: Unbox a => Vector a -> a Source #

O(1) Last element, without checking if the vector is empty.

## Monadic indexing

unsafeIndexM :: (Unbox a, Monad m) => Vector a -> Int -> m a Source #

O(1) Indexing in a monad, without bounds checks. See `indexM` for an explanation of why this is useful.

unsafeHeadM :: (Unbox a, Monad m) => Vector a -> m a Source #

O(1) First element in a monad, without checking for empty vectors. See `indexM` for an explanation of why this is useful.

unsafeLastM :: (Unbox a, Monad m) => Vector a -> m a Source #

O(1) Last element in a monad, without checking for empty vectors. See `indexM` for an explanation of why this is useful.

## Extracting subvectors

unsafeSlice Source #

Arguments

:: Unbox a |
=> Int | `i` starting index
-> Int | `n` length
-> Vector a |
-> Vector a |

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 :: Unbox a => Vector a -> Vector a Source #

O(1) Yield all but the last element without copying. The vector may not be empty, but this is not checked.

unsafeTail :: Unbox a => Vector a -> Vector a Source #

O(1) Yield all but the first element without copying. The vector may not be empty, but this is not checked.

unsafeTake :: Unbox a => Int -> Vector a -> Vector a Source #

O(1) Yield the first `n` elements without copying. The vector must contain at least `n` elements, but this is not checked.

unsafeDrop :: Unbox a => Int -> Vector a -> Vector a Source #

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

unsafeUpd :: Unbox a => Vector a -> [(Int, a)] -> Vector a Source #

Same as (`//`), but without bounds checking.

unsafeUpdate :: Unbox a => Vector a -> Vector (Int, a) -> Vector a Source #

Same as `update`, but without bounds checking.

unsafeUpdate_ :: Unbox a => Vector a -> Vector Int -> Vector a -> Vector a Source #

Same as `update_`, but without bounds checking.

## Accumulations

unsafeAccum :: Unbox a => (a -> b -> a) -> Vector a -> [(Int, b)] -> Vector a Source #

Same as `accum`, but without bounds checking.

unsafeAccumulate :: (Unbox a, Unbox b) => (a -> b -> a) -> Vector a -> Vector (Int, b) -> Vector a Source #

Same as `accumulate`, but without bounds checking.

unsafeAccumulate_ :: (Unbox a, Unbox b) => (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a Source #

Same as `accumulate_`, but without bounds checking.

## Permutations

unsafeBackpermute :: Unbox a => Vector a -> Vector Int -> Vector a Source #

Same as `backpermute`, but without bounds checking.

# Conversions

## Mutable vectors

unsafeFreeze :: (Unbox a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a) Source #

O(1) Unsafely convert a mutable vector to an immutable one without copying. The mutable vector may not be used after this operation.

unsafeThaw :: (Unbox a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a) Source #

O(1) Unsafely convert an immutable vector to a mutable one without copying. Note that this is a very dangerous function and generally it's only safe to read from the resulting vector. In this case, the immutable vector could be used safely as well.

Problems with mutation happen because GHC has a lot of freedom to introduce sharing. As a result mutable vectors produced by `unsafeThaw` may or may not share the 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 that all calls to `do_something` will use same buffer with possibly disastrous results. Whether such aliasing happens or not depends on the program in question, optimization levels, and GHC flags.

All in all, attempts to modify a vector produced by `unsafeThaw` fall out of domain of software engineering and into realm of black magic, dark rituals, and unspeakable horrors. The only advice that could be given is: "Don't attempt to mutate a vector produced by `unsafeThaw` unless you know how to prevent GHC from aliasing buffers accidentally. We don't."

unsafeCopy :: (Unbox a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m () Source #

O(n) Copy an immutable vector into a mutable one. The two vectors must have the same length. This is not checked.