HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulemultiset-0.3.4.3Haskell2010

Data.MultiSet

An efficient implementation of multisets, also sometimes called bags.

A multiset is like a set, but it can contain multiple copies of the same element. Unless otherwise specified all insert and remove opertions affect only a single copy of an element. For example the minimal element before and after deleteMin could be the same, only with one less occurrence.

Since many function names (but not the type name) clash with Prelude names, this module is usually imported qualified, e.g.

 import Data.MultiSet (MultiSet)
 import qualified Data.MultiSet as MultiSet

The implementation of MultiSet is based on the Data.Map module.

Note that the implementation is left-biased -- the elements of a first argument are always preferred to the second, for example in union or insert. Of course, left-biasing can only be observed when equality is an equivalence relation instead of structural equality.

In the complexity of functions n refers to the number of distinct elements, t is the total number of elements.

Contents
  1. MultiSet type
  2. Operators
  3. Query
  4. Construction
  5. Combine
  6. Filter
  7. Map
  8. Monadic
  9. Fold
  10. Min/Max
  11. Conversion
  12. List
  13. Ordered list
  14. Occurrence lists
  15. Map
  16. Set
  17. Debugging
Synopsis
  • Packagemultiset-0.3.4.3
  • Exports67
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMultiSet.hs

MultiSet type

newtypenewtype MultiSet a

A multiset of values a. The same value can occur multiple times.

Instances9Foldable, Eq, Data, Ord, Read, Show, …

Operators

Query

Construction

Combine

valueunion :: Ord a => MultiSet a -> MultiSet a -> MultiSet a

O(n+m). The union of two multisets. The union adds the occurrences together.

The implementation uses the efficient hedge-union algorithm. Hedge-union is more efficient on (bigset union smallset).

valuemaxUnion :: Ord a => MultiSet a -> MultiSet a -> MultiSet a

O(n+m). The union of two multisets. The number of occurrences of each element in the union is the maximum of the number of occurrences in the arguments (instead of the sum).

The implementation uses the efficient hedge-union algorithm. Hedge-union is more efficient on (bigset union smallset).

valueintersection :: Ord a => MultiSet a -> MultiSet a -> MultiSet a

O(n+m). The intersection of two multisets. Elements of the result come from the first multiset, so for example

import qualified Data.MultiSet as MS
data AB = A | B deriving Show
instance Ord AB where compare _ _ = EQ
instance Eq AB where _ == _ = True
main = print (MS.singleton A `MS.intersection` MS.singleton B,
              MS.singleton B `MS.intersection` MS.singleton A)

prints (fromList [A],fromList [B]).

Filter

valuepartition :: (a -> Bool) -> MultiSet a -> (MultiSet a, MultiSet a)

O(n). Partition the multiset into two multisets, one with all elements that satisfy the predicate and one with all elements that don't satisfy the predicate. See also split.

valuesplit :: Ord a => a -> MultiSet a -> (MultiSet a, MultiSet a)

O(log n). The expression (split x set) is a pair (set1,set2) where all elements in set1 are lower than x and all elements in set2 larger than x. x is not found in neither set1 nor set2.

Map

valuemapMonotonic :: (a -> b) -> MultiSet a -> MultiSet b

O(n). mapMonotonic f s == map f s, but works only when f is strictly monotonic. The precondition is not checked. Semi-formally, we have:

and [x < y ==> f x < f y | x <- ls, y <- ls]
                    ==> mapMonotonic f s == map f s
    where ls = toList s

Monadic

Fold

valuefold :: (a -> b -> b) -> b -> MultiSet a -> b

O(t). Fold over the elements of a multiset in an unspecified order.

Min/Max

valuemaxView :: MultiSet a -> Maybe (a, MultiSet a)

O(log n). Retrieves the maximal element of the multiset, and the set with that element removed. Returns Nothing when passed an empty multiset.

Examples:

Example1 expression
maxView $ fromList ['a', 'a', 'b', 'c']Just ('c',fromOccurList [('a',2),('b',1)])
valueminView :: MultiSet a -> Maybe (a, MultiSet a)

O(log n). Retrieves the minimal element of the multiset, and the set with that element removed. Returns Nothing when passed an empty multiset.

Examples:

Example1 expression
minView $ fromList ['a', 'a', 'b', 'c']Just ('a',fromOccurList [('a',1),('b',1),('c',1)])

Conversion

List

valuedistinctElems :: MultiSet a -> [a]

O(n). The distinct elements of a multiset, each element occurs only once in the list.

distinctElems = map fst . toOccurList

Ordered list

valuefromAscList :: Eq a => [a] -> MultiSet a

O(t). Build a multiset from an ascending list in linear time. The precondition (input list is ascending) is not checked.

valuefromDistinctAscList :: [a] -> MultiSet a

O(n). Build a multiset from an ascending list of distinct elements in linear time. The precondition (input list is strictly ascending) is not checked.

Occurrence lists

valuefromOccurList :: Ord a => [(a, Occur)] -> MultiSet a

O(n*log n). Create a multiset from a list of element/occurrence pairs. Occurrences must be positive. The precondition (all occurrences > 0) is not checked.

valuefromAscOccurList :: Eq a => [(a, Occur)] -> MultiSet a

O(n). Build a multiset from an ascending list of element/occurrence pairs in linear time. Occurrences must be positive. The precondition (input list is ascending, all occurrences > 0) is not checked.

O(n). Build a multiset from an ascending list of elements/occurrence pairs where each elements appears only once. Occurrences must be positive. The precondition (input list is strictly ascending, all occurrences > 0) is not checked.

Map

O(1). Convert a Map from elements to occurrences to a multiset. Assumes that the Map contains only values larger than zero. The precondition (all elements > 0) is not checked.

Set

Debugging

valueshowTreeWith :: Show a => Bool -> Bool -> MultiSet a -> String

O(n). The expression (showTreeWith hang wide map) shows the tree that implements the set. If hang is True, a hanging tree is shown otherwise a rotated tree is shown. If wide is True, an extra wide version is shown.

Set> putStrLn $ showTreeWith True False $ fromDistinctAscList [1,1,2,3,4,5]
(1*) 4
+--(1*) 2
|  +--(2*) 1
|  +--(1*) 3
+--(1*) 5

Set> putStrLn $ showTreeWith True True $ fromDistinctAscList [1,1,2,3,4,5]
(1*) 4
|
+--(1*) 2
|  |
|  +--(2*) 1
|  |
|  +--(1*) 3
|
+--(1*) 5

Set> putStrLn $ showTreeWith False True $ fromDistinctAscList [1,1,2,3,4,5]
+--(1*) 5
|
(1*) 4
|
|  +--(1*) 3
|  |
+--(1*) 2
   |
   +--(2*) 1