Skip to content

jnbooth/bitwise-enum

Repository files navigation

bitwise-enum

Efficient sets over bounded enumerations, using bitwise operations based on containers and EdisonCore. All operations apart from folds are constant-time. In many cases, the compiler may use constant folding to optimize EnumSets away entirely. For example, in the following code:

import Data.Enum.Set as E

data Foo = A | B | C | D | E | F | G | H deriving (Bounded, Enum, Eq, Ord)

instance E.AsEnumSet Foo

addFoos :: E.EnumSet Foo -> E.EnumSet Foo
addFoos = E.delete A . E.insert B

bar :: E.EnumSet Foo
bar = addFoos $ E.fromFoldable [A, C, E]

barHasA :: Bool
barHasA = E.member A bar

With -O or -O2 , bar will compile to GHC.Types.W# 22## and barHasA will compile to GHC.Types.False.

By default, Words are used as the representation. Other representations may be chosen in the class instance:

{-# LANGUAGE TypeFamilies #-}

import Data.Enum.Set as E
import Data.Word (Word64)

data Foo = A | B | C | D | E | F | G | H deriving (Bounded, Enum, Eq, Ord, Show)

instance E.AsEnumSet Foo where
    type EnumSetRep Foo = Word64

About

Bitwise operations on bounded enumerations.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published