bit-array 0.1.0.1 → 0.1.1
raw patch · 5 files changed
+62/−86 lines, 5 filesdep −loch-thdep −placeholdersdep ~basedep ~doctestdep ~filepathsetup-changedPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependencies removed: loch-th, placeholders
Dependency ranges changed: base, doctest, filepath, numeric-qq
API changes (from Hackage documentation)
- BitArray: instance (Data.Bits.Bits a, GHC.Num.Num a) => Data.String.IsString (BitArray.BitArray a)
- BitArray: instance (Data.Bits.Bits a, GHC.Num.Num a) => GHC.Read.Read (BitArray.BitArray a)
- BitArray: instance Data.Bits.Bits a => GHC.Show.Show (BitArray.BitArray a)
+ BitArray: instance Data.Bits.FiniteBits a => Data.Bits.FiniteBits (BitArray.BitArray a)
+ BitArray: instance Data.Bits.FiniteBits a => Data.String.IsString (BitArray.BitArray a)
+ BitArray: instance Data.Bits.FiniteBits a => GHC.Read.Read (BitArray.BitArray a)
+ BitArray: instance Data.Bits.FiniteBits a => GHC.Show.Show (BitArray.BitArray a)
- BitArray: foldr :: (Bits a, Num a) => (a -> b -> b) -> b -> BitArray a -> b
+ BitArray: foldr :: (FiniteBits a) => (a -> b -> b) -> b -> BitArray a -> b
- BitArray: fromBoolList :: (Bits a, Num a) => [Bool] -> BitArray a
+ BitArray: fromBoolList :: (FiniteBits a) => [Bool] -> BitArray a
- BitArray: fromList :: (Bits a, Num a) => [a] -> BitArray a
+ BitArray: fromList :: (FiniteBits a) => [a] -> BitArray a
- BitArray: map :: (Bits a, Num a, Bits b, Num b) => (a -> b) -> BitArray a -> BitArray b
+ BitArray: map :: (FiniteBits a, FiniteBits b) => (a -> b) -> BitArray a -> BitArray b
- BitArray: mapM_ :: (Bits a, Num a, Monad m) => (a -> m b) -> BitArray a -> m ()
+ BitArray: mapM_ :: (FiniteBits a, Monad m) => (a -> m b) -> BitArray a -> m ()
- BitArray: parseString :: (Bits a, Num a) => String -> Maybe (BitArray a)
+ BitArray: parseString :: (FiniteBits a) => String -> Maybe (BitArray a)
- BitArray: toBoolList :: (Bits a) => BitArray a -> [Bool]
+ BitArray: toBoolList :: (FiniteBits a) => BitArray a -> [Bool]
- BitArray: toList :: (Bits a, Num a) => BitArray a -> [a]
+ BitArray: toList :: (FiniteBits a) => BitArray a -> [a]
- BitArray: toString :: (Bits a) => BitArray a -> String
+ BitArray: toString :: (FiniteBits a) => BitArray a -> String
- BitArray: traverse_ :: (Bits a, Num a, Applicative f) => (a -> f b) -> BitArray a -> f ()
+ BitArray: traverse_ :: (FiniteBits a, Applicative f) => (a -> f b) -> BitArray a -> f ()
Files
- Setup.hs +1/−2
- bit-array.cabal +6/−9
- library/BitArray.hs +53/−57
- library/BitArray/Parser.hs +2/−2
- library/BitArray/Prelude.hs +0/−16
Setup.hs view
@@ -3,7 +3,7 @@ import Distribution.Package ( PackageName(PackageName), Package, PackageId, InstalledPackageId, packageVersion, packageName ) import Distribution.PackageDescription ( PackageDescription(), TestSuite(..) ) import Distribution.Simple ( defaultMainWithHooks, UserHooks(..), simpleUserHooks )-import Distribution.Simple.Utils ( rewriteFile, createDirectoryIfMissingVerbose, copyFiles )+import Distribution.Simple.Utils ( rewriteFile, createDirectoryIfMissingVerbose ) import Distribution.Simple.BuildPaths ( autogenModulesDir ) import Distribution.Simple.Setup ( BuildFlags(buildVerbosity), Flag(..), fromFlag, HaddockFlags(haddockDistPref)) import Distribution.Simple.LocalBuildInfo ( withLibLBI, withTestLBI, LocalBuildInfo(), ComponentLocalBuildInfo(componentPackageDeps) )@@ -17,7 +17,6 @@ generateBuildModule (fromFlag (buildVerbosity flags)) pkg lbi buildHook simpleUserHooks pkg lbi hooks flags , postHaddock = \args flags pkg lbi -> do- copyFiles normal (haddockOutputDir flags pkg) [("images","Hierarchy.png")] postHaddock simpleUserHooks args flags pkg lbi }
bit-array.cabal view
@@ -1,7 +1,7 @@ name: bit-array version:- 0.1.0.1+ 0.1.1 synopsis: A bit array (aka bitset, bitmap, bit vector) API for numeric types description:@@ -47,12 +47,9 @@ exposed-modules: BitArray build-depends:- -- debugging:- loch-th == 0.2.*,- placeholders == 0.1.*, -- general:- numeric-qq >= 0.1.2 && < 0.2,- base >= 4.5 && < 5+ numeric-qq >= 0.1.3 && < 0.2,+ base >= 4.7 && < 5 default-extensions: Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFunctor, DeriveGeneric, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, ImpredicativeTypes, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples default-language:@@ -69,10 +66,10 @@ ghc-options: -threaded build-depends:- doctest == 0.9.*,+ doctest >= 0.9 && < 0.11, directory == 1.2.*,- filepath == 1.3.*,- base+ filepath >= 1.3 && < 1.5,+ base >= 4.5 && < 5 default-extensions: Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFunctor, DeriveGeneric, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, ImpredicativeTypes, LambdaCase, LiberalTypeSynonyms, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators default-language:
library/BitArray.hs view
@@ -8,75 +8,71 @@ -- |--- A @newtype@ wrapper which provides an array-like interface to a type, --- which has instances of 'Bits' and 'Num'.--- +-- A @newtype@ wrapper which provides an array-like interface to a type,+-- which has instances of 'Bits', 'FiniteBits' and 'Num'.+-- -- You can construct bit arrays by wrapping numeric values:--- +-- -- >>> BitArray (7 :: Int8) -- [qq|00000111|]--- +-- -- or directly from numeric literals:--- +-- -- >>> 7 :: BitArray Int8 -- [qq|00000111|]--- --- or using a binary notation quasi-quoter, +--+-- or using a binary notation quasi-quoter, -- assuming you have the @QuasiQuotes@ pragma turned on:--- +-- -- >>> [qq|0111|] :: BitArray Int8 -- [qq|00000111|]--- --- @BitArray@ derives the 'Bits' instance from the base type,--- so it supports all the standard bitwise operations as well.--- --- Note that this library does not support the 'Integer' type,--- since 'Integer' has no implementation of the 'bitSize' function,--- which this library heavily relies on.--- You will get a runtime exception if you use it with 'Integer'.+--+-- @BitArray@ derives the 'Bits' and 'FiniteBits' instances from the base type,+-- so it supports all the standard bitwise operations for fixed-size integral+-- types. newtype BitArray a = BitArray a- deriving (Bounded, Enum, Eq, Integral, Data, Num, Ord, Real, Ix, Generic, - Typeable, Bits)+ deriving (Bounded, Enum, Eq, Integral, Data, Num, Ord, Real, Ix, Generic,+ Typeable, Bits, FiniteBits) --- | +-- | -- Produces a literal of zeros and ones.--- +-- -- >>> show (BitArray (5 :: Int8)) -- "[qq|00000101|]"-instance (Bits a) => Show (BitArray a) where+instance (FiniteBits a) => Show (BitArray a) where show = wrap . toString where wrap = ("[qq|" ++) . (++ "|]") --- | +-- | -- Parses a literal of zeros and ones.--- +-- -- >>> read "[qq|1110|]" :: BitArray Int8 -- [qq|00001110|]--- +-- -- >>> unwrap (read "[qq|1110|]") :: Int -- 14-instance (Bits a, Num a) => Read (BitArray a) where+instance (FiniteBits a) => Read (BitArray a) where readsPrec = const $ ReadP.readP_to_S $ parser where- parser = + parser = BitArray <$> ReadP.string "[qq|" *> Parser.bits <* ReadP.string "|]" -instance (Bits a, Num a) => IsString (BitArray a) where- fromString = +instance (FiniteBits a) => IsString (BitArray a) where+ fromString = fromMaybe (error "Unparsable bit array string") . parseString -- * Constructors and converters ------------------------- -- |--- A binary number quasi-quoter. +-- A binary number quasi-quoter. -- Produces a numeric literal at compile time. -- Can be used to construct both bit arrays and integral numbers.--- +-- -- >>> [qq|011|] :: Int -- 3--- +-- -- >>> [qq|011|] :: BitArray Int8 -- [qq|00000011|] qq = NumericQQ.bin@@ -90,78 +86,78 @@ -- | -- Convert into a binary notation string.--- +-- -- >>> toString (BitArray (5 :: Int8)) -- "00000101"-toString :: (Bits a) => BitArray a -> String+toString :: (FiniteBits a) => BitArray a -> String toString = fmap (\case True -> '1'; False -> '0') . reverse . toBoolList -- | -- Parse a binary notation string.--- +-- -- >>> parseString "123" :: Maybe (BitArray Int8) -- Nothing--- +-- -- >>> parseString "101" :: Maybe (BitArray Int8) -- Just [qq|00000101|]-parseString :: (Bits a, Num a) => String -> Maybe (BitArray a)+parseString :: (FiniteBits a) => String -> Maybe (BitArray a) parseString = fmap fst . listToMaybe . ReadP.readP_to_S Parser.bits -- ** Lists ------------------------- --- | +-- | -- Convert into a list of set bits.--- +-- -- The list is ordered from least significant to most significant bit. {-# INLINABLE toList #-}-toList :: (Bits a, Num a) => BitArray a -> [a]-toList (BitArray w) = - processIndexes [0 .. (pred . bitSize) w]+toList :: (FiniteBits a) => BitArray a -> [a]+toList (BitArray w) =+ processIndexes [0 .. (pred . finiteBitSize) w] where- processIndexes = filter (\w' -> w .&. w' /= 0) . fmap bit+ processIndexes = filter (\w' -> w .&. w' /= zeroBits) . fmap bit -- | Construct from a list of set bits. {-# INLINABLE fromList #-}-fromList :: (Bits a, Num a) => [a] -> BitArray a-fromList = BitArray . inline Foldable.foldr (.|.) 0+fromList :: (FiniteBits a) => [a] -> BitArray a+fromList = BitArray . inline Foldable.foldr (.|.) zeroBits --- | +-- | -- Convert into a list of boolean values, -- which represent the \"set\" flags of each bit.--- +-- -- The list is ordered from least significant to most significant bit. {-# INLINABLE toBoolList #-}-toBoolList :: (Bits a) => BitArray a -> [Bool]-toBoolList (BitArray w) = testBit w <$> [0 .. (pred . bitSize) w]+toBoolList :: (FiniteBits a) => BitArray a -> [Bool]+toBoolList (BitArray w) = testBit w <$> [0 .. (pred . finiteBitSize) w] --- | +-- | -- Construct from a list of boolean flags for the "set" status of each bit.--- +-- -- The list must be ordered from least significant to most significant bit. {-# INLINABLE fromBoolList #-}-fromBoolList :: (Bits a, Num a) => [Bool] -> BitArray a-fromBoolList = inline fromList . fmap (bit . fst) . filter snd . zip [0..]+fromBoolList :: (FiniteBits a) => [Bool] -> BitArray a+fromBoolList = inline fromList . fmap (bit . fst) . filter snd . zip [zeroBits..] -- * Utils ------------------------- -- | Map over the set bits. {-# INLINABLE map #-}-map :: (Bits a, Num a, Bits b, Num b) => (a -> b) -> BitArray a -> BitArray b+map :: (FiniteBits a, FiniteBits b) => (a -> b) -> BitArray a -> BitArray b map f = inline fromList . fmap f . inline toList -- | Perform a right-associative fold over the set bits. {-# INLINABLE foldr #-}-foldr :: (Bits a, Num a) => (a -> b -> b) -> b -> BitArray a -> b+foldr :: (FiniteBits a) => (a -> b -> b) -> b -> BitArray a -> b foldr step init = inline Foldable.foldr step init . inline toList -- | Traverse thru set bits. {-# INLINABLE mapM_ #-}-mapM_ :: (Bits a, Num a, Monad m) => (a -> m b) -> BitArray a -> m ()+mapM_ :: (FiniteBits a, Monad m) => (a -> m b) -> BitArray a -> m () mapM_ f = inline Foldable.mapM_ f . inline toList -- | Traverse thru set bits. {-# INLINABLE traverse_ #-}-traverse_ :: (Bits a, Num a, Applicative f) => (a -> f b) -> BitArray a -> f ()+traverse_ :: (FiniteBits a, Applicative f) => (a -> f b) -> BitArray a -> f () traverse_ f = inline Foldable.traverse_ f . inline toList
library/BitArray/Parser.hs view
@@ -18,6 +18,6 @@ '1' -> return (return index) _ -> return empty -bits :: (Bits a, Num a) => ReadP a-bits = foldr (.|.) 0 . map bit <$> bitIndexes+bits :: (FiniteBits a) => ReadP a+bits = foldr (.|.) zeroBits . map bit <$> bitIndexes
library/BitArray/Prelude.hs view
@@ -1,8 +1,6 @@ module BitArray.Prelude ( module Exports,- bug,- bottom, bool, ) where@@ -50,20 +48,6 @@ import Data.STRef as Exports import Control.Monad.ST as Exports import Debug.Trace as Exports hiding (traceM)---- placeholders---------------------------import Development.Placeholders as Exports---- custom---------------------------import qualified Debug.Trace.LocationTH--bug = [e| $(Debug.Trace.LocationTH.failure) . (msg <>) |]- where- msg = "A \"bit-array\" package bug: " :: String--bottom = [e| $bug "Bottom evaluated" |] bool :: a -> a -> Bool -> a bool f _ False = f