@@ -79,6 +79,7 @@ module Data.Vector.Generic.Mutable (
79
79
) where
80
80
81
81
import Control.Monad ((<=<) )
82
+ import Control.Monad.ST
82
83
import Data.Vector.Generic.Mutable.Base
83
84
import qualified Data.Vector.Generic.Base as V
84
85
@@ -91,7 +92,7 @@ import Data.Vector.Fusion.Bundle.Size
91
92
import Data.Vector.Fusion.Util ( delay_inline )
92
93
import Data.Vector.Internal.Check
93
94
94
- import Control.Monad.Primitive ( PrimMonad (.. ), RealWorld , stToPrim )
95
+ import Control.Monad.Primitive ( PrimMonad (.. ), stToPrim )
95
96
96
97
import Prelude
97
98
( Ord , Monad , Bool (.. ), Int , Maybe (.. ), Either (.. ), Ordering (.. )
@@ -106,8 +107,7 @@ import Data.Bits ( Bits(shiftR) )
106
107
-- Internal functions
107
108
-- ------------------
108
109
109
- unsafeAppend1 :: (PrimMonad m , MVector v a )
110
- => v (PrimState m ) a -> Int -> a -> m (v (PrimState m ) a )
110
+ unsafeAppend1 :: (MVector v a ) => v s a -> Int -> a -> ST s (v s a )
111
111
{-# INLINE_INNER unsafeAppend1 #-}
112
112
-- NOTE: The case distinction has to be on the outside because
113
113
-- GHC creates a join point for the unsafeWrite even when everything
@@ -122,8 +122,7 @@ unsafeAppend1 v i x
122
122
checkIndex Internal i (length v') $ unsafeWrite v' i x
123
123
return v'
124
124
125
- unsafePrepend1 :: (PrimMonad m , MVector v a )
126
- => v (PrimState m ) a -> Int -> a -> m (v (PrimState m ) a , Int )
125
+ unsafePrepend1 :: (MVector v a ) => v s a -> Int -> a -> ST s (v s a , Int )
127
126
{-# INLINE_INNER unsafePrepend1 #-}
128
127
unsafePrepend1 v i x
129
128
| i /= 0 = do
@@ -207,7 +206,7 @@ unstream :: (PrimMonad m, MVector v a)
207
206
=> Bundle u a -> m (v (PrimState m ) a )
208
207
-- NOTE: replace INLINE_FUSED by INLINE? (also in unstreamR)
209
208
{-# INLINE_FUSED unstream #-}
210
- unstream s = munstream (Bundle. lift s)
209
+ unstream s = stToPrim $ munstream (Bundle. lift s)
211
210
212
211
-- | Create a new mutable vector and fill it with elements from the monadic
213
212
-- stream. The vector will grow exponentially if the maximum size of the stream
@@ -243,9 +242,8 @@ munstreamUnknown s
243
242
$ unsafeSlice 0 n v'
244
243
where
245
244
{-# INLINE_INNER put #-}
246
- put (v,i) x = do
247
- v' <- unsafeAppend1 v i x
248
- return (v',i+ 1 )
245
+ put (v,i) x = stToPrim $ do v' <- unsafeAppend1 v i x
246
+ return (v',i+ 1 )
249
247
250
248
251
249
-- | Create a new mutable vector and fill it with elements from the 'Bundle'.
@@ -255,7 +253,7 @@ vunstream :: (PrimMonad m, V.Vector v a)
255
253
=> Bundle v a -> m (V. Mutable v (PrimState m ) a )
256
254
-- NOTE: replace INLINE_FUSED by INLINE? (also in unstreamR)
257
255
{-# INLINE_FUSED vunstream #-}
258
- vunstream s = vmunstream (Bundle. lift s)
256
+ vunstream s = stToPrim $ vmunstream (Bundle. lift s)
259
257
260
258
-- | Create a new mutable vector and fill it with elements from the monadic
261
259
-- stream. The vector will grow exponentially if the maximum size of the stream
@@ -311,7 +309,7 @@ unstreamR :: (PrimMonad m, MVector v a)
311
309
=> Bundle u a -> m (v (PrimState m ) a )
312
310
-- NOTE: replace INLINE_FUSED by INLINE? (also in unstream)
313
311
{-# INLINE_FUSED unstreamR #-}
314
- unstreamR s = munstreamR (Bundle. lift s)
312
+ unstreamR s = stToPrim $ munstreamR (Bundle. lift s)
315
313
316
314
-- | Create a new mutable vector and fill it with elements from the monadic
317
315
-- stream from right to left. The vector will grow exponentially if the maximum
@@ -350,7 +348,7 @@ munstreamRUnknown s
350
348
$ unsafeSlice i (n- i) v'
351
349
where
352
350
{-# INLINE_INNER put #-}
353
- put (v,i) x = unsafePrepend1 v i x
351
+ put (v,i) x = stToPrim $ unsafePrepend1 v i x
354
352
355
353
-- Length
356
354
-- ------
@@ -563,10 +561,9 @@ enlarge_delta :: MVector v a => v s a -> Int
563
561
enlarge_delta v = max (length v) 1
564
562
565
563
-- | Grow a vector logarithmically.
566
- enlarge :: (PrimMonad m , MVector v a )
567
- => v (PrimState m ) a -> m (v (PrimState m ) a )
564
+ enlarge :: (MVector v a ) => v s a -> ST s (v s a )
568
565
{-# INLINE enlarge #-}
569
- enlarge v = stToPrim $ do
566
+ enlarge v = do
570
567
vnew <- unsafeGrow v by
571
568
basicInitialize $ basicUnsafeSlice (length v) by vnew
572
569
return vnew
@@ -996,10 +993,10 @@ unsafeMove dst src = check Unsafe "length mismatch" (length dst == length src)
996
993
accum :: forall m v a b u . (HasCallStack , PrimMonad m , MVector v a )
997
994
=> (a -> b -> a ) -> v (PrimState m ) a -> Bundle u (Int , b ) -> m ()
998
995
{-# INLINE accum #-}
999
- accum f ! v s = Bundle. mapM_ upd s
996
+ accum f ! v s = stToPrim $ Bundle. mapM_ upd s
1000
997
where
1001
998
{-# INLINE_INNER upd #-}
1002
- upd :: HasCallStack => (Int , b ) -> m ()
999
+ upd :: HasCallStack => (Int , b ) -> ST ( PrimState m ) ()
1003
1000
upd (i,b) = do
1004
1001
a <- checkIndex Bounds i n $ unsafeRead v i
1005
1002
unsafeWrite v i (f a b)
@@ -1008,18 +1005,18 @@ accum f !v s = Bundle.mapM_ upd s
1008
1005
update :: forall m v a u . (HasCallStack , PrimMonad m , MVector v a )
1009
1006
=> v (PrimState m ) a -> Bundle u (Int , a ) -> m ()
1010
1007
{-# INLINE update #-}
1011
- update ! v s = Bundle. mapM_ upd s
1008
+ update ! v s = stToPrim $ Bundle. mapM_ upd s
1012
1009
where
1013
1010
{-# INLINE_INNER upd #-}
1014
- upd :: HasCallStack => (Int , a ) -> m ()
1011
+ upd :: HasCallStack => (Int , a ) -> ST ( PrimState m ) ()
1015
1012
upd (i,b) = checkIndex Bounds i n $ unsafeWrite v i b
1016
1013
1017
1014
! n = length v
1018
1015
1019
1016
unsafeAccum :: (PrimMonad m , MVector v a )
1020
1017
=> (a -> b -> a ) -> v (PrimState m ) a -> Bundle u (Int , b ) -> m ()
1021
1018
{-# INLINE unsafeAccum #-}
1022
- unsafeAccum f ! v s = Bundle. mapM_ upd s
1019
+ unsafeAccum f ! v s = stToPrim $ Bundle. mapM_ upd s
1023
1020
where
1024
1021
{-# INLINE_INNER upd #-}
1025
1022
upd (i,b) = do
@@ -1028,17 +1025,17 @@ unsafeAccum f !v s = Bundle.mapM_ upd s
1028
1025
! n = length v
1029
1026
1030
1027
unsafeUpdate :: (PrimMonad m , MVector v a )
1031
- => v (PrimState m ) a -> Bundle u (Int , a ) -> m ()
1028
+ => v (PrimState m ) a -> Bundle u (Int , a ) -> m ()
1032
1029
{-# INLINE unsafeUpdate #-}
1033
- unsafeUpdate ! v s = Bundle. mapM_ upd s
1030
+ unsafeUpdate ! v s = stToPrim $ Bundle. mapM_ upd s
1034
1031
where
1035
1032
{-# INLINE_INNER upd #-}
1036
1033
upd (i,b) = checkIndex Unsafe i n $ unsafeWrite v i b
1037
1034
! n = length v
1038
1035
1039
1036
reverse :: (PrimMonad m , MVector v a ) => v (PrimState m ) a -> m ()
1040
1037
{-# INLINE reverse #-}
1041
- reverse ! v = reverse_loop 0 (length v - 1 )
1038
+ reverse ! v = stToPrim $ reverse_loop 0 (length v - 1 )
1042
1039
where
1043
1040
reverse_loop i j | i < j = do
1044
1041
unsafeSwap v i j
@@ -1048,11 +1045,11 @@ reverse !v = reverse_loop 0 (length v - 1)
1048
1045
unstablePartition :: forall m v a . (PrimMonad m , MVector v a )
1049
1046
=> (a -> Bool ) -> v (PrimState m ) a -> m Int
1050
1047
{-# INLINE unstablePartition #-}
1051
- unstablePartition f ! v = from_left 0 (length v)
1048
+ unstablePartition f ! v = stToPrim $ from_left 0 (length v)
1052
1049
where
1053
1050
-- NOTE: GHC 6.10.4 panics without the signatures on from_left and
1054
1051
-- from_right
1055
- from_left :: Int -> Int -> m Int
1052
+ from_left :: Int -> Int -> ST ( PrimState m ) Int
1056
1053
from_left i j
1057
1054
| i == j = return i
1058
1055
| otherwise = do
@@ -1061,7 +1058,7 @@ unstablePartition f !v = from_left 0 (length v)
1061
1058
then from_left (i+ 1 ) j
1062
1059
else from_right i (j- 1 )
1063
1060
1064
- from_right :: Int -> Int -> m Int
1061
+ from_right :: Int -> Int -> ST ( PrimState m ) Int
1065
1062
from_right i j
1066
1063
| i == j = return i
1067
1064
| otherwise = do
@@ -1078,7 +1075,8 @@ unstablePartitionBundle :: (PrimMonad m, MVector v a)
1078
1075
=> (a -> Bool ) -> Bundle u a -> m (v (PrimState m ) a , v (PrimState m ) a )
1079
1076
{-# INLINE unstablePartitionBundle #-}
1080
1077
unstablePartitionBundle f s
1081
- = case upperBound (Bundle. size s) of
1078
+ = stToPrim
1079
+ $ case upperBound (Bundle. size s) of
1082
1080
Just n -> unstablePartitionMax f s n
1083
1081
Nothing -> partitionUnknown f s
1084
1082
@@ -1087,7 +1085,7 @@ unstablePartitionMax :: (PrimMonad m, MVector v a)
1087
1085
-> m (v (PrimState m ) a , v (PrimState m ) a )
1088
1086
{-# INLINE unstablePartitionMax #-}
1089
1087
unstablePartitionMax f s n
1090
- = do
1088
+ = stToPrim $ do
1091
1089
v <- checkLength Internal n $ unsafeNew n
1092
1090
let {-# INLINE_INNER put #-}
1093
1091
put (i, j) x
@@ -1105,15 +1103,15 @@ partitionBundle :: (PrimMonad m, MVector v a)
1105
1103
=> (a -> Bool ) -> Bundle u a -> m (v (PrimState m ) a , v (PrimState m ) a )
1106
1104
{-# INLINE partitionBundle #-}
1107
1105
partitionBundle f s
1108
- = case upperBound (Bundle. size s) of
1106
+ = stToPrim
1107
+ $ case upperBound (Bundle. size s) of
1109
1108
Just n -> partitionMax f s n
1110
1109
Nothing -> partitionUnknown f s
1111
1110
1112
1111
partitionMax :: (PrimMonad m , MVector v a )
1113
1112
=> (a -> Bool ) -> Bundle u a -> Int -> m (v (PrimState m ) a , v (PrimState m ) a )
1114
1113
{-# INLINE partitionMax #-}
1115
- partitionMax f s n
1116
- = do
1114
+ partitionMax f s n = stToPrim $ do
1117
1115
v <- checkLength Internal n $ unsafeNew n
1118
1116
1119
1117
let {-# INLINE_INNER put #-}
@@ -1138,8 +1136,7 @@ partitionMax f s n
1138
1136
partitionUnknown :: (PrimMonad m , MVector v a )
1139
1137
=> (a -> Bool ) -> Bundle u a -> m (v (PrimState m ) a , v (PrimState m ) a )
1140
1138
{-# INLINE partitionUnknown #-}
1141
- partitionUnknown f s
1142
- = do
1139
+ partitionUnknown f s = stToPrim $ do
1143
1140
v1 <- unsafeNew 0
1144
1141
v2 <- unsafeNew 0
1145
1142
(v1', n1, v2', n2) <- Bundle. foldM' put (v1, 0 , v2, 0 ) s
@@ -1165,15 +1162,16 @@ partitionWithBundle :: (PrimMonad m, MVector v a, MVector v b, MVector v c)
1165
1162
=> (a -> Either b c ) -> Bundle u a -> m (v (PrimState m ) b , v (PrimState m ) c )
1166
1163
{-# INLINE partitionWithBundle #-}
1167
1164
partitionWithBundle f s
1168
- = case upperBound (Bundle. size s) of
1165
+ = stToPrim
1166
+ $ case upperBound (Bundle. size s) of
1169
1167
Just n -> partitionWithMax f s n
1170
1168
Nothing -> partitionWithUnknown f s
1171
1169
1172
1170
partitionWithMax :: (PrimMonad m , MVector v a , MVector v b , MVector v c )
1173
1171
=> (a -> Either b c ) -> Bundle u a -> Int -> m (v (PrimState m ) b , v (PrimState m ) c )
1174
1172
{-# INLINE partitionWithMax #-}
1175
1173
partitionWithMax f s n
1176
- = do
1174
+ = stToPrim $ do
1177
1175
v1 <- unsafeNew n
1178
1176
v2 <- unsafeNew n
1179
1177
let {-# INLINE_INNER put #-}
@@ -1194,7 +1192,7 @@ partitionWithUnknown :: forall m v u a b c.
1194
1192
=> (a -> Either b c ) -> Bundle u a -> m (v (PrimState m ) b , v (PrimState m ) c )
1195
1193
{-# INLINE partitionWithUnknown #-}
1196
1194
partitionWithUnknown f s
1197
- = do
1195
+ = stToPrim $ do
1198
1196
v1 <- unsafeNew 0
1199
1197
v2 <- unsafeNew 0
1200
1198
(v1', n1, v2', n2) <- Bundle. foldM' put (v1, 0 , v2, 0 ) s
@@ -1204,14 +1202,14 @@ partitionWithUnknown f s
1204
1202
where
1205
1203
put :: (v (PrimState m ) b , Int , v (PrimState m ) c , Int )
1206
1204
-> a
1207
- -> m (v (PrimState m ) b , Int , v (PrimState m ) c , Int )
1205
+ -> ST ( PrimState m ) (v (PrimState m ) b , Int , v (PrimState m ) c , Int )
1208
1206
{-# INLINE_INNER put #-}
1209
1207
put (v1, i1, v2, i2) x = case f x of
1210
1208
Left b -> do
1211
- v1' <- unsafeAppend1 v1 i1 b
1209
+ v1' <- stToPrim $ unsafeAppend1 v1 i1 b
1212
1210
return (v1', i1+ 1 , v2, i2)
1213
1211
Right c -> do
1214
- v2' <- unsafeAppend1 v2 i2 c
1212
+ v2' <- stToPrim $ unsafeAppend1 v2 i2 c
1215
1213
return (v1, i1, v2', i2+ 1 )
1216
1214
1217
1215
-- Modifying vectors
0 commit comments