diff options
Diffstat (limited to 'lib/Data/Packed/ST.hs')
-rw-r--r-- | lib/Data/Packed/ST.hs | 20 |
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 | |||
87 | writeVector :: Storable t => STVector s t -> Int -> t -> ST s () | 89 | writeVector :: Storable t => STVector s t -> Int -> t -> ST s () |
88 | writeVector = safeIndexV unsafeWriteVector | 90 | writeVector = safeIndexV unsafeWriteVector |
89 | 91 | ||
92 | {-# NOINLINE newUndefinedVector #-} | ||
93 | newUndefinedVector :: Element t => Int -> ST s (STVector s t) | ||
94 | newUndefinedVector = unsafeIOToST . fmap STVector . createVector | ||
95 | |||
90 | {-# NOINLINE newVector #-} | 96 | {-# NOINLINE newVector #-} |
91 | newVector :: Element t => t -> Int -> ST s (STVector s t) | 97 | newVector :: Element t => t -> Int -> ST s (STVector s t) |
92 | newVector v = unsafeThawVector . constant v | 98 | newVector 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 | |||
153 | writeMatrix :: Storable t => STMatrix s t -> Int -> Int -> t -> ST s () | 163 | writeMatrix :: Storable t => STMatrix s t -> Int -> Int -> t -> ST s () |
154 | writeMatrix = safeIndexM unsafeWriteMatrix | 164 | writeMatrix = safeIndexM unsafeWriteMatrix |
155 | 165 | ||
166 | {-# NOINLINE newUndefinedMatrix #-} | ||
167 | newUndefinedMatrix :: Element t => MatrixOrder -> Int -> Int -> ST s (STMatrix s t) | ||
168 | newUndefinedMatrix order r c = unsafeIOToST $ fmap STMatrix $ createMatrix order r c | ||
169 | |||
156 | {-# NOINLINE newMatrix #-} | 170 | {-# NOINLINE newMatrix #-} |
157 | newMatrix :: Element t => t -> Int -> Int -> ST s (STMatrix s t) | 171 | newMatrix :: Element t => t -> Int -> Int -> ST s (STMatrix s t) |
158 | newMatrix v r c = unsafeThawMatrix . reshape c . constant v $ r*c | 172 | newMatrix v r c = unsafeThawMatrix $ reshape c $ runSTVector $ newVector v (r*c) |