vector-binary-instances-0.2.1.0/0000755000000000000000000000000012132427660014612 5ustar0000000000000000vector-binary-instances-0.2.1.0/LICENSE0000644000000000000000000000276012132427660015624 0ustar0000000000000000Copyright Don Stewart 2010-2012 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. vector-binary-instances-0.2.1.0/Setup.hs0000644000000000000000000000011012132427660016236 0ustar0000000000000000#!/usr/bin/env runhaskell import Distribution.Simple main = defaultMain vector-binary-instances-0.2.1.0/vector-binary-instances.cabal0000644000000000000000000000522512132427660022353 0ustar0000000000000000-- 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.2.1.0 -- 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. Specific instances are provided for unboxed, boxed and storable vectors. . 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: https://github.com/bos/vector-binary-instances bug-reports: https://github.com/bos/vector-binary-instances/issues -- 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: dons00@gmail.com, bos@serpentine.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.6 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.6, binary, cereal source-repository head type: git location: https://github.com/bos/vector-binary-instances vector-binary-instances-0.2.1.0/Data/0000755000000000000000000000000012132427660015463 5ustar0000000000000000vector-binary-instances-0.2.1.0/Data/Vector/0000755000000000000000000000000012132427660016725 5ustar0000000000000000vector-binary-instances-0.2.1.0/Data/Vector/Binary.hs0000644000000000000000000000531112132427660020505 0ustar0000000000000000{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} -------------------------------------------------------------------- -- | -- Module : Data.Vector.Binary -- Copyright : (c) Don Stewart 2010-2012 -- License : BSD3 -- -- Maintainer: Don Stewart -- 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 Control.Monad import qualified Data.Vector.Generic as G import qualified Data.Vector.Unboxed as U import qualified Data.Vector.Storable as S import Data.Vector (Vector) import System.IO.Unsafe import qualified Data.Vector.Generic.Mutable as M import Foreign.Storable (Storable) -- Enumerate the instances to avoid the nasty overlapping instances. -- | Boxed, generic vectors. instance Binary a => Binary (Vector a) where put = putGeneric get = getGeneric {-# INLINE get #-} -- | Unboxed vectors instance (U.Unbox a, Binary a) => Binary (U.Vector a) where put = putGeneric get = getGeneric {-# INLINE get #-} -- | Storable vectors instance (Storable a, Binary a) => Binary (S.Vector a) where put = putGeneric get = getGeneric {-# INLINE get #-} ------------------------------------------------------------------------ -- this is morally sound, if very awkward. -- all effects are contained, and can't escape the unsafeFreeze getGeneric :: (G.Vector v a, Binary a) => Get (v a) {-# INLINE getGeneric #-} getGeneric = do n <- get -- new unitinialized array mv <- lift $ M.new n let fill i | i < n = do x <- get (unsafePerformIO $ M.unsafeWrite mv i x) `seq` return () fill (i+1) | otherwise = return () fill 0 lift $ G.unsafeFreeze mv -- | Generic put for anything in the G.Vector class. putGeneric :: (G.Vector v a, Binary a) => v a -> Put {-# INLINE putGeneric #-} putGeneric v = do put (G.length v) G.mapM_ put v lift :: IO b -> Get b lift = return .unsafePerformIO vector-binary-instances-0.2.1.0/Data/Vector/Cereal.hs0000644000000000000000000000434012132427660020455 0ustar0000000000000000{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} -------------------------------------------------------------------- -- | -- Module : Data.Vector.Cereal -- Copyright : (c) Don Stewart 2010-2012 -- License : BSD3 -- -- Maintainer: Don Stewart -- 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 Control.Monad import qualified Data.Vector.Generic as G import qualified Data.Vector.Unboxed as U import qualified Data.Vector.Storable as S import Data.Vector (Vector) import System.IO.Unsafe import qualified Data.Vector.Generic.Mutable as M import Foreign.Storable (Storable) -- | Boxed, generic vectors. instance Serialize a => Serialize (Vector a) where put = putGeneric get = getGeneric {-# INLINE get #-} -- | Unboxed vectors instance (U.Unbox a, Serialize a) => Serialize (U.Vector a) where put = putGeneric get = getGeneric {-# INLINE get #-} -- | Storable vectors instance (Storable a, Serialize a) => Serialize (S.Vector a) where put = putGeneric get = getGeneric {-# INLINE get #-} ------------------------------------------------------------------------ -- this is morally sound, if very awkward. -- all effects are contained, and can't escape the unsafeFreeze getGeneric :: (G.Vector v a, Serialize a) => Get (v a) {-# INLINE getGeneric #-} getGeneric = do n <- get -- new unitinialized array mv <- lift $ M.new n let fill i | i < n = do x <- get (unsafePerformIO $ M.unsafeWrite mv i x) `seq` return () fill (i+1) | otherwise = return () fill 0 lift $ G.unsafeFreeze mv -- | Generic put for anything in the G.Vector class. putGeneric :: (G.Vector v a, Serialize a) => v a -> Put {-# INLINE putGeneric #-} putGeneric v = do put (G.length v) G.mapM_ put v lift :: IO b -> Get b lift = return .unsafePerformIO