{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_GHC -Wall #-}
module Data.Primitive.PrimArray
(
PrimArray(..)
, MutablePrimArray(..)
, newPrimArray
, resizeMutablePrimArray
#if __GLASGOW_HASKELL__ >= 710
, shrinkMutablePrimArray
#endif
, readPrimArray
, writePrimArray
, indexPrimArray
, A.unsafeFreezePrimArray
, A.unsafeThawPrimArray
, copyPrimArray
, copyMutablePrimArray
#if __GLASGOW_HASKELL__ >= 708
, A.copyPrimArrayToPtr
, A.copyMutablePrimArrayToPtr
#endif
, setPrimArray
, A.sameMutablePrimArray
, A.getSizeofMutablePrimArray
, A.sizeofMutablePrimArray
, A.sizeofPrimArray
, A.foldrPrimArray
, A.foldrPrimArray'
, A.foldlPrimArray
, A.foldlPrimArray'
, A.foldlPrimArrayM'
, A.traversePrimArray_
, A.itraversePrimArray_
, A.mapPrimArray
, A.imapPrimArray
, A.generatePrimArray
, A.replicatePrimArray
, A.filterPrimArray
, A.mapMaybePrimArray
, A.traversePrimArray
, A.itraversePrimArray
, A.generatePrimArrayA
, A.replicatePrimArrayA
, A.filterPrimArrayA
, A.mapMaybePrimArrayA
, A.traversePrimArrayP
, A.itraversePrimArrayP
, A.generatePrimArrayP
, A.replicatePrimArrayP
, A.filterPrimArrayP
, A.mapMaybePrimArrayP
) where
import Control.Monad.Primitive (PrimMonad,PrimState)
import Control.Exception (throw, ArrayException(..))
import Data.Primitive.Types (Prim)
import "primitive" Data.Primitive.PrimArray (PrimArray,MutablePrimArray)
import qualified "primitive" Data.Primitive.PrimArray as A
import GHC.Stack
import qualified Data.List as L
check :: HasCallStack => String -> Bool -> a -> a
check _ True x = x
check errMsg False _ = throw (IndexOutOfBounds $ "Data.Primitive.PrimArray." ++ errMsg ++ "\n" ++ prettyCallStack callStack)
newPrimArray :: forall m a. (HasCallStack, PrimMonad m, Prim a) => Int -> m (MutablePrimArray (PrimState m) a)
newPrimArray n = check "newPrimArray: negative size" (n>=0) (A.newPrimArray n)
resizeMutablePrimArray :: forall m a. (HasCallStack, PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> Int
-> m (MutablePrimArray (PrimState m) a)
resizeMutablePrimArray marr n = check "resizeMutablePrimArray: negative size" (n>=0) (A.resizeMutablePrimArray marr n)
#if __GLASGOW_HASKELL__ >= 710
shrinkMutablePrimArray :: forall m a. (HasCallStack, PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> Int
-> m ()
shrinkMutablePrimArray marr n = do
old <- A.getSizeofMutablePrimArray marr
check "shrinkMutablePrimArray: illegal new size" (n>=0 && n <= old) (A.shrinkMutablePrimArray marr n)
#endif
readPrimArray :: (HasCallStack, Prim a, PrimMonad m) => MutablePrimArray (PrimState m) a -> Int -> m a
readPrimArray marr i = do
siz <- A.getSizeofMutablePrimArray marr
let explain = L.concat
[ "[size: "
, show siz
, ", index: "
, show i
, "]"
]
check ("readPrimArray: index of out bounds " ++ explain) (i>=0 && i<siz) (A.readPrimArray marr i)
writePrimArray ::
(HasCallStack, Prim a, PrimMonad m)
=> MutablePrimArray (PrimState m) a
-> Int
-> a
-> m ()
writePrimArray marr i x = do
siz <- A.getSizeofMutablePrimArray marr
let explain = L.concat
[ "[size: "
, show siz
, ", index: "
, show i
, "]"
]
check ("writePrimArray: index of out bounds " ++ explain) (i>=0 && i<siz) (A.writePrimArray marr i x)
indexPrimArray :: forall a. Prim a => PrimArray a -> Int -> a
indexPrimArray arr i =
let sz = A.sizeofPrimArray arr
explain = L.concat
[ "[size: "
, show sz
, ", index: "
, show i
, "]"
]
in check ("indexPrimArray: index of out bounds " ++ explain)
(i>=0 && i< sz)
(A.indexPrimArray arr i)
setPrimArray :: forall m a. (HasCallStack, Prim a, PrimMonad m)
=> MutablePrimArray (PrimState m) a
-> Int
-> Int
-> a
-> m ()
setPrimArray dst doff sz x = do
arrSz <- A.getSizeofMutablePrimArray dst
let explain = L.concat
[ "[size: "
, show arrSz
, ", offset: "
, show doff
, ", length: "
, show sz
, "]"
]
check ("setPrimArray: index range of out bounds " ++ explain)
(doff>=0 && (doff+sz)<=arrSz)
(A.setPrimArray dst doff sz x)
copyMutablePrimArray :: forall m a. (HasCallStack, PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> Int
-> MutablePrimArray (PrimState m) a
-> Int
-> Int
-> m ()
copyMutablePrimArray marr1 s1 marr2 s2 l = do
siz1 <- A.getSizeofMutablePrimArray marr1
siz2 <- A.getSizeofMutablePrimArray marr2
check "copyMutablePrimArray: index range of out bounds"
(s1>=0 && s2>=0 && l>=0 && (s2+l)<=siz2 && (s1+l)<=siz1)
(A.copyMutablePrimArray marr1 s1 marr2 s2 l)
copyPrimArray :: forall m a.
(HasCallStack, PrimMonad m, Prim a)
=> MutablePrimArray (PrimState m) a
-> Int
-> PrimArray a
-> Int
-> Int
-> m ()
copyPrimArray marr s1 arr s2 l = do
siz <- A.getSizeofMutablePrimArray marr
check "copyPrimArray: index range of out bounds"
(s1>=0 && s2>=0 && l>=0 && (s2+l)<= A.sizeofPrimArray arr && (s1+l)<=siz)
(A.copyPrimArray marr s1 arr s2 l)