Skip to content

Instantly share code, notes, and snippets.

@rblaze
Last active December 18, 2015 23:09
Show Gist options
  • Save rblaze/5859979 to your computer and use it in GitHub Desktop.
Save rblaze/5859979 to your computer and use it in GitHub Desktop.
saveVector :: String -> Vector a -> IO ()
saveVector name vec =
withFile name WriteMode $ \h -> do
let (ptr, len) = U.unsafeToForeignPtr0 vec
withForeignPtr ptr $ \p -> hPutBuf h p (len * sizeOf (0 :: a))
loadVector :: String -> IO Vector a
loadVector name =
withFile name ReadMode $ \h -> do
len <- liftM fromIntegral $ hFileSize h
ptr <- mallocForeignPtrBytes len
_ <- withForeignPtr ptr $ \p -> hGetBuf h p len
return $ U.unsafeFromForeignPtr0 ptr (len `div` sizeof (0 :: a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment