2 % (c) The AQUA Project, Glasgow University, 1997
4 \section[MutableArray]{The @MutableArray@ interface}
6 Mutable (byte)arrays interface, re-exports type types and operations
7 over them from @ArrBase@. Have to be used in conjunction with
13 MutableArray(..), -- not abstract
20 newArray, -- :: Ix ix => (ix,ix) -> elt -> ST s (MutableArray s ix elt)
25 newDoubleArray, -- :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
27 boundsOfArray, -- :: Ix ix => MutableArray s ix elt -> (ix, ix)
28 boundsOfByteArray, -- :: Ix ix => MutableByteArray s ix -> (ix, ix)
30 readArray, -- :: Ix ix => MutableArray s ix elt -> ix -> ST s elt
32 readCharArray, -- :: Ix ix => MutableByteArray s ix -> ix -> ST s Char
33 readIntArray, -- :: Ix ix => MutableByteArray s ix -> ix -> ST s Int
34 readAddrArray, -- :: Ix ix => MutableByteArray s ix -> ix -> ST s Addr
35 readFloatArray, -- :: Ix ix => MutableByteArray s ix -> ix -> ST s Float
36 readDoubleArray, -- :: Ix ix => MutableByteArray s ix -> ix -> ST s Double
38 writeArray, -- :: Ix ix => MutableArray s ix elt -> ix -> elt -> ST s ()
39 writeCharArray, -- :: Ix ix => MutableByteArray s ix -> ix -> Char -> ST s ()
40 writeIntArray, -- :: Ix ix => MutableByteArray s ix -> ix -> Int -> ST s ()
41 writeAddrArray, -- :: Ix ix => MutableByteArray s ix -> ix -> Addr -> ST s ()
42 writeFloatArray, -- :: Ix ix => MutableByteArray s ix -> ix -> Float -> ST s ()
43 writeDoubleArray, -- :: Ix ix => MutableByteArray s ix -> ix -> Double -> ST s ()
45 freezeArray, -- :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
46 freezeCharArray, -- :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
47 freezeIntArray, -- :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
48 freezeAddrArray, -- :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
49 freezeFloatArray, -- :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
50 freezeDoubleArray, -- :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
52 unsafeFreezeArray, -- :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
53 unsafeFreezeByteArray, -- :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
54 thawArray, -- :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
56 -- the sizes are reported back are *in bytes*.
57 sizeofByteArray, -- :: Ix ix => ByteArray ix -> Int
58 sizeofMutableByteArray, -- :: Ix ix => MutableByteArray s ix -> Int
61 readWord8Array, -- :: Ix ix => MutableByteArray s ix -> Word8
62 readWord16Array, -- :: Ix ix => MutableByteArray s ix -> Word16
63 readWord32Array, -- :: Ix ix => MutableByteArray s ix -> Word32
68 import PrelBase (sizeofMutableByteArray#, sizeofByteArray#, Int(..) )
75 sizeofByteArray :: Ix ix => ByteArray ix -> Int
76 sizeofByteArray (ByteArray _ arr#) =
77 case (sizeofByteArray# arr#) of
80 sizeofMutableByteArray :: Ix ix => MutableByteArray s ix -> Int
81 sizeofMutableByteArray (MutableByteArray _ arr#) =
82 case (sizeofMutableByteArray# arr#) of
88 readWord8Array :: Ix ix => MutableByteArray RealWorld ix -> ix -> IO Word8
89 readWord16Array :: Ix ix => MutableByteArray RealWorld ix -> ix -> IO Word16
90 readWord32Array :: Ix ix => MutableByteArray RealWorld ix -> ix -> IO Word32
92 {- NB!!: The index for an array is in units of the element type being read -}
94 readWord8Array (MutableByteArray ixs arr#) n@(I# n#) =
95 case sizeofMutableByteArray# arr# of
97 | n# ># (bytes# -# 1#) -> fail (userError "readWord8Array: index out of bounds "++show n)
98 | otherwise -> IO $ \ s# ->
99 case readCharArray# barr# n# s# of
100 StateAndChar# s2# r# -> IOok s2# (W8# (int2Word# (ord# r#)))
102 readWord16Array (MutableByteArray ixs arr#) n@(I# n#) =
103 case sizeofMutableByteArray# arr# of
105 | (2# *# n#) ># (bytes# -# 1#) -> fail (userError "readWord16Array: index out of bounds "++show n)
106 | otherwise -> IO $ \ s# ->
107 case readWordArray# barr# n# s# of
108 StateAndInt# s2# w# -> IOok s2# (wordToWord16 (W# w#))
110 readWord32Array (MutableByteArray ixs arr#) n@(I# n#) =
111 case sizeofMutableByteArray# arr# of
113 | (4# *# n#) ># (bytes# -# 1#) -> fail (userError "readWord32Array: index out of bounds "++show n)
114 | otherwise -> IO $ \ s# ->
115 case readWordArray# barr# n# s# of
116 StateAndInt# s2# w# -> IOok s2# (wordToWord32 (W# w#))