-- | Helper functions for creating new arrays.

module Data.PrimitiveArray.Internal where

import Data.Primitive
import Control.Monad.ST
import Control.Monad (forM_)

-- | Create a new m-array with a default value.

newWith :: (Prim a) => Int -> a -> ST s (MutableByteArray s)
newWith l z = do
  marr <- new l z
  forM_ [0..l-1] $ \k -> (writeByteArray marr k z)
  return marr

-- | In 'new', 'z' is not used.

new :: (Prim a) => Int -> a -> ST s (MutableByteArray s)
new l z = do
  marr <- newByteArray (l * sizeOf z)
  return marr