module HaskellWorks.Data.Bits.BitLength
(
BitLength(..)
, elemBitLength
, elemBitEnd
) where
import qualified Data.Vector as DV
import qualified Data.Vector.Storable as DVS
import Data.Word
import HaskellWorks.Data.AtIndex
import HaskellWorks.Data.Naive
import HaskellWorks.Data.Positioning
import Prelude hiding (length)
class BitLength v where
bitLength :: v -> Count
endPosition :: v -> Position
endPosition = toPosition . bitLength
elemBitLength :: (AtIndex v, BitLength (Elem v)) => v -> Count
elemBitLength v = bitLength (v !!! 0)
elemBitEnd :: (AtIndex v, BitLength (Elem v)) => v -> Position
elemBitEnd v = endPosition (v !!! 0)
instance BitLength Bool where
bitLength _ = 1
instance BitLength [Bool] where
bitLength = fromIntegral . length
instance BitLength Word8 where
bitLength _ = 8
instance BitLength Word16 where
bitLength _ = 16
instance BitLength Word32 where
bitLength _ = 32
instance BitLength Word64 where
bitLength _ = 64
instance BitLength (Naive Word8) where
bitLength _ = 8
instance BitLength (Naive Word16) where
bitLength _ = 16
instance BitLength (Naive Word32) where
bitLength _ = 32
instance BitLength (Naive Word64) where
bitLength _ = 64
instance BitLength [Word8] where
bitLength v = fromIntegral (length v) * bitLength (head v)
instance BitLength [Word16] where
bitLength v = fromIntegral (length v) * bitLength (head v)
instance BitLength [Word32] where
bitLength v = fromIntegral (length v) * bitLength (head v)
instance BitLength [Word64] where
bitLength v = fromIntegral (length v) * bitLength (head v)
instance BitLength (DV.Vector Word8) where
bitLength v = length v * bitLength (v !!! 0)
instance BitLength (DV.Vector Word16) where
bitLength v = length v * bitLength (v !!! 0)
instance BitLength (DV.Vector Word32) where
bitLength v = length v * bitLength (v !!! 0)
instance BitLength (DV.Vector Word64) where
bitLength v = length v * bitLength (v !!! 0)
instance BitLength (DVS.Vector Word8) where
bitLength v = length v * bitLength (v !!! 0)
instance BitLength (DVS.Vector Word16) where
bitLength v = length v * bitLength (v !!! 0)
instance BitLength (DVS.Vector Word32) where
bitLength v = length v * bitLength (v !!! 0)
instance BitLength (DVS.Vector Word64) where
bitLength v = length v * bitLength (v !!! 0)