Martin Stein <[EMAIL PROTECTED]> writes:
>
..
>
> Yes, I tried to use them but missed some functions to handle
> ByteArrays, particularly
> 1. How can I get a ByteArray? (freezing a MutableByteArray
> seems to be a little strange)
Strange as it might be, but that's the way :)If you want to
hide the use of MutableByteArrays, you can without too much
effort:
-- untested code.
doubleByteArray :: [Double] -> ByteArray Int
doubleByteArray dbls = runST $ do
marr <- newDoubleArray (0,len)
zipWithM_ (writeDoubleArray marr) [0..] dlbs
unsafeFreezeByteArray marr
where
len = max 0 (length dbls - 1)
> 2. How can I thaw a ByteArray to get a MutableByteArray?
>
By manually copying over the bytes - here's the version that I'll
probably add to the MutableArray interface:
\begin{code}
thawByteArray :: Ix ix => ByteArray ix -> ST s (MutableByteArray s ix)
thawByteArray (ByteArray ixs barr#) =
{-
The implementation is made more complex by the
fact that the indexes are in units of whatever
base types that's stored in the byte array.
-}
case (sizeofByteArray# barr#) of
i# -> do
marr <- newCharArray (0,I# i#)
mapM_ (\ idx@(I# idx#) ->
writeCharArray marr idx (C# (indexCharArray# barr# idx#)))
[0..]
let (MutableByteArray _ arr#) = marr
return (MutableByteArray ixs arr#)
\end{code}
The unsafe version of this and thawArray should be supported in the
next release too.
--Sigbjorn
Mutable array ops (was: file operations)
Sigbjorn Finne (Intl Vendor) Fri, 5 Mar 1999 11:28:27 +0100 (MET)
- Re: Mutable array ops (was: file operations) Sigbjorn Finne (Intl Vendor)
- Re: Mutable array ops (was: file operati... Martin Stein
- RE: Mutable array ops (was: file operati... Sigbjorn Finne (Intl Vendor)
- Re: Mutable array ops (was: file ope... Martin Stein
