module ToySolver.Internal.Data.IOURef
( IOURef
, newIOURef
, readIOURef
, writeIOURef
, modifyIOURef
) where
import Data.Array.Base
import Data.Array.IO
newtype IOURef a = IOURef (IOUArray Int a) deriving (Eq)
newIOURef :: (MArray IOUArray a IO) => a -> IO (IOURef a)
newIOURef x = do
a <- newArray (0,0) x
return $ IOURef a
readIOURef :: (MArray IOUArray a IO) => IOURef a -> IO a
readIOURef (IOURef a) = unsafeRead a 0
writeIOURef :: (MArray IOUArray a IO) => IOURef a -> a -> IO ()
writeIOURef (IOURef a) x = unsafeWrite a 0 x
modifyIOURef :: (MArray IOUArray a IO) => IOURef a -> (a -> a) -> IO ()
modifyIOURef ref f = do
x <- readIOURef ref
writeIOURef ref (f x)