@@ -45,6 +45,10 @@ import qualified Data.Vector.Primitive.Mutable as MPV
45
45
import qualified Data.Vector.Storable.Mutable as MSV
46
46
import qualified Data.Vector.Unboxed.Mutable as MUV
47
47
import qualified GHC.Arr
48
+ import qualified Foreign.Marshal.Array as Foreign
49
+ import Foreign.Ptr (Ptr )
50
+ import Foreign.Storable (Storable )
51
+ import qualified Foreign.Storable as Foreign
48
52
49
53
-- | The parent typeclass for all mutable containers.
50
54
--
@@ -72,6 +76,8 @@ instance MutableContainer (MUV.MVector s a) where
72
76
type MCState (MUV. MVector s a ) = s
73
77
instance MutableContainer (GHC.Arr. STArray s i e ) where
74
78
type MCState (GHC.Arr. STArray s i e ) = s
79
+ instance MutableContainer (Ptr a ) where
80
+ type MCState (Ptr a ) = PrimState IO
75
81
76
82
-- | Typeclass for single-cell mutable references.
77
83
--
@@ -227,7 +233,7 @@ instance MPV.Prim a => MutableCollection (MPV.MVector s a) where
227
233
type CollElement (MPV. MVector s a ) = a
228
234
newColl = MPV. new 0
229
235
{-# INLINE newColl #-}
230
- instance MSV. Storable a => MutableCollection (MSV. MVector s a ) where
236
+ instance Storable a => MutableCollection (MSV. MVector s a ) where
231
237
type CollElement (MSV. MVector s a ) = a
232
238
newColl = MSV. new 0
233
239
{-# INLINE newColl #-}
@@ -239,6 +245,10 @@ instance (GHC.Arr.Ix i, Num i) => MutableCollection (GHC.Arr.STArray s i e) wher
239
245
type CollElement (GHC.Arr. STArray s i e ) = e
240
246
newColl = primToPrim $ GHC.Arr. newSTArray (0 ,0 ) undefined
241
247
{-# INLINE newColl #-}
248
+ instance Storable a => MutableCollection (Ptr a ) where
249
+ type CollElement (Ptr a ) = a
250
+ newColl = primToPrim $ Foreign. mallocArray 0
251
+ {-# INLINE newColl #-}
242
252
243
253
-- | Containers that can be initialized with n elements.
244
254
class MutableCollection c => MutableInitialSizedCollection c where
@@ -254,7 +264,7 @@ instance MPV.Prim a => MutableInitialSizedCollection (MPV.MVector s a) where
254
264
type CollIndex (MPV. MVector s a ) = Int
255
265
newCollOfSize = MPV. new
256
266
{-# INLINE newCollOfSize #-}
257
- instance MSV. Storable a => MutableInitialSizedCollection (MSV. MVector s a ) where
267
+ instance Storable a => MutableInitialSizedCollection (MSV. MVector s a ) where
258
268
type CollIndex (MSV. MVector s a ) = Int
259
269
newCollOfSize = MSV. new
260
270
{-# INLINE newCollOfSize #-}
@@ -266,6 +276,10 @@ instance (GHC.Arr.Ix i, Num i) => MutableInitialSizedCollection (GHC.Arr.STArray
266
276
type CollIndex (GHC.Arr. STArray s i e ) = i
267
277
newCollOfSize x = primToPrim $ GHC.Arr. newSTArray (0 ,x) undefined
268
278
{-# INLINE newCollOfSize #-}
279
+ instance Storable a => MutableInitialSizedCollection (Ptr a ) where
280
+ type CollIndex (Ptr a ) = Int
281
+ newCollOfSize = primToPrim . Foreign. mallocArray
282
+ {-# INLINE newCollOfSize #-}
269
283
270
284
class MutableInitialSizedCollection c => MutableIndexing c where
271
285
readIndex :: (PrimMonad m , PrimState m ~ MCState c ) => c -> CollIndex c -> m (CollElement c )
@@ -280,7 +294,7 @@ instance MPV.Prim a => MutableIndexing (MPV.MVector s a) where
280
294
{-# INLINE readIndex #-}
281
295
writeIndex = MPV. write
282
296
{-# INLINE writeIndex #-}
283
- instance MSV. Storable a => MutableIndexing (MSV. MVector s a ) where
297
+ instance Storable a => MutableIndexing (MSV. MVector s a ) where
284
298
readIndex = MSV. read
285
299
{-# INLINE readIndex #-}
286
300
writeIndex = MSV. write
@@ -295,6 +309,11 @@ instance (GHC.Arr.Ix i, Num i) => MutableIndexing (GHC.Arr.STArray s i e) where
295
309
{-# INLINE readIndex #-}
296
310
writeIndex c i e = primToPrim $ GHC.Arr. writeSTArray c i e
297
311
{-# INLINE writeIndex #-}
312
+ instance Storable a => MutableIndexing (Ptr a ) where
313
+ readIndex p i = primToPrim $ Foreign. peekElemOff p i
314
+ {-# INLINE readIndex #-}
315
+ writeIndex p i e = primToPrim $ Foreign. pokeElemOff p i e
316
+ {-# INLINE writeIndex #-}
298
317
299
318
-- | Take a value from the front of the collection, if available.
300
319
--
0 commit comments