| 1 | {-# LANGUAGE CPP, BangPatterns, MagicHash, UnboxedTuples #-} |
| 2 | -- |
| 3 | -- (c) The University of Glasgow 2002 |
| 4 | -- |
| 5 | -- Unboxed mutable Ints |
| 6 | |
| 7 | module FastMutInt( |
| 8 | FastMutInt, newFastMutInt, |
| 9 | readFastMutInt, writeFastMutInt |
| 10 | ) where |
| 11 | |
| 12 | #define SIZEOF_HSINT 4 |
| 13 | |
| 14 | import GHC.Exts |
| 15 | # if __GLASGOW_HASKELL__>=612 |
| 16 | import GHC.IO (IO(IO)) |
| 17 | #else |
| 18 | import GHC.IOBase (IO(IO)) |
| 19 | #endif |
| 20 | |
| 21 | data FastMutInt = FastMutInt (MutableByteArray# RealWorld) |
| 22 | |
| 23 | newFastMutInt :: IO FastMutInt |
| 24 | newFastMutInt = IO $ \s -> |
| 25 | case newByteArray# size s of { (# s, arr #) -> |
| 26 | (# s, FastMutInt arr #) } |
| 27 | where !(I# size) = SIZEOF_HSINT |
| 28 | |
| 29 | readFastMutInt :: FastMutInt -> IO Int |
| 30 | readFastMutInt (FastMutInt arr) = IO $ \s -> |
| 31 | case readIntArray# arr 0# s of { (# s, i #) -> |
| 32 | (# s, I# i #) } |
| 33 | |
| 34 | writeFastMutInt :: FastMutInt -> Int -> IO () |
| 35 | writeFastMutInt (FastMutInt arr) (I# i) = IO $ \s -> |
| 36 | case writeIntArray# arr 0# i s of { s -> |
| 37 | (# s, () #) } |
| 38 |