hw-prim 0.0.3.1 → 0.0.3.2
raw patch · 2 files changed
+64/−4 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ HaskellWorks.Data.FromByteString: instance HaskellWorks.Data.FromByteString.FromByteString (Data.Vector.Storable.Vector GHC.Word.Word16)
+ HaskellWorks.Data.FromByteString: instance HaskellWorks.Data.FromByteString.FromByteString (Data.Vector.Storable.Vector GHC.Word.Word32)
+ HaskellWorks.Data.FromByteString: instance HaskellWorks.Data.FromByteString.FromByteString (Data.Vector.Storable.Vector GHC.Word.Word64)
+ HaskellWorks.Data.FromByteString: instance HaskellWorks.Data.FromByteString.FromByteString (Data.Vector.Storable.Vector GHC.Word.Word8)
+ HaskellWorks.Data.Vector.VectorLike: type family Elem v;
+ HaskellWorks.Data.Vector.VectorLike: }
- HaskellWorks.Data.Vector.VectorLike: class VectorLike v where type family Elem v
+ HaskellWorks.Data.Vector.VectorLike: class VectorLike v where type Elem v where {
Files
- hw-prim.cabal +6/−2
- src/HaskellWorks/Data/FromByteString.hs +58/−2
hw-prim.cabal view
@@ -1,5 +1,5 @@ name: hw-prim-version: 0.0.3.1+version: 0.0.3.2 synopsis: Primitive functions and data types description: Please see README.md homepage: http://github.com/haskell-works/hw-prim#readme@@ -36,9 +36,9 @@ , HaskellWorks.Data.Vector.StorableVectorLike , HaskellWorks.Data.Vector.VectorLike build-depends: base >= 4 && < 5- , vector , bytestring , random+ , vector default-language: Haskell2010 ghc-options: -Wall -O2 -msse4.2@@ -49,8 +49,12 @@ main-is: Spec.hs other-modules: HaskellWorks.Data.SearchSpec build-depends: base >= 4 && < 5+ , bytestring , hspec+ , hw-prim , QuickCheck+ , random+ , vector ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall default-language: Haskell2010
src/HaskellWorks/Data/FromByteString.hs view
@@ -1,10 +1,66 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE InstanceSigs #-}+ module HaskellWorks.Data.FromByteString ( FromByteString(..) ) where -import Data.ByteString.Internal+import Data.Bits+import qualified Data.ByteString as BS+import qualified Data.Vector.Storable as DVS+import Data.Word -- | Class for byte-string-like datastructures class FromByteString a where -- | Convert a byte string to a value of type @a- fromByteString :: ByteString -> a+ fromByteString :: BS.ByteString -> a++instance FromByteString (DVS.Vector Word8) where+ fromByteString :: BS.ByteString -> DVS.Vector Word8+ fromByteString bs = DVS.unfoldrN (BS.length bs) gen bs+ where gen :: BS.ByteString -> Maybe (Word8, BS.ByteString)+ gen cs = case BS.uncons cs of+ Just (d, ds) -> Just (d, ds)+ Nothing -> Nothing++instance FromByteString (DVS.Vector Word16) where+ fromByteString :: BS.ByteString -> DVS.Vector Word16+ fromByteString bs = DVS.unfoldrN (BS.length bs `div` 2 + 2) gen bs+ where gen :: BS.ByteString -> Maybe (Word16, BS.ByteString)+ gen cs = case BS.uncons cs of+ Just (d, ds) -> gen' 8 (fromIntegral d) ds+ Nothing -> Nothing+ gen' :: Int -> Word16 -> BS.ByteString -> Maybe (Word16, BS.ByteString)+ gen' n w cs+ | n >= 16 = Just (w, cs)+ | otherwise = case BS.uncons cs of+ Just (d, ds) -> gen' (n + 8) (w .|. (fromIntegral d `shiftL` fromIntegral n)) ds+ Nothing -> Just (w, cs)++instance FromByteString (DVS.Vector Word32) where+ fromByteString :: BS.ByteString -> DVS.Vector Word32+ fromByteString bs = DVS.unfoldrN (BS.length bs `div` 4 + 4) gen bs+ where gen :: BS.ByteString -> Maybe (Word32, BS.ByteString)+ gen cs = case BS.uncons cs of+ Just (d, ds) -> gen' 8 (fromIntegral d) ds+ Nothing -> Nothing+ gen' :: Int -> Word32 -> BS.ByteString -> Maybe (Word32, BS.ByteString)+ gen' n w cs+ | n >= 32 = Just (w, cs)+ | otherwise = case BS.uncons cs of+ Just (d, ds) -> gen' (n + 8) (w .|. (fromIntegral d `shiftL` fromIntegral n)) ds+ Nothing -> Just (w, cs)++instance FromByteString (DVS.Vector Word64) where+ fromByteString :: BS.ByteString -> DVS.Vector Word64+ fromByteString bs = DVS.unfoldrN (BS.length bs `div` 8 + 8) gen bs+ where gen :: BS.ByteString -> Maybe (Word64, BS.ByteString)+ gen cs = case BS.uncons cs of+ Just (d, ds) -> gen' 8 (fromIntegral d) ds+ Nothing -> Nothing+ gen' :: Int -> Word64 -> BS.ByteString -> Maybe (Word64, BS.ByteString)+ gen' n w cs+ | n >= 64 = Just (w, cs)+ | otherwise = case BS.uncons cs of+ Just (d, ds) -> gen' (n + 8) (w .|. (fromIntegral d `shiftL` fromIntegral n)) ds+ Nothing -> Just (w, cs)