packages feed

Z-Data 0.1.2.0 → 0.1.3.0

raw patch · 10 files changed

+136/−40 lines, 10 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- Z.Data.Text: data Text
- Z.Data.Text: getUTF8Bytes :: Text -> Bytes
- Z.Data.Text.Extra: breakOnAllOverlapping :: Text -> Text -> [(Text, Text)]
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CBool
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CChar
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CClock
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CDouble
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CFloat
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CInt
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CIntMax
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CIntPtr
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CLLong
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CLong
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CPtrdiff
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CSChar
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CSUSeconds
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CShort
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CSigAtomic
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CSize
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CTime
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CUChar
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CUInt
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CUIntMax
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CUIntPtr
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CULLong
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CULong
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CUSeconds
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CUShort
+ Z.Data.Array.UnalignedAccess: instance Z.Data.Array.UnalignedAccess.UnalignedAccess Foreign.C.Types.CWchar
+ Z.Data.Text: InvalidUTF8Exception :: CallStack -> InvalidUTF8Exception
+ Z.Data.Text: Text :: Bytes -> Text
+ Z.Data.Text: [getUTF8Bytes] :: Text -> Bytes
+ Z.Data.Text: data InvalidUTF8Exception
+ Z.Data.Text: index :: HasCallStack => Text -> Int -> Char
+ Z.Data.Text: indexMaybe :: Text -> Int -> Maybe Char
+ Z.Data.Text: indexMaybeR :: Text -> Int -> Maybe Char
+ Z.Data.Text: indexR :: HasCallStack => Text -> Int -> Char
+ Z.Data.Text: newtype Text
+ Z.Data.Text.Base: index :: HasCallStack => Text -> Int -> Char
+ Z.Data.Text.Base: indexR :: HasCallStack => Text -> Int -> Char
+ Z.Data.Vector: c2w :: Char -> Word8
+ Z.Data.Vector: chunkOverhead :: Int
+ Z.Data.Vector: defaultChunkSize :: Int
+ Z.Data.Vector: defaultInitSize :: Int
+ Z.Data.Vector: index :: (Vec v a, HasCallStack) => v a -> Int -> a
+ Z.Data.Vector: indexM :: (Vec v a, Monad m, HasCallStack) => v a -> Int -> m a
+ Z.Data.Vector: indexMaybe :: Vec v a => v a -> Int -> Maybe a
+ Z.Data.Vector: mapIPair' :: (a -> b) -> IPair a -> IPair b
+ Z.Data.Vector: maximum :: (Vec v a, Ord a, HasCallStack) => v a -> a
+ Z.Data.Vector: minimum :: (Vec v a, Ord a, HasCallStack) => v a -> a
+ Z.Data.Vector: smallChunkSize :: Int
+ Z.Data.Vector: w2c :: Word8 -> Char
+ Z.Foreign: clearMBA :: MBA# a -> IO ()
+ Z.Foreign: peekMBA :: UnalignedAccess x => MBA# a -> Int -> IO x
+ Z.Foreign: pokeMBA :: UnalignedAccess x => MBA# a -> Int -> x -> IO ()

Files

