Skip to content

Commit

Permalink
Clean up BitMask
Browse files Browse the repository at this point in the history
  • Loading branch information
bhamiltoncx committed Aug 21, 2015
1 parent 2055e3f commit a2891bd
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions Data/Text/ICU/BitMask.hs
Expand Up @@ -11,42 +11,22 @@ module Data.Text.ICU.BitMask
-- * Functions
, fromBitMask
, highestValueInBitMask
, lowestValueInBitMask
, toBitMask
) where


import Data.Bits ((.&.), (.|.), shiftL)
import Data.Bits ((.&.), (.|.))
import Data.Maybe (listToMaybe)
import Control.Monad (msum, mzero)

class ToBitMask a where
toBitMask :: a -> Int
-- | Using a DefaultSignatures extension to declare a default signature with
-- an `Enum` constraint without affecting the constraints of the class itself.
default toBitMask :: Enum a => a -> Int
toBitMask = shiftL 1 . fromEnum

instance ( ToBitMask a ) => ToBitMask [a] where
toBitMask = foldr (.|.) 0 . map toBitMask

-- | Not making this a typeclass, since it already generalizes over all
-- imaginable instances with help of `MonadPlus`.
fromBitMask ::
( Enum a, Bounded a, ToBitMask a ) =>
Int -> [a]
fromBitMask bm = msum $ map asInBM $ enumFrom minBound where
asInBM a = if isInBitMask bm a then return a else mzero

lowestValueInBitMask ::
( Enum a, Bounded a, ToBitMask a ) =>
Int -> Maybe a
lowestValueInBitMask = listToMaybe . fromBitMask

highestValueInBitMask ::
( Enum a, Bounded a, ToBitMask a ) =>
Int -> Maybe a
highestValueInBitMask = listToMaybe . reverse . fromBitMask

isInBitMask :: ( ToBitMask a ) => Int -> a -> Bool
isInBitMask bm a = let aBM = toBitMask a in aBM == aBM .&. bm
instance (ToBitMask a) => ToBitMask [a] where
toBitMask = foldr ((.|.) . toBitMask) 0

fromBitMask :: (Enum a, Bounded a, ToBitMask a) => Int -> [a]
fromBitMask bm = filter inBitMask $ enumFrom minBound
where inBitMask val = (bm .&. toBitMask val) == toBitMask val

highestValueInBitMask :: (Enum a, Bounded a, ToBitMask a) => Int -> Maybe a
highestValueInBitMask = listToMaybe . reverse . fromBitMask

0 comments on commit a2891bd

Please sign in to comment.