67ee0149f2489ee492fbac7dcbeb16a2f547c2e7
[ghc.git] / libraries / base / GHC / IOArray.hs
1 {-# LANGUAGE NoImplicitPrelude #-}
2 {-# OPTIONS_GHC -funbox-strict-fields #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : GHC.IOArray
7 -- Copyright : (c) The University of Glasgow 2008
8 -- License : see libraries/base/LICENSE
9 --
10 -- Maintainer : cvs-ghc@haskell.org
11 -- Stability : internal
12 -- Portability : non-portable (GHC Extensions)
13 --
14 -- The IOArray type
15 --
16 -----------------------------------------------------------------------------
17
18 module GHC.IOArray (
19 IOArray(..),
20 newIOArray, unsafeReadIOArray, unsafeWriteIOArray,
21 readIOArray, writeIOArray,
22 boundsIOArray
23 ) where
24
25 import GHC.Base
26 import GHC.IO
27 import GHC.Arr
28
29 -- ---------------------------------------------------------------------------
30 -- | An 'IOArray' is a mutable, boxed, non-strict array in the 'IO' monad.
31 -- The type arguments are as follows:
32 --
33 -- * @i@: the index type of the array (should be an instance of 'Ix')
34 --
35 -- * @e@: the element type of the array.
36 --
37 --
38
39 newtype IOArray i e = IOArray (STArray RealWorld i e)
40
41 -- explicit instance because Haddock can't figure out a derived one
42 instance Eq (IOArray i e) where
43 IOArray x == IOArray y = x == y
44
45 -- |Build a new 'IOArray'
46 newIOArray :: Ix i => (i,i) -> e -> IO (IOArray i e)
47 {-# INLINE newIOArray #-}
48 newIOArray lu initial = stToIO $ do {marr <- newSTArray lu initial; return (IOArray marr)}
49
50 -- | Read a value from an 'IOArray'
51 unsafeReadIOArray :: Ix i => IOArray i e -> Int -> IO e
52 {-# INLINE unsafeReadIOArray #-}
53 unsafeReadIOArray (IOArray marr) i = stToIO (unsafeReadSTArray marr i)
54
55 -- | Write a new value into an 'IOArray'
56 unsafeWriteIOArray :: Ix i => IOArray i e -> Int -> e -> IO ()
57 {-# INLINE unsafeWriteIOArray #-}
58 unsafeWriteIOArray (IOArray marr) i e = stToIO (unsafeWriteSTArray marr i e)
59
60 -- | Read a value from an 'IOArray'
61 readIOArray :: Ix i => IOArray i e -> i -> IO e
62 readIOArray (IOArray marr) i = stToIO (readSTArray marr i)
63
64 -- | Write a new value into an 'IOArray'
65 writeIOArray :: Ix i => IOArray i e -> i -> e -> IO ()
66 writeIOArray (IOArray marr) i e = stToIO (writeSTArray marr i e)
67
68 {-# INLINE boundsIOArray #-}
69 boundsIOArray :: IOArray i e -> (i,i)
70 boundsIOArray (IOArray marr) = boundsSTArray marr