Skip to content

Commit 76dc63e

Browse files
added Ptr arrays
1 parent de5d0a5 commit 76dc63e

File tree

1 file changed

+22
-3
lines changed
  • mutable-containers/src/Data/Mutable

1 file changed

+22
-3
lines changed

mutable-containers/src/Data/Mutable/Class.hs

+22-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ import qualified Data.Vector.Primitive.Mutable as MPV
4545
import qualified Data.Vector.Storable.Mutable as MSV
4646
import qualified Data.Vector.Unboxed.Mutable as MUV
4747
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
4852

4953
-- | The parent typeclass for all mutable containers.
5054
--
@@ -72,6 +76,8 @@ instance MutableContainer (MUV.MVector s a) where
7276
type MCState (MUV.MVector s a) = s
7377
instance MutableContainer (GHC.Arr.STArray s i e) where
7478
type MCState (GHC.Arr.STArray s i e) = s
79+
instance MutableContainer (Ptr a) where
80+
type MCState (Ptr a) = PrimState IO
7581

7682
-- | Typeclass for single-cell mutable references.
7783
--
@@ -227,7 +233,7 @@ instance MPV.Prim a => MutableCollection (MPV.MVector s a) where
227233
type CollElement (MPV.MVector s a) = a
228234
newColl = MPV.new 0
229235
{-# INLINE newColl #-}
230-
instance MSV.Storable a => MutableCollection (MSV.MVector s a) where
236+
instance Storable a => MutableCollection (MSV.MVector s a) where
231237
type CollElement (MSV.MVector s a) = a
232238
newColl = MSV.new 0
233239
{-# INLINE newColl #-}
@@ -239,6 +245,10 @@ instance (GHC.Arr.Ix i, Num i) => MutableCollection (GHC.Arr.STArray s i e) wher
239245
type CollElement (GHC.Arr.STArray s i e) = e
240246
newColl = primToPrim $ GHC.Arr.newSTArray (0,0) undefined
241247
{-# 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 #-}
242252

243253
-- | Containers that can be initialized with n elements.
244254
class MutableCollection c => MutableInitialSizedCollection c where
@@ -254,7 +264,7 @@ instance MPV.Prim a => MutableInitialSizedCollection (MPV.MVector s a) where
254264
type CollIndex (MPV.MVector s a) = Int
255265
newCollOfSize = MPV.new
256266
{-# INLINE newCollOfSize #-}
257-
instance MSV.Storable a => MutableInitialSizedCollection (MSV.MVector s a) where
267+
instance Storable a => MutableInitialSizedCollection (MSV.MVector s a) where
258268
type CollIndex (MSV.MVector s a) = Int
259269
newCollOfSize = MSV.new
260270
{-# INLINE newCollOfSize #-}
@@ -266,6 +276,10 @@ instance (GHC.Arr.Ix i, Num i) => MutableInitialSizedCollection (GHC.Arr.STArray
266276
type CollIndex (GHC.Arr.STArray s i e) = i
267277
newCollOfSize x = primToPrim $ GHC.Arr.newSTArray (0,x) undefined
268278
{-# INLINE newCollOfSize #-}
279+
instance Storable a => MutableInitialSizedCollection (Ptr a) where
280+
type CollIndex (Ptr a) = Int
281+
newCollOfSize = primToPrim . Foreign.mallocArray
282+
{-# INLINE newCollOfSize #-}
269283

270284
class MutableInitialSizedCollection c => MutableIndexing c where
271285
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
280294
{-# INLINE readIndex #-}
281295
writeIndex = MPV.write
282296
{-# INLINE writeIndex #-}
283-
instance MSV.Storable a => MutableIndexing (MSV.MVector s a) where
297+
instance Storable a => MutableIndexing (MSV.MVector s a) where
284298
readIndex = MSV.read
285299
{-# INLINE readIndex #-}
286300
writeIndex = MSV.write
@@ -295,6 +309,11 @@ instance (GHC.Arr.Ix i, Num i) => MutableIndexing (GHC.Arr.STArray s i e) where
295309
{-# INLINE readIndex #-}
296310
writeIndex c i e = primToPrim $ GHC.Arr.writeSTArray c i e
297311
{-# 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 #-}
298317

299318
-- | Take a value from the front of the collection, if available.
300319
--

0 commit comments

Comments
 (0)