contiguous 0.1.0.0 → 0.2.0.0
raw patch · 2 files changed
+65/−10 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.Primitive.Contiguous: foldlM' :: (Contiguous arr, Element arr a, Monad m) => (b -> a -> m b) -> b -> arr a -> m b
+ Data.Primitive.Contiguous: unsafeFromListN :: (Contiguous arr, Element arr a) => Int -> [a] -> arr a
+ Data.Primitive.Contiguous: unsafeFromListReverseN :: (Contiguous arr, Element arr a) => Int -> [a] -> arr a
- Data.Primitive.Contiguous: foldr :: (Contiguous arr, Element arr b) => (b -> c -> c) -> c -> arr b -> c
+ Data.Primitive.Contiguous: foldr :: (Contiguous arr, Element arr a) => (a -> b -> b) -> b -> arr a -> b
Files
- contiguous.cabal +2/−1
- src/Data/Primitive/Contiguous.hs +63/−9
contiguous.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.0 name: contiguous-version: 0.1.0.0+version: 0.2.0.0 homepage: https://github.com/andrewthad/contiguous bug-reports: https://github.com/andrewthad/contiguous/issues author: Andrew Martin@@ -29,4 +29,5 @@ base >=4.9 && <5 , primitive >= 0.6.4 default-language: Haskell2010+ ghc-options: -O2 -Wall
src/Data/Primitive/Contiguous.hs view
@@ -8,23 +8,24 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeFamilyDependencies #-} {-# LANGUAGE UnboxedTuples #-}--{-# OPTIONS_GHC -O2 -Wall #-} module Data.Primitive.Contiguous ( Contiguous(..) , Always , map+ , foldr , foldl' , foldr' , foldMap'+ , foldlM'+ , unsafeFromListN+ , unsafeFromListReverseN ) where -import Prelude hiding (map)+import Prelude hiding (map,foldr) import Control.Monad.ST (ST,runST) import Data.Kind (Type) import Data.Primitive import GHC.Exts (ArrayArray#,Constraint)-import qualified Prelude as P class Always a instance Always a@@ -48,7 +49,6 @@ copyMutable :: Element arr b => Mutable arr s b -> Int -> Mutable arr s b -> Int -> Int -> ST s () clone :: Element arr b => arr b -> Int -> Int -> arr b cloneMutable :: Element arr b => Mutable arr s b -> Int -> Int -> ST s (Mutable arr s b)- foldr :: Element arr b => (b -> c -> c) -> c -> arr b -> c equals :: (Element arr b, Eq b) => arr b -> arr b -> Bool unlift :: arr b -> ArrayArray# lift :: ArrayArray# -> arr b@@ -71,7 +71,6 @@ copyMutable = copyMutablePrimArray clone = clonePrimArray cloneMutable = cloneMutablePrimArray- foldr = foldrPrimArray equals = (==) unlift = toArrayArray# lift = fromArrayArray#@@ -94,7 +93,6 @@ copyMutable = copyMutableArray clone = cloneArray cloneMutable = cloneMutableArray- foldr = P.foldr equals = (==) unlift = toArrayArray# lift = fromArrayArray#@@ -117,7 +115,6 @@ copyMutable = copyMutableUnliftedArray clone = cloneUnliftedArray cloneMutable = cloneMutableUnliftedArray- foldr = foldrUnliftedArray equals = (==) unlift = toArrayArray# lift = fromArrayArray#@@ -144,7 +141,7 @@ emptyUnliftedArray = runST (unsafeNewUnliftedArray 0 >>= unsafeFreezeUnliftedArray) {-# NOINLINE emptyUnliftedArray #-} -+-- | Map over the elements of an array. map :: (Contiguous arr, Element arr b, Element arr c) => (b -> c) -> arr b -> arr c map f a = runST $ do mb <- new (size a)@@ -158,6 +155,17 @@ unsafeFreeze mb {-# INLINABLE map #-} +-- | Right fold over the element of an array.+foldr :: (Contiguous arr, Element arr a) => (a -> b -> b) -> b -> arr a -> b+foldr f z arr = go 0+ where+ !sz = size arr+ go !i+ | sz > i = case index# arr i of+ (# x #) -> f x (go (i+1))+ | otherwise = z++-- | Strict left fold over the elements of an array. foldl' :: (Contiguous arr, Element arr a) => (b -> a -> b) -> b -> arr a -> b foldl' f !z !ary = let@@ -168,6 +176,7 @@ in go 0 z {-# INLINABLE foldl' #-} +-- | Strict right fold over the elements of an array. foldr' :: (Contiguous arr, Element arr a) => (a -> b -> b) -> b -> arr a -> b foldr' f !z !ary = let@@ -178,6 +187,7 @@ in go (size ary - 1) z {-# INLINABLE foldr' #-} +-- | Strict monoidal fold over the elements of an array. foldMap' :: (Contiguous arr, Element arr a, Monoid m) => (a -> m) -> arr a -> m foldMap' f !ary =@@ -189,6 +199,19 @@ in go 0 mempty {-# INLINABLE foldMap' #-} +-- | Strict left monadic fold over the elements of an array.+foldlM' :: (Contiguous arr, Element arr a, Monad m) => (b -> a -> m b) -> b -> arr a -> m b+foldlM' f z0 arr = go 0 z0+ where+ !sz = size arr+ go !i !acc1+ | i < sz = do+ let (# x #) = index# arr i+ acc2 <- f acc1 x+ go (i + 1) acc2+ | otherwise = return acc1+{-# INLINABLE foldlM' #-}+ clonePrimArray :: Prim a => PrimArray a -> Int -> Int -> PrimArray a clonePrimArray !arr !off !len = runST $ do marr <- newPrimArray len@@ -203,3 +226,34 @@ return marr {-# INLINE cloneMutablePrimArray #-} +-- | Create an array from a list. If the given length does+-- not match the actual length, this function has undefined+-- behavior.+unsafeFromListN :: (Contiguous arr, Element arr a)+ => Int -- ^ length of list+ -> [a] -- ^ list+ -> arr a+unsafeFromListN n l = runST $ do+ m <- new n+ let go !_ [] = return ()+ go !ix (x : xs) = do+ write m ix x+ go (ix+1) xs+ go 0 l+ unsafeFreeze m++-- | Create an array from a list, reversing the order of the+-- elements. If the given length does not match the actual length,+-- this function has undefined behavior.+unsafeFromListReverseN :: (Contiguous arr, Element arr a)+ => Int+ -> [a]+ -> arr a+unsafeFromListReverseN n l = runST $ do+ m <- new n+ let go !_ [] = return ()+ go !ix (x : xs) = do+ write m ix x+ go (ix-1) xs+ go (n - 1) l+ unsafeFreeze m