Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
A simple multiset implementation
All complexities below use m for the number of distinct elements and n for the total number of elements.
- data Multiset v
- null :: Multiset v -> Bool
- size :: Multiset v -> Int
- distinctSize :: Multiset v -> Int
- empty :: Multiset v
- singleton :: Ord v => v -> Multiset v
- fromMap :: (Integral a, Ord v) => Map v a -> Multiset v
- fromMap' :: (Integral a, Ord v) => Map v a -> Maybe (Multiset v)
- fromList :: Ord v => [v] -> Multiset v
- fromCountsList :: (Integral a, Ord v) => [(v, a)] -> Multiset v
- fromCountsList' :: (Integral a, Ord v) => [(v, a)] -> Maybe (Multiset v)
- member :: Ord v => v -> Multiset v -> Bool
- notMember :: Ord v => v -> Multiset v -> Bool
- (!) :: Ord v => Multiset v -> v -> Int
- count :: Ord v => v -> Multiset v -> Int
- incr :: Ord v => Int -> v -> Multiset v -> Multiset v
- incr' :: Ord v => Int -> v -> Multiset v -> Maybe (Multiset v)
- insert :: Ord v => v -> Multiset v -> Multiset v
- remove :: Ord v => v -> Multiset v -> Multiset v
- remove' :: Ord v => v -> Multiset v -> Maybe (Multiset v)
- filter :: Ord v => (v -> Bool) -> Multiset v -> Multiset v
- map :: (Ord v1, Ord v2) => (v1 -> v2) -> Multiset v1 -> Multiset v2
- mapCounts :: Ord v => (Int -> Int) -> Multiset v -> Multiset v
- max :: Ord v => Multiset v -> Multiset v -> Multiset v
- min :: Ord v => Multiset v -> Multiset v -> Multiset v
- sum :: Ord v => Multiset v -> Multiset v -> Multiset v
- unionWith :: Ord v => (Int -> Int -> Int) -> Multiset v -> Multiset v -> Multiset v
- difference :: Ord v => Multiset v -> Multiset v -> Multiset v
- intersectionWith :: Ord v => (Int -> Int -> Int) -> Multiset v -> Multiset v -> Multiset v
- toList :: Multiset v -> [v]
- toCountsList :: Multiset v -> [(v, Int)]
- toAscCountsList :: Multiset v -> [(v, Int)]
- toDescCountsList :: Multiset v -> [(v, Int)]
- elems :: Multiset v -> Set v
Documentation
A multiset
Tests
size :: Multiset v -> Int Source #
The total number of elements in the multiset.
O(m) Note that this isn't the number of distinct elements,
distinctSize
provides it.
distinctSize :: Multiset v -> Int Source #
O(1) The number of distinct elements in the multiset.
Construction
fromMap :: (Integral a, Ord v) => Map v a -> Multiset v Source #
O(m * log m) Build a multiset from a map.
Negative counts are ignored; see fromMap'
for a stricter version.
fromMap' :: (Integral a, Ord v) => Map v a -> Maybe (Multiset v) Source #
O(m * log m) Build a multiset from a map.
If at least one of the counts is negative, this method will return
Nothing
.
fromCountsList :: (Integral a, Ord v) => [(v, a)] -> Multiset v Source #
O(m * log m) Build a multiset from a list of counts.
Counts of duplicate entries are added together.
fromCountsList' :: (Integral a, Ord v) => [(v, a)] -> Maybe (Multiset v) Source #
O(m * log m) Build a multiset from a list of counts.
Counts of duplicate entries are added together. Returns Nothing
if the
total count for any element is negative.
Accessors
member :: Ord v => v -> Multiset v -> Bool Source #
O(log m) Whether the element is present at least once.
count :: Ord v => v -> Multiset v -> Int Source #
O(1) The number of times the element is present in the multiset.
0 if absent.
Update
incr :: Ord v => Int -> v -> Multiset v -> Multiset v Source #
O(log m) Increment the count of element.
The increment can be negative (removing elements). Resulting negative counts
are considered 0 (see incr'
for a stricter implementation)..
incr' :: Ord v => Int -> v -> Multiset v -> Maybe (Multiset v) Source #
O(log m) Increment the count of element.
Resulting negative counts are considered 0.
remove :: Ord v => v -> Multiset v -> Multiset v Source #
O(log m) Remove a single element. Does nothing if the element isn't present.
remove' :: Ord v => v -> Multiset v -> Maybe (Multiset v) Source #
Remove a single element. Returns Nothing
if the element wasn't already in.
map :: (Ord v1, Ord v2) => (v1 -> v2) -> Multiset v1 -> Multiset v2 Source #
Map on the multiset's values.
mapCounts :: Ord v => (Int -> Int) -> Multiset v -> Multiset v Source #
Map on the multiset's counts.
Combination
max :: Ord v => Multiset v -> Multiset v -> Multiset v Source #
Convenience methods to get the max of two multisets.
min :: Ord v => Multiset v -> Multiset v -> Multiset v Source #
Convenience methods to get the min of two multisets.
sum :: Ord v => Multiset v -> Multiset v -> Multiset v Source #
Convenience methods to get the sum of two multisets.
unionWith :: Ord v => (Int -> Int -> Int) -> Multiset v -> Multiset v -> Multiset v Source #
Generic union method.
difference :: Ord v => Multiset v -> Multiset v -> Multiset v Source #
The first set minus the second. Resulting negative counts are ignored.
intersectionWith :: Ord v => (Int -> Int -> Int) -> Multiset v -> Multiset v -> Multiset v Source #
Generic intersection method.
toList :: Multiset v -> [v] Source #
Convert the multiset to a list; elements will be repeated according to their count.
toCountsList :: Multiset v -> [(v, Int)] Source #
toAscCountsList :: Multiset v -> [(v, Int)] Source #
toDescCountsList :: Multiset v -> [(v, Int)] Source #