Remove Control.Parallel*, now in package parallel
[packages/random.git] / Data / Array / IO / Internals.hs
1 {-# OPTIONS_GHC -#include "HsBase.h" #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Data.Array.IO.Internal
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
7 --
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : non-portable (uses Data.Array.Base)
11 --
12 -- Mutable boxed and unboxed arrays in the IO monad.
13 --
14 -----------------------------------------------------------------------------
15
16 -- #hide
17 module Data.Array.IO.Internals (
18 IOArray(..), -- instance of: Eq, Typeable
19 IOUArray(..), -- instance of: Eq, Typeable
20 castIOUArray, -- :: IOUArray ix a -> IO (IOUArray ix b)
21 ) where
22
23 import Prelude
24
25 import Data.Array.MArray
26 import Data.Int
27 import Data.Word
28 import Data.Typeable
29
30 #ifdef __HUGS__
31 import Hugs.IOArray
32 #endif
33
34 import Control.Monad.ST ( RealWorld, stToIO )
35 import Foreign.Ptr ( Ptr, FunPtr )
36 import Foreign.StablePtr ( StablePtr )
37 import Data.Array.Base
38
39 #ifdef __GLASGOW_HASKELL__
40 import GHC.IOBase
41 import GHC.Base
42 #endif /* __GLASGOW_HASKELL__ */
43
44 #include "Typeable.h"
45
46 INSTANCE_TYPEABLE2(IOArray,iOArrayTc,"IOArray")
47
48 -----------------------------------------------------------------------------
49 -- | Instance declarations for 'IOArray's
50
51 instance MArray IOArray e IO where
52 #if defined(__HUGS__)
53 getBounds = return . boundsIOArray
54 #elif defined(__GLASGOW_HASKELL__)
55 {-# INLINE getBounds #-}
56 getBounds (IOArray marr) = stToIO $ getBounds marr
57 #endif
58 newArray = newIOArray
59 unsafeRead = unsafeReadIOArray
60 unsafeWrite = unsafeWriteIOArray
61
62 -----------------------------------------------------------------------------
63 -- Flat unboxed mutable arrays (IO monad)
64
65 -- | Mutable, unboxed, strict arrays in the 'IO' monad. The type
66 -- arguments are as follows:
67 --
68 -- * @i@: the index type of the array (should be an instance of 'Ix')
69 --
70 -- * @e@: the element type of the array. Only certain element types
71 -- are supported: see "Data.Array.MArray" for a list of instances.
72 --
73 newtype IOUArray i e = IOUArray (STUArray RealWorld i e)
74
75 INSTANCE_TYPEABLE2(IOUArray,iOUArrayTc,"IOUArray")
76
77 instance MArray IOUArray Bool IO where
78 {-# INLINE getBounds #-}
79 getBounds (IOUArray arr) = stToIO $ getBounds arr
80 {-# INLINE newArray #-}
81 newArray lu init = stToIO $ do
82 marr <- newArray lu init; return (IOUArray marr)
83 {-# INLINE newArray_ #-}
84 newArray_ lu = stToIO $ do
85 marr <- newArray_ lu; return (IOUArray marr)
86 {-# INLINE unsafeRead #-}
87 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
88 {-# INLINE unsafeWrite #-}
89 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
90
91 instance MArray IOUArray Char IO where
92 {-# INLINE getBounds #-}
93 getBounds (IOUArray arr) = stToIO $ getBounds arr
94 {-# INLINE newArray #-}
95 newArray lu init = stToIO $ do
96 marr <- newArray lu init; return (IOUArray marr)
97 {-# INLINE newArray_ #-}
98 newArray_ lu = stToIO $ do
99 marr <- newArray_ lu; return (IOUArray marr)
100 {-# INLINE unsafeRead #-}
101 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
102 {-# INLINE unsafeWrite #-}
103 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
104
105 instance MArray IOUArray Int IO where
106 {-# INLINE getBounds #-}
107 getBounds (IOUArray arr) = stToIO $ getBounds arr
108 {-# INLINE newArray #-}
109 newArray lu init = stToIO $ do
110 marr <- newArray lu init; return (IOUArray marr)
111 {-# INLINE newArray_ #-}
112 newArray_ lu = stToIO $ do
113 marr <- newArray_ lu; return (IOUArray marr)
114 {-# INLINE unsafeRead #-}
115 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
116 {-# INLINE unsafeWrite #-}
117 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
118
119 instance MArray IOUArray Word IO where
120 {-# INLINE getBounds #-}
121 getBounds (IOUArray arr) = stToIO $ getBounds arr
122 {-# INLINE newArray #-}
123 newArray lu init = stToIO $ do
124 marr <- newArray lu init; return (IOUArray marr)
125 {-# INLINE newArray_ #-}
126 newArray_ lu = stToIO $ do
127 marr <- newArray_ lu; return (IOUArray marr)
128 {-# INLINE unsafeRead #-}
129 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
130 {-# INLINE unsafeWrite #-}
131 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
132
133 instance MArray IOUArray (Ptr a) IO where
134 {-# INLINE getBounds #-}
135 getBounds (IOUArray arr) = stToIO $ getBounds arr
136 {-# INLINE newArray #-}
137 newArray lu init = stToIO $ do
138 marr <- newArray lu init; return (IOUArray marr)
139 {-# INLINE newArray_ #-}
140 newArray_ lu = stToIO $ do
141 marr <- newArray_ lu; return (IOUArray marr)
142 {-# INLINE unsafeRead #-}
143 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
144 {-# INLINE unsafeWrite #-}
145 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
146
147 instance MArray IOUArray (FunPtr a) IO where
148 {-# INLINE getBounds #-}
149 getBounds (IOUArray arr) = stToIO $ getBounds arr
150 {-# INLINE newArray #-}
151 newArray lu init = stToIO $ do
152 marr <- newArray lu init; return (IOUArray marr)
153 {-# INLINE newArray_ #-}
154 newArray_ lu = stToIO $ do
155 marr <- newArray_ lu; return (IOUArray marr)
156 {-# INLINE unsafeRead #-}
157 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
158 {-# INLINE unsafeWrite #-}
159 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
160
161 instance MArray IOUArray Float IO where
162 {-# INLINE getBounds #-}
163 getBounds (IOUArray arr) = stToIO $ getBounds arr
164 {-# INLINE newArray #-}
165 newArray lu init = stToIO $ do
166 marr <- newArray lu init; return (IOUArray marr)
167 {-# INLINE newArray_ #-}
168 newArray_ lu = stToIO $ do
169 marr <- newArray_ lu; return (IOUArray marr)
170 {-# INLINE unsafeRead #-}
171 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
172 {-# INLINE unsafeWrite #-}
173 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
174
175 instance MArray IOUArray Double IO where
176 {-# INLINE getBounds #-}
177 getBounds (IOUArray arr) = stToIO $ getBounds arr
178 {-# INLINE newArray #-}
179 newArray lu init = stToIO $ do
180 marr <- newArray lu init; return (IOUArray marr)
181 {-# INLINE newArray_ #-}
182 newArray_ lu = stToIO $ do
183 marr <- newArray_ lu; return (IOUArray marr)
184 {-# INLINE unsafeRead #-}
185 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
186 {-# INLINE unsafeWrite #-}
187 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
188
189 instance MArray IOUArray (StablePtr a) IO where
190 {-# INLINE getBounds #-}
191 getBounds (IOUArray arr) = stToIO $ getBounds arr
192 {-# INLINE newArray #-}
193 newArray lu init = stToIO $ do
194 marr <- newArray lu init; return (IOUArray marr)
195 {-# INLINE newArray_ #-}
196 newArray_ lu = stToIO $ do
197 marr <- newArray_ lu; return (IOUArray marr)
198 {-# INLINE unsafeRead #-}
199 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
200 {-# INLINE unsafeWrite #-}
201 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
202
203 instance MArray IOUArray Int8 IO where
204 {-# INLINE getBounds #-}
205 getBounds (IOUArray arr) = stToIO $ getBounds arr
206 {-# INLINE newArray #-}
207 newArray lu init = stToIO $ do
208 marr <- newArray lu init; return (IOUArray marr)
209 {-# INLINE newArray_ #-}
210 newArray_ lu = stToIO $ do
211 marr <- newArray_ lu; return (IOUArray marr)
212 {-# INLINE unsafeRead #-}
213 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
214 {-# INLINE unsafeWrite #-}
215 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
216
217 instance MArray IOUArray Int16 IO where
218 {-# INLINE getBounds #-}
219 getBounds (IOUArray arr) = stToIO $ getBounds arr
220 {-# INLINE newArray #-}
221 newArray lu init = stToIO $ do
222 marr <- newArray lu init; return (IOUArray marr)
223 {-# INLINE newArray_ #-}
224 newArray_ lu = stToIO $ do
225 marr <- newArray_ lu; return (IOUArray marr)
226 {-# INLINE unsafeRead #-}
227 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
228 {-# INLINE unsafeWrite #-}
229 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
230
231 instance MArray IOUArray Int32 IO where
232 {-# INLINE getBounds #-}
233 getBounds (IOUArray arr) = stToIO $ getBounds arr
234 {-# INLINE newArray #-}
235 newArray lu init = stToIO $ do
236 marr <- newArray lu init; return (IOUArray marr)
237 {-# INLINE newArray_ #-}
238 newArray_ lu = stToIO $ do
239 marr <- newArray_ lu; return (IOUArray marr)
240 {-# INLINE unsafeRead #-}
241 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
242 {-# INLINE unsafeWrite #-}
243 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
244
245 instance MArray IOUArray Int64 IO where
246 {-# INLINE getBounds #-}
247 getBounds (IOUArray arr) = stToIO $ getBounds arr
248 {-# INLINE newArray #-}
249 newArray lu init = stToIO $ do
250 marr <- newArray lu init; return (IOUArray marr)
251 {-# INLINE newArray_ #-}
252 newArray_ lu = stToIO $ do
253 marr <- newArray_ lu; return (IOUArray marr)
254 {-# INLINE unsafeRead #-}
255 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
256 {-# INLINE unsafeWrite #-}
257 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
258
259 instance MArray IOUArray Word8 IO where
260 {-# INLINE getBounds #-}
261 getBounds (IOUArray arr) = stToIO $ getBounds arr
262 {-# INLINE newArray #-}
263 newArray lu init = stToIO $ do
264 marr <- newArray lu init; return (IOUArray marr)
265 {-# INLINE newArray_ #-}
266 newArray_ lu = stToIO $ do
267 marr <- newArray_ lu; return (IOUArray marr)
268 {-# INLINE unsafeRead #-}
269 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
270 {-# INLINE unsafeWrite #-}
271 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
272
273 instance MArray IOUArray Word16 IO where
274 {-# INLINE getBounds #-}
275 getBounds (IOUArray arr) = stToIO $ getBounds arr
276 {-# INLINE newArray #-}
277 newArray lu init = stToIO $ do
278 marr <- newArray lu init; return (IOUArray marr)
279 {-# INLINE newArray_ #-}
280 newArray_ lu = stToIO $ do
281 marr <- newArray_ lu; return (IOUArray marr)
282 {-# INLINE unsafeRead #-}
283 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
284 {-# INLINE unsafeWrite #-}
285 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
286
287 instance MArray IOUArray Word32 IO where
288 {-# INLINE getBounds #-}
289 getBounds (IOUArray arr) = stToIO $ getBounds arr
290 {-# INLINE newArray #-}
291 newArray lu init = stToIO $ do
292 marr <- newArray lu init; return (IOUArray marr)
293 {-# INLINE newArray_ #-}
294 newArray_ lu = stToIO $ do
295 marr <- newArray_ lu; return (IOUArray marr)
296 {-# INLINE unsafeRead #-}
297 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
298 {-# INLINE unsafeWrite #-}
299 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
300
301 instance MArray IOUArray Word64 IO where
302 {-# INLINE getBounds #-}
303 getBounds (IOUArray arr) = stToIO $ getBounds arr
304 {-# INLINE newArray #-}
305 newArray lu init = stToIO $ do
306 marr <- newArray lu init; return (IOUArray marr)
307 {-# INLINE newArray_ #-}
308 newArray_ lu = stToIO $ do
309 marr <- newArray_ lu; return (IOUArray marr)
310 {-# INLINE unsafeRead #-}
311 unsafeRead (IOUArray marr) i = stToIO (unsafeRead marr i)
312 {-# INLINE unsafeWrite #-}
313 unsafeWrite (IOUArray marr) i e = stToIO (unsafeWrite marr i e)
314
315 -- | Casts an 'IOUArray' with one element type into one with a
316 -- different element type. All the elements of the resulting array
317 -- are undefined (unless you know what you\'re doing...).
318 castIOUArray :: IOUArray ix a -> IO (IOUArray ix b)
319 castIOUArray (IOUArray marr) = stToIO $ do
320 marr' <- castSTUArray marr
321 return (IOUArray marr')
322