Safe Haskell | None |
---|---|
Language | Haskell2010 |
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 CBitSet
Example:
{--}
data Flag
= FlagXXX
| FlagYYY
| FlagWWW
deriving (Show,Eq,Enum,CBitSet)
-- 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
.
- data BitSet b a
- class CBitSet a where
- null :: FiniteBits b => BitSet b a -> Bool
- empty :: FiniteBits b => BitSet b a
- singleton :: (Bits b, CBitSet a) => a -> BitSet b a
- insert :: (Bits b, CBitSet a) => BitSet b a -> a -> BitSet b a
- delete :: (Bits b, CBitSet a) => BitSet b a -> a -> BitSet b a
- toBits :: BitSet b a -> b
- fromBits :: (CBitSet a, FiniteBits b) => b -> BitSet b a
- member :: (CBitSet a, FiniteBits b) => BitSet b a -> a -> Bool
- elem :: (CBitSet a, FiniteBits b) => a -> BitSet b a -> Bool
- notMember :: (CBitSet a, FiniteBits b) => BitSet b a -> a -> Bool
- elems :: (CBitSet a, FiniteBits b) => BitSet b a -> [a]
- intersection :: FiniteBits b => BitSet b a -> BitSet b a -> BitSet b a
- union :: FiniteBits b => BitSet b a -> BitSet b a -> BitSet b a
- unions :: FiniteBits b => [BitSet b a] -> BitSet b a
- fromListToBits :: (CBitSet a, FiniteBits b, Foldable m) => m a -> b
- toListFromBits :: (CBitSet a, FiniteBits b) => b -> [a]
- fromList :: (CBitSet a, FiniteBits b, Foldable m) => m a -> BitSet b a
- toList :: (CBitSet a, FiniteBits b) => BitSet b a -> [a]
Documentation
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.
(FiniteBits b, CBitSet a) => IsList (BitSet b a) Source # | |
Eq b => Eq (BitSet b a) Source # | |
Ord b => Ord (BitSet b a) Source # | |
(Show a, CBitSet a, FiniteBits b) => Show (BitSet b a) Source # | |
Storable b => Storable (BitSet b a) Source # | |
(FiniteBits b, Integral b, CBitSet a) => Field (BitSet b a) Source # | |
type Item (BitSet b a) Source # | |
class CBitSet a where Source #
Bit set indexed with a
toBitOffset :: a -> Int Source #
Return the bit offset of an element
toBitOffset :: Enum a => a -> Int Source #
Return the bit offset of an element
fromBitOffset :: Int -> a Source #
Return the value associated with a bit offset
fromBitOffset :: Enum a => Int -> a Source #
Return the value associated with a bit offset
empty :: FiniteBits b => BitSet b a Source #
Empty bitset
delete :: (Bits b, CBitSet a) => BitSet b a -> a -> BitSet b a Source #
Remove an element from the set
member :: (CBitSet a, FiniteBits b) => BitSet b a -> a -> Bool Source #
Test if an element is in the set
elem :: (CBitSet a, FiniteBits b) => a -> BitSet b a -> Bool Source #
Test if an element is in the set
notMember :: (CBitSet a, FiniteBits b) => BitSet b a -> a -> Bool Source #
Test if an element is not in the set
intersection :: FiniteBits b => BitSet b a -> BitSet b a -> BitSet b a Source #
Intersection of two sets
fromListToBits :: (CBitSet a, FiniteBits 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 :: (CBitSet a, FiniteBits b) => b -> [a] Source #
Convert a bitset into a list of Enum elements