data-flagset-1.0.0.0: An efficient data type for sets of flags

Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.FlagSet

Contents

Description

Introduction

This module provides a data type for a set of flags. Flags are stored efficiently as bits of an unsigned integer.

This module is meant to be imported qualified:

import qualified Data.FlagSet as FlagSet
import Data.FlagSet (FlagSet)

The API is basically the same as that of Data.Set from the containers package.

The functions fromList and member also have aliases that can be used unqualified:

import Data.FlagSet (flags, hasFlag)

Examples

import qualified Data.FlagSet as FlagSet
import Data.FlagSet (FlagSet, flags, hasFlag)

data WeekDay = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
     deriving (Eq, Show, Enum, Bounded)

workDays = flags [Monday .. Friday]
weekEndDays = flags [Saturday, Sunday]
allDays = FlagSet.full

worksOnTheWeekEnd :: FlagSet WeekDay -> Bool
worksOnTheWeekEnd = not . FlagSet.null . FlagSet.intersection weekEndDays

worksOnSunday :: FlagSet WeekDay -> Bool
worksOnSunday = hasFlag Sunday

workDayPay :: Rational
workDayPay = ...
weekEndDayPay :: Rational
weekEndDayPay = ...

pay :: FlagSet WeekDay -> Rational
pay daysWorking = workDayPay * countDays workDays + weekEndDayPay * countDays weekEndDays
    where
        countDays = fromIntegral . FlagSet.size . FlagSet.intersection daysWorking

Synopsis

Types

data FlagSet a Source

A set of flags.

For most functions operating on flag sets, there must be an Enum instance for a. Some functions also require Bounded.

You must ensure that fromEnum only returns values in the range [0 .. 31], otherwise an error can occur.

In the Monoid instance, mempty is empty and mappend/<> is union.

Instances

Eq (FlagSet a) 
Data a => Data (FlagSet a) 
Ord (FlagSet a) 
(Show a, Enum a) => Show (FlagSet a) 
Monoid (FlagSet a) 
Typeable (* -> *) FlagSet 

Construction

fromList :: Enum a => [a] -> FlagSet a Source

Create a flag set from a list of flags. Input list can contain duplicates.

flags :: Enum a => [a] -> FlagSet a Source

Alias for fromList that can be imported unqualified.

empty :: FlagSet a Source

The empty flag set.

singleton :: Enum a => a -> FlagSet a Source

A flag set containing a single value.

full :: (Enum a, Bounded a) => FlagSet a Source

The flag set that contains every value.

Modification

insert :: Enum a => a -> FlagSet a -> FlagSet a Source

Insert a value into a flag set.

delete :: Enum a => a -> FlagSet a -> FlagSet a Source

Remove a value from a flag set.

union :: FlagSet a -> FlagSet a -> FlagSet a Source

The union of two flag sets.

unions :: [FlagSet a] -> FlagSet a Source

The union of multiple flag sets.

difference :: FlagSet a -> FlagSet a -> FlagSet a Source

The difference of two flag sets.

intersection :: FlagSet a -> FlagSet a -> FlagSet a Source

The intersection of two flag sets.

Tests

member :: Enum a => a -> FlagSet a -> Bool Source

Test whether a flag set contains a value.

hasFlag :: Enum a => a -> FlagSet a -> Bool Source

Alias for member that can be imported unqualified.

null :: FlagSet a -> Bool Source

Test whether a flag set is empty.

Other

toList :: Enum a => FlagSet a -> [a] Source

Convert a flag set to a list of values. The values will be ordered according to the order defined by fromEnum and there will not be any duplicates.

size :: FlagSet a -> Int Source

The number of values in a flag set.