multimap-1.1: A multimap.

Safe HaskellSafe-Inferred

Data.Multimap

Contents

Description

A very simple Multimap, based on Map from the containers package.

Synopsis

Multimap type

data Multimap k v Source

A Multimap with keys k and values v.

A key can have multiple values (but not zero). The same value can be added multiple times (thus no constraints are ever imposed on v).

Internally this is simply a Map k [v]. See toMap for accessing the underlying Map.

Query

null :: Multimap k a -> BoolSource

O(1). Check whether the multimap is empty or not.

size :: Multimap k a -> IntSource

O(1). The number of elements in the multimap.

numKeys :: Multimap k a -> Word32Source

O(1). The number of keys in the multimap.

As this is a multimap, the number of keys is not necessarily equal to the number of values.

numValues :: Multimap k a -> Word32Source

O(1). The number of values in the multimap.

As this is a multimap, the number of keys is not necessarily equal to the number of values.

member :: Ord k => Multimap k a -> k -> BoolSource

O(log n). Is the key a member of the multimap?

notMember :: Ord k => Multimap k a -> k -> BoolSource

O(log n). Is the key not a member of the multimap?

lookup :: Ord k => k -> Multimap k a -> [a]Source

O(log n). Lookup the value at a key in the map.

The function will return the corrsponding values as a List, or the empty list if no values are associated witht the given key.

Operators

(!) :: Ord k => Multimap k a -> k -> [a]Source

As flip lookup

Construction

empty :: Multimap k aSource

O(1). The empty multimap.

Insertion

insert :: Ord k => k -> a -> Multimap k a -> Multimap k aSource

O(log n). Insert a new key and value in the map.

Delete

delete :: Ord k => k -> a -> Multimap k a -> Multimap k aSource

O(log n). Delete a key and all its values from the map.

Traversal

map :: (a -> b) -> Multimap k a -> Multimap k bSource

Map a function over all values in the map.

mapKeys :: Ord k2 => (k1 -> k2) -> Multimap k1 a -> Multimap k2 aSource

mapKeys f s is the multimap obtained by applying f to each key of s.

mapWithKey :: (k -> a -> b) -> Multimap k a -> Multimap k bSource

Map a function over all key/value pairs in the map.

Folds

foldr :: (a -> b -> b) -> b -> Multimap k a -> bSource

Fold the values in the map using the given right-associative binary operator.

foldl :: (a -> b -> a) -> a -> Multimap k b -> aSource

Fold the values in the map using the given left-associative binary operator.

foldrWithKey :: (k -> a -> b -> b) -> b -> Multimap k a -> bSource

O(n). Fold the keys and values in the map using the given right-associative binary operator, taking into account not only the value but also the key.

foldlWithKey :: (a -> k -> b -> a) -> a -> Multimap k b -> aSource

O(n). Fold the keys and values in the map using the given left-associative binary operator, taking into account not only the value but also the key.

Conversion

elems :: Multimap k a -> [[a]]Source

O(n). Return all elements of the multimap in the ascending order of their keys.

A list of lists is returned since a key can have multiple values. Use concat to flatten.

keys :: Multimap k a -> [k]Source

O(n). Return all keys of the multimap in ascending order.

keysSet :: Multimap k a -> Set kSource

O(n). The set of all keys of the multimap.

assocs :: Multimap k a -> [(k, [a])]Source

O(n). Return all key/value pairs in the multimap in ascending key order.

toMap :: Multimap k a -> Map k [a]Source

O(1). Return the map of lists.

toMapOfSets :: Ord a => Multimap k a -> Map k (Set a)Source

/O(k*m*log m) where k is the number of keys and m the maximum number of elements associated with a single key/

toList :: Multimap k a -> [(k, a)]Source

Return a flattened list of key/value pairs.

fromList :: Ord k => [(k, a)] -> Multimap k aSource

O(n*log n) Create a multimap from a list of key/value pairs.

 fromList xs == foldr (uncurry insert) empty

fromMap :: Map k [a] -> Multimap k aSource

Turns a map of lists into a multimap.

Min/Max

findMin :: Multimap k a -> Maybe kSource

O(log n) Find the minimal key of the multimap.

findMax :: Multimap k a -> Maybe kSource

O(log n) Find the maximal key of the multimap.

findMinWithValues :: Multimap k a -> Maybe (k, [a])Source

O(log n) Find the minimal key and the values associated with it.

findMaxWithValues :: Multimap k a -> Maybe (k, [a])Source

O(log n) Find the maximal key and the values associated with it.