Skip to content

Instantly share code, notes, and snippets.

@fffej
Created May 5, 2010 06:03
Show Gist options
  • Save fffej/390438 to your computer and use it in GitHub Desktop.
Save fffej/390438 to your computer and use it in GitHub Desktop.
import qualified Data.Vector.Unboxed.Mutable as M
import qualified Data.Vector.Generic.Mutable as GM
import Control.Monad
type DVector = M.IOVector Double
data Grid = Grid Int DVector
-- |Create an empty vector
emptyGrid :: Int -> IO Grid
emptyGrid sz = do
d <- GM.unsafeNewWith (vectorLength sz) 0
return (Grid sz d)
-- |Translate from 2D to 1D co-ordinates
ix :: Int -> (Int,Int) -> Int
ix n (i,j) = i + (n+2) * j
-- |Write a single value at the given co-ordinates
writeVal :: Grid -> (Int,Int) -> Double -> IO ()
writeVal (Grid sz d) p = GM.unsafeWrite d (ix sz p)
-- |Write multiple values
setVals :: Grid -> [((Int,Int),Double)] -> IO ()
setVals g vals = forM_ vals (uncurry (writeVal g))
-- |Read the value at the given point
readVal :: Grid -> (Int,Int) -> IO Double
readVal (Grid sz d) p = GM.unsafeRead d (ix sz p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment