Skip to content

Commit cc188b5

Browse files
added Mutable Primiative Arrays
1 parent f845c2f commit cc188b5

File tree

4 files changed

+117
-45
lines changed

4 files changed

+117
-45
lines changed

mutable-containers/mutable-containers.cabal

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.34.7.
3+
-- This file has been generated from package.yaml by hpack version 0.36.0.
44
--
55
-- see: https://github.com/sol/hpack
66

@@ -28,6 +28,7 @@ library
2828
exposed-modules:
2929
Data.Mutable
3030
other-modules:
31+
Data.Mutable.Array
3132
Data.Mutable.BRef
3233
Data.Mutable.Class
3334
Data.Mutable.Deque

mutable-containers/src/Data/Mutable.hs

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ module Data.Mutable
3434
, asBDeque
3535
, DLList
3636
, asDLList
37+
, Array (..)
38+
, ArrayMemoryProperties (..)
3739
-- * Type classes
3840
, MutableContainer (..)
3941
, MutableRef (..)
4042
, MutableAtomicRef (..)
4143
, MutableCollection (..)
42-
, MutableInitialSizedCollection (..)
43-
, MutableIndexing (..)
44+
, MutableAllocatedCollection (..)
45+
, MutableIndexingWrite (..)
4446
, MutablePushFront (..)
4547
, MutablePushBack (..)
4648
, MutablePopFront (..)
@@ -65,6 +67,7 @@ import Data.Mutable.PRef
6567
import Data.Mutable.BRef
6668
import Data.Mutable.Deque
6769
import Data.Mutable.DLList
70+
import Data.Mutable.Array
6871
import Data.Vector.Unboxed (Unbox)
6972
import Data.Primitive (Prim)
7073
import Data.Vector.Storable (Storable)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{-# LANGUAGE KindSignatures #-}
2+
{-# LANGUAGE ConstraintKinds #-}
3+
{-# LANGUAGE UndecidableInstances #-}
4+
{-# LANGUAGE DataKinds #-}
5+
{-# LANGUAGE TypeFamilies #-}
6+
{-# LANGUAGE TypeOperators #-}
7+
{-# LANGUAGE StandaloneKindSignatures #-}
8+
{-# LANGUAGE ScopedTypeVariables #-}
9+
{-# LANGUAGE TypeApplications #-}
10+
{-# LANGUAGE FlexibleInstances #-}
11+
{-# LANGUAGE FlexibleContexts #-}
12+
module Data.Mutable.Array
13+
( Array (..)
14+
, ArrayMemoryProperties (..)
15+
) where
16+
17+
import Data.Mutable.Class
18+
import Data.Word
19+
import GHC.TypeLits
20+
import Data.Kind (Constraint)
21+
import Data.Proxy (Proxy(Proxy))
22+
import Unsafe.Coerce (unsafeCoerce)
23+
import Control.Monad.Primitive
24+
25+
import Data.Primitive.ByteArray (MutableByteArray, newByteArray, newPinnedByteArray, newAlignedPinnedByteArray, writeByteArray)
26+
import Data.Primitive.Types (Prim)
27+
28+
newtype Array (p :: ArrayMemoryProperties) e s = Array (MutableByteArray s)
29+
30+
data ArrayMemoryProperties = Regular | Pinned | AlignedPinned Nat
31+
32+
instance MutableContainer (Array p e s) where
33+
type MCState (Array p e s) = s
34+
35+
instance MutableCollection (Array Regular e s) where
36+
type CollElement (Array Regular e s) = e
37+
newColl = coerceToArray $ newByteArray 0
38+
instance MutableCollection (Array Pinned e s) where
39+
type CollElement (Array Pinned e s) = e
40+
newColl = coerceToArray $ newPinnedByteArray 0
41+
instance KnownNat n => MutableCollection (Array (AlignedPinned n) e s) where
42+
type CollElement (Array (AlignedPinned n) e s) = e
43+
newColl = coerceToArray $ newAlignedPinnedByteArray 0 alignment
44+
where
45+
alignment = fromIntegral $ natVal $ Proxy @n
46+
47+
type instance CollIndex (Array _ _ _) = Int
48+
instance MutableAllocatedCollection (Array Regular e s) where
49+
newCollOfSize = coerceToArray . newByteArray
50+
{-# INLINE newCollOfSize #-}
51+
instance MutableAllocatedCollection (Array Pinned e s) where
52+
newCollOfSize = coerceToArray . newPinnedByteArray
53+
{-# INLINE newCollOfSize #-}
54+
instance KnownNat n => MutableAllocatedCollection (Array (AlignedPinned n) e s) where
55+
newCollOfSize = coerceToArray . flip newAlignedPinnedByteArray alignment
56+
where
57+
alignment = fromIntegral $ natVal $ Proxy @n
58+
{-# INLINE newCollOfSize #-}
59+
60+
coerceToArray :: m (MutableByteArray s) -> m (Array p e s)
61+
coerceToArray = unsafeCoerce
62+
63+
instance (Prim (CollElement (Array p e s)), MutableAllocatedCollection (Array p e s)) => MutableIndexingWrite (Array p e s) where
64+
writeIndex (Array c) i x = writeByteArray c i x
65+
66+
type IsPow2 :: Nat -> Constraint
67+
type IsPow2 x = IsPow2' (Mod x 2) x
68+
type IsPow2' :: Nat -> Nat -> Constraint
69+
type family IsPow2' m x where
70+
IsPow2' _ 2 = ()
71+
IsPow2' 1 x = TypeError (ShowType x :<>: Text " is not a power of 2.")
72+
IsPow2' 0 x = IsPow2' 0 (Div x 2)

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

+38-42
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ module Data.Mutable.Class
2121
, MutableRef (..)
2222
, MutableAtomicRef (..)
2323
, MutableCollection (..)
24-
, MutableInitialSizedCollection (..)
25-
, MutableIndexing (..)
24+
, MutableAllocatedCollection (..)
25+
, CollIndex
26+
, MutableIndexingWrite (..)
2627
, MutablePushFront (..)
2728
, MutablePushBack (..)
2829
, MutablePopFront (..)
@@ -229,90 +230,85 @@ instance Monoid w => MutableCollection (MutVar s w) where
229230
instance MutableCollection (MV.MVector s a) where
230231
type CollElement (MV.MVector s a) = a
231232
newColl = MV.new 0
232-
{-# INLINE newColl #-}
233233
instance MPV.Prim a => MutableCollection (MPV.MVector s a) where
234234
type CollElement (MPV.MVector s a) = a
235235
newColl = MPV.new 0
236-
{-# INLINE newColl #-}
237236
instance Storable a => MutableCollection (MSV.MVector s a) where
238237
type CollElement (MSV.MVector s a) = a
239238
newColl = MSV.new 0
240-
{-# INLINE newColl #-}
241239
instance MUV.Unbox a => MutableCollection (MUV.MVector s a) where
242240
type CollElement (MUV.MVector s a) = a
243241
newColl = MUV.new 0
244-
{-# INLINE newColl #-}
245242
instance (GHC.Arr.Ix i, Num i) => MutableCollection (GHC.Arr.STArray s i e) where
246243
type CollElement (GHC.Arr.STArray s i e) = e
247244
newColl = primToPrim $ GHC.Arr.newSTArray (0,0) undefined
248-
{-# INLINE newColl #-}
249245
instance Storable a => MutableCollection (Ptr a) where
250246
type CollElement (Ptr a) = a
251247
newColl = primToPrim $ Foreign.mallocArray 0
252-
{-# INLINE newColl #-}
253248

254249
-- | Containers that can be initialized with n elements.
255-
class MutableCollection c => MutableInitialSizedCollection c where
256-
type CollIndex c
250+
type family CollIndex c
251+
252+
class MutableCollection c => MutableAllocatedCollection c where
257253
newCollOfSize :: (PrimMonad m, PrimState m ~ MCState c)
258254
=> CollIndex c
259255
-> m c
260-
instance MutableInitialSizedCollection (MV.MVector s a) where
261-
type CollIndex (MV.MVector s a) = Int
256+
type instance CollIndex (MV.MVector s a) = Int
257+
instance MutableAllocatedCollection (MV.MVector s a) where
262258
newCollOfSize = MV.new
263259
{-# INLINE newCollOfSize #-}
264-
instance MPV.Prim a => MutableInitialSizedCollection (MPV.MVector s a) where
265-
type CollIndex (MPV.MVector s a) = Int
260+
type instance CollIndex (MPV.MVector s a) = Int
261+
instance MPV.Prim a => MutableAllocatedCollection (MPV.MVector s a) where
266262
newCollOfSize = MPV.new
267263
{-# INLINE newCollOfSize #-}
268-
instance Storable a => MutableInitialSizedCollection (MSV.MVector s a) where
269-
type CollIndex (MSV.MVector s a) = Int
264+
type instance CollIndex (MSV.MVector s a) = Int
265+
instance Storable a => MutableAllocatedCollection (MSV.MVector s a) where
270266
newCollOfSize = MSV.new
271267
{-# INLINE newCollOfSize #-}
272-
instance MUV.Unbox a => MutableInitialSizedCollection (MUV.MVector s a) where
273-
type CollIndex (MUV.MVector s a) = Int
268+
type instance CollIndex (MUV.MVector s a) = Int
269+
instance MUV.Unbox a => MutableAllocatedCollection (MUV.MVector s a) where
274270
newCollOfSize = MUV.new
275271
{-# INLINE newCollOfSize #-}
276-
instance (GHC.Arr.Ix i, Num i) => MutableInitialSizedCollection (GHC.Arr.STArray s i e) where
277-
type CollIndex (GHC.Arr.STArray s i e) = i
272+
type instance CollIndex (GHC.Arr.STArray s i e) = i
273+
instance (GHC.Arr.Ix i, Num i) => MutableAllocatedCollection (GHC.Arr.STArray s i e) where
278274
newCollOfSize x = primToPrim $ GHC.Arr.newSTArray (0,x) undefined
279275
{-# INLINE newCollOfSize #-}
280-
instance Storable a => MutableInitialSizedCollection (Ptr a) where
281-
type CollIndex (Ptr a) = Int
276+
type instance CollIndex (Ptr a) = Int
277+
instance Storable a => MutableAllocatedCollection (Ptr a) where
282278
newCollOfSize = primToPrim . Foreign.mallocArray
283279
{-# INLINE newCollOfSize #-}
284280

285-
class MutableInitialSizedCollection c => MutableIndexing c where
286-
readIndex :: (PrimMonad m, PrimState m ~ MCState c) => c -> CollIndex c -> m (CollElement c)
281+
class MutableAllocatedCollection c => MutableIndexingWrite c where
282+
-- readIndex :: (PrimMonad m, PrimState m ~ MCState c) => c -> CollIndex c -> m (CollElement c)
287283
writeIndex :: (PrimMonad m, PrimState m ~ MCState c) => c -> CollIndex c -> CollElement c -> m ()
288-
instance MutableIndexing (MV.MVector s a) where
289-
readIndex = MV.read
290-
{-# INLINE readIndex #-}
284+
instance MutableIndexingWrite (MV.MVector s a) where
285+
-- readIndex = MV.read
286+
-- {-# INLINE readIndex #-}
291287
writeIndex = MV.write
292288
{-# INLINE writeIndex #-}
293-
instance MPV.Prim a => MutableIndexing (MPV.MVector s a) where
294-
readIndex = MPV.read
295-
{-# INLINE readIndex #-}
289+
instance MPV.Prim a => MutableIndexingWrite (MPV.MVector s a) where
290+
-- readIndex = MPV.read
291+
-- {-# INLINE readIndex #-}
296292
writeIndex = MPV.write
297293
{-# INLINE writeIndex #-}
298-
instance Storable a => MutableIndexing (MSV.MVector s a) where
299-
readIndex = MSV.read
300-
{-# INLINE readIndex #-}
294+
instance Storable a => MutableIndexingWrite (MSV.MVector s a) where
295+
-- readIndex = MSV.read
296+
-- {-# INLINE readIndex #-}
301297
writeIndex = MSV.write
302298
{-# INLINE writeIndex #-}
303-
instance MUV.Unbox a => MutableIndexing (MUV.MVector s a) where
304-
readIndex = MUV.read
305-
{-# INLINE readIndex #-}
299+
instance MUV.Unbox a => MutableIndexingWrite (MUV.MVector s a) where
300+
-- readIndex = MUV.read
301+
-- {-# INLINE readIndex #-}
306302
writeIndex = MUV.write
307303
{-# INLINE writeIndex #-}
308-
instance (GHC.Arr.Ix i, Num i) => MutableIndexing (GHC.Arr.STArray s i e) where
309-
readIndex c i = primToPrim $ GHC.Arr.readSTArray c i
310-
{-# INLINE readIndex #-}
304+
instance (GHC.Arr.Ix i, Num i) => MutableIndexingWrite (GHC.Arr.STArray s i e) where
305+
-- readIndex c i = primToPrim $ GHC.Arr.readSTArray c i
306+
-- {-# INLINE readIndex #-}
311307
writeIndex c i e = primToPrim $ GHC.Arr.writeSTArray c i e
312308
{-# INLINE writeIndex #-}
313-
instance Storable a => MutableIndexing (Ptr a) where
314-
readIndex p i = primToPrim $ Foreign.peekElemOff p i
315-
{-# INLINE readIndex #-}
309+
instance Storable a => MutableIndexingWrite (Ptr a) where
310+
-- readIndex p i = primToPrim $ Foreign.peekElemOff p i
311+
-- {-# INLINE readIndex #-}
316312
writeIndex p i e = primToPrim $ Foreign.pokeElemOff p i e
317313
{-# INLINE writeIndex #-}
318314

0 commit comments

Comments
 (0)