{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Control.Concurrent.Classy.STM.TArray (TArray) where
import Data.Array (Array, bounds)
import Data.Array.Base (IArray(numElements), MArray(..),
arrEleBottom, listArray, unsafeAt)
import Data.Ix (rangeSize)
import Control.Monad.STM.Class
newtype TArray stm i e = TArray (Array i (TVar stm e))
instance MonadSTM stm => MArray (TArray stm) e stm where
getBounds :: forall i. Ix i => TArray stm i e -> stm (i, i)
getBounds (TArray Array i (TVar stm e)
a) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall i e. Array i e -> (i, i)
bounds Array i (TVar stm e)
a)
newArray :: forall i. Ix i => (i, i) -> e -> stm (TArray stm i e)
newArray (i, i)
b e
e =
forall (stm :: * -> *) i e. Array i (TVar stm e) -> TArray stm i e
TArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (i, i)
b forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. Monad m => Int -> m a -> m [a]
rep (forall a. Ix a => (a, a) -> Int
rangeSize (i, i)
b) (forall (stm :: * -> *) a. MonadSTM stm => a -> stm (TVar stm a)
newTVar e
e)
newArray_ :: forall i. Ix i => (i, i) -> stm (TArray stm i e)
newArray_ (i, i)
b = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (i, i)
b forall a. a
arrEleBottom
unsafeRead :: forall i. Ix i => TArray stm i e -> Int -> stm e
unsafeRead (TArray Array i (TVar stm e)
a) = forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> stm a
readTVar forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> Int -> e
unsafeAt Array i (TVar stm e)
a
unsafeWrite :: forall i. Ix i => TArray stm i e -> Int -> e -> stm ()
unsafeWrite (TArray Array i (TVar stm e)
a) = forall (stm :: * -> *) a. MonadSTM stm => TVar stm a -> a -> stm ()
writeTVar forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> Int -> e
unsafeAt Array i (TVar stm e)
a
getNumElements :: forall i. Ix i => TArray stm i e -> stm Int
getNumElements (TArray Array i (TVar stm e)
a) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall (a :: * -> * -> *) e i. (IArray a e, Ix i) => a i e -> Int
numElements Array i (TVar stm e)
a)
rep :: Monad m => Int -> m a -> m [a]
rep :: forall (m :: * -> *) a. Monad m => Int -> m a -> m [a]
rep Int
n m a
m = forall {t}. (Eq t, Num t) => t -> [a] -> m [a]
go Int
n [] where
go :: t -> [a] -> m [a]
go t
0 [a]
xs = forall (f :: * -> *) a. Applicative f => a -> f a
pure [a]
xs
go t
i [a]
xs = do
a
x <- m a
m
t -> [a] -> m [a]
go (t
iforall a. Num a => a -> a -> a
-t
1) (a
xforall a. a -> [a] -> [a]
:[a]
xs)