Maintainer | Ziyang Liu <free@cofree.io> |
---|---|
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Multimaps, where values behave like (non empty) sets.
Multimaps whose values behave like lists are in Data.Multimap. Multimaps whose values behave like maps (i.e., two-dimensional tables) are in Data.Multimap.Table.
The implementation is backed by a
. The
differences between Map
k (Set
a)
and Multimap
k a
include:Map
k (Set
a)
- A key is only present in a
SetMultimap
if it is associated with at least one values, i.e., a key is never associated with an empty set of values. lookup
(or!
) returns a possibly empty set. Unlike regular maps, the!
operator is total for multimaps.- Functions like
map
,adjust
,update
, etc., take functions on individual values (e.g.,a -> b
) as opposed to, e.g.,
.Set
a ->Set
b union
andunions
union the values when there are duplicate keys, rather than being left- or right-biased.- The
difference
function computes set differences for values of keys that exist in both maps. - The
size
function returns the total number of values for all keys in the multimap, not the number of distinct keys. The latter can be obtained by first getting thekeysSet
or first converting the multimap to a regular map viatoMap
.
In the following Big-O notations, unless otherwise noted, n denotes the size of the multimap, k denotes the number of distinct keys, and m denotes the maximum number of values associated with a single key.
Synopsis
- data SetMultimap k a
- empty :: SetMultimap k a
- singleton :: k -> a -> SetMultimap k a
- fromMap :: Map k (Set a) -> SetMultimap k a
- fromList :: (Ord k, Ord a) => [(k, a)] -> SetMultimap k a
- insert :: (Ord k, Ord a) => k -> a -> SetMultimap k a -> SetMultimap k a
- delete :: Ord k => k -> SetMultimap k a -> SetMultimap k a
- deleteWithValue :: (Ord k, Ord a) => k -> a -> SetMultimap k a -> SetMultimap k a
- deleteMax :: Ord k => k -> SetMultimap k a -> SetMultimap k a
- deleteMin :: Ord k => k -> SetMultimap k a -> SetMultimap k a
- adjust :: (Ord k, Ord a) => (a -> a) -> k -> SetMultimap k a -> SetMultimap k a
- adjustWithKey :: (Ord k, Ord a) => (k -> a -> a) -> k -> SetMultimap k a -> SetMultimap k a
- update :: (Ord k, Ord a) => (a -> Maybe a) -> k -> SetMultimap k a -> SetMultimap k a
- updateWithKey :: (Ord k, Ord a) => (k -> a -> Maybe a) -> k -> SetMultimap k a -> SetMultimap k a
- alter :: Ord k => (Set a -> Set a) -> k -> SetMultimap k a -> SetMultimap k a
- alterWithKey :: Ord k => (k -> Set a -> Set a) -> k -> SetMultimap k a -> SetMultimap k a
- lookup :: Ord k => k -> SetMultimap k a -> Set a
- (!) :: Ord k => SetMultimap k a -> k -> Set a
- member :: Ord k => k -> SetMultimap k a -> Bool
- notMember :: Ord k => k -> SetMultimap k a -> Bool
- null :: SetMultimap k a -> Bool
- notNull :: SetMultimap k a -> Bool
- size :: SetMultimap k a -> Int
- union :: (Ord k, Ord a) => SetMultimap k a -> SetMultimap k a -> SetMultimap k a
- unions :: (Foldable f, Ord k, Ord a) => f (SetMultimap k a) -> SetMultimap k a
- difference :: (Ord k, Ord a) => SetMultimap k a -> SetMultimap k a -> SetMultimap k a
- map :: Ord b => (a -> b) -> SetMultimap k a -> SetMultimap k b
- mapWithKey :: Ord b => (k -> a -> b) -> SetMultimap k a -> SetMultimap k b
- foldr :: (a -> b -> b) -> b -> SetMultimap k a -> b
- foldl :: (a -> b -> a) -> a -> SetMultimap k b -> a
- foldrWithKey :: (k -> a -> b -> b) -> b -> SetMultimap k a -> b
- foldlWithKey :: (a -> k -> b -> a) -> a -> SetMultimap k b -> a
- foldMapWithKey :: Monoid m => (k -> a -> m) -> SetMultimap k a -> m
- foldr' :: (a -> b -> b) -> b -> SetMultimap k a -> b
- foldl' :: (a -> b -> a) -> a -> SetMultimap k b -> a
- foldrWithKey' :: (k -> a -> b -> b) -> b -> SetMultimap k a -> b
- foldlWithKey' :: (a -> k -> b -> a) -> a -> SetMultimap k b -> a
- elems :: SetMultimap k a -> [a]
- keys :: SetMultimap k a -> [k]
- assocs :: SetMultimap k a -> [(k, a)]
- keysSet :: SetMultimap k a -> Set k
- toList :: SetMultimap k a -> [(k, a)]
- toAscList :: SetMultimap k a -> [(k, a)]
- toDescList :: SetMultimap k a -> [(k, a)]
- toMap :: SetMultimap k a -> Map k (Set a)
- fromMultimap :: Ord a => Multimap k a -> SetMultimap k a
- toMultimapAsc :: SetMultimap k a -> Multimap k a
- toMultimapDesc :: SetMultimap k a -> Multimap k a
- filter :: (a -> Bool) -> SetMultimap k a -> SetMultimap k a
- filterWithKey :: (k -> a -> Bool) -> SetMultimap k a -> SetMultimap k a
- filterKey :: (k -> Bool) -> SetMultimap k a -> SetMultimap k a
- filterM :: (Ord k, Ord a, Applicative t) => (a -> t Bool) -> SetMultimap k a -> t (SetMultimap k a)
- filterWithKeyM :: (Ord k, Ord a, Applicative t) => (k -> a -> t Bool) -> SetMultimap k a -> t (SetMultimap k a)
- mapMaybe :: Ord b => (a -> Maybe b) -> SetMultimap k a -> SetMultimap k b
- mapMaybeWithKey :: Ord b => (k -> a -> Maybe b) -> SetMultimap k a -> SetMultimap k b
- mapEither :: (Ord b, Ord c) => (a -> Either b c) -> SetMultimap k a -> (SetMultimap k b, SetMultimap k c)
- mapEitherWithKey :: (Ord b, Ord c) => (k -> a -> Either b c) -> SetMultimap k a -> (SetMultimap k b, SetMultimap k c)
- lookupMin :: SetMultimap k a -> Maybe (k, Set a)
- lookupMax :: SetMultimap k a -> Maybe (k, Set a)
- lookupLT :: Ord k => k -> SetMultimap k a -> Maybe (k, Set a)
- lookupGT :: Ord k => k -> SetMultimap k a -> Maybe (k, Set a)
- lookupLE :: Ord k => k -> SetMultimap k a -> Maybe (k, Set a)
- lookupGE :: Ord k => k -> SetMultimap k a -> Maybe (k, Set a)
Multimap type
data SetMultimap k a Source #
Instances
Construction
empty :: SetMultimap k a Source #
O(1). The empty multimap.
size empty === 0
singleton :: k -> a -> SetMultimap k a Source #
O(1). A multimap with a single element.
singleton 1 'a' === fromList [(1, 'a')] size (singleton 1 'a') === 1
fromMap :: Map k (Set a) -> SetMultimap k a Source #
O(k). A key is retained only if it is associated with a non-empty set of values.
From Unordered Lists
fromList :: (Ord k, Ord a) => [(k, a)] -> SetMultimap k a Source #
O(n*log n) where n is the length of the input list. Build a multimap from a list of key/value pairs.
fromList ([] :: [(Int, Char)]) === empty fromList [(1, 'b'), (2, 'a'), (1, 'b')] === fromList [(1, 'b'), (2, 'a')]
Insertion
insert :: (Ord k, Ord a) => k -> a -> SetMultimap k a -> SetMultimap k a Source #
O(log m * log k). If the key exists in the multimap, the new value will be inserted into the set of values for the key. It is a no-op if the value already exists in the set.
insert 1 'a' empty === singleton 1 'a' insert 1 'a' (fromList [(1, 'b'), (2, 'a')]) === fromList [(1, 'a'), (1, 'b'), (2, 'a')] insert 1 'a' (fromList [(1, 'a'), (2, 'c')]) === fromList [(1, 'a'), (2, 'c')]
Deletion/Update
delete :: Ord k => k -> SetMultimap k a -> SetMultimap k a Source #
O(log k). Delete a key and all its values from the map.
delete 1 (fromList [(1,'a'),(1,'b'),(2,'c')]) === singleton 2 'c'
deleteWithValue :: (Ord k, Ord a) => k -> a -> SetMultimap k a -> SetMultimap k a Source #
O(log m * log k). Remove the first occurrence of the value associated with the key, if exists.
deleteWithValue 1 'c' (fromList [(1,'a'),(1,'b'),(2,'c')]) === fromList [(1,'a'),(1,'b'),(2,'c')] deleteWithValue 1 'c' (fromList [(2,'c'),(1,'c')]) === singleton 2 'c'
deleteMax :: Ord k => k -> SetMultimap k a -> SetMultimap k a Source #
O(log m * log k). Remove the maximal value associated with the key, if exists.
deleteMax 3 (fromList [(1,'a'),(1,'b'),(2,'c')]) === fromList [(1,'a'),(1,'b'),(2,'c')] deleteMax 1 (fromList [(1,'a'),(1,'b'),(2,'c')]) === fromList [(1,'a'),(2,'c')]
deleteMin :: Ord k => k -> SetMultimap k a -> SetMultimap k a Source #
O(log m * log k). Remove the minimal value associated with the key, if exists.
deleteMin 3 (fromList [(1,'a'),(1,'b'),(2,'c')]) === fromList [(1,'a'),(1,'b'),(2,'c')] deleteMin 1 (fromList [(1,'a'),(1,'b'),(2,'c')]) === fromList [(1,'b'),(2,'c')]
adjust :: (Ord k, Ord a) => (a -> a) -> k -> SetMultimap k a -> SetMultimap k a Source #
O(m * log m * log k), assuming the function a -> a
takes O(1).
Update values at a specific key, if exists.
Since values are sets, the result may be smaller than the original multimap.
adjust ("new " ++) 1 (fromList [(1,"a"),(1,"b"),(2,"c")]) === fromList [(1,"new a"),(1,"new b"),(2,"c")] adjust (const "z") 1 (fromList [(1,"a"),(1,"b"),(2,"c")]) === fromList [(1,"z"),(2,"c")]
adjustWithKey :: (Ord k, Ord a) => (k -> a -> a) -> k -> SetMultimap k a -> SetMultimap k a Source #
O(m * log m * log k), assuming the function k -> a -> a
takes O(1).
Update values at a specific key, if exists.
Since values are sets, the result may be smaller than the original multimap.
adjustWithKey (\k x -> show k ++ ":new " ++ x) 1 (fromList [(1,"a"),(1,"b"),(2,"c")]) === fromList [(1,"1:new a"),(1,"1:new b"),(2,"c")]
update :: (Ord k, Ord a) => (a -> Maybe a) -> k -> SetMultimap k a -> SetMultimap k a Source #
O(m * log m * log k), assuming the function a ->
takes O(1). The expression (Maybe
a
) updates the
values at key update
f k mapk
, if exists. If f
returns Nothing
for a value, the
value is deleted.
let f x = if x == "a" then Just "new a" else Nothing in do update f 1 (fromList [(1,"a"),(1,"b"),(2,"c")]) === fromList [(1,"new a"),(2, "c")] update f 1 (fromList [(1,"b"),(1,"c"),(2,"c")]) === singleton 2 "c"
updateWithKey :: (Ord k, Ord a) => (k -> a -> Maybe a) -> k -> SetMultimap k a -> SetMultimap k a Source #
O(m * log m * log k), assuming the function k -> a ->
takes O(1). The expression (Maybe
a
) updates the
values at key updateWithKey
f k mapk
, if exists. If f
returns Nothing
for a value, the
value is deleted.
let f k x = if x == "a" then Just (show k ++ ":new a") else Nothing in do updateWithKey f 1 (fromList [(1,"a"),(1,"b"),(2,"c")]) === fromList [(1,"1:new a"),(2,"c")] updateWithKey f 1 (fromList [(1,"b"),(1,"c"),(2,"c")]) === singleton 2 "c"
alter :: Ord k => (Set a -> Set a) -> k -> SetMultimap k a -> SetMultimap k a Source #
O(log k), assuming the function
takes O(1).
The expression (Set
a -> Set
a
) alters the values at k, if exists.alter
f k map
let (f, g) = (const Set.empty, Set.insert 'c') in do alter f 1 (fromList [(1, 'a'), (2, 'b')]) === singleton 2 'b' alter f 3 (fromList [(1, 'a'), (2, 'b')]) === fromList [(1, 'a'), (2, 'b')] alter g 1 (fromList [(1, 'a'), (2, 'b')]) === fromList [(1, 'c'), (1, 'a'), (2, 'b')] alter g 1 (fromList [(1, 'c'), (2, 'b')]) === fromList [(1, 'c'), (2, 'b')] alter g 3 (fromList [(1, 'a'), (2, 'b')]) === fromList [(1, 'a'), (2, 'b'), (3, 'c')]
alterWithKey :: Ord k => (k -> Set a -> Set a) -> k -> SetMultimap k a -> SetMultimap k a Source #
O(log k), assuming the function k ->
takes O(1).
The expression (Set
a -> Set
a
) alters the values at k, if exists.alterWithKey
f k map
let (f, g) = (const (const Set.empty), Set.insert . show) in do alterWithKey f 1 (fromList [(1, "a"), (2, "b")]) === singleton 2 "b" alterWithKey f 3 (fromList [(1, "a"), (2, "b")]) === fromList [(1, "a"), (2, "b")] alterWithKey g 1 (fromList [(1, "a"), (2, "b")]) === fromList [(1, "1"), (1, "a"), (2, "b")] alterWithKey g 3 (fromList [(1, "a"), (2, "b")]) === fromList [(1, "a"), (2, "b"), (3, "3")]
Query
Lookup
lookup :: Ord k => k -> SetMultimap k a -> Set a Source #
O(log k). Lookup the values at a key in the map. It returns an empty set if the key is not in the map.
(!) :: Ord k => SetMultimap k a -> k -> Set a infixl 9 Source #
O(log k). Lookup the values at a key in the map. It returns an empty set if the key is not in the map.
fromList [(3, 'a'), (5, 'b'), (3, 'c')] ! 3 === Set.fromList "ac" fromList [(3, 'a'), (5, 'b'), (3, 'c')] ! 2 === Set.empty
member :: Ord k => k -> SetMultimap k a -> Bool Source #
O(log k). Is the key a member of the map?
A key is a member of the map if and only if there is at least one value associated with it.
member 1 (fromList [(1, 'a'), (2, 'b'), (2, 'c')]) === True member 1 (deleteMax 1 (fromList [(2, 'c'), (1, 'c')])) === False
notMember :: Ord k => k -> SetMultimap k a -> Bool Source #
O(log k). Is the key not a member of the map?
A key is a member of the map if and only if there is at least one value associated with it.
notMember 1 (fromList [(1, 'a'), (2, 'b'), (2, 'c')]) === False notMember 1 (deleteMin 1 (fromList [(2, 'c'), (1, 'c')])) === True
Size
null :: SetMultimap k a -> Bool Source #
O(1). Is the multimap empty?
Data.Multimap.Set.null empty === True Data.Multimap.Set.null (singleton 1 'a') === False
notNull :: SetMultimap k a -> Bool Source #
O(1). Is the multimap non-empty?
notNull empty === False notNull (singleton 1 'a') === True
size :: SetMultimap k a -> Int Source #
The total number of values for all keys.
size
is evaluated lazily. Forcing the size for the first time takes up to
O(k) and subsequent forces take O(1).
size empty === 0 size (singleton 1 'a') === 1 size (fromList [(1, 'a'), (2, 'b'), (2, 'c'), (2, 'b')]) === 3
Combine
Union
union :: (Ord k, Ord a) => SetMultimap k a -> SetMultimap k a -> SetMultimap k a Source #
Union two multimaps, unioning values for duplicate keys.
union (fromList [(1,'a'),(2,'b'),(2,'c')]) (fromList [(1,'d'),(2,'b')]) === fromList [(1,'a'),(1,'d'),(2,'b'),(2,'c')]
unions :: (Foldable f, Ord k, Ord a) => f (SetMultimap k a) -> SetMultimap k a Source #
Union a number of multimaps, unioning values for duplicate keys.
unions [fromList [(1,'a'),(2,'b'),(2,'c')], fromList [(1,'d'),(2,'b')]] === fromList [(1,'a'),(1,'d'),(2,'b'),(2,'c')]
Difference
difference :: (Ord k, Ord a) => SetMultimap k a -> SetMultimap k a -> SetMultimap k a Source #
Difference of two multimaps.
If a key exists in the first multimap but not the second, it remains unchanged in the result. If a key exists in both multimaps, a set difference is performed on their values.
difference (fromList [(1,'a'),(2,'b'),(2,'c')]) (fromList [(1,'d'),(2,'b'),(2,'a')]) === fromList [(1,'a'),(2,'c')]
Traversal
Map
map :: Ord b => (a -> b) -> SetMultimap k a -> SetMultimap k b Source #
O(n * log m), assuming the function a -> b
takes O(1).
Map a function over all values in the map.
Since values are sets, the result may be smaller than the original multimap.
Data.Multimap.Set.map (++ "x") (fromList [(1,"a"),(2,"b")]) === fromList [(1,"ax"),(2,"bx")] Data.Multimap.Set.map (const "c") (fromList [(1,"a"),(1,"b"),(2,"b")]) === fromList [(1,"c"),(2,"c")]
mapWithKey :: Ord b => (k -> a -> b) -> SetMultimap k a -> SetMultimap k b Source #
O(n * log m), assuming the function k -> a -> b
takes O(1).
Map a function over all values in the map.
Since values are sets, the result may be smaller than the original multimap.
mapWithKey (\k x -> show k ++ ":" ++ x) (fromList [(1,"a"),(2,"b")]) === fromList [(1,"1:a"),(2,"2:b")]
Folds
foldr :: (a -> b -> b) -> b -> SetMultimap k a -> b Source #
O(n). Fold the values in the map using the given right-associative binary operator.
Data.Multimap.Set.foldr ((+) . length) 0 (fromList [(1, "hello"), (1, "world"), (2, "!")]) === 11
foldl :: (a -> b -> a) -> a -> SetMultimap k b -> a Source #
O(n). Fold the values in the map using the given left-associative binary operator.
Data.Multimap.Set.foldl (\len -> (+ len) . length) 0 (fromList [(1, "hello"), (1, "world"), (2, "!")]) === 11
foldrWithKey :: (k -> a -> b -> b) -> b -> SetMultimap k a -> b Source #
O(n). Fold the key/value pairs in the map using the given right-associative binary operator.
foldrWithKey (\k a len -> length (show k) + length a + len) 0 (fromList [(1, "hello"), (1, "world"), (20, "!")]) === 15
foldlWithKey :: (a -> k -> b -> a) -> a -> SetMultimap k b -> a Source #
O(n). Fold the key/value pairs in the map using the given left-associative binary operator.
foldlWithKey (\len k a -> length (show k) + length a + len) 0 (fromList [(1, "hello"), (1, "world"), (20, "!")]) === 15
foldMapWithKey :: Monoid m => (k -> a -> m) -> SetMultimap k a -> m Source #
O(n). Fold the key/value pairs in the map using the given monoid.
foldMapWithKey (\k x -> show k ++ ":" ++ x) (fromList [(1, "a"), (1, "c"), (2, "b")]) === "1:a1:c2:b"
Strict Folds
foldr' :: (a -> b -> b) -> b -> SetMultimap k a -> b Source #
O(n). A strict version of foldr
. Each application of the
operator is evaluated before using the result in the next application.
This function is strict in the starting value.
Data.Multimap.Set.foldr' ((+) . length) 0 (fromList [(1, "hello"), (1, "world"), (2, "!")]) === 11
foldl' :: (a -> b -> a) -> a -> SetMultimap k b -> a Source #
O(n). A strict version of foldl
. Each application of the
operator is evaluated before using the result in the next application.
This function is strict in the starting value.
Data.Multimap.Set.foldl' (\len -> (+ len) . length) 0 (fromList [(1, "hello"), (1, "world"), (2, "!")]) === 11
foldrWithKey' :: (k -> a -> b -> b) -> b -> SetMultimap k a -> b Source #
O(n). A strict version of foldrWithKey
. Each application of the
operator is evaluated before using the result in the next application.
This function is strict in the starting value.
foldrWithKey' (\k a len -> length (show k) + length a + len) 0 (fromList [(1, "hello"), (1, "world"), (20, "!")]) === 15
foldlWithKey' :: (a -> k -> b -> a) -> a -> SetMultimap k b -> a Source #
O(n). A strict version of foldlWithKey
. Each application of the
operator is evaluated before using the result in the next application.
This function is strict in the starting value.
foldlWithKey' (\len k a -> length (show k) + length a + len) 0 (fromList [(1, "hello"), (1, "world"), (20, "!")]) === 15
Conversion
elems :: SetMultimap k a -> [a] Source #
O(n). Return all elements of the multimap in ascending order of their keys. Elements of each key appear in ascending order.
elems (fromList [(2,'a'),(1,'b'),(3,'d'),(3,'c'),(1,'b')]) === "bacd" elems (empty :: SetMultimap Int Char) === []
keys :: SetMultimap k a -> [k] Source #
O(k). Return all keys of the multimap in ascending order.
keys (fromList [(2,'a'),(1,'b'),(3,'c'),(1,'b')]) === [1,2,3] keys (empty :: SetMultimap Int Char) === []
assocs :: SetMultimap k a -> [(k, a)] Source #
An alias for toAscList
.
keysSet :: SetMultimap k a -> Set k Source #
O(k). The set of all keys of the multimap.
keysSet (fromList [(2,'a'),(1,'b'),(3,'c'),(1,'b')]) === Set.fromList [1,2,3] keysSet (empty :: SetMultimap Int Char) === Set.empty
Lists
toList :: SetMultimap k a -> [(k, a)] Source #
Convert the multimap into a list of key/value pairs.
toList (fromList [(2,'a'),(1,'b'),(3,'c'),(1,'a')]) === [(1,'a'),(1,'b'),(2,'a'),(3,'c')]
Ordered lists
toAscList :: SetMultimap k a -> [(k, a)] Source #
Convert the multimap into a list of key/value pairs in ascending order of keys. Elements of each key appear in ascending order.
toAscList (fromList [(2,'a'),(1,'b'),(3,'c'),(1,'a')]) === [(1,'a'),(1,'b'),(2,'a'),(3,'c')]
toDescList :: SetMultimap k a -> [(k, a)] Source #
Convert the multimap into a list of key/value pairs in descending order of keys. Elements of each key appear in descending order.
toDescList (fromList [(2,'a'),(1,'b'),(3,'c'),(1,'a')]) === [(3,'c'),(2,'a'),(1,'b'),(1,'a')]
Maps
Multimaps
fromMultimap :: Ord a => Multimap k a -> SetMultimap k a Source #
Convert a Multimap
to a SetMultimap
.
fromMultimap (Data.Multimap.fromList [(1,'a'),(1,'b'),(2,'c')]) === Data.Multimap.Set.fromList [(1,'a'),(1,'b'),(2,'c')]
toMultimapAsc :: SetMultimap k a -> Multimap k a Source #
Convert a SetMultimap
to a Multimap
where the values of each key
are in ascending order.
toMultimapAsc (Data.Multimap.Set.fromList [(1,'a'),(1,'b'),(2,'c')]) === Data.Multimap.fromList [(1,'a'),(1,'b'),(2,'c')]
toMultimapDesc :: SetMultimap k a -> Multimap k a Source #
Convert a SetMultimap
to a Multimap
where the values of each key
are in descending order.
toMultimapDesc (Data.Multimap.Set.fromList [(1,'a'),(1,'b'),(2,'c')]) === Data.Multimap.fromList [(1,'b'),(1,'a'),(2,'c')]
Filter
filter :: (a -> Bool) -> SetMultimap k a -> SetMultimap k a Source #
O(n), assuming the predicate function takes O(1). Retain all values that satisfy the predicate. A key is removed if none of its values satisfies the predicate.
Data.Multimap.Set.filter (> 'a') (fromList [(1,'a'),(1,'b'),(2,'a')]) === singleton 1 'b' Data.Multimap.Set.filter (< 'a') (fromList [(1,'a'),(1,'b'),(2,'a')]) === empty
filterWithKey :: (k -> a -> Bool) -> SetMultimap k a -> SetMultimap k a Source #
O(n), assuming the predicate function takes O(1). Retain all key/value pairs that satisfy the predicate. A key is removed if none of its values satisfies the predicate.
filterWithKey (\k a -> even k && a > 'a') (fromList [(1,'a'),(1,'b'),(2,'a'),(2,'b')]) === singleton 2 'b'
filterKey :: (k -> Bool) -> SetMultimap k a -> SetMultimap k a Source #
O(k), assuming the predicate function takes O(1). Retain all keys that satisfy the predicate.
filterM :: (Ord k, Ord a, Applicative t) => (a -> t Bool) -> SetMultimap k a -> t (SetMultimap k a) Source #
Generalized filter
.
let f a | a > 'b' = Just True | a < 'b' = Just False | a == 'b' = Nothing in do filterM f (fromList [(1,'a'),(1,'b'),(2,'a'),(2,'c')]) === Nothing filterM f (fromList [(1,'a'),(1,'c'),(2,'a'),(2,'c')]) === Just (fromList [(1,'c'),(2,'c')])
filterWithKeyM :: (Ord k, Ord a, Applicative t) => (k -> a -> t Bool) -> SetMultimap k a -> t (SetMultimap k a) Source #
Generalized filterWithKey
.
| Generalized filterWithKey
.
let f k a | even k && a > 'b' = Just True | odd k && a < 'b' = Just False | otherwise = Nothing in do filterWithKeyM f (fromList [(1,'a'),(1,'c'),(2,'a'),(2,'c')]) === Nothing filterWithKeyM f (fromList [(1,'a'),(3,'a'),(2,'c'),(4,'c')]) === Just (fromList [(2,'c'),(4,'c')])
mapMaybe :: Ord b => (a -> Maybe b) -> SetMultimap k a -> SetMultimap k b Source #
mapMaybeWithKey :: Ord b => (k -> a -> Maybe b) -> SetMultimap k a -> SetMultimap k b Source #
mapEither :: (Ord b, Ord c) => (a -> Either b c) -> SetMultimap k a -> (SetMultimap k b, SetMultimap k c) Source #
mapEitherWithKey :: (Ord b, Ord c) => (k -> a -> Either b c) -> SetMultimap k a -> (SetMultimap k b, SetMultimap k c) Source #
O(n * log m), assuming the function k -> a ->
takes O(1).
Map key/value pairs and separate the Either
b cLeft
and Right
results.
mapEitherWithKey (\k a -> if even k && a < 'b' then Left a else Right a) (fromList [(1,'a'),(1,'c'),(2,'a'),(2,'c')]) === (fromList [(2,'a')],fromList [(1,'a'),(1,'c'),(2,'c')])
Min/Max
lookupMin :: SetMultimap k a -> Maybe (k, Set a) Source #
O(log n). Return the smallest key and the associated values. Returns Nothing
if the map is empty.
lookupMin (fromList [(1,'a'),(1,'c'),(2,'c')]) === Just (1, Set.fromList "ac") lookupMin (empty :: SetMultimap Int Char) === Nothing
lookupMax :: SetMultimap k a -> Maybe (k, Set a) Source #
O(log n). Return the largest key and the associated values. Returns Nothing
if the map is empty.
lookupMax (fromList [(1,'a'),(1,'c'),(2,'c')]) === Just (2, Set.fromList "c") lookupMax (empty :: SetMultimap Int Char) === Nothing
lookupLT :: Ord k => k -> SetMultimap k a -> Maybe (k, Set a) Source #
O(log n). Return the largest key smaller than the given one, and the associated values, if exist.
lookupLT 1 (fromList [(1,'a'),(3,'b'),(3,'c'),(5,'c')]) === Nothing lookupLT 4 (fromList [(1,'a'),(3,'b'),(3,'c'),(5,'c')]) === Just (3, Set.fromList "bc")
lookupGT :: Ord k => k -> SetMultimap k a -> Maybe (k, Set a) Source #
O(log n). Return the smallest key larger than the given one, and the associated values, if exist.
lookupGT 5 (fromList [(1,'a'),(3,'b'),(3,'c'),(5,'c')]) === Nothing lookupGT 2 (fromList [(1,'a'),(3,'b'),(3,'c'),(5,'c')]) === Just (3, Set.fromList "bc")
lookupLE :: Ord k => k -> SetMultimap k a -> Maybe (k, Set a) Source #
O(log n). Return the largest key smaller than or equal to the given one, and the associated values, if exist.
lookupLE 0 (fromList [(1,'a'),(3,'b'),(3,'c'),(5,'c')]) === Nothing lookupLE 1 (fromList [(1,'a'),(3,'b'),(3,'c'),(5,'c')]) === Just (1, Set.fromList "a") lookupLE 4 (fromList [(1,'a'),(3,'b'),(3,'c'),(5,'c')]) === Just (3, Set.fromList "bc")
lookupGE :: Ord k => k -> SetMultimap k a -> Maybe (k, Set a) Source #
O(log n). Return the smallest key larger than or equal to the given one, and the associated values, if exist.
lookupGE 6 (fromList [(1,'a'),(3,'b'),(3,'c'),(5,'c')]) === Nothing lookupGE 5 (fromList [(1,'a'),(3,'b'),(3,'c'),(5,'c')]) === Just (5, Set.fromList "c") lookupGE 2 (fromList [(1,'a'),(3,'b'),(3,'c'),(5,'c')]) === Just (3, Set.fromList "bc")