ChangeLog.md view
@@ -1,8 +1,13 @@ # Revision history for Z-Data +## 0.1.3.0  -- 2020-09-19++* Add indexing funtion to `Z.Data.Vector` and `Z.Data.Text`.+* Add `peekMBA`, `pokeMBA` and `clearMBA` to `Z.Foreign`.+ ## 0.1.2.0  -- 2020-09-19 -* Rename 'read/write/indexWord8ArrayAs' to 'read/write/indexWord8ArrayAs#'.-* Add 'read/write/indexWord8ArrayAs', 'read/write/indexPrimWord8ArrayAs'.+* Rename `read/write/indexWord8ArrayAs` to `read/write/indexWord8ArrayAs#`.+* Add `read/write/indexWord8ArrayAs`, `read/write/indexPrimWord8ArrayAs`. * Fix JSON encoding code in generic instance(constructor with single payload case). 
Z-Data.cabal view
@@ -1,5 +1,5 @@ name:                       Z-Data-version:                    0.1.2.0+version:                    0.1.3.0 synopsis:                   Array, vector and text description:                This package provides array, slice and text operations license:                    BSD3
Z/Data/Array/UnalignedAccess.hs view
@@ -3,6 +3,9 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MagicHash         #-} {-# LANGUAGE UnboxedTuples     #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE StandaloneDeriving #-}+{-# LANGUAGE DataKinds #-}  {-| Module      : Z.Data.Array.UnalignedAccess@@ -18,14 +21,15 @@  module Z.Data.Array.UnalignedAccess where +import           Control.Monad.Primitive+import           Data.Primitive.ByteArray+import           Data.Primitive.PrimArray import           GHC.Int import           GHC.Prim import           GHC.Types import           GHC.Word import           GHC.Float (stgFloatToWord32, stgWord32ToFloat, stgWord64ToDouble, stgDoubleToWord64)-import           Control.Monad.Primitive-import           Data.Primitive.ByteArray-import           Data.Primitive.PrimArray+import           Foreign.C.Types  -- toggle these defs to test different implements #define USE_BSWAP@@ -789,6 +793,7 @@  -------------------------------------------------------------------------------- +-- | Char's instance use 31bit wide char prim-op. instance UnalignedAccess Char where     {-# INLINE unalignedSize #-}     unalignedSize = UnalignedSize 4@@ -838,3 +843,32 @@         in BE (C# (chr# x#)) #endif +--------------------------------------------------------------------------------++-- Prim instances for newtypes in Foreign.C.Types+deriving instance UnalignedAccess CChar+deriving instance UnalignedAccess CSChar+deriving instance UnalignedAccess CUChar+deriving instance UnalignedAccess CShort+deriving instance UnalignedAccess CUShort+deriving instance UnalignedAccess CInt+deriving instance UnalignedAccess CUInt+deriving instance UnalignedAccess CLong+deriving instance UnalignedAccess CULong+deriving instance UnalignedAccess CPtrdiff+deriving instance UnalignedAccess CSize+deriving instance UnalignedAccess CWchar+deriving instance UnalignedAccess CSigAtomic+deriving instance UnalignedAccess CLLong+deriving instance UnalignedAccess CULLong+deriving instance UnalignedAccess CBool+deriving instance UnalignedAccess CIntPtr+deriving instance UnalignedAccess CUIntPtr+deriving instance UnalignedAccess CIntMax+deriving instance UnalignedAccess CUIntMax+deriving instance UnalignedAccess CClock+deriving instance UnalignedAccess CTime+deriving instance UnalignedAccess CUSeconds+deriving instance UnalignedAccess CSUSeconds+deriving instance UnalignedAccess CFloat+deriving instance UnalignedAccess CDouble
Z/Data/Text.hs view
@@ -19,11 +19,13 @@  module Z.Data.Text (   -- * Text type-    Text, getUTF8Bytes-  , validate, validateMaybe+    Text(..)+  , validate+  , InvalidUTF8Exception(..)+  , validateMaybe+  , index, indexMaybe, indexR, indexMaybeR   -- * Basic creating   , empty, singleton, copy-  -- * Building text   , replicate, cycleN   -- * Conversion between list   , pack, packN, packR, packRN@@ -41,8 +43,6 @@   , concat, concatMap     -- ** Special folds   , count, all, any-  -- * Searching by equality-  , elem, notElem   -- * Slice manipulation   , cons, snoc   , uncons, unsnoc@@ -69,6 +69,8 @@   , intercalateElem   , transpose   -- * Search+  -- ** searching by equality+  , elem, notElem   -- ** element-wise search   , find, findR   , filter, partition
Z/Data/Text/Base.hs view
@@ -27,11 +27,10 @@   , validate   , InvalidUTF8Exception(..)   , validateMaybe-  , replicate-  , cycleN-  , indexMaybe, charByteIndex, indexMaybeR, charByteIndexR+  , index, indexMaybe, charByteIndex, indexR, indexMaybeR, charByteIndexR   -- * Basic creating   , empty, singleton, copy+  , replicate , cycleN   -- * Conversion between list   , pack, packN, packR, packRN   , unpack, unpackR@@ -215,6 +214,14 @@             arr <- unsafeFreezePrimArray marr             return $ Text (PrimVector arr 0 len) +-- | /O(n)/ Get the nth codepoint from 'Text', throw @IndexOutOfVectorRange n callStack@+-- when out of bound.+index :: HasCallStack => Text -> Int -> Char+{-# INLINABLE index #-}+index t n = case t `indexMaybe` n of Nothing -> throw (V.IndexOutOfVectorRange n callStack)+                                     Just x  -> x++ -- | /O(n)/ Get the nth codepoint from 'Text'. indexMaybe :: Text -> Int -> Maybe Char {-# INLINABLE indexMaybe #-}@@ -243,6 +250,13 @@         | i >= end = i         | j >= n = i         | otherwise = let l' = decodeCharLen ba i in go (i+l') (j+1)++-- | /O(n)/ Get the nth codepoint from 'Text' counting from the end,+-- throw @IndexOutOfVectorRange n callStack@ when out of bound.+indexR :: HasCallStack => Text -> Int -> Char+{-# INLINABLE indexR #-}+indexR t n = case t `indexMaybeR` n of Nothing -> throw (V.IndexOutOfVectorRange n callStack)+                                       Just x  -> x  -- | /O(n)/ Get the nth codepoint from 'Text' counting from the end. indexMaybeR :: Text -> Int -> Maybe Char
Z/Data/Text/Extra.hs view
@@ -30,8 +30,7 @@   , splitAt   , takeWhile, takeWhileR, dropWhile, dropWhileR, dropAround   , break, span-  , breakR, spanR, breakOn-  , breakOnAll, breakOnAllOverlapping+  , breakR, spanR, breakOn, breakOnAll   , group, groupBy   , stripPrefix, stripSuffix   , split, splitWith, splitOn@@ -305,14 +304,6 @@ {-# INLINE breakOnAll #-} breakOnAll (Text needle) (Text haystack@(V.PrimVector arr s l)) =     List.map breaker (V.indices needle haystack False)-  where-    breaker i = (Text (V.PrimVector arr s (i-s)), Text (V.PrimVector arr i (s+l-i)))---- | Overlapping version of 'breakOnAll'.-breakOnAllOverlapping :: Text -> Text -> [(Text, Text)]-{-# INLINE breakOnAllOverlapping #-}-breakOnAllOverlapping (Text needle) (Text haystack@(V.PrimVector arr s l)) =-    List.map breaker (V.indicesOverlapping needle haystack False)   where     breaker i = (Text (V.PrimVector arr s (i-s)), Text (V.PrimVector arr i (s+l-i))) 
Z/Data/Text/UTF8Codec.hs view
@@ -167,6 +167,7 @@                 w4# = indexWord8Array# ba# (idx# +# 3#)             in (# chr4# w1# w2# w3# w4#, 4# #) + -- | Decode a codepoint's length in bytes -- -- This function assumed all bytes are UTF-8 encoded, and the index param point to the@@ -182,7 +183,7 @@ -- This function is marked as @NOINLINE@ to reduce code size, and stop messing up simplifier -- due to too much branches. decodeCharLen# :: ByteArray# -> Int# -> Int#-{-# INLINE decodeCharLen# #-} -- This branchy code make GHC impossible to fuse, DON'T inline+{-# INLINE decodeCharLen# #-} decodeCharLen# ba# idx# = case indexWord8Array# ba# idx# of     w1#         | isTrue# (w1# `leWord#` 0x7F##) -> 1#
Z/Data/Vector.hs view
@@ -78,11 +78,12 @@ module Z.Data.Vector (   -- * The Vec typeclass     Vec(IArray)+  , indexMaybe, index, indexM   -- * Boxed and unboxed vector type   , Vector   , PrimVector   -- ** Word8 vector-  , Bytes, packASCII+  , Bytes, packASCII, w2c, c2w   -- * Basic creating   , empty, singleton, copy   -- * Conversion between list@@ -97,7 +98,7 @@   , foldr', ifoldr', foldr1', foldr1Maybe'     -- ** Special folds   , concat, concatMap-  , maximumMaybe, minimumMaybe+  , maximum, minimum, maximumMaybe, minimumMaybe   , sum   , count   , product, product'@@ -111,8 +112,6 @@   , cycleN   , unfoldr   , unfoldrN-  -- * Searching by equality-  , elem, notElem, elemIndex   -- * Slice manipulation   , cons, snoc   , uncons, unsnoc@@ -143,6 +142,8 @@   , scanl', scanl1'   , scanr', scanr1'   -- * Search+  -- ** searching by equality+  , elem, notElem, elemIndex   -- ** element-wise search   , find, findR   , findIndices, elemIndices@@ -167,7 +168,11 @@   , vecW8, vecW16, vecW32, vecW64, vecWord   , vecI8, vecI16, vecI32, vecI64, vecInt   -- * Misc-  , IPair(..)+  , IPair(..), mapIPair'+  , defaultInitSize+  , chunkOverhead+  , defaultChunkSize+  , smallChunkSize   , VectorException(..)   , castVector  ) where
Z/Data/Vector/Base.hs view
@@ -45,8 +45,7 @@   , Vector(..)   , PrimVector(..)   -- ** Word8 vector-  , Bytes, packASCII-  , w2c, c2w+  , Bytes, packASCII, w2c, c2w   -- * Creating utilities   , create, create', creating, creating', createN, createN2   , empty, singleton, copy@@ -62,8 +61,7 @@   , foldr', ifoldr', foldr1', foldr1Maybe'     -- ** Special folds   , concat, concatMap-  , maximum, minimum-  , maximumMaybe, minimumMaybe+  , maximum, minimum, maximumMaybe, minimumMaybe   , sum   , count   , product, product'
Z/Foreign.hs view
@@ -61,8 +61,8 @@   ( -- ** Unsafe FFI     withPrimArrayUnsafe   , withMutablePrimArrayUnsafe-  , allocMutableByteArrayUnsafe   , withPrimVectorUnsafe+  , allocMutableByteArrayUnsafe   , withPrimUnsafe   , allocPrimUnsafe     -- ** Safe FFI@@ -74,18 +74,25 @@   , allocPrimSafe     -- ** Pointer helpers   , BA#, MBA#+  , clearMBA, peekMBA, pokeMBA   , clearPtr   , castPtr   -- ** re-export+  , module Data.Primitive.ByteArray   , module Data.Primitive.Ptr+  , module Z.Data.Array.UnalignedAccess   ) where  import           Control.Monad.Primitive import           Data.Primitive+import           Data.Word import           Data.Primitive.Ptr+import           Data.Primitive.ByteArray import           Foreign.C.Types import           GHC.Ptr+import           GHC.Prim import           Z.Data.Array+import           Z.Data.Array.UnalignedAccess import           Z.Data.Vector.Base  -- | Type alias for 'ByteArray#'.@@ -114,6 +121,20 @@ -- A 'MutableByteArray#' COULD BE MOVED BY GC DURING SAFE FFI CALL. type MBA# a = MutableByteArray# RealWorld +-- | Read field from 'MBA#' with offset.+peekMBA :: (UnalignedAccess x) => MBA# a -> Int -> IO x+peekMBA mba# =  readWord8ArrayAs (MutableByteArray mba#)++-- | Write field to 'MBA#' ith offst.+pokeMBA :: (UnalignedAccess x) => MBA# a -> Int -> x -> IO()+pokeMBA mba# =  writeWord8ArrayAs (MutableByteArray mba#)++clearMBA :: MBA# a -> IO ()+clearMBA mba# = do+    let mba = (MutableByteArray mba#)+    siz <- getSizeofMutableByteArray mba+    setByteArray mba 0 siz (0 :: Word8)+ -- | Pass primitive array to unsafe FFI as pointer. -- -- Enable 'UnliftedFFITypes' extension in your haskell code, use proper pointer type and @CSize/CSsize@@@ -145,8 +166,28 @@ withMutablePrimArrayUnsafe mpa@(MutablePrimArray mba#) f =     getSizeofMutablePrimArray mpa >>= f mba# -allocMutableByteArrayUnsafe :: Int      -- ^ In bytes not element-                            -> (MBA# a -> IO b) -> IO b++-- | Allocate some bytes and pass to FFI as pointer.+--+-- This function allocate some unpinned bytes and pass to FFI as MBA#, example usage with hsc2hs:+--+-- @+--      allocMutableByteArrayUnsafe (#size c_struct) $ \ p ->+--+--          pokeMBA# p (#offset c_struct c_field1) field1+--          pokeMBA# p (#offset c_struct c_field2) field2+--+--          c_ffi p ....+--+--          field1' <- peekMBA# p (#offset c_struct c_field1)+--          field2' <- peekMBA# p (#offset c_struct c_field2)+--          ...+--          return (CStruct field1' field2')+-- @+--+-- USE THIS FUNCTION WITH UNSAFE FFI CALL ONLY.+allocMutableByteArrayUnsafe :: Int      -- ^ number of bytes+                  -> (MBA# a -> IO b) -> IO b {-# INLINE allocMutableByteArrayUnsafe #-} allocMutableByteArrayUnsafe len f = do     (MutableByteArray mba#) <- newByteArray len@@ -183,6 +224,7 @@     !a <- readPrimArray mpa 0     return (a, b) +-- | like 'withPrimUnsafe', but don't write initial value. allocPrimUnsafe :: (Prim a) => (MBA# a -> IO b) -> IO (a, b) {-# INLINE allocPrimUnsafe #-} allocPrimUnsafe f = do@@ -196,7 +238,7 @@ -- | Pass primitive array to safe FFI as pointer. -- -- Use proper pointer type and @CSize/CSsize@ to marshall @Ptr a@ and @Int@ arguments on C side.--- The memory pointed by 'Ptr a' will not moved.+-- The memory pointed by 'Ptr a' will not moved during call. After call returned, pointer is no longer valid. -- -- The second 'Int' arguement is the element size not the bytes size. --@@ -216,7 +258,7 @@  -- | Pass mutable primitive array to unsafe FFI as pointer. ----- The mutable version of 'withPrimArraySafe'.+-- The mutable version of 'withPrimArraySafe'. After call returned, pointer is no longer valid. -- -- Don't pass a forever loop to this function, see <https://ghc.haskell.org/trac/ghc/ticket/14346 #14346>. withMutablePrimArraySafe :: (Prim a) => MutablePrimArray RealWorld a -> (Ptr a -> Int -> IO b) -> IO b@@ -231,6 +273,9 @@         copyMutablePrimArray buf 0 marr 0 siz         withMutablePrimArrayContents buf $ \ ptr -> f ptr siz +-- | Allocate a primitive array and pass to FFI as pointer.+--+-- After call returned, pointer is no longer valid. allocMutablePrimArraySafe :: (Prim a) => Int -- ^ in number of elements not bytes                           -> (Ptr a -> IO b) -> IO b {-# INLINE allocMutablePrimArraySafe #-}@@ -241,7 +286,7 @@ -- | Pass 'PrimVector' to unsafe FFI as pointer -- -- The 'PrimVector' version of 'withPrimArraySafe'. The 'Ptr' is already pointed--- to the first element, thus no offset is provided.+-- to the first element, thus no offset is provided. After call returned, pointer is no longer valid. -- -- Don't pass a forever loop to this function, see <https://ghc.haskell.org/trac/ghc/ticket/14346 #14346>. withPrimVectorSafe :: forall a b. Prim a => PrimVector a -> (Ptr a -> Int -> IO b) -> IO b@@ -269,6 +314,7 @@     !a <- readPrimArray buf 0     return (a, b) +-- | like 'withPrimSafe', but don't write initial value. allocPrimSafe :: forall a b. Prim a => (Ptr a -> IO b) -> IO (a, b) {-# INLINE allocPrimSafe #-} allocPrimSafe f = do