{-# LANGUAGE MagicHash, NoImplicitPrelude, UnboxedTuples #-}

module GHC.Event.Arr
    (
      Arr(..)
    , new
    , size
    , read
    , write
    ) where

import GHC.Base (($))
import GHC.Prim (MutableArray#, RealWorld, newArray#, readArray#,
                 sizeofMutableArray#, writeArray#)
import GHC.Types (IO(..), Int(..))

data Arr a = Arr (MutableArray# RealWorld a)

new :: a -> Int -> IO (Arr a)
new :: forall a. a -> Int -> IO (Arr a)
new a
defval (I# Int#
n#) = (State# RealWorld -> (# State# RealWorld, Arr a #)) -> IO (Arr a)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Arr a #)) -> IO (Arr a))
-> (State# RealWorld -> (# State# RealWorld, Arr a #))
-> IO (Arr a)
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s0# ->
  case Int#
-> a
-> State# RealWorld
-> (# State# RealWorld, MutableArray# RealWorld a #)
forall a d.
Int# -> a -> State# d -> (# State# d, MutableArray# d a #)
newArray# Int#
n# a
defval State# RealWorld
s0# of (# State# RealWorld
s1#, MutableArray# RealWorld a
marr# #) -> (# State# RealWorld
s1#, MutableArray# RealWorld a -> Arr a
forall a. MutableArray# RealWorld a -> Arr a
Arr MutableArray# RealWorld a
marr# #)

size :: Arr a -> Int
size :: forall a. Arr a -> Int
size (Arr MutableArray# RealWorld a
a) = Int# -> Int
I# (MutableArray# RealWorld a -> Int#
forall d a. MutableArray# d a -> Int#
sizeofMutableArray# MutableArray# RealWorld a
a)

read :: Arr a -> Int -> IO a
read :: forall a. Arr a -> Int -> IO a
read (Arr MutableArray# RealWorld a
a) (I# Int#
n#) = (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, a #)) -> IO a)
-> (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s0# ->
  case MutableArray# RealWorld a
-> Int# -> State# RealWorld -> (# State# RealWorld, a #)
forall d a.
MutableArray# d a -> Int# -> State# d -> (# State# d, a #)
readArray# MutableArray# RealWorld a
a Int#
n# State# RealWorld
s0# of (# State# RealWorld
s1#, a
val #) -> (# State# RealWorld
s1#, a
val #)

write :: Arr a -> Int -> a -> IO ()
write :: forall a. Arr a -> Int -> a -> IO ()
write (Arr MutableArray# RealWorld a
a) (I# Int#
n#) a
val = (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s0# ->
  case MutableArray# RealWorld a
-> Int# -> a -> State# RealWorld -> State# RealWorld
forall d a. MutableArray# d a -> Int# -> a -> State# d -> State# d
writeArray# MutableArray# RealWorld a
a Int#
n# a
val State# RealWorld
s0# of State# RealWorld
s1# -> (# State# RealWorld
s1#, () #)