multi-containers-0.2: A few multimap variants.
MaintainerZiyang Liu <free@cofree.io>
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Multimap.Set

Description

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 Map k (Set a). The differences between Multimap k a and Map k (Set a) include:

  • 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 and unions 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 the keysSet or first converting the multimap to a regular map via toMap.

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

Multimap type

data SetMultimap k a Source #

Instances

Instances details
Eq2 SetMultimap Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

liftEq2 :: (a -> b -> Bool) -> (c -> d -> Bool) -> SetMultimap a c -> SetMultimap b d -> Bool #

Ord2 SetMultimap Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

liftCompare2 :: (a -> b -> Ordering) -> (c -> d -> Ordering) -> SetMultimap a c -> SetMultimap b d -> Ordering #

Show2 SetMultimap Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

liftShowsPrec2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> Int -> SetMultimap a b -> ShowS #

liftShowList2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> [SetMultimap a b] -> ShowS #

Foldable (SetMultimap k) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

fold :: Monoid m => SetMultimap k m -> m #

foldMap :: Monoid m => (a -> m) -> SetMultimap k a -> m #

foldMap' :: Monoid m => (a -> m) -> SetMultimap k a -> m #

foldr :: (a -> b -> b) -> b -> SetMultimap k a -> b #

foldr' :: (a -> b -> b) -> b -> SetMultimap k a -> b #

foldl :: (b -> a -> b) -> b -> SetMultimap k a -> b #

foldl' :: (b -> a -> b) -> b -> SetMultimap k a -> b #

foldr1 :: (a -> a -> a) -> SetMultimap k a -> a #

foldl1 :: (a -> a -> a) -> SetMultimap k a -> a #

toList :: SetMultimap k a -> [a] #

null :: SetMultimap k a -> Bool #

length :: SetMultimap k a -> Int #

elem :: Eq a => a -> SetMultimap k a -> Bool #

maximum :: Ord a => SetMultimap k a -> a #

minimum :: Ord a => SetMultimap k a -> a #

sum :: Num a => SetMultimap k a -> a #

product :: Num a => SetMultimap k a -> a #

Eq k => Eq1 (SetMultimap k) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

liftEq :: (a -> b -> Bool) -> SetMultimap k a -> SetMultimap k b -> Bool #

Ord k => Ord1 (SetMultimap k) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

liftCompare :: (a -> b -> Ordering) -> SetMultimap k a -> SetMultimap k b -> Ordering #

Show k => Show1 (SetMultimap k) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> SetMultimap k a -> ShowS #

liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [SetMultimap k a] -> ShowS #

(Eq k, Eq a) => Eq (SetMultimap k a) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

(==) :: SetMultimap k a -> SetMultimap k a -> Bool #

(/=) :: SetMultimap k a -> SetMultimap k a -> Bool #

(Data k, Data a, Ord a, Ord k) => Data (SetMultimap k a) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> SetMultimap k a -> c (SetMultimap k a) #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (SetMultimap k a) #

toConstr :: SetMultimap k a -> Constr #

dataTypeOf :: SetMultimap k a -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (SetMultimap k a)) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (SetMultimap k a)) #

gmapT :: (forall b. Data b => b -> b) -> SetMultimap k a -> SetMultimap k a #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> SetMultimap k a -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> SetMultimap k a -> r #

gmapQ :: (forall d. Data d => d -> u) -> SetMultimap k a -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> SetMultimap k a -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> SetMultimap k a -> m (SetMultimap k a) #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> SetMultimap k a -> m (SetMultimap k a) #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> SetMultimap k a -> m (SetMultimap k a) #

(Ord k, Ord a) => Ord (SetMultimap k a) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

compare :: SetMultimap k a -> SetMultimap k a -> Ordering #

(<) :: SetMultimap k a -> SetMultimap k a -> Bool #

(<=) :: SetMultimap k a -> SetMultimap k a -> Bool #

(>) :: SetMultimap k a -> SetMultimap k a -> Bool #

(>=) :: SetMultimap k a -> SetMultimap k a -> Bool #

max :: SetMultimap k a -> SetMultimap k a -> SetMultimap k a #

min :: SetMultimap k a -> SetMultimap k a -> SetMultimap k a #

(Ord k, Ord a, Read k, Read a) => Read (SetMultimap k a) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

(Show k, Show a) => Show (SetMultimap k a) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

showsPrec :: Int -> SetMultimap k a -> ShowS #

show :: SetMultimap k a -> String #

showList :: [SetMultimap k a] -> ShowS #

(Ord k, Ord a) => Semigroup (SetMultimap k a) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

(<>) :: SetMultimap k a -> SetMultimap k a -> SetMultimap k a #

sconcat :: NonEmpty (SetMultimap k a) -> SetMultimap k a #

stimes :: Integral b => b -> SetMultimap k a -> SetMultimap k a #

(Ord k, Ord a) => Monoid (SetMultimap k a) Source # 
Instance details

Defined in Data.Multimap.Set.Internal

Methods

mempty :: SetMultimap k a #

mappend :: SetMultimap k a -> SetMultimap k a -> SetMultimap k a #

mconcat :: [SetMultimap k a] -> SetMultimap k a #

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 -> Maybe a takes O(1). The expression (update f k map) updates the values at key k, 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 -> Maybe a takes O(1). The expression (updateWithKey f k map) updates the values at key k, 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 Set a -> Set a takes O(1). The expression (alter f k map) alters the values at k, if exists.

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 -> Set a -> Set a takes O(1). The expression (alterWithKey f k map) alters the values at k, if exists.

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

toMap :: SetMultimap k a -> Map k (Set a) Source #

O(1). Convert the multimap into a regular map.

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 #

O(n * log m), assuming the function a -> Maybe b takes O(1). Map values and collect the Just results.

mapMaybe (\a -> if a == "a" then Just "new a" else Nothing) (fromList [(1,"a"),(1,"b"),(2,"a"),(2,"c")])
  === fromList [(1,"new a"),(2,"new a")]

mapMaybeWithKey :: Ord b => (k -> a -> Maybe b) -> SetMultimap k a -> SetMultimap k b Source #

O(n * log m), assuming the function k -> a -> Maybe b takes O(1). Map key/value pairs and collect the Just results.

mapMaybeWithKey (\k a -> if k > 1 && a == "a" then Just "new a" else Nothing) (fromList [(1,"a"),(1,"b"),(2,"a"),(2,"c")])
  === singleton 2 "new a"

mapEither :: (Ord b, Ord c) => (a -> Either b c) -> SetMultimap k a -> (SetMultimap k b, SetMultimap k c) Source #

O(n * log m), assuming the function a -> Either b c takes O(1). Map values and separate the Left and Right results.

mapEither (\a -> if a < 'b' then Left a else Right a) (fromList [(1,'a'),(1,'c'),(2,'a'),(2,'c')])
  === (fromList [(1,'a'),(2,'a')],fromList [(1,'c'),(2,'c')])

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 -> Either b c takes O(1). Map key/value pairs and separate the Left 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")