Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Efficient sets over bounded enumerations, using bitwise operations based on
containers
and
EdisonCore.
In many cases, EnumSet
s may be optimised away entirely by constant folding
at compile-time. For example, in the following code:
import Data.Enum.Set.Base as E data Foo = A | B | C | D | E | F | G | H deriving (Bounded, Enum, Eq, Ord, Show) addFoos :: E.EnumSet Word Foo -> E.EnumSet Word Foo addFoos = E.delete A . E.insert B bar :: E.EnumSet Word Foo bar = addFoos $ E.fromFoldable [A, C, E] barHasB :: Bool barHasB = E.member A bar
With -O
or -O2
, bar
will compile to GHC.Types.W# 22##
and
barHasA
will compile to GHC.Types.False
.
For type EnumSet W E
, W
should be a Word
-like type that implements
Bits
and Num
, and E
should be a type that implements Eq
and Enum
equivalently and is a bijection to Int
over its range.
EnumSet W E
can only store a value of E
if the result of applying
fromEnum
to the value is positive and less than the number of bits in W
.
For this reason, it is preferable for E
to be a type that derives Eq
and
Enum
, and for W
to have more bits than the number of constructors of E
.
For type EnumSet W E
, if the highest fromEnum
value of E
is 29,
W
should be Word
, because it always has at least 30 bits.
Otherwise, options include Word32
, Word64
, and the
wide-word package's
Data.WideWord.Word128.
Foreign types may also be used.
Data.Enum.Set provides an alternate type alias that moves the underlying
representation to an associated type token, so that e.g.
EnumSet Word64 MyEnum
is replaced by EnumSet MyEnum
, and reexports this
module with adjusted type signatures.
Note: complexity calculations assume that W
implements Bits
with
constant-time functions, as is the case with Word
etc. If this
is not the case, the complexity of those operations should be added to the
complexity of EnumSet
functions.
Synopsis
- data EnumSet word a
- empty :: forall w a. Bits w => EnumSet w a
- singleton :: forall w a. (Bits w, Enum a) => a -> EnumSet w a
- fromFoldable :: forall f w a. (Foldable f, Bits w, Enum a) => f a -> EnumSet w a
- insert :: forall w a. (Bits w, Enum a) => a -> EnumSet w a -> EnumSet w a
- delete :: forall w a. (Bits w, Enum a) => a -> EnumSet w a -> EnumSet w a
- member :: forall w a. (Bits w, Enum a) => a -> EnumSet w a -> Bool
- notMember :: forall w a. (Bits w, Enum a) => a -> EnumSet w a -> Bool
- null :: forall w a. Bits w => EnumSet w a -> Bool
- size :: forall w a. (Bits w, Num w) => EnumSet w a -> Int
- isSubsetOf :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> Bool
- union :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> EnumSet w a
- difference :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> EnumSet w a
- (\\) :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> EnumSet w a
- symmetricDifference :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> EnumSet w a
- intersection :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> EnumSet w a
- filter :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> Bool) -> EnumSet w a -> EnumSet w a
- partition :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> Bool) -> EnumSet w a -> (EnumSet w a, EnumSet w a)
- map :: forall w a b. (FiniteBits w, Num w, Enum a, Enum b) => (a -> b) -> EnumSet w a -> EnumSet w b
- map' :: forall v w a b. (FiniteBits v, FiniteBits w, Num v, Num w, Enum a, Enum b) => (a -> b) -> EnumSet v a -> EnumSet w b
- foldl :: forall w a b. (FiniteBits w, Num w, Enum a) => (b -> a -> b) -> b -> EnumSet w a -> b
- foldl' :: forall w a b. (FiniteBits w, Num w, Enum a) => (b -> a -> b) -> b -> EnumSet w a -> b
- foldr :: forall w a b. (FiniteBits w, Num w, Enum a) => (a -> b -> b) -> b -> EnumSet w a -> b
- foldr' :: forall w a b. (FiniteBits w, Num w, Enum a) => (a -> b -> b) -> b -> EnumSet w a -> b
- foldl1 :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> a -> a) -> EnumSet w a -> a
- foldl1' :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> a -> a) -> EnumSet w a -> a
- foldr1 :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> a -> a) -> EnumSet w a -> a
- foldr1' :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> a -> a) -> EnumSet w a -> a
- foldMap :: forall m w a. (Monoid m, FiniteBits w, Num w, Enum a) => (a -> m) -> EnumSet w a -> m
- traverse :: forall f w a. (Applicative f, FiniteBits w, Num w, Enum a) => (a -> f a) -> EnumSet w a -> f (EnumSet w a)
- any :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> Bool) -> EnumSet w a -> Bool
- all :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> Bool) -> EnumSet w a -> Bool
- minimum :: forall w a. (FiniteBits w, Num w, Enum a) => EnumSet w a -> a
- maximum :: forall w a. (FiniteBits w, Num w, Enum a) => EnumSet w a -> a
- deleteMin :: forall w a. (FiniteBits w, Num w) => EnumSet w a -> EnumSet w a
- deleteMax :: forall w a. (FiniteBits w, Num w) => EnumSet w a -> EnumSet w a
- minView :: forall w a. (FiniteBits w, Num w, Enum a) => EnumSet w a -> Maybe (a, EnumSet w a)
- maxView :: forall w a. (FiniteBits w, Num w, Enum a) => EnumSet w a -> Maybe (a, EnumSet w a)
- toList :: forall w a. (FiniteBits w, Num w, Enum a) => EnumSet w a -> [a]
- fromRaw :: forall w a. w -> EnumSet w a
- toRaw :: forall w a. EnumSet w a -> w
Set type
A set of values a
with representation word
,
implemented as bitwise operations.
Instances
Construction
fromFoldable :: forall f w a. (Foldable f, Bits w, Enum a) => f a -> EnumSet w a Source #
O(n). Create a set from a finite foldable data structure.
Insertion
insert :: forall w a. (Bits w, Enum a) => a -> EnumSet w a -> EnumSet w a Source #
O(1). Add a value to the set.
Deletion
delete :: forall w a. (Bits w, Enum a) => a -> EnumSet w a -> EnumSet w a Source #
O(1). Delete a value in the set.
Query
member :: forall w a. (Bits w, Enum a) => a -> EnumSet w a -> Bool Source #
O(1). Is the value a member of the set?
notMember :: forall w a. (Bits w, Enum a) => a -> EnumSet w a -> Bool Source #
O(1). Is the value not in the set?
size :: forall w a. (Bits w, Num w) => EnumSet w a -> Int Source #
O(1). The number of elements in the set.
isSubsetOf :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> Bool Source #
O(1). Is this a subset?
(s1 `isSubsetOf` s2)
tells whether s1
is a subset of s2
.
Combine
union :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> EnumSet w a Source #
O(1). The union of two sets.
difference :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> EnumSet w a Source #
O(1). Difference between two sets.
(\\) :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> EnumSet w a infixl 9 Source #
O(1). See difference
.
symmetricDifference :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> EnumSet w a Source #
O(1). Elements which are in either set, but not both.
intersection :: forall w a. Bits w => EnumSet w a -> EnumSet w a -> EnumSet w a Source #
O(1). The intersection of two sets.
Filter
filter :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> Bool) -> EnumSet w a -> EnumSet w a Source #
O(n). Filter all elements that satisfy some predicate.
partition :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> Bool) -> EnumSet w a -> (EnumSet w a, EnumSet w a) Source #
O(n). Partition the set according to some predicate. The first set contains all elements that satisfy the predicate, the second all elements that fail the predicate.
Map
map :: forall w a b. (FiniteBits w, Num w, Enum a, Enum b) => (a -> b) -> EnumSet w a -> EnumSet w b Source #
O(n).
is the set obtained by applying map
f sf
to each element of s
.
It's worth noting that the size of the result may be smaller if,
for some (x,y)
, x /= y && f x == f y
.
map' :: forall v w a b. (FiniteBits v, FiniteBits w, Num v, Num w, Enum a, Enum b) => (a -> b) -> EnumSet v a -> EnumSet w b Source #
O(n). Apply map
while converting the underlying representation of the
set to some other representation.
Folds
foldl :: forall w a b. (FiniteBits w, Num w, Enum a) => (b -> a -> b) -> b -> EnumSet w a -> b Source #
O(n). Left fold.
foldl' :: forall w a b. (FiniteBits w, Num w, Enum a) => (b -> a -> b) -> b -> EnumSet w a -> b Source #
O(n). Left fold with strict accumulator.
foldr :: forall w a b. (FiniteBits w, Num w, Enum a) => (a -> b -> b) -> b -> EnumSet w a -> b Source #
O(n). Right fold.
foldr' :: forall w a b. (FiniteBits w, Num w, Enum a) => (a -> b -> b) -> b -> EnumSet w a -> b Source #
O(n). Right fold with strict accumulator.
foldl1 :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> a -> a) -> EnumSet w a -> a Source #
O(n). Left fold on non-empty sets.
foldl1' :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> a -> a) -> EnumSet w a -> a Source #
O(n). Left fold on non-empty sets with strict accumulator.
foldr1 :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> a -> a) -> EnumSet w a -> a Source #
O(n). Right fold on non-empty sets.
foldr1' :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> a -> a) -> EnumSet w a -> a Source #
O(n). Right fold on non-empty sets with strict accumulator.
Special folds
foldMap :: forall m w a. (Monoid m, FiniteBits w, Num w, Enum a) => (a -> m) -> EnumSet w a -> m Source #
O(n). Map each element of the structure to a monoid, and combine the results.
traverse :: forall f w a. (Applicative f, FiniteBits w, Num w, Enum a) => (a -> f a) -> EnumSet w a -> f (EnumSet w a) Source #
any :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> Bool) -> EnumSet w a -> Bool Source #
O(n). Check if any element satisfies some predicate.
all :: forall w a. (FiniteBits w, Num w, Enum a) => (a -> Bool) -> EnumSet w a -> Bool Source #
O(n). Check if all elements satisfy some predicate.
Min/Max
minimum :: forall w a. (FiniteBits w, Num w, Enum a) => EnumSet w a -> a Source #
O(1). The minimal element of a non-empty set.
maximum :: forall w a. (FiniteBits w, Num w, Enum a) => EnumSet w a -> a Source #
O(1). The maximal element of a non-empty set.
deleteMin :: forall w a. (FiniteBits w, Num w) => EnumSet w a -> EnumSet w a Source #
O(1). Delete the minimal element.
deleteMax :: forall w a. (FiniteBits w, Num w) => EnumSet w a -> EnumSet w a Source #
O(1). Delete the maximal element.
minView :: forall w a. (FiniteBits w, Num w, Enum a) => EnumSet w a -> Maybe (a, EnumSet w a) Source #
O(1). Retrieves the minimal element of the set, and the set stripped of that element, or Nothing if passed an empty set.
maxView :: forall w a. (FiniteBits w, Num w, Enum a) => EnumSet w a -> Maybe (a, EnumSet w a) Source #
O(1). Retrieves the maximal element of the set, and the set stripped of that element, or Nothing if passed an empty set.
Conversion
toList :: forall w a. (FiniteBits w, Num w, Enum a) => EnumSet w a -> [a] Source #
O(n). Convert the set to a list of values.