summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/ST.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Packed/ST.hs')
-rw-r--r--lib/Data/Packed/ST.hs20
1 files changed, 17 insertions, 3 deletions
diff --git a/lib/Data/Packed/ST.hs b/lib/Data/Packed/ST.hs
index 91b7968..9c9c3b3 100644
--- a/lib/Data/Packed/ST.hs
+++ b/lib/Data/Packed/ST.hs
@@ -1,4 +1,4 @@
1{-# OPTIONS -XTypeOperators -XRank2Types -XFlexibleContexts #-} 1{-# OPTIONS -XTypeOperators -XRank2Types -XFlexibleContexts -XBangPatterns #-}
2 2
3----------------------------------------------------------------------------- 3-----------------------------------------------------------------------------
4-- | 4-- |
@@ -23,8 +23,10 @@ module Data.Packed.ST (
23 STMatrix, newMatrix, thawMatrix, freezeMatrix, runSTMatrix, 23 STMatrix, newMatrix, thawMatrix, freezeMatrix, runSTMatrix,
24 readMatrix, writeMatrix, modifyMatrix, liftSTMatrix, 24 readMatrix, writeMatrix, modifyMatrix, liftSTMatrix,
25 -- * Unsafe functions 25 -- * Unsafe functions
26 newUndefinedVector,
26 unsafeReadVector, unsafeWriteVector, 27 unsafeReadVector, unsafeWriteVector,
27 unsafeThawVector, unsafeFreezeVector, 28 unsafeThawVector, unsafeFreezeVector,
29 newUndefinedMatrix,
28 unsafeReadMatrix, unsafeWriteMatrix, 30 unsafeReadMatrix, unsafeWriteMatrix,
29 unsafeThawMatrix, unsafeFreezeMatrix 31 unsafeThawMatrix, unsafeFreezeMatrix
30) where 32) where
@@ -87,9 +89,17 @@ readVector = safeIndexV unsafeReadVector
87writeVector :: Storable t => STVector s t -> Int -> t -> ST s () 89writeVector :: Storable t => STVector s t -> Int -> t -> ST s ()
88writeVector = safeIndexV unsafeWriteVector 90writeVector = safeIndexV unsafeWriteVector
89 91
92{-# NOINLINE newUndefinedVector #-}
93newUndefinedVector :: Element t => Int -> ST s (STVector s t)
94newUndefinedVector = unsafeIOToST . fmap STVector . createVector
95
90{-# NOINLINE newVector #-} 96{-# NOINLINE newVector #-}
91newVector :: Element t => t -> Int -> ST s (STVector s t) 97newVector :: Element t => t -> Int -> ST s (STVector s t)
92newVector v = unsafeThawVector . constant v 98newVector x n = do
99 v <- newUndefinedVector n
100 let go (-1) = return v
101 go !k = unsafeWriteVector v k x >> go (k-1 :: Int)
102 go (n-1)
93 103
94------------------------------------------------------------------------- 104-------------------------------------------------------------------------
95 105
@@ -153,6 +163,10 @@ readMatrix = safeIndexM unsafeReadMatrix
153writeMatrix :: Storable t => STMatrix s t -> Int -> Int -> t -> ST s () 163writeMatrix :: Storable t => STMatrix s t -> Int -> Int -> t -> ST s ()
154writeMatrix = safeIndexM unsafeWriteMatrix 164writeMatrix = safeIndexM unsafeWriteMatrix
155 165
166{-# NOINLINE newUndefinedMatrix #-}
167newUndefinedMatrix :: Element t => MatrixOrder -> Int -> Int -> ST s (STMatrix s t)
168newUndefinedMatrix order r c = unsafeIOToST $ fmap STMatrix $ createMatrix order r c
169
156{-# NOINLINE newMatrix #-} 170{-# NOINLINE newMatrix #-}
157newMatrix :: Element t => t -> Int -> Int -> ST s (STMatrix s t) 171newMatrix :: Element t => t -> Int -> Int -> ST s (STMatrix s t)
158newMatrix v r c = unsafeThawMatrix . reshape c . constant v $ r*c 172newMatrix v r c = unsafeThawMatrix $ reshape c $ runSTVector $ newVector v (r*c)