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