binary-enum 0.1.0.0 → 0.1.2.0
raw patch · 2 files changed
+23/−11 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Data.Binary.Enum: instance (GHC.Enum.Enum a, GHC.Classes.Eq a, GHC.Classes.Eq t, GHC.Num.Num t, GHC.Real.Integral t, Data.Binary.Class.Binary t) => Data.Binary.Class.Binary (Data.Binary.Enum.BitMap t a)
+ Data.Binary.Enum: instance (GHC.Enum.Enum a, GHC.Classes.Eq a, GHC.Classes.Eq t, GHC.Real.Integral t, Data.Binary.Class.Binary t) => Data.Binary.Class.Binary (Data.Binary.Enum.BitMap t a)
+ Data.Binary.Enum: instance (GHC.Enum.Enum a, GHC.Enum.Enum t, Data.Binary.Class.Binary t) => GHC.Enum.Enum (Data.Binary.Enum.BitEnc t a)
+ Data.Binary.Enum: instance (GHC.Enum.Enum a, GHC.Enum.Enum t, GHC.Classes.Eq a, GHC.Classes.Eq t, GHC.Num.Num t) => GHC.Enum.Enum (Data.Binary.Enum.BitMap t a)
+ Data.Binary.Enum: instance GHC.Show.Show a => GHC.Show.Show (Data.Binary.Enum.BitEnc t a)
+ Data.Binary.Enum: instance GHC.Show.Show a => GHC.Show.Show (Data.Binary.Enum.BitMap t a)
Files
- Data/Binary/Enum.hs +22/−10
- binary-enum.cabal +1/−1
Data/Binary/Enum.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}+{-# LANGUAGE ViewPatterns, ScopedTypeVariables, StandaloneDeriving #-} module Data.Binary.Enum where @@ -10,18 +10,30 @@ -- | we can store Enum values inside values we know how to store, so let newtype BitMap t a = BitMap [a] -- ^ Map lets you encode 'flags' compressing distinct eg. packing 8 diferent flags per byte+deriving instance (Show a) => Show (BitMap t a)+ newtype BitEnc t a = BitEnc a- -- ^ t is a store type, you will need to make sure thats your enum fits there+ -- ^ t is a store type, you will need to make sure that your enum fits there+ -- eg. you want to store `Bool` as `Word64` field `BitEnc Word64 Bool`+deriving instance (Show a) => Show (BitEnc t a) instance (Enum a, Enum t, Binary t) => Binary (BitEnc t a) where put (BitEnc a) = put (toEnum . fromEnum $ a :: t) get = BitEnc . toEnum . fromEnum <$> (get :: Get t) -instance (Enum a, Eq a, Eq t, Num t, Integral t, Binary t) => Binary (BitMap t a) where- put (BitMap a) = put ( foldr (\(fromEnum -> x) y -> 2 ^ x + y ) 0 (DL.nub a) :: t )- get = BitMap . process 0 <$> (get :: Get t)- where- process _ 0 = []- process k (flip divMod 2 -> (d, m))- | m == 1 = toEnum k : process (k + 1) d- | otherwise = process (k + 1) d+instance (Enum a, Eq a, Eq t, Integral t, Binary t) => Binary (BitMap t a) where+ put a = put ( (toEnum (fromEnum a)) :: t )+ get = toEnum . fromEnum <$> (get :: Get t)++instance (Enum a, Enum t, Eq a, Eq t, Num t) => Enum (BitMap t a) where+ fromEnum (BitMap a) = fromEnum (foldr (\(fromEnum -> x) y -> 2 ^ x + y ) 0 (DL.nub a) :: t)+ toEnum = BitMap . process 0 -- (fromEnum k :: t)+ where+ process _ 0 = []+ process k (flip divMod 2 -> (d, m))+ | m == 1 = toEnum k : process (k + 1) d+ | otherwise = process (k + 1) d++instance (Enum a, Enum t, Binary t) => Enum (BitEnc t a) where+ fromEnum (BitEnc a) = fromEnum a+ toEnum = BitEnc . toEnum . fromEnum
binary-enum.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: binary-enum-version: 0.1.0.0+version: 0.1.2.0 synopsis: Simple wrappers around enum types description: allowing to serise to/from binary both as values and bitmasks homepage: https://github.com/tolysz/binary-enum