1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
<sect2> <idx/MutableArray/
<label id="MutableArray">
<p>
The <tt/MutableArray/ interface provide operations for reading and
writing values to mutable arrays. There's two kinds of
mutable arrays, the mutatable version of Haskell <tt/Array/s
and <em/mutable byte arrays/, chunks of memory containing
values of some basic type.
<sect3> <idx/Mutable arrays/
<label id="MutableArray:mutable-arrays">
<p>
The mutable array section of the API provides the following
operations:
<tscreen><code>
-- mutable arrays:
newArray :: Ix ix -> (ix,ix) -> elt -> ST s (MutableArray s ix elt)
boundsOfArray :: Ix ix => MutableArray s ix elt -> (ix, ix)
readArray :: Ix ix => MutableArray s ix elt -> ix -> ST s elt
writeArray :: Ix ix => MutableArray s ix elt -> ix -> elt -> ST s ()
freezeArray :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
thawArray :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
unsafeFreezeArray :: Ix ix => MutableArray s ix elt -> ST s (Array ix elt)
unsafeThawArray :: Ix ix => Array ix elt -> ST s (MutableArray s ix elt)
</code></tscreen>
<nidx>newArray</nidx>
<nidx>boundsOfArray</nidx>
<nidx>readArray</nidx>
<nidx>writeArray</nidx>
<nidx>freezeArray</nidx>
<nidx>thawArray</nidx>
<nidx>unsafeFreezeArray</nidx>
<nidx>unsafeThawArray</nidx>
<bf/Remarks:/
<itemize>
<item>
The <tt/freezeArray/ action converts a mutable array into an
immutable one by copying, whereas <tt/unsafeFreezeArray/ returns
an immutable array that is effectively just the type cast version
of the mutable array. Should you write to the mutable array after
it has been (unsafely) frozen, you'll side-effect the immutable
array in the process. Please don't :-)
<item>
The operation <tt/thawArray/ goes the other way, converting
an immutable <tt/Array/ into a mutable one. This is done by
copying. The operation <tt/unsafeThawArray/ is also provided,
which places the same kind of proof obligation on the programmer
as <tt/unsafeFreezeArray/ does.
</itemize>
<sect3> <idx/Mutable byte arrays/
<label id="MutableArray:mutable-byte-arrays">
<p>
<tscreen><code>
-- creators:
newCharArray :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
newAddrArray :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
newIntArray :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
newWordArray :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
newFloatArray :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
newDoubleArray :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
newStablePtrArray :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
boundsOfMutableByteArray
:: Ix ix => MutableByteArray s ix -> (ix, ix)
readCharArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Char
readIntArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Int
readAddrArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Addr
readFloatArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Float
readDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Double
readStablePtrArray :: Ix ix => MutableByteArray s ix -> ix -> ST s (StablePtr a)
readWord8Array :: Ix ix => MutableByteArray s ix -> ix -> ST s Word8
readWord16Array :: Ix ix => MutableByteArray s ix -> ix -> ST s Word16
readWord32Array :: Ix ix => MutableByteArray s ix -> ix -> ST s Word32
readWord64Array :: Ix ix => MutableByteArray s ix -> ix -> ST s Word64
readInt8Array :: Ix ix => MutableByteArray s ix -> ix -> ST s Int8
readInt16Array :: Ix ix => MutableByteArray s ix -> ix -> ST s Int16
readInt32Array :: Ix ix => MutableByteArray s ix -> ix -> ST s Int32
readInt64Array :: Ix ix => MutableByteArray s ix -> ix -> ST s Int64
writeCharArray :: Ix ix => MutableByteArray s ix -> ix -> Char -> ST s ()
writeIntArray :: Ix ix => MutableByteArray s ix -> ix -> Int -> ST s ()
writeAddrArray :: Ix ix => MutableByteArray s ix -> ix -> Addr -> ST s ()
writeFloatArray :: Ix ix => MutableByteArray s ix -> ix -> Float -> ST s ()
writeDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> Double -> ST s ()
writeStablePtrArray :: Ix ix => MutableByteArray s ix -> ix -> StablePtr a -> ST s ()
writeWord8Array :: Ix ix => MutableByteArray s ix -> ix -> Word8 -> ST s ()
writeWord16Array :: Ix ix => MutableByteArray s ix -> ix -> Word16 -> ST s ()
writeWord32Array :: Ix ix => MutableByteArray s ix -> ix -> Word32 -> ST s ()
writeWord64Array :: Ix ix => MutableByteArray s ix -> ix -> Word64 -> ST s ()
writeInt8Array :: Ix ix => MutableByteArray s ix -> ix -> Int8 -> ST s ()
writeInt16Array :: Ix ix => MutableByteArray s ix -> ix -> Int16 -> ST s ()
writeInt32Array :: Ix ix => MutableByteArray s ix -> ix -> Int32 -> ST s ()
writeInt64Array :: Ix ix => MutableByteArray s ix -> ix -> Int64 -> ST s ()
freezeCharArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
freezeIntArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
freezeAddrArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
freezeFloatArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
freezeDoubleArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
freezeStablePtrArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
unsafeFreezeByteArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
sizeofMutableByteArray :: Ix ix => MutableByteArray s ix -> Int
thawByteArray :: Ix ix => ByteArray ixt -> ST s (MutableByteArray s ix)
unsafeThawByteArray :: Ix ix => ByteArray ixt -> ST s (MutableByteArray s ix)
</code></tscreen>
<nidx>newCharArray</nidx>
<nidx>newAddrArray</nidx>
<nidx>newIntArray</nidx>
<nidx>newFloatArray</nidx>
<nidx>newDoubleArray</nidx>
<nidx>boundsOfMutableByteArray</nidx>
<nidx>readCharArray</nidx>
<nidx>readIntArray</nidx>
<nidx>readAddrArray</nidx>
<nidx>readFloatArray</nidx>
<nidx>readDoubleArray</nidx>
<nidx>readWord8Array</nidx>
<nidx>readWord16Array</nidx>
<nidx>readWord32Array</nidx>
<nidx>readWord64Array</nidx>
<nidx>readInt8Array</nidx>
<nidx>readInt16Array</nidx>
<nidx>readInt32Array</nidx>
<nidx>readInt64Array</nidx>
<nidx>writeCharArray</nidx>
<nidx>writeIntArray</nidx>
<nidx>writeAddrArray</nidx>
<nidx>writeFloatArray</nidx>
<nidx>writeDoubleArray</nidx>
<nidx>writeWord8Array</nidx>
<nidx>writeWord16Array</nidx>
<nidx>writeWord32Array</nidx>
<nidx>writeWord64Array</nidx>
<nidx>writeInt8Array</nidx>
<nidx>writeInt16Array</nidx>
<nidx>writeInt32Array</nidx>
<nidx>writeInt64Array</nidx>
<nidx>freezeCharArray</nidx>
<nidx>freezeIntArray</nidx>
<nidx>freezeAddrArray</nidx>
<nidx>freezeFloatArray</nidx>
<nidx>freezeDoubleArray</nidx>
<nidx>unsafeFreezeByteArray</nidx>
<nidx>unsafeThawByteArray</nidx>
<nidx>thawByteArray</nidx>
<bf/Remarks:/
<itemize>
<item>
A Mutable byte array is created by specifying its size in units of
some basic type. For example,
<tscreen><code>
mkPair :: ST s (MutableByteArray s Int)
mkPair = newIntArray (0,1)
</code></tscreen>
creates a mutable array capable of storing two <tt/Int/s. Notice
that the range size <em/is not in bytes/, but in units of the
basic type.
<item>
A mutable byte array is not parameterised over the kind of values
it contains. A consequence of this is that it is possible to
have byte arrays containing a mix of basic types, or even read
a value from the array at a different type from which it was
written, e.g.,
<tscreen><code>
isLitteEndian :: IO Bool
isLitteEndian = stToIO $ do
x <- newIntArray (0,1)
writeIntArray x 1
v <- readCharArray x 0
return (v == chr 1)
</code></tscreen>
It's left as an exercise for the reader to determine whether having
byte arrays not be parameterised over the type of values they
contain is a bug or a feature..
<item>
As for mutable arrays, operations for turning mutable byte arrays
into immutable byte arrays are also provided by the <tt/freeze*/
class of actions. There's also the non-copying
<tt/unsafeFreezeByteArray/.
<p>
<item>
Operations for going the other way, where an immutable byte
array is 'thawed' are also provided. <tt/thawByteArray/ does
this by copying, whereas <tt/unsafeThawByteArray/ does not
<item>
The operation <tt/sizeofMutableByteArray/ returns the size of
the array, <em/in bytes./
</itemize>
|