packages feed

matrix-market-attoparsec 0.1.1.0 → 0.1.1.1

raw patch · 4 files changed

+48/−35 lines, 4 filesPVP: minor bump suggested

API additions: PVP suggests at least a minor version bump

API changes (from Hackage documentation)

+ Data.Matrix.MatrixMarket: FormatExportNotSupported :: String -> String -> ExportError
+ Data.Matrix.MatrixMarket: data ExportError

Files

matrix-market-attoparsec.cabal view
@@ -1,5 +1,5 @@ name:                matrix-market-attoparsec-version:             0.1.1.0+version:             0.1.1.1 synopsis:            Parsing and serialization functions for the NIST Matrix Market format description:         Parsing and serialization functions for the NIST Matrix Market format. homepage:            https://github.com/ocramz/matrix-market-attoparsec
src/Control/Exception/Common.hs view
@@ -6,13 +6,14 @@ -- import Control.Monad.Catch (MonadThrow (..)) import Data.Typeable -- (TypeRep, Typeable, typeRep) -+-- | Exceptions related to loading/importing data data ImportError = FileParseError String String deriving (Eq, Typeable) instance Show ImportError where   show = \case     FileParseError s s2 -> unwords [s, ": File parse error:", s2] instance Exception ImportError +-- | Exceptions related to serializing/storing data data ExportError = FormatExportNotSupported String String deriving (Eq, Typeable) instance Show ExportError where   show = \case
src/Data/Matrix/MatrixMarket.hs view
@@ -1,7 +1,7 @@ ----------------------------------------------------------------------------- -- | -- Module      :  Data.Matrix.MatrixMarket--- Copyright   :  (c) Marco Zocca 2017+-- Copyright   :  (c) Marco Zocca 2017-2020 -- License     :  BSD2 (see the file LICENSE) -- -- Maintainer  :  zocca marco gmail@@ -29,6 +29,6 @@         -- ** Array-related         dimArr, numDatArr,         -- * Exceptions-        ImportError(..)) where+        ImportError(..), ExportError(..)) where  import Data.Matrix.MatrixMarket.Internal as M
src/Data/Matrix/MatrixMarket/Internal.hs view
@@ -1,3 +1,4 @@+ {-# LANGUAGE GADTs, OverloadedStrings, DeriveFunctor #-} ----------------------------------------------------------------------------- -- |@@ -23,11 +24,12 @@         Format (Coordinate, Array), Structure (General, Symmetric, Hermitian, Skew),         nnz, dim, numDat,         dimArr, numDatArr,-        ImportError(..)) where+        ImportError(..), ExportError(..)) where    import Control.Applicative                      hiding ( many )+import Data.Functor (($>))  import Data.Int import qualified Data.Char as C@@ -37,7 +39,7 @@ -- import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy.Char8 as B import qualified Data.Attoparsec.Lazy           as L-import qualified Data.ByteString.Lazy           as L+import qualified Data.ByteString.Lazy           as LBS  import Control.Monad.Catch import Control.Exception.Common (ImportError(..), ExportError(..))@@ -107,22 +109,22 @@ integral = skipSpace' *> decimal  format :: Parser Format-format =  string "coordinate" *> pure Coordinate-      <|> string "array"      *> pure Array+format =  string "coordinate" $> Coordinate+      <|> string "array"      $> Array       <?> "matrix format"  field :: Parser Field-field =  string "real"    *> pure R-     <|> string "complex" *> pure C-     <|> string "integer" *> pure I-     <|> string "pattern" *> pure P+field =  string "real"    $> R+     <|> string "complex" $> C+     <|> string "integer" $> I+     <|> string "pattern" $> P      <?> "matrix field"  structure :: Parser Structure-structure =  string "general"        *> pure General-         <|> string "symmetric"      *> pure Symmetric-         <|> string "hermitian"      *> pure Hermitian-         <|> string "skew-symmetric" *> pure Skew+structure =  string "general"        $> General+         <|> string "symmetric"      $> Symmetric+         <|> string "hermitian"      $> Hermitian+         <|> string "skew-symmetric" $> Skew          <?> "matrix structure"  header :: Parser (Format,Field,Structure)@@ -183,21 +185,27 @@        _ -> fail "integer and pattern cases not relevant for the dense case"  --- | Load a matrix (sparse, i.e. in Coordinate format) from file+-- | Load a matrix (sparse, i.e. in Coordinate format) from file.+--+-- Uses 'readMatrix'' internally readMatrix :: FilePath -> IO (Matrix S.Scientific)-readMatrix file = L.readFile file >>= readMatrix'+readMatrix file = LBS.readFile file >>= readMatrix' --- | Load a matrix (sparse, i.e. in Coordinate format) from a lazy 'L.Bytestring'.-readMatrix' :: L.ByteString -> IO (Matrix S.Scientific)+-- | Load a matrix (sparse, i.e. in Coordinate format) from a lazy 'LBS.ByteString'.+--+-- Throws a 'FileParseError' if the input cannot be parsed+readMatrix' :: LBS.ByteString -> IO (Matrix S.Scientific) readMatrix' chunks =   case L.parse matrix chunks of     L.Fail _ _ msg      -> throwM (FileParseError "readMatrix" msg)     L.Done _ mtx        -> return mtx  -- | Load a dense matrix (i.e. a matrix or vector in Array format) from file+--+-- Throws a 'FileParseError' if the input cannot be parsed readArray :: FilePath -> IO (Array S.Scientific) readArray file = do-  chunks <- L.readFile file+  chunks <- LBS.readFile file   case L.parse array chunks of     L.Fail _ _ msg      -> throwM (FileParseError "readArray" msg)     L.Done _ mtx        -> return mtx@@ -221,29 +229,33 @@  -- %%MatrixMarket matrix coordinate real general -headerStr :: Format -> Field -> Structure -> L.ByteString+headerStr :: Format -> Field -> Structure -> LBS.ByteString headerStr f t s =     B.pack $ unwords ["%%MatrixMarket matrix",                       showFormat f, showField t, showStruct s]  -nl :: L.ByteString+nl :: LBS.ByteString nl = toLBS "\n" -showLines :: (a -> String) -> [a] -> L.ByteString-showLines showf d = L.concat (L.pack . withNewline . showf <$> d) where+showLines :: (a -> String) -> [a] -> LBS.ByteString+showLines showf d = LBS.concat (LBS.pack . withNewline . showf <$> d) where   withNewline x = toEnum . C.ord <$> x ++ "\n"   --- | Serialize a sparse matrix in Coordinate format+-- | Serialize a sparse matrix in Coordinate format to a file+--+-- Uses 'writeMatrix'' internally writeMatrix :: Show b => FilePath -> Matrix b -> IO () writeMatrix fp mat = do   mbs <- writeMatrix' mat-  L.writeFile fp mbs+  LBS.writeFile fp mbs --- | Serialize a sparse matrix in Coordinate format as a bytestring-writeMatrix' :: (MonadThrow m, Show b) => Matrix b -> m L.ByteString+-- | Serialize a sparse matrix in Coordinate format as a 'LBS.Bytestring'+--+-- Throws a 'FormatExportNotSupported' if the user tries to serialize a 'PatternMatrix' value.+writeMatrix' :: (MonadThrow m, Show b) => Matrix b -> m LBS.ByteString writeMatrix' mat =   case mat of (RMatrix d nz s dat) ->                 pure $ matrixByteString d nz R s dat@@ -254,7 +266,7 @@               _ -> throwM (FormatExportNotSupported "writeMatrix" "PatternMatrix not implemented yet")      where        matrixByteString di nz t s d =-         L.concat [headerStr Coordinate t s,+         LBS.concat [headerStr Coordinate t s,                    nl,                    headerSzMatrix di nz,                    nl,@@ -266,11 +278,11 @@ -- | Serialize a dense matrix in Array format writeArray :: Show a => FilePath -> Array a -> IO () writeArray file arr =-  case arr of (RArray d s dat) -> L.writeFile file (arrayByteString d R s dat)-              (CArray d s dat) -> L.writeFile file (arrayByteString d C s dat)+  case arr of (RArray d s dat) -> LBS.writeFile file (arrayByteString d R s dat)+              (CArray d s dat) -> LBS.writeFile file (arrayByteString d C s dat)     where       arrayByteString di t s d =-        L.concat [headerStr Array t s,+        LBS.concat [headerStr Array t s,             nl,             headerSzArray di,             nl,@@ -327,8 +339,8 @@  -- | String -> lazy ByteString -- NB: do not abuse-toLBS :: String -> L.ByteString-toLBS x = L.pack $ (toEnum . C.ord) <$> x+toLBS :: String -> LBS.ByteString+toLBS x = LBS.pack $ (toEnum . C.ord) <$> x