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