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