haskus-binary-1.5: Haskus binary format manipulation

Safe HaskellNone
LanguageHaskell2010

Haskus.Binary.BitSet

Description

A bit set based on Enum to name the bits. Use bitwise operations and minimal storage in a safer way.

Similar to Data.Bitset.Generic from bitset package, but

  • We don't have the Num constraint
  • We dont use the deprecated bitSize function
  • We use countTrailingZeros instead of iterating on the number of bits
  • We add a typeclass BitOffset

Example:

{--}
data Flag
   = FlagXXX
   | FlagYYY
   | FlagWWW
   deriving (Show,Eq,Enum,BitOffset)

-- Adapt the backing type, here we choose Word16
type Flags = BitSet Word16 Flag

Then you can convert (for free) a Word16 into Flags with fromBits and convert back with toBits.

You can check if a flag is set or not with member and notMember and get a list of set flags with toList. You can insert or delete flags. You can also perform set operations such as union and intersection.

Synopsis

Documentation

data BitSet b a Source #

A bit set: use bitwise operations (fast!) and minimal storage (sizeOf basetype)

  • b is the base type (Bits b)
  • a is the element type (Enum a)

The elements in the Enum a are flags corresponding to each bit of b starting from the least-significant bit.

Instances
(FiniteBits b, IndexableBits b, BitOffset a, Eq b) => IsList (BitSet b a) Source # 
Instance details

Defined in Haskus.Binary.BitSet

Associated Types

type Item (BitSet b a) :: Type #

Methods

fromList :: [Item (BitSet b a)] -> BitSet b a #

fromListN :: Int -> [Item (BitSet b a)] -> BitSet b a #

toList :: BitSet b a -> [Item (BitSet b a)] #

Eq b => Eq (BitSet b a) Source # 
Instance details

Defined in Haskus.Binary.BitSet

Methods

(==) :: BitSet b a -> BitSet b a -> Bool #

(/=) :: BitSet b a -> BitSet b a -> Bool #

Ord b => Ord (BitSet b a) Source # 
Instance details

Defined in Haskus.Binary.BitSet

Methods

compare :: BitSet b a -> BitSet b a -> Ordering #

(<) :: BitSet b a -> BitSet b a -> Bool #

(<=) :: BitSet b a -> BitSet b a -> Bool #

(>) :: BitSet b a -> BitSet b a -> Bool #

(>=) :: BitSet b a -> BitSet b a -> Bool #

max :: BitSet b a -> BitSet b a -> BitSet b a #

min :: BitSet b a -> BitSet b a -> BitSet b a #

(Show a, BitOffset a, FiniteBits b, IndexableBits b, Eq b) => Show (BitSet b a) Source # 
Instance details

Defined in Haskus.Binary.BitSet

Methods

showsPrec :: Int -> BitSet b a -> ShowS #

show :: BitSet b a -> String #

showList :: [BitSet b a] -> ShowS #

Storable b => Storable (BitSet b a) Source # 
Instance details

Defined in Haskus.Binary.BitSet

Methods

peekIO :: Ptr (BitSet b a) -> IO (BitSet b a) Source #

pokeIO :: Ptr (BitSet b a) -> BitSet b a -> IO () Source #

alignment :: BitSet b a -> Word Source #

sizeOf :: BitSet b a -> Word Source #

(FiniteBits b, Integral b, BitOffset a) => Field (BitSet b a) Source # 
Instance details

Defined in Haskus.Binary.BitField

Methods

fromField :: Integral b0 => BitSet b a -> b0

toField :: Integral b0 => b0 -> BitSet b a

type Item (BitSet b a) Source # 
Instance details

Defined in Haskus.Binary.BitSet

type Item (BitSet b a) = a

class BitOffset a where Source #

Bit set indexed with a

Minimal complete definition

Nothing

Methods

toBitOffset :: a -> Word Source #

Return the bit offset of an element

toBitOffset :: Enum a => a -> Word Source #

Return the bit offset of an element

fromBitOffset :: Word -> a Source #

Return the value associated with a bit offset

fromBitOffset :: Enum a => Word -> a Source #

Return the value associated with a bit offset

Instances
BitOffset Int Source #

It can be useful to get the indexes of the set bits

Instance details

Defined in Haskus.Binary.BitSet

BitOffset Word Source #

It can be useful to get the indexes of the set bits

Instance details

Defined in Haskus.Binary.BitSet

null :: (FiniteBits b, Eq b) => BitSet b a -> Bool Source #

Indicate if the set is empty

empty :: FiniteBits b => BitSet b a Source #

Empty bitset

singleton :: (IndexableBits b, BitOffset a) => a -> BitSet b a Source #

Create a BitSet from a single element

insert :: (IndexableBits b, BitOffset a) => BitSet b a -> a -> BitSet b a Source #

Insert an element in the set

delete :: (IndexableBits b, BitOffset a) => BitSet b a -> a -> BitSet b a Source #

Remove an element from the set

toBits :: BitSet b a -> b Source #

Unwrap the bitset

fromBits :: (BitOffset a, FiniteBits b) => b -> BitSet b a Source #

Wrap a bitset

member :: (BitOffset a, FiniteBits b, IndexableBits b) => BitSet b a -> a -> Bool Source #

Test if an element is in the set

elem :: (BitOffset a, FiniteBits b, IndexableBits b) => a -> BitSet b a -> Bool Source #

Test if an element is in the set

notMember :: (BitOffset a, FiniteBits b, IndexableBits b) => BitSet b a -> a -> Bool Source #

Test if an element is not in the set

elems :: (BitOffset a, FiniteBits b, IndexableBits b, Eq b) => BitSet b a -> [a] Source #

Retrieve elements in the set

intersection :: (FiniteBits b, Bitwise b) => BitSet b a -> BitSet b a -> BitSet b a Source #

Intersection of two sets

union :: (FiniteBits b, Bitwise b) => BitSet b a -> BitSet b a -> BitSet b a Source #

Intersection of two sets

unions :: (FiniteBits b, Bitwise b) => [BitSet b a] -> BitSet b a Source #

Intersection of several sets

fromListToBits :: (BitOffset a, FiniteBits b, IndexableBits b, Foldable m) => m a -> b Source #

Convert a list of enum elements into a bitset Warning: b must have enough bits to store the given elements! (we don't perform any check, for performance reason)

toListFromBits :: (BitOffset a, FiniteBits b, IndexableBits b, Eq b) => b -> [a] Source #

Convert a bitset into a list of Enum elements

enumerateSetBits :: (BitOffset a, FiniteBits b, IndexableBits b, Eq b, Bounded a, Enum a) => b -> [a] Source #

Convert a bitset into a list of Enum elements by testing the Enum values successively.

The difference with toListFromBits is that extra values in the BitSet will be ignored.

fromList :: (BitOffset a, IndexableBits b, FiniteBits b, Foldable m) => m a -> BitSet b a Source #

Convert a Foldable into a set

toList :: (BitOffset a, FiniteBits b, IndexableBits b, Eq b) => BitSet b a -> [a] Source #

Convert a set into a list