{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE RankNTypes #-}

{-|
This module reexports the functionality in 'Data.Vector.Generic.Mutable'
which maps well to explicitly sized vectors.

Functions returning a vector determine the size from the type context
unless they have a @'@ suffix in which case they take an explicit 'Proxy'
argument.

Functions where the resultant vector size is not known until runtime
are not exported.
-}

module Data.Vector.Generic.Mutable.Sized
 ( MVector
   -- * Accessors
   -- ** Length information
  , length
  , length'
  , null
   -- ** Extracting subvectors
  , slice
  , slice'
  , init
  , tail
  , take
  , take'
  , drop
  , drop'
  , splitAt
  , splitAt'
  -- ** Overlaps
  , overlaps
  -- * Construction
  -- ** Initialisation
  , new
  , unsafeNew
  , replicate
  , replicate'
  , replicateM
  , replicateM'
  , clone
  -- ** Growing
  , grow
  , growFront
  -- ** Restricting memory usage
  , clear
  -- * Accessing individual elements
  , read
  , read'
  , write
  , write'
  , modify
  , modify'
  , swap
  , exchange
  , exchange'
  , unsafeRead
  , unsafeWrite
  , unsafeModify
  , unsafeSwap
  , unsafeExchange
#if MIN_VERSION_vector(0,12,0)
  -- * Modifying vectors
  , nextPermutation
#endif
  -- ** Filling and copying
  , set
  , copy
  , move
  , unsafeCopy
    -- * Conversions
    -- ** Unsized Mutable Vectors
  , toSized
  , withSized
  , fromSized
  ) where

import qualified Data.Vector.Generic.Mutable as VGM
import Data.Vector.Generic.Mutable.Sized.Internal
import GHC.TypeLits
import Data.Finite
import Data.Finite.Internal
import Data.Maybe
import Data.Proxy
import Control.Monad.Primitive
import Prelude hiding ( length, null, replicate, init,
                        tail, take, drop, splitAt, read )

-- * Accessors

-- ** Length information

-- | /O(1)/ Yield the length of the mutable vector as an 'Int'.
length :: forall v n s a. KnownNat n
       => MVector v n s a -> Int
length :: MVector v n s a -> Int
length _ = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline length #-}

-- | /O(1)/ Yield the length of the mutable vector as a 'Proxy'.
length' :: forall v n s a. ()
        => MVector v n s a -> Proxy n
length' :: MVector v n s a -> Proxy n
length' _ = Proxy n
forall k (t :: k). Proxy t
Proxy
{-# inline length' #-}

-- | /O(1)/ Check whether the mutable vector is empty.
null :: forall v n s a. KnownNat n
       => MVector v n s a -> Bool
null :: MVector v n s a -> Bool
null _ = Maybe (n :~: 0) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (n :~: 0) -> Bool) -> Maybe (n :~: 0) -> Bool
forall a b. (a -> b) -> a -> b
$ Proxy n
forall k (t :: k). Proxy t
Proxy @n Proxy n -> Proxy 0 -> Maybe (n :~: 0)
forall (a :: Nat) (b :: Nat).
(KnownNat a, KnownNat b) =>
Proxy a -> Proxy b -> Maybe (a :~: b)
`sameNat` Proxy 0
forall k (t :: k). Proxy t
Proxy @0
{-# inline null #-}

-- ** Extracting subvectors

-- | /O(1)/ Yield a slice of the mutable vector without copying it with an
-- inferred length argument.
slice :: forall v i n k s a p. (KnownNat i, KnownNat n, VGM.MVector v a)
      => p i -- ^ starting index
      -> MVector v (i+n+k) s a
      -> MVector v n s a
slice :: p i -> MVector v ((i + n) + k) s a -> MVector v n s a
slice start :: p i
start (MVector v :: v s a
v) = v s a -> MVector v n s a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
VGM.unsafeSlice Int
i Int
n v s a
v)
  where i :: Int
i = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (p i -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal p i
start)
        n :: Int
n = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline slice #-}

-- | /O(1)/ Yield a slice of the mutable vector without copying it with an
-- explicit length argument.
slice' :: forall v i n k s a p
        . (KnownNat i, KnownNat n, VGM.MVector v a)
       => p i -- ^ starting index
       -> p n -- ^ length
       -> MVector v (i+n+k) s a
       -> MVector v n s a
slice' :: p i -> p n -> MVector v ((i + n) + k) s a -> MVector v n s a
slice' start :: p i
start _ = p i -> MVector v ((i + n) + k) s a -> MVector v n s a
forall (v :: * -> * -> *) (i :: Nat) (n :: Nat) (k :: Nat) s a
       (p :: Nat -> *).
(KnownNat i, KnownNat n, MVector v a) =>
p i -> MVector v ((i + n) + k) s a -> MVector v n s a
slice p i
start
{-# inline slice' #-}

-- | /O(1)/ Yield all but the last element of a non-empty mutable vector
-- without copying.
init :: forall v n s a. VGM.MVector v a
     => MVector v (n+1) s a -> MVector v n s a
init :: MVector v (n + 1) s a -> MVector v n s a
init (MVector v :: v s a
v) = v s a -> MVector v n s a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (v s a -> v s a
forall (v :: * -> * -> *) a s. MVector v a => v s a -> v s a
VGM.unsafeInit v s a
v)
{-# inline init #-}

-- | /O(1)/ Yield all but the first element of a non-empty mutable vector
-- without copying.
tail :: forall v n s a. VGM.MVector v a
     => MVector v (1+n) s a -> MVector v n s a
tail :: MVector v (1 + n) s a -> MVector v n s a
tail (MVector v :: v s a
v) = v s a -> MVector v n s a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (v s a -> v s a
forall (v :: * -> * -> *) a s. MVector v a => v s a -> v s a
VGM.unsafeTail v s a
v)
{-# inline tail #-}

-- | /O(1)/ Yield the first @n@ elements. The resulting vector always contains
-- this many elements. The length of the resulting vector is inferred from the
-- type.
take :: forall v n k s a. (KnownNat n, VGM.MVector v a)
     => MVector v (n+k) s a -> MVector v n s a
take :: MVector v (n + k) s a -> MVector v n s a
take (MVector v :: v s a
v) = v s a -> MVector v n s a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (Int -> v s a -> v s a
forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
VGM.unsafeTake Int
i v s a
v)
  where i :: Int
i = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline take #-}

-- | /O(1)/ Yield the first @n@ elements. The resulting vector always contains
-- this many elements. The length of the resulting vector is given explicitly
-- as a 'Proxy' argument.
take' :: forall v n k s a p. (KnownNat n, VGM.MVector v a)
      => p n -> MVector v (n+k) s a -> MVector v n s a
take' :: p n -> MVector v (n + k) s a -> MVector v n s a
take' _ = MVector v (n + k) s a -> MVector v n s a
forall (v :: * -> * -> *) (n :: Nat) (k :: Nat) s a.
(KnownNat n, MVector v a) =>
MVector v (n + k) s a -> MVector v n s a
take
{-# inline take' #-}

-- | /O(1)/ Yield all but the the first @n@ elements. The given vector must
-- contain at least this many elements. The length of the resulting vector is
-- inferred from the type.
drop :: forall v n k s a. (KnownNat n, VGM.MVector v a)
     => MVector v (n+k) s a -> MVector v k s a
drop :: MVector v (n + k) s a -> MVector v k s a
drop (MVector v :: v s a
v) = v s a -> MVector v k s a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (Int -> v s a -> v s a
forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
VGM.unsafeDrop Int
i v s a
v)
  where i :: Int
i = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
{-# inline drop #-}

-- | /O(1)/ Yield all but the the first @n@ elements. The given vector must
-- contain at least this many elements. The length of the resulting vector is
-- given explicitly as a 'Proxy' argument.
drop' :: forall v n k s a p. (KnownNat n, VGM.MVector v a)
      => p n -> MVector v (n+k) s a -> MVector v k s a
drop' :: p n -> MVector v (n + k) s a -> MVector v k s a
drop' _ = MVector v (n + k) s a -> MVector v k s a
forall (v :: * -> * -> *) (n :: Nat) (k :: Nat) s a.
(KnownNat n, MVector v a) =>
MVector v (n + k) s a -> MVector v k s a
drop
{-# inline drop' #-}

-- | /O(1)/ Yield the first @n@ elements, paired with the rest, without copying.
-- The lengths of the resulting vectors are inferred from the type.
splitAt :: forall v n m s a. (KnownNat n, VGM.MVector v a)
        => MVector v (n+m) s a -> (MVector v n s a, MVector v m s a)
splitAt :: MVector v (n + m) s a -> (MVector v n s a, MVector v m s a)
splitAt (MVector v :: v s a
v) = (v s a -> MVector v n s a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector v s a
a, v s a -> MVector v m s a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector v s a
b)
  where i :: Int
i = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
        (a :: v s a
a, b :: v s a
b) = Int -> v s a -> (v s a, v s a)
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> v s a -> (v s a, v s a)
VGM.splitAt Int
i v s a
v
{-# inline splitAt #-}

-- | /O(1)/ Yield the first @n@ elements, paired with the rest, without
-- copying. The length of the first resulting vector is passed explicitly as a
-- 'Proxy' argument.
splitAt' :: forall v n m s a p. (KnownNat n, VGM.MVector v a)
         => p n -> MVector v (n+m) s a -> (MVector v n s a, MVector v m s a)
splitAt' :: p n -> MVector v (n + m) s a -> (MVector v n s a, MVector v m s a)
splitAt' _ = MVector v (n + m) s a -> (MVector v n s a, MVector v m s a)
forall (v :: * -> * -> *) (n :: Nat) (m :: Nat) s a.
(KnownNat n, MVector v a) =>
MVector v (n + m) s a -> (MVector v n s a, MVector v m s a)
splitAt
{-# inline splitAt' #-}

-- ** Overlaps

-- | /O(1)/ Check whether two vectors overlap. 
overlaps :: forall v n k s a. VGM.MVector v a
         => MVector v n s a
         -> MVector v k s a
         -> Bool
overlaps :: MVector v n s a -> MVector v k s a -> Bool
overlaps (MVector v :: v s a
v) (MVector u :: v s a
u) = v s a -> v s a -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
VGM.overlaps v s a
v v s a
u
{-# inline overlaps #-}

-- * Construction

-- ** Initialisation

-- | Create a mutable vector where the length is inferred from the type.
new :: forall v n m a. (KnownNat n, PrimMonad m, VGM.MVector v a)
    => m (MVector v n (PrimState m) a)
new :: m (MVector v n (PrimState m) a)
new = v (PrimState m) a -> MVector v n (PrimState m) a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (v (PrimState m) a -> MVector v n (PrimState m) a)
-> m (v (PrimState m) a) -> m (MVector v n (PrimState m) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
VGM.new (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n)))
{-# inline new #-}

-- | Create a mutable vector where the length is inferred from the type.
-- The memory is not initialized.
unsafeNew :: forall v n m a. (KnownNat n, PrimMonad m, VGM.MVector v a)
          => m (MVector v n (PrimState m) a)
unsafeNew :: m (MVector v n (PrimState m) a)
unsafeNew = v (PrimState m) a -> MVector v n (PrimState m) a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (v (PrimState m) a -> MVector v n (PrimState m) a)
-> m (v (PrimState m) a) -> m (MVector v n (PrimState m) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
VGM.new (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n)))
{-# inline unsafeNew #-}

-- | Create a mutable vector where the length is inferred from the type and
-- fill it with an initial value.
replicate :: forall v n m a. (KnownNat n, PrimMonad m, VGM.MVector v a)
          => a -> m (MVector v n (PrimState m) a)
replicate :: a -> m (MVector v n (PrimState m) a)
replicate = (v (PrimState m) a -> MVector v n (PrimState m) a)
-> m (v (PrimState m) a) -> m (MVector v n (PrimState m) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap v (PrimState m) a -> MVector v n (PrimState m) a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (m (v (PrimState m) a) -> m (MVector v n (PrimState m) a))
-> (a -> m (v (PrimState m) a))
-> a
-> m (MVector v n (PrimState m) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> a -> m (v (PrimState m) a)
VGM.replicate (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n)))
{-# inline replicate #-}

-- | Create a mutable vector where the length is given explicitly as
-- a 'Proxy' argument and fill it with an initial value.
replicate' :: forall v n m a p. (KnownNat n, PrimMonad m, VGM.MVector v a)
           => p n -> a -> m (MVector v n (PrimState m) a)
replicate' :: p n -> a -> m (MVector v n (PrimState m) a)
replicate' _ = a -> m (MVector v n (PrimState m) a)
forall (v :: * -> * -> *) (n :: Nat) (m :: * -> *) a.
(KnownNat n, PrimMonad m, MVector v a) =>
a -> m (MVector v n (PrimState m) a)
replicate
{-# inline replicate' #-}

-- | Create a mutable vector where the length is inferred from the type and
-- fill it with values produced by repeatedly executing the monadic action.
replicateM :: forall v n m a. (KnownNat n, PrimMonad m, VGM.MVector v a)
           => m a -> m (MVector v n (PrimState m) a)
replicateM :: m a -> m (MVector v n (PrimState m) a)
replicateM = (v (PrimState m) a -> MVector v n (PrimState m) a)
-> m (v (PrimState m) a) -> m (MVector v n (PrimState m) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap v (PrimState m) a -> MVector v n (PrimState m) a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (m (v (PrimState m) a) -> m (MVector v n (PrimState m) a))
-> (m a -> m (v (PrimState m) a))
-> m a
-> m (MVector v n (PrimState m) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> m a -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m a -> m (v (PrimState m) a)
VGM.replicateM (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n)))
{-# inline replicateM #-}

-- | Create a mutable vector where the length is given explicitly as
-- a 'Proxy' argument and fill it with values produced by repeatedly
-- executing the monadic action.
replicateM' :: forall v n m a p. (KnownNat n, PrimMonad m, VGM.MVector v a)
           => p n -> m a -> m (MVector v n (PrimState m) a)
replicateM' :: p n -> m a -> m (MVector v n (PrimState m) a)
replicateM' _ = m a -> m (MVector v n (PrimState m) a)
forall (v :: * -> * -> *) (n :: Nat) (m :: * -> *) a.
(KnownNat n, PrimMonad m, MVector v a) =>
m a -> m (MVector v n (PrimState m) a)
replicateM
{-# inline replicateM' #-}

-- | Create a copy of a mutable vector.
clone :: forall v n m a. (PrimMonad m, VGM.MVector v a)
      => MVector v n (PrimState m) a -> m (MVector v n (PrimState m) a)
clone :: MVector v n (PrimState m) a -> m (MVector v n (PrimState m) a)
clone (MVector v :: v (PrimState m) a
v) = v (PrimState m) a -> MVector v n (PrimState m) a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (v (PrimState m) a -> MVector v n (PrimState m) a)
-> m (v (PrimState m) a) -> m (MVector v n (PrimState m) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m (v (PrimState m) a)
VGM.clone v (PrimState m) a
v
{-# inline clone #-}

-- ** Growing

-- | Grow a mutable vector by an amount given explicitly as a 'Proxy'
-- argument.
grow :: forall v n k m a p. (KnownNat k, PrimMonad m, VGM.MVector v a)
      => p k -> MVector v n (PrimState m) a -> m (MVector v (n + k) (PrimState m) a)
grow :: p k
-> MVector v n (PrimState m) a
-> m (MVector v (n + k) (PrimState m) a)
grow _ (MVector v :: v (PrimState m) a
v) = v (PrimState m) a -> MVector v (n + k) (PrimState m) a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (v (PrimState m) a -> MVector v (n + k) (PrimState m) a)
-> m (v (PrimState m) a) -> m (MVector v (n + k) (PrimState m) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> v (PrimState m) a -> Int -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
VGM.unsafeGrow v (PrimState m) a
v (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy k -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy k
forall k (t :: k). Proxy t
Proxy :: Proxy k)))
{-# inline grow #-}

-- | Grow a mutable vector (from the front) by an amount given explicitly
-- as a 'Proxy' argument.
growFront :: forall v n k m a p. (KnownNat k, PrimMonad m, VGM.MVector v a)
      => p k -> MVector v n (PrimState m) a -> m (MVector v (n + k) (PrimState m) a)
growFront :: p k
-> MVector v n (PrimState m) a
-> m (MVector v (n + k) (PrimState m) a)
growFront _ (MVector v :: v (PrimState m) a
v) = v (PrimState m) a -> MVector v (n + k) (PrimState m) a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector (v (PrimState m) a -> MVector v (n + k) (PrimState m) a)
-> m (v (PrimState m) a) -> m (MVector v (n + k) (PrimState m) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
    v (PrimState m) a -> Int -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
VGM.unsafeGrowFront v (PrimState m) a
v (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Proxy k -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy k
forall k (t :: k). Proxy t
Proxy :: Proxy k)))
{-# inline growFront #-}

-- ** Restricting memory usage

-- | Reset all elements of the vector to some undefined value, clearing all
-- references to external objects.
clear :: (PrimMonad m, VGM.MVector v a) => MVector v n (PrimState m) a -> m ()
clear :: MVector v n (PrimState m) a -> m ()
clear (MVector v :: v (PrimState m) a
v) = v (PrimState m) a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m ()
VGM.clear v (PrimState m) a
v
{-# inline clear #-}

-- * Accessing individual elements

-- | /O(1)/ Yield the element at a given type-safe position using 'Finite'.
read :: forall v n m a. (PrimMonad m, VGM.MVector v a)
      => MVector v n (PrimState m) a -> Finite n -> m a
read :: MVector v n (PrimState m) a -> Finite n -> m a
read (MVector v :: v (PrimState m) a
v) (Finite i :: Integer
i) = v (PrimState m) a
v v (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
`VGM.unsafeRead` Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i
{-# inline read #-}

-- | /O(1)/ Yield the element at a given type-safe position using 'Proxy'.
read' :: forall v n k a m p. (KnownNat k, PrimMonad m, VGM.MVector v a)
       => MVector v (n+k+1) (PrimState m) a -> p k -> m a
read' :: MVector v ((n + k) + 1) (PrimState m) a -> p k -> m a
read' (MVector v :: v (PrimState m) a
v) p :: p k
p = v (PrimState m) a
v v (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
`VGM.unsafeRead` Integer -> Int
forall a. Num a => Integer -> a
fromInteger (p k -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal p k
p)
{-# inline read' #-}

-- | /O(1)/ Yield the element at a given 'Int' position without bounds
-- checking.
unsafeRead :: forall v n a m. (PrimMonad m, VGM.MVector v a)
           => MVector v n (PrimState m) a -> Int -> m a
unsafeRead :: MVector v n (PrimState m) a -> Int -> m a
unsafeRead (MVector v :: v (PrimState m) a
v) i :: Int
i = v (PrimState m) a
v v (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
`VGM.unsafeRead` Int
i
{-# inline unsafeRead #-}

-- | /O(1)/ Replace the element at a given type-safe position using 'Finite'.
write :: forall v n m a. (PrimMonad m, VGM.MVector v a)
      => MVector v n (PrimState m) a -> Finite n -> a -> m ()
write :: MVector v n (PrimState m) a -> Finite n -> a -> m ()
write (MVector v :: v (PrimState m) a
v) (Finite i :: Integer
i) = v (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite v (PrimState m) a
v (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i)
{-# inline write #-}

-- | /O(1)/ Replace the element at a given type-safe position using 'Proxy'.
write' :: forall v n k a m p. (KnownNat k, PrimMonad m, VGM.MVector v a)
       => MVector v (n+k+1) (PrimState m) a -> p k -> a -> m ()
write' :: MVector v ((n + k) + 1) (PrimState m) a -> p k -> a -> m ()
write' (MVector v :: v (PrimState m) a
v) p :: p k
p = v (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite v (PrimState m) a
v (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (p k -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal p k
p))
{-# inline write' #-}

-- | /O(1)/ Replace the element at a given 'Int' position without bounds
-- checking.
unsafeWrite :: forall v n m a. (PrimMonad m, VGM.MVector v a)
      => MVector v n (PrimState m) a -> Int -> a -> m ()
unsafeWrite :: MVector v n (PrimState m) a -> Int -> a -> m ()
unsafeWrite (MVector v :: v (PrimState m) a
v) = v (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.unsafeWrite v (PrimState m) a
v
{-# inline unsafeWrite #-}

-- | /O(1)/ Modify the element at a given type-safe position using 'Finite'.
modify :: forall v n m a. (PrimMonad m, VGM.MVector v a)
       => MVector v n (PrimState m) a -> (a -> a) -> Finite n -> m ()
modify :: MVector v n (PrimState m) a -> (a -> a) -> Finite n -> m ()
modify (MVector v :: v (PrimState m) a
v) f :: a -> a
f (Finite i :: Integer
i) = v (PrimState m) a -> (a -> a) -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> a) -> Int -> m ()
VGM.unsafeModify v (PrimState m) a
v a -> a
f (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i)
{-# inline modify #-}

-- | /O(1)/ Modify the element at a given type-safe position using 'Proxy'.
modify' :: forall v n k a m p. (KnownNat k, PrimMonad m, VGM.MVector v a)
        => MVector v (n+k+1) (PrimState m) a -> (a -> a) -> p k -> m ()
modify' :: MVector v ((n + k) + 1) (PrimState m) a -> (a -> a) -> p k -> m ()
modify' (MVector v :: v (PrimState m) a
v) f :: a -> a
f p :: p k
p = v (PrimState m) a -> (a -> a) -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> a) -> Int -> m ()
VGM.unsafeModify v (PrimState m) a
v a -> a
f (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (p k -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal p k
p))
{-# inline modify' #-}

-- | /O(1)/ Modify the element at a given 'Int' position without bounds
-- checking.
unsafeModify :: forall v n m a. (PrimMonad m, VGM.MVector v a)
       => MVector v n (PrimState m) a -> (a -> a) -> Int -> m ()
unsafeModify :: MVector v n (PrimState m) a -> (a -> a) -> Int -> m ()
unsafeModify (MVector v :: v (PrimState m) a
v) = v (PrimState m) a -> (a -> a) -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> a) -> Int -> m ()
VGM.unsafeModify v (PrimState m) a
v
{-# inline unsafeModify #-}

-- | /O(1)/ Swap the elements at given type-safe positions using 'Finite's.
swap :: forall v n m a. (PrimMonad m, VGM.MVector v a)
     => MVector v n (PrimState m) a -> Finite n -> Finite n -> m ()
swap :: MVector v n (PrimState m) a -> Finite n -> Finite n -> m ()
swap (MVector v :: v (PrimState m) a
v) (Finite i :: Integer
i) (Finite j :: Integer
j) = v (PrimState m) a -> Int -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> Int -> m ()
VGM.unsafeSwap v (PrimState m) a
v (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i) (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
j)
{-# inline swap #-}

-- | /O(1)/ Swap the elements at given 'Int' positions without bounds
-- checking.
unsafeSwap :: forall v n m a. (PrimMonad m, VGM.MVector v a)
           => MVector v n (PrimState m) a -> Int -> Int -> m ()
unsafeSwap :: MVector v n (PrimState m) a -> Int -> Int -> m ()
unsafeSwap (MVector v :: v (PrimState m) a
v) = v (PrimState m) a -> Int -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> Int -> m ()
VGM.unsafeSwap v (PrimState m) a
v
{-# inline unsafeSwap #-}

-- | /O(1)/ Replace the element at a given type-safe position and return
-- the old element, using 'Finite'.
exchange :: forall v n m a. (PrimMonad m, VGM.MVector v a)
         => MVector v n (PrimState m) a -> Finite n -> a -> m a
exchange :: MVector v n (PrimState m) a -> Finite n -> a -> m a
exchange (MVector v :: v (PrimState m) a
v) (Finite i :: Integer
i) = v (PrimState m) a -> Int -> a -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m a
VGM.unsafeExchange v (PrimState m) a
v (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i)
{-# inline exchange #-}

-- | /O(1)/ Replace the element at a given type-safe position and return
-- the old element, using 'Finite'.
exchange' :: forall v n k a m p. (KnownNat k, PrimMonad m, VGM.MVector v a)
          => MVector v (n+k+1) (PrimState m) a -> p k -> a -> m a
exchange' :: MVector v ((n + k) + 1) (PrimState m) a -> p k -> a -> m a
exchange' (MVector v :: v (PrimState m) a
v) p :: p k
p = v (PrimState m) a -> Int -> a -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m a
VGM.unsafeExchange v (PrimState m) a
v (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (p k -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal p k
p))
{-# inline exchange' #-}

-- | /O(1)/ Replace the element at a given 'Int' position and return
-- the old element. No bounds checks are performed.
unsafeExchange :: forall v n m a. (PrimMonad m, VGM.MVector v a)
         => MVector v n (PrimState m) a -> Int -> a -> m a
unsafeExchange :: MVector v n (PrimState m) a -> Int -> a -> m a
unsafeExchange (MVector v :: v (PrimState m) a
v) = v (PrimState m) a -> Int -> a -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m a
VGM.unsafeExchange v (PrimState m) a
v
{-# inline unsafeExchange #-}

#if MIN_VERSION_vector(0,12,0)
-- * Modifying vectors

-- | Compute the next permutation (in lexicographic order) of a given vector
-- in-place.  Returns 'False' when the input is the last permutation.
nextPermutation :: forall v n e m. (Ord e, PrimMonad m, VGM.MVector v e)
                => MVector v n (PrimState m) e -> m Bool
nextPermutation :: MVector v n (PrimState m) e -> m Bool
nextPermutation (MVector v :: v (PrimState m) e
v) = v (PrimState m) e -> m Bool
forall (m :: * -> *) e (v :: * -> * -> *).
(PrimMonad m, Ord e, MVector v e) =>
v (PrimState m) e -> m Bool
VGM.nextPermutation v (PrimState m) e
v
{-# inline nextPermutation #-}
#endif

-- ** Filling and copying

-- | Set all elements of the vector to the given value.
set :: (PrimMonad m, VGM.MVector v a) => MVector v n (PrimState m) a -> a -> m ()
set :: MVector v n (PrimState m) a -> a -> m ()
set (MVector v :: v (PrimState m) a
v) = v (PrimState m) a -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> a -> m ()
VGM.set v (PrimState m) a
v
{-# inline set #-}

-- | Copy a vector. The two vectors may not overlap.
copy :: (PrimMonad m, VGM.MVector v a)
     => MVector v n (PrimState m) a       -- ^ target
     -> MVector v n (PrimState m) a       -- ^ source
     -> m ()
copy :: MVector v n (PrimState m) a -> MVector v n (PrimState m) a -> m ()
copy (MVector v :: v (PrimState m) a
v) (MVector u :: v (PrimState m) a
u)
    | v (PrimState m) a
v v (PrimState m) a -> v (PrimState m) a -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
`VGM.overlaps` v (PrimState m) a
u = [Char] -> m ()
forall a. HasCallStack => [Char] -> a
error "copy: overlapping vectors"
    | Bool
otherwise          = v (PrimState m) a -> v (PrimState m) a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
VGM.unsafeCopy v (PrimState m) a
v v (PrimState m) a
u
{-# inline copy #-}

-- | Copy a vector. The two vectors may not overlap. This is not checked.
unsafeCopy :: (PrimMonad m, VGM.MVector v a)
           => MVector v n (PrimState m) a       -- ^ target
           -> MVector v n (PrimState m) a       -- ^ source
           -> m ()
unsafeCopy :: MVector v n (PrimState m) a -> MVector v n (PrimState m) a -> m ()
unsafeCopy (MVector v :: v (PrimState m) a
v) (MVector u :: v (PrimState m) a
u) = v (PrimState m) a -> v (PrimState m) a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
VGM.unsafeCopy v (PrimState m) a
v v (PrimState m) a
u
{-# inline unsafeCopy #-}

-- | Move the contents of a vector.  If the two vectors do not overlap,
-- this is equivalent to 'copy'.  Otherwise, the copying is performed as if
-- the source vector were copied to a temporary vector and then the
-- temporary vector was copied to the target vector.
move :: (PrimMonad m, VGM.MVector v a)
     => MVector v n (PrimState m) a       -- ^ target
     -> MVector v n (PrimState m) a       -- ^ source
     -> m ()
move :: MVector v n (PrimState m) a -> MVector v n (PrimState m) a -> m ()
move (MVector v :: v (PrimState m) a
v) (MVector u :: v (PrimState m) a
u) = v (PrimState m) a -> v (PrimState m) a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
VGM.unsafeMove v (PrimState m) a
v v (PrimState m) a
u
{-# inline move #-}

-- * Conversions

-- ** Unsized Mutable Vectors

-- | Convert a 'Data.Vector.Generic.Mutable.MVector' into
-- a 'Data.Vector.Generic.Mutable.Sized.MVector' if it has the correct
-- size, otherwise return Nothing.
--
-- Note that this does no copying; the returned 'MVector' is a reference to
-- the exact same vector in memory as the given one, and any modifications
-- to it are also reflected in the given
-- 'Data.Vector.Generic.Mutable.MVector'.
toSized :: forall v n s a. (VGM.MVector v a, KnownNat n)
        => v s a -> Maybe (MVector v n s a)
toSized :: v s a -> Maybe (MVector v n s a)
toSized v :: v s a
v
  | Integer
n' Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
VGM.length v s a
v) = MVector v n s a -> Maybe (MVector v n s a)
forall a. a -> Maybe a
Just (v s a -> MVector v n s a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector v s a
v)
  | Bool
otherwise                         = Maybe (MVector v n s a)
forall a. Maybe a
Nothing
  where n' :: Integer
n' = Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n)
{-# inline toSized #-}

-- | Takes a 'Data.Vector.Generic.Mutable.MVector' and returns
-- a continuation providing a 'Data.Vector.Generic.Mutable.Sized.MVector'
-- with a size parameter @n@ that is determined at runtime based on the
-- length of the input vector.
--
-- Essentially converts a 'Data.Vector.Generic.Mutable.MVector' into
-- a 'Data.Vector.Generic.Sized.MVector' with the correct size parameter
-- @n@.
--
-- Note that this does no copying; the returned 'MVector' is a reference to
-- the exact same vector in memory as the given one, and any modifications
-- to it are also reflected in the given
-- 'Data.Vector.Generic.Mutable.MVector'.
withSized :: forall v s a r. VGM.MVector v a
          => v s a -> (forall n. KnownNat n => MVector v n s a -> r) -> r
withSized :: v s a
-> (forall (n :: Nat). KnownNat n => MVector v n s a -> r) -> r
withSized v :: v s a
v f :: forall (n :: Nat). KnownNat n => MVector v n s a -> r
f = case Integer -> Maybe SomeNat
someNatVal (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
VGM.length v s a
v)) of
    Just (SomeNat (Proxy n
Proxy :: Proxy n)) -> MVector v n s a -> r
forall (n :: Nat). KnownNat n => MVector v n s a -> r
f (v s a -> MVector v n s a
forall (v :: * -> * -> *) (n :: Nat) s a. v s a -> MVector v n s a
MVector v s a
v :: MVector v n s a)
    Nothing -> [Char] -> r
forall a. HasCallStack => [Char] -> a
error "withSized: VGM.length returned negative length."

-- | Convert a 'Data.Vector.Generic.Mutable.Sized.MVector' into a
-- 'Data.Vector.Generic.Mutable.MVector'.
--
-- Note that this does no copying; the returned
-- 'Data.Vector.Generic.Mutable.MVector' is a reference to the exact same
-- vector in memory as the given one, and any modifications to it are also
-- reflected in the given 'MVector'.
fromSized :: MVector v n s a -> v s a
fromSized :: MVector v n s a -> v s a
fromSized (MVector v :: v s a
v) = v s a
v
{-# inline fromSized #-}