vector-binary-instances (empty) → 0.1
raw patch · 5 files changed
+225/−0 lines, 5 filesdep +basedep +binarydep +cerealsetup-changed
Dependencies added: base, binary, cereal, vector
Files
- Data/Vector/Binary.hs +62/−0
- Data/Vector/Cereal.hs +47/−0
- LICENSE +30/−0
- Setup.hs +3/−0
- vector-binary-instances.cabal +83/−0
+ Data/Vector/Binary.hs view
@@ -0,0 +1,62 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE UndecidableInstances #-}+--------------------------------------------------------------------+-- |+-- Module : Data.Vector.Binary+-- Copyright : (c) Don Stewart 2010++-- License : BSD3+--+-- Maintainer: Don Stewart <dons@galois.com>+-- Stability : provisional+-- Portability: GHC only++-- Instances for Binary for the types defined in the vector package,+-- making it easy to serialize vectors to and from disk. We use the+-- generic interface to vectors, so all vector types are supported.+--+-- To serialize a vector:+--+-- > *Data.Vector.Binary> let v = Data.Vector.fromList [1..10]+-- > *Data.Vector.Binary> v+-- > fromList [1,2,3,4,5,6,7,8,9,10] :: Data.Vector.Vector+-- > *Data.Vector.Binary> encode v+-- > Chunk "\NUL\NUL\NUL\NUL\NUL...\NUL\NUL\NUL\t\NUL\NUL\NUL\NUL\n" Empty+--+-- Which you can in turn compress before writing to disk:+--+-- > compress . encode $ v+-- > Chunk "\US\139\b\NUL\NUL\N...\229\240,\254:\NUL\NUL\NUL" Empty+--+--------------------------------------------------------------------++module Data.Vector.Binary () where++import Data.Binary+import qualified Data.Vector.Generic as G+import Control.Monad++instance (G.Vector v a, Binary a) => Binary (v a) where+ put v = do+ put (G.length v)+ mapM_ put (G.toList v)++ -- Uses too much space going via lists.+ -- XXX todo: fill directly from a stream. + get = do+ n <- get+ xs <- getMany n+ return (G.fromList xs)+++-- | 'getMany n' get 'n' elements in order, without blowing the stack.+getMany :: Binary a => Int -> Get [a]+getMany n = go [] n+ where+ go xs 0 = return $! reverse xs+ go xs i = do x <- get+ -- we must seq x to avoid stack overflows due to laziness in+ -- (>>=)+ x `seq` go (x:xs) (i-1)+{-# INLINE getMany #-}+
+ Data/Vector/Cereal.hs view
@@ -0,0 +1,47 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE UndecidableInstances #-}+--------------------------------------------------------------------+-- |+-- Module : Data.Vector.Cereal+-- Copyright : (c) Don Stewart 2010++-- License : BSD3+--+-- Maintainer: Don Stewart <dons@galois.com>+-- Stability : provisional+-- Portability: GHC only+--+-- Instances for Serialize for the types defined in the vector package,+-- making it easy to serialize vectors to and from disk. We use the+-- generic interface to vectors, so all vector types are supported.+--+--------------------------------------------------------------------++module Data.Vector.Cereal () where++import Data.Serialize+import qualified Data.Vector.Generic as G+import Control.Monad++instance (G.Vector v a, Serialize a) => Serialize (v a) where+ put v = do+ put (G.length v)+ mapM_ put (G.toList v)++ get = do+ n <- get+ xs <- getMany n+ return (G.fromList xs)+++-- | 'getMany n' get 'n' elements in order, without blowing the stack.+getMany :: Serialize a => Int -> Get [a]+getMany n = go [] n+ where+ go xs 0 = return $! reverse xs+ go xs i = do x <- get+ -- we must seq x to avoid stack overflows due to laziness in+ -- (>>=)+ x `seq` go (x:xs) (i-1)+{-# INLINE getMany #-}+
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Don Stewart 2010++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Don Stewart nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+import Distribution.Simple+main = defaultMain
+ vector-binary-instances.cabal view
@@ -0,0 +1,83 @@+-- binary-vector-instances.cabal auto-generated by cabal init. For+-- additional options, see+-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.+-- The name of the package.+Name: vector-binary-instances++-- The package version. See the Haskell package versioning policy+-- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for+-- standards guiding when and how versions should be incremented.+Version: 0.1++-- A short (one-line) description of the package.+Synopsis: Instances of Data.Binary and Data.Serialize for vector++-- A longer description of the package.+Description: + Instances for Binary for the types defined in the vector package,+ making it easy to serialize vectors to and from disk. We use the+ generic interface to vectors, so all vector types are supported.+ . + To serialize a vector:+ .+ > *Data.Vector.Binary> let v = Data.Vector.fromList [1..10]+ > *Data.Vector.Binary> v+ > fromList [1,2,3,4,5,6,7,8,9,10] :: Data.Vector.Vector+ > *Data.Vector.Binary> encode v+ > Chunk "\NUL\NUL\NUL\NUL\NUL...\NUL\NUL\NUL\t\NUL\NUL\NUL\NUL\n" Empty+ . + Which you can in turn compress before writing to disk:+ .+ > compress . encode $ v+ > Chunk "\US\139\b\NUL\NUL\N...\229\240,\254:\NUL\NUL\NUL" Empty+ +-- URL for the project homepage or repository.+Homepage: http://code.haskell.org/~dons/code/binary-vector-instances++-- The license under which the package is released.+License: BSD3++-- The file containing the license text.+License-file: LICENSE++-- The package author(s).+Author: Don Stewart++-- An email address to which users can send suggestions, bug reports,+-- and patches.+Maintainer: dons@galois.com++-- A copyright notice.+-- Copyright: ++-- Stability of the pakcage (experimental, provisional, stable...)+Stability: Experimental++Category: Data++Build-type: Simple++-- Extra files to be distributed with the package, such as examples or+-- a README.+-- Extra-source-files: ++-- Constraint on the version of Cabal needed to build this package.+Cabal-version: >=1.2+++Library+ -- Modules exported by the library.+ Exposed-modules: + Data.Vector.Binary,+ Data.Vector.Cereal+ + -- Packages needed in order to build this package.+ Build-depends: + base > 3 && < 6,+ vector >= 0.5,+ binary,+ cereal++ Extensions:+ FlexibleInstances,+ UndecidableInstances