@@ -30,6 +30,7 @@ module Data.Primitive.Contiguous.Class
3030import Data.Primitive
3131import Data.Primitive.Contiguous.Shim
3232import Data.Primitive.Unlifted.Array
33+ import Data.Primitive.Unlifted.SmallArray
3334import Prelude hiding
3435 ( all
3536 , any
@@ -64,8 +65,10 @@ import Control.Monad.ST.Run (runArrayST, runPrimArrayST, runSmallArrayST, runUnl
6465import Data.Kind (Type )
6566import Data.Primitive.Unlifted.Array ()
6667import Data.Primitive.Unlifted.Array.Primops (MutableUnliftedArray # (MutableUnliftedArray #), UnliftedArray # (UnliftedArray #))
68+ import Data.Primitive.Unlifted.SmallArray.Primops (SmallUnliftedArray # (SmallUnliftedArray #), SmallMutableUnliftedArray # (SmallMutableUnliftedArray #))
6769import Data.Primitive.Unlifted.Class (PrimUnlifted )
6870import GHC.Exts (Array #, Constraint , MutableArray #, SmallArray #, SmallMutableArray #, TYPE , sizeofArray #, sizeofByteArray #)
71+ import GHC.ST (ST (ST ))
6972
7073import qualified Control.DeepSeq as DS
7174import qualified Data.Primitive.Unlifted.Class as Class
@@ -885,6 +888,150 @@ instance ContiguousU SmallArray where
885888 {-# INLINE liftMut #-}
886889 liftMut x = SmallMutableArray x
887890
891+ instance Contiguous (SmallUnliftedArray_ unlifted_a ) where
892+ type Mutable (SmallUnliftedArray_ unlifted_a ) = SmallMutableUnliftedArray_ unlifted_a
893+ type Element (SmallUnliftedArray_ unlifted_a ) = PrimUnliftsInto unlifted_a
894+ type Sliced (SmallUnliftedArray_ unlifted_a ) = Slice (SmallUnliftedArray_ unlifted_a )
895+ type MutableSliced (SmallUnliftedArray_ unlifted_a ) = MutableSlice (SmallUnliftedArray_ unlifted_a )
896+ {-# INLINE new #-}
897+ new n = unsafeNewSmallUnliftedArray n
898+ {-# INLINE empty #-}
899+ empty = emptySmallUnliftedArray
900+ {-# INLINE index #-}
901+ index = indexSmallUnliftedArray
902+ {-# INLINE indexM #-}
903+ indexM arr ix = pure (indexSmallUnliftedArray arr ix)
904+ {-# INLINE index# #-}
905+ index# arr ix = (# indexSmallUnliftedArray arr ix # )
906+ {-# INLINE read #-}
907+ read = readSmallUnliftedArray
908+ {-# INLINE write #-}
909+ write = writeSmallUnliftedArray
910+ {-# INLINE null #-}
911+ null a = case sizeofSmallUnliftedArray a of
912+ 0 -> True
913+ _ -> False
914+ {-# INLINE slice #-}
915+ slice base offset length = Slice {offset, length , base = unlift base}
916+ {-# INLINE sliceMut #-}
917+ sliceMut baseMut offsetMut lengthMut = MutableSlice {offsetMut, lengthMut, baseMut = unliftMut baseMut}
918+ {-# INLINE toSlice #-}
919+ toSlice base = Slice {offset = 0 , length = size base, base = unlift base}
920+ {-# INLINE toSliceMut #-}
921+ toSliceMut baseMut = do
922+ lengthMut <- sizeMut baseMut
923+ pure MutableSlice {offsetMut = 0 , lengthMut, baseMut = unliftMut baseMut}
924+ {-# INLINE freeze_ #-}
925+ freeze_ = freezeSmallUnliftedArray
926+ {-# INLINE unsafeFreeze #-}
927+ unsafeFreeze = unsafeFreezeSmallUnliftedArray
928+ {-# INLINE size #-}
929+ size = sizeofSmallUnliftedArray
930+ {-# INLINE sizeMut #-}
931+ sizeMut = getSizeofSmallMutableUnliftedArray
932+ {-# INLINE thaw_ #-}
933+ thaw_ = thawSmallUnliftedArray
934+ {-# INLINE equals #-}
935+ equals = (==)
936+ {-# INLINE equalsMut #-}
937+ equalsMut = sameSmallMutableUnliftedArray
938+ {-# INLINE singleton #-}
939+ singleton a = runST $ do
940+ marr <- newSmallUnliftedArray 1 a
941+ unsafeFreezeSmallUnliftedArray marr
942+ {-# INLINE doubleton #-}
943+ doubleton a b = runST $ do
944+ m <- newSmallUnliftedArray 2 a
945+ writeSmallUnliftedArray m 1 b
946+ unsafeFreezeSmallUnliftedArray m
947+ {-# INLINE tripleton #-}
948+ tripleton a b c = runST $ do
949+ m <- newSmallUnliftedArray 3 a
950+ writeSmallUnliftedArray m 1 b
951+ writeSmallUnliftedArray m 2 c
952+ unsafeFreezeSmallUnliftedArray m
953+ {-# INLINE quadrupleton #-}
954+ quadrupleton a b c d = runST $ do
955+ m <- newSmallUnliftedArray 4 a
956+ writeSmallUnliftedArray m 1 b
957+ writeSmallUnliftedArray m 2 c
958+ writeSmallUnliftedArray m 3 d
959+ unsafeFreezeSmallUnliftedArray m
960+ {-# INLINE quintupleton #-}
961+ quintupleton a b c d e = runST $ do
962+ m <- newSmallUnliftedArray 5 a
963+ writeSmallUnliftedArray m 1 b
964+ writeSmallUnliftedArray m 2 c
965+ writeSmallUnliftedArray m 3 d
966+ writeSmallUnliftedArray m 4 e
967+ unsafeFreezeSmallUnliftedArray m
968+ {-# INLINE sextupleton #-}
969+ sextupleton a b c d e f = runST $ do
970+ m <- newSmallUnliftedArray 6 a
971+ writeSmallUnliftedArray m 1 b
972+ writeSmallUnliftedArray m 2 c
973+ writeSmallUnliftedArray m 3 d
974+ writeSmallUnliftedArray m 4 e
975+ writeSmallUnliftedArray m 5 f
976+ unsafeFreezeSmallUnliftedArray m
977+ {-# INLINE rnf #-}
978+ rnf ! ary =
979+ let ! sz = sizeofSmallUnliftedArray ary
980+ go ! ix =
981+ if ix < sz
982+ then
983+ let ! x = indexSmallUnliftedArray ary ix
984+ in DS. rnf x `seq` go (ix + 1 )
985+ else ()
986+ in go 0
987+ {-# INLINE clone_ #-}
988+ clone_ = cloneSmallUnliftedArray
989+ {-# INLINE cloneMut_ #-}
990+ cloneMut_ = cloneSmallMutableUnliftedArray
991+ {-# INLINE copy_ #-}
992+ copy_ = copySmallUnliftedArray
993+ {-# INLINE copyMut_ #-}
994+ copyMut_ = copySmallMutableUnliftedArray
995+ {-# INLINE replicateMut #-}
996+ replicateMut = newSmallUnliftedArray
997+ {-# INLINE run #-}
998+ run = runSmallUnliftedArrayST
999+ {-# INLINE shrink #-}
1000+ shrink ! arr ! n = do
1001+ shrinkSmallMutableUnliftedArray arr n
1002+ pure arr
1003+ {-# INLINE unsafeShrinkAndFreeze #-}
1004+ unsafeShrinkAndFreeze ! arr ! n = do
1005+ shrinkSmallMutableUnliftedArray arr n
1006+ unsafeFreezeSmallUnliftedArray arr
1007+
1008+
1009+ newtype SmallUnliftedArray ## (u :: TYPE UnliftedRep ) (a :: Type )
1010+ = SmallUnliftedArray ## (Exts. SmallArray# u )
1011+ newtype SmallMutableUnliftedArray ## (u :: TYPE UnliftedRep ) s (a :: Type )
1012+ = SmallMutableUnliftedArray ## (Exts. SmallMutableArray# s u )
1013+
1014+ instance ContiguousU (SmallUnliftedArray_ unlifted_a ) where
1015+ type Unlifted (SmallUnliftedArray_ unlifted_a ) = SmallUnliftedArray ## unlifted_a
1016+ type UnliftedMut (SmallUnliftedArray_ unlifted_a ) = SmallMutableUnliftedArray ## unlifted_a
1017+ {-# INLINE resize #-}
1018+ resize = resizeSmallUnliftedArray
1019+ {-# INLINE unlift #-}
1020+ unlift (SmallUnliftedArray (SmallUnliftedArray # x)) = SmallUnliftedArray ## x
1021+ {-# INLINE unliftMut #-}
1022+ unliftMut (SmallMutableUnliftedArray (SmallMutableUnliftedArray # x)) = SmallMutableUnliftedArray ## x
1023+ {-# INLINE lift #-}
1024+ lift (SmallUnliftedArray ## x) = SmallUnliftedArray (SmallUnliftedArray # x)
1025+ {-# INLINE liftMut #-}
1026+ liftMut (SmallMutableUnliftedArray ## x) = SmallMutableUnliftedArray (SmallMutableUnliftedArray # x)
1027+
1028+
1029+ -- NOTE: Currently missing from the `run-st` library
1030+ -- c.f. https://github.com/byteverse/run-st/issues/5
1031+ runSmallUnliftedArrayST :: (forall s . ST s (SmallUnliftedArray_ unlifted_a a )) -> SmallUnliftedArray_ unlifted_a a
1032+ {-# INLINE runSmallUnliftedArrayST #-}
1033+ runSmallUnliftedArrayST f = SmallUnliftedArray (Exts. runRW# (\ s0 -> case f of ST g -> case g s0 of (# _, SmallUnliftedArray r # ) -> r))
1034+
8881035instance Contiguous PrimArray where
8891036 type Mutable PrimArray = MutablePrimArray
8901037 type Element PrimArray = Prim
0 commit comments