1 % -----------------------------------------------------------------------------
2 % $Id: PrelByteArr.lhs,v 1.7 2000/06/30 13:39:35 simonmar Exp $
4 % (c) The University of Glasgow, 1994-2000
7 \section[PrelByteArr]{Module @PrelByteArr@}
9 Byte-arrays are flat arrays of non-pointers only.
12 {-# OPTIONS -fno-implicit-prelude #-}
14 module PrelByteArr where
16 import {-# SOURCE #-} PrelErr ( error )
19 import PrelList (foldl)
27 %*********************************************************
29 \subsection{The @Array@ types}
31 %*********************************************************
34 data Ix ix => ByteArray ix = ByteArray ix ix ByteArray#
35 data Ix ix => MutableByteArray s ix = MutableByteArray ix ix (MutableByteArray# s)
37 instance CCallable (ByteArray ix)
38 instance CCallable (MutableByteArray RealWorld ix)
39 -- Note the RealWorld! You can only ccall with MutableByteArray args
40 -- which are in the real world. When this was missed out, the result
41 -- was that a CCallOpId had a free tyvar, and since the compiler doesn't
42 -- expect that it didn't get zonked or substituted. Bad news.
44 instance Eq (MutableByteArray s ix) where
45 MutableByteArray _ _ arr1# == MutableByteArray _ _ arr2#
46 = sameMutableByteArray# arr1# arr2#
49 %*********************************************************
51 \subsection{Operations on mutable arrays}
53 %*********************************************************
55 Idle ADR question: What's the tradeoff here between flattening these
56 datatypes into @MutableArray ix ix (MutableArray# s elt)@ and using
57 it as is? As I see it, the former uses slightly less heap and
58 provides faster access to the individual parts of the bounds while the
59 code used has the benefit of providing a ready-made @(lo, hi)@ pair as
60 required by many array-related functions. Which wins? Is the
61 difference significant (probably not).
63 Idle AJG answer: When I looked at the outputted code (though it was 2
64 years ago) it seems like you often needed the tuple, and we build
65 it frequently. Now we've got the overloading specialiser things
66 might be different, though.
69 newCharArray, newIntArray, newWordArray, newAddrArray, newFloatArray, newDoubleArray
70 :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
72 {-# SPECIALIZE newCharArray :: IPr -> ST s (MutableByteArray s Int) #-}
73 {-# SPECIALIZE newIntArray :: IPr -> ST s (MutableByteArray s Int) #-}
74 {-# SPECIALIZE newWordArray :: IPr -> ST s (MutableByteArray s Int) #-}
75 {-# SPECIALIZE newAddrArray :: IPr -> ST s (MutableByteArray s Int) #-}
76 {-# SPECIALIZE newFloatArray :: IPr -> ST s (MutableByteArray s Int) #-}
77 {-# SPECIALIZE newDoubleArray :: IPr -> ST s (MutableByteArray s Int) #-}
79 newCharArray (l,u) = ST $ \ s# ->
80 case rangeSize (l,u) of { I# n# ->
81 case (newCharArray# n# s#) of { (# s2#, barr# #) ->
82 (# s2#, MutableByteArray l u barr# #) }}
84 newIntArray (l,u) = ST $ \ s# ->
85 case rangeSize (l,u) of { I# n# ->
86 case (newIntArray# n# s#) of { (# s2#, barr# #) ->
87 (# s2#, MutableByteArray l u barr# #) }}
89 newWordArray (l,u) = ST $ \ s# ->
90 case rangeSize (l,u) of { I# n# ->
91 case (newWordArray# n# s#) of { (# s2#, barr# #) ->
92 (# s2#, MutableByteArray l u barr# #) }}
94 newAddrArray (l,u) = ST $ \ s# ->
95 case rangeSize (l,u) of { I# n# ->
96 case (newAddrArray# n# s#) of { (# s2#, barr# #) ->
97 (# s2#, MutableByteArray l u barr# #) }}
99 newFloatArray (l,u) = ST $ \ s# ->
100 case rangeSize (l,u) of { I# n# ->
101 case (newFloatArray# n# s#) of { (# s2#, barr# #) ->
102 (# s2#, MutableByteArray l u barr# #) }}
104 newDoubleArray (l,u) = ST $ \ s# ->
105 case rangeSize (l,u) of { I# n# ->
106 case (newDoubleArray# n# s#) of { (# s2#, barr# #) ->
107 (# s2#, MutableByteArray l u barr# #) }}
110 readCharArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Char
111 readIntArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Int
112 readWordArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Word
113 readAddrArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Addr
114 readFloatArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Float
115 readDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Double
117 {-# SPECIALIZE readCharArray :: MutableByteArray s Int -> Int -> ST s Char #-}
118 {-# SPECIALIZE readIntArray :: MutableByteArray s Int -> Int -> ST s Int #-}
119 {-# SPECIALIZE readAddrArray :: MutableByteArray s Int -> Int -> ST s Addr #-}
120 --NO:{-# SPECIALIZE readFloatArray :: MutableByteArray s Int -> Int -> ST s Float #-}
121 {-# SPECIALIZE readDoubleArray :: MutableByteArray s Int -> Int -> ST s Double #-}
123 readCharArray (MutableByteArray l u barr#) n = ST $ \ s# ->
124 case (index (l,u) n) of { I# n# ->
125 case readCharArray# barr# n# s# of { (# s2#, r# #) ->
128 readIntArray (MutableByteArray l u barr#) n = ST $ \ s# ->
129 case (index (l,u) n) of { I# n# ->
130 case readIntArray# barr# n# s# of { (# s2#, r# #) ->
133 readWordArray (MutableByteArray l u barr#) n = ST $ \ s# ->
134 case (index (l,u) n) of { I# n# ->
135 case readWordArray# barr# n# s# of { (# s2#, r# #) ->
138 readAddrArray (MutableByteArray l u barr#) n = ST $ \ s# ->
139 case (index (l,u) n) of { I# n# ->
140 case readAddrArray# barr# n# s# of { (# s2#, r# #) ->
143 readFloatArray (MutableByteArray l u barr#) n = ST $ \ s# ->
144 case (index (l,u) n) of { I# n# ->
145 case readFloatArray# barr# n# s# of { (# s2#, r# #) ->
148 readDoubleArray (MutableByteArray l u barr#) n = ST $ \ s# ->
149 case (index (l,u) n) of { I# n# ->
150 case readDoubleArray# barr# n# s# of { (# s2#, r# #) ->
153 --Indexing of ordinary @Arrays@ is standard Haskell and isn't defined here.
154 indexCharArray :: Ix ix => ByteArray ix -> ix -> Char
155 indexIntArray :: Ix ix => ByteArray ix -> ix -> Int
156 indexWordArray :: Ix ix => ByteArray ix -> ix -> Word
157 indexAddrArray :: Ix ix => ByteArray ix -> ix -> Addr
158 indexFloatArray :: Ix ix => ByteArray ix -> ix -> Float
159 indexDoubleArray :: Ix ix => ByteArray ix -> ix -> Double
161 {-# SPECIALIZE indexCharArray :: ByteArray Int -> Int -> Char #-}
162 {-# SPECIALIZE indexIntArray :: ByteArray Int -> Int -> Int #-}
163 {-# SPECIALIZE indexAddrArray :: ByteArray Int -> Int -> Addr #-}
164 --NO:{-# SPECIALIZE indexFloatArray :: ByteArray Int -> Int -> Float #-}
165 {-# SPECIALIZE indexDoubleArray :: ByteArray Int -> Int -> Double #-}
167 indexCharArray (ByteArray l u barr#) n
168 = case (index (l,u) n) of { I# n# ->
169 case indexCharArray# barr# n# of { r# ->
172 indexIntArray (ByteArray l u barr#) n
173 = case (index (l,u) n) of { I# n# ->
174 case indexIntArray# barr# n# of { r# ->
177 indexWordArray (ByteArray l u barr#) n
178 = case (index (l,u) n) of { I# n# ->
179 case indexWordArray# barr# n# of { r# ->
182 indexAddrArray (ByteArray l u barr#) n
183 = case (index (l,u) n) of { I# n# ->
184 case indexAddrArray# barr# n# of { r# ->
187 indexFloatArray (ByteArray l u barr#) n
188 = case (index (l,u) n) of { I# n# ->
189 case indexFloatArray# barr# n# of { r# ->
192 indexDoubleArray (ByteArray l u barr#) n
193 = case (index (l,u) n) of { I# n# ->
194 case indexDoubleArray# barr# n# of { r# ->
197 writeCharArray :: Ix ix => MutableByteArray s ix -> ix -> Char -> ST s ()
198 writeIntArray :: Ix ix => MutableByteArray s ix -> ix -> Int -> ST s ()
199 writeWordArray :: Ix ix => MutableByteArray s ix -> ix -> Word -> ST s ()
200 writeAddrArray :: Ix ix => MutableByteArray s ix -> ix -> Addr -> ST s ()
201 writeFloatArray :: Ix ix => MutableByteArray s ix -> ix -> Float -> ST s ()
202 writeDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> Double -> ST s ()
204 {-# SPECIALIZE writeCharArray :: MutableByteArray s Int -> Int -> Char -> ST s () #-}
205 {-# SPECIALIZE writeIntArray :: MutableByteArray s Int -> Int -> Int -> ST s () #-}
206 {-# SPECIALIZE writeAddrArray :: MutableByteArray s Int -> Int -> Addr -> ST s () #-}
207 --NO:{-# SPECIALIZE writeFloatArray :: MutableByteArray s Int -> Int -> Float -> ST s () #-}
208 {-# SPECIALIZE writeDoubleArray :: MutableByteArray s Int -> Int -> Double -> ST s () #-}
210 writeCharArray (MutableByteArray l u barr#) n (C# ele) = ST $ \ s# ->
211 case index (l,u) n of { I# n# ->
212 case writeCharArray# barr# n# ele s# of { s2# ->
215 writeIntArray (MutableByteArray l u barr#) n (I# ele) = ST $ \ s# ->
216 case index (l,u) n of { I# n# ->
217 case writeIntArray# barr# n# ele s# of { s2# ->
220 writeWordArray (MutableByteArray l u barr#) n (W# ele) = ST $ \ s# ->
221 case index (l,u) n of { I# n# ->
222 case writeWordArray# barr# n# ele s# of { s2# ->
225 writeAddrArray (MutableByteArray l u barr#) n (A# ele) = ST $ \ s# ->
226 case index (l,u) n of { I# n# ->
227 case writeAddrArray# barr# n# ele s# of { s2# ->
230 writeFloatArray (MutableByteArray l u barr#) n (F# ele) = ST $ \ s# ->
231 case index (l,u) n of { I# n# ->
232 case writeFloatArray# barr# n# ele s# of { s2# ->
235 writeDoubleArray (MutableByteArray l u barr#) n (D# ele) = ST $ \ s# ->
236 case index (l,u) n of { I# n# ->
237 case writeDoubleArray# barr# n# ele s# of { s2# ->