module Data.Vector.Fixed.Mutable (
Arity
, arity
, Mutable
, DimM
, MVector(..)
, lengthM
, read
, write
, clone
, IVector(..)
, index
, freeze
, thaw
, constructVec
, inspectVec
) where
import Control.Monad.ST
import Control.Monad.Primitive
import Data.Typeable (Proxy(..))
import GHC.TypeLits
import Data.Vector.Fixed.Cont (Dim,PeanoNum(..),Peano,Arity,Fun(..),Vector(..),ContVec,arity,apply,accum,length)
import Prelude hiding (read,length)
type family Mutable (v :: * -> *) :: * -> * -> *
type family DimM (v :: * -> * -> *) :: Nat
class (Arity (DimM v)) => MVector v a where
copy :: PrimMonad m
=> v (PrimState m) a
-> v (PrimState m) a
-> m ()
move :: PrimMonad m
=> v (PrimState m) a
-> v (PrimState m) a
-> m ()
new :: PrimMonad m => m (v (PrimState m) a)
unsafeRead :: PrimMonad m => v (PrimState m) a -> Int -> m a
unsafeWrite :: PrimMonad m => v (PrimState m) a -> Int -> a -> m ()
lengthM :: forall v s a. (Arity (DimM v)) => v s a -> Int
lengthM _ = arity (Proxy :: Proxy (DimM v))
clone :: (PrimMonad m, MVector v a) => v (PrimState m) a -> m (v (PrimState m) a)
clone v = do
u <- new
move v u
return u
read :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m a
read v i
| i < 0 || i >= lengthM v = error "Data.Vector.Fixed.Mutable.read: index out of range"
| otherwise = unsafeRead v i
write :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m ()
write v i x
| i < 0 || i >= lengthM v = error "Data.Vector.Fixed.Mutable.write: index out of range"
| otherwise = unsafeWrite v i x
class (Dim v ~ DimM (Mutable v), MVector (Mutable v) a) => IVector v a where
unsafeFreeze :: PrimMonad m => Mutable v (PrimState m) a -> m (v a)
unsafeThaw :: PrimMonad m => v a -> m (Mutable v (PrimState m) a)
unsafeIndex :: v a -> Int -> a
index :: IVector v a => v a -> Int -> a
index v i | i < 0 || i >= length v = error "Data.Vector.Fixed.Mutable.!: index out of bounds"
| otherwise = unsafeIndex v i
freeze :: (PrimMonad m, IVector v a) => Mutable v (PrimState m) a -> m (v a)
freeze v = unsafeFreeze =<< clone v
thaw :: (PrimMonad m, IVector v a) => v a -> m (Mutable v (PrimState m) a)
thaw v = clone =<< unsafeThaw v
inspectVec :: forall v a b. (Arity (Dim v), IVector v a) => v a -> Fun (Peano (Dim v)) a b -> b
inspectVec v
= inspect cv
where
cv :: ContVec (Dim v) a
cv = apply (\(Const i) -> (unsafeIndex v i, Const (i+1)))
(Const 0 :: Const Int (Peano (Dim v)))
newtype Const a n = Const a
constructVec :: forall v a. (Arity (Dim v), IVector v a) => Fun (Peano (Dim v)) a (v a)
constructVec =
accum step
(\(T_new _ st) -> runST $ unsafeFreeze =<< st :: v a)
(T_new 0 new :: T_new v a (Peano (Dim v)))
data T_new v a n = T_new Int (forall s. ST s (Mutable v s a))
step :: (IVector v a) => T_new v a ('S n) -> a -> T_new v a n
step (T_new i st) x = T_new (i+1) $ do
mv <- st
unsafeWrite mv i x
return mv