-- | This is an internal module.
--
-- It works like "Data.STRef", but is specialized to 'Int' and more efficient.

module Data.RRBVector.Internal.IntRef
    ( IntRef
    , newIntRef
    , readIntRef
    , writeIntRef
    ) where

import Control.Monad.ST
import Data.Primitive.PrimArray

-- | A mutable 'Int' reference.
newtype IntRef s = IntRef (MutablePrimArray s Int)

-- | \(O(1)\). Create a new 'IntRef' that is initialized with @0@.
newIntRef :: Int -> ST s (IntRef s)
newIntRef :: forall s. Int -> ST s (IntRef s)
newIntRef Int
i = do
    MutablePrimArray s Int
arr <- forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray Int
1
    forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s Int
arr Int
0 Int
i
    forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall s. MutablePrimArray s Int -> IntRef s
IntRef MutablePrimArray s Int
arr)
{-# INLINE newIntRef #-}

-- | \(O(1)\). Read the content of the 'IntRef'.
readIntRef :: IntRef s -> ST s Int
readIntRef :: forall s. IntRef s -> ST s Int
readIntRef (IntRef MutablePrimArray s Int
arr) = forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> m a
readPrimArray MutablePrimArray s Int
arr Int
0
{-# INLINE readIntRef #-}

-- | \(O(1)\). Write a value to the 'IntRef'.
writeIntRef :: IntRef s -> Int -> ST s ()
writeIntRef :: forall s. IntRef s -> Int -> ST s ()
writeIntRef (IntRef MutablePrimArray s Int
arr) = forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutablePrimArray (PrimState m) a -> Int -> a -> m ()
writePrimArray MutablePrimArray s Int
arr Int
0
{-# INLINE writeIntRef #-}