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