[Haskell-cafe] Array copying

Andrew Coppin andrewcoppin at btinternet.com
Sun Dec 2 06:55:44 EST 2007

Andrew Coppin wrote:
> copy :: Word32 -> IOUArray Word32 Bool -> Word32 -> IO (IOUArray 
> Word32 Bool)
> copy p grid size = do
>  let size' = size * p
>  grid' <- newArray (1,size') False
>  mapM_
>    (\n -> do
>      b <- readArray grid n
>      if b
>        then mapM_ (\x -> writeArray grid' (n + size*x) True) [0..p-1]
>        else return ()
>    )
>    [1..size]
>  return grid'

Actually, thinking about this... for most kinds of arrays (whether boxed 
or unboxed, mutable or immutable) there's probably a more efficient way 
to copy the data then this. Maybe we should add something to the various 
array APIs to allow efficient copying of arrays / large chunks of arrays?

(In the case of an unboxed array of bits, you can probably copy whole 
32-bit or 64-bit words with a few machine instructions, for example.)

More information about the Haskell-Cafe mailing list