diff options
author | Ben Gamari <bgamari.foss@gmail.com> | 2017-03-15 14:30:33 -0400 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2017-03-15 15:23:22 -0400 |
commit | cc9d574a578090d17d1597628e44371003cb19a7 (patch) | |
tree | e93ecdb8e419e17c28003d9e15e78fe055691b45 /compiler/utils/EnumSet.hs | |
parent | a7be163196f452530b61cbb526631db946d20e8b (diff) | |
download | haskell-cc9d574a578090d17d1597628e44371003cb19a7.tar.gz |
Introduce and use EnumSet in DynFlags
This factors out a repeated pattern found in DynFlags, where we use an
IntSet and Enum to represent sets of flags.
Requires bump of haddock submodule.
Test Plan: validate
Reviewers: austin, goldfire
Subscribers: rwbarton, thomie, snowleopard
Differential Revision: https://phabricator.haskell.org/D3331
Diffstat (limited to 'compiler/utils/EnumSet.hs')
-rw-r--r-- | compiler/utils/EnumSet.hs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/compiler/utils/EnumSet.hs b/compiler/utils/EnumSet.hs new file mode 100644 index 0000000000..aa36b788aa --- /dev/null +++ b/compiler/utils/EnumSet.hs @@ -0,0 +1,33 @@ +-- | An tiny wrapper around 'IntSet.IntSet' for representing sets of 'Enum' +-- things. +module EnumSet + ( EnumSet + , member + , insert + , delete + , toList + , fromList + , empty + ) where + +import qualified Data.IntSet as IntSet + +newtype EnumSet a = EnumSet IntSet.IntSet + +member :: Enum a => a -> EnumSet a -> Bool +member x (EnumSet s) = IntSet.member (fromEnum x) s + +insert :: Enum a => a -> EnumSet a -> EnumSet a +insert x (EnumSet s) = EnumSet $ IntSet.insert (fromEnum x) s + +delete :: Enum a => a -> EnumSet a -> EnumSet a +delete x (EnumSet s) = EnumSet $ IntSet.delete (fromEnum x) s + +toList :: Enum a => EnumSet a -> [a] +toList (EnumSet s) = map toEnum $ IntSet.toList s + +fromList :: Enum a => [a] -> EnumSet a +fromList = EnumSet . IntSet.fromList . map fromEnum + +empty :: EnumSet a +empty = EnumSet IntSet.empty |