-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Various bit twiddling and bitwise serialization primitives -- @package bits @version 0.4 -- | Calculate a number of fiddly bit operations using fast de Bruijn -- multiplication tables. module Data.Bits.Extras class (Num t, FiniteBits t) => Ranked t where lsb n = rank n - 1 rank 0 = 0 rank n = lsb n + 1 lsb :: Ranked t => t -> Int rank :: Ranked t => t -> Int nlz :: Ranked t => t -> Int log2 :: Word32 -> Int -- | Calculate the most significant set bit. msb :: Ranked t => t -> Int w8 :: Integral a => a -> Word8 w16 :: Integral a => a -> Word16 w32 :: Integral a => a -> Word32 w64 :: Integral a => a -> Word64 assignBit :: Bits b => b -> Int -> Bool -> b -- | zeroBits is the value with all bits unset. -- -- The following laws ought to hold (for all valid bit indices -- n): -- --
clearBit zeroBits n == -- zeroBits
setBit zeroBits n == bit -- n
testBit zeroBits n == False
popCount zeroBits == 0
-- putBitsFrom from b = putBits from 0 b --putBitsFrom :: (MonadPut m, Bits b) => Int -> b -> Coding m () instance MonadPut m => MonadPut (Coding m) instance MonadGet m => MonadGet (Coding m) instance MonadReader e m => MonadReader e (Coding m) instance MonadState s m => MonadState s (Coding m) instance MonadTrans Coding instance MonadPlus m => MonadPlus (Coding m) instance (Monad m, Alternative m) => Alternative (Coding m) instance Monad m => Monad (Coding m) instance Monad m => Applicative (Coding m) instance Functor (Coding m) module Data.Bits.Coded -- | Unaligned codes class Coded c where encodeMany = traverse_ encode encode :: (Coded c, MonadPut m) => c -> Coding m () encodeMany :: (Coded c, MonadPut m, Foldable t) => t c -> Coding m () decode :: (Coded c, MonadGet m) => Coding m c -- | Unary-coded integers -- --
-- >>> runPutL . runEncode $ encode (Unary 1) >> flush -- "\128" -- -- >>> runPutL . runEncode $ encode (Unary 7) >> flush -- "\254" --newtype Unary n Unary :: n -> Unary n unUnary :: Unary n -> n -- | Representation for Elias Gamma and Delta codes. A -- positive integer n is encoded by encoding the position of its -- most significant bit, and then the binary representation of the rest -- of the number. newtype Elias c n Elias :: n -> Elias c n unElias :: Elias c n -> n -- | Elias Gamma codes the position of the most significant in -- Unary. type Gamma c = Elias (Unary c) -- | Elias Delta codes the position of the most significant bit in Elias -- Gamma. type Delta c n = Elias (Gamma c n) n runEncode :: MonadPut m => Coding m () -> m () runDecode :: MonadGet m => Coding m a -> m a instance Eq n => Eq (Unary n) instance Ord n => Ord (Unary n) instance Read n => Read (Unary n) instance Show n => Show (Unary n) instance Num n => Num (Unary n) instance Integral n => Integral (Unary n) instance Real n => Real (Unary n) instance Enum n => Enum (Unary n) instance Eq n => Eq (Elias c n) instance Ord n => Ord (Elias c n) instance Read n => Read (Elias c n) instance Show n => Show (Elias c n) instance Num n => Num (Elias c n) instance Real n => Real (Elias c n) instance Integral n => Integral (Elias c n) instance Enum n => Enum (Elias c n) instance (Coded c, Integral c, Ranked n) => Coded (Elias c n) instance Integral n => Coded (Unary n)