HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

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.

  • 2 types
  • 65 values
  • Packagemultiset-0.3.4.3
  • Exports67
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMultiSet.hs

MultiSet type

2 declarations
newtypenewtype MultiSet a
#

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

Instances9Foldable, Eq, Data, Ord, Read, Show, …
typetype Occur = Int
#

The number of occurrences of an element

Operators

1 declaration

Query

8 declarations
valueoccur :: Ord a => a -> MultiSet a -> Occur
#

O(log n). The number of occurrences of an element in a multiset.

Construction

7 declarations
valueinsertMany :: Ord a => a -> Occur -> MultiSet a -> MultiSet a
#

O(log n). Insert an element in a multiset a given number of times.

Negative numbers remove occurrences of the given element.

valuedeleteMany :: Ord a => a -> Occur -> MultiSet a -> MultiSet a
#

O(log n). Delete an element from a multiset a given number of times.

Negative numbers add occurrences of the given element.

Combine

5 declarations
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).

valuedifference :: Ord a => MultiSet a -> MultiSet a -> MultiSet a
#

O(n+m). Difference of two multisets. The implementation uses an efficient hedge algorithm comparable with hedge-union.

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

4 declarations
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

6 declarations
valuemap :: Ord b => (a -> b) -> MultiSet a -> MultiSet b
#

O(n*log n). map f s is the multiset obtained by applying f to each element of s.

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

2 declarations

Fold

2 declarations
valuefold :: (a -> b -> b) -> b -> MultiSet a -> b
#

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

valuefoldOccur :: (a -> Occur -> b -> b) -> b -> MultiSet a -> b
#

O(n). Fold over the elements of a multiset with their occurrences.

Min/Max

10 declarations
valuefindMin :: MultiSet a -> a
#

O(log n). The minimal element of a multiset.

valuefindMax :: MultiSet a -> a
#

O(log n). The maximal element of a multiset.

valuedeleteFindMin :: MultiSet a -> (a, MultiSet a)
#

O(log n). Delete and find the minimal element.

deleteFindMin set = (findMin set, deleteMin set)
valuedeleteFindMax :: MultiSet a -> (a, MultiSet a)
#

O(log n). Delete and find the maximal element.

deleteFindMax set = (findMax set, deleteMax set)
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

0 declarations

List

valueelems :: MultiSet a -> [a]
#

O(t). The elements of a multiset.

valuedistinctElems :: MultiSet a -> [a]
#

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

distinctElems = map fst . toOccurList
valuetoList :: MultiSet a -> [a]
#

O(t). Convert the multiset to a list of elements.

valuefromList :: Ord a => [a] -> MultiSet a
#

O(t*log t). Create a multiset from a list of elements.

Ordered list

valuetoAscList :: MultiSet a -> [a]
#

O(t). Convert the multiset to an ascending list of elements.

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.

valuefromDistinctAscOccurList :: [(a, Occur)] -> MultiSet a
#

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

valuefromOccurMap :: Map a Occur -> MultiSet a
#

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

3 declarations
valueshowTree :: Show a => MultiSet a -> String
#

O(n). Show the tree that implements the set. The tree is shown in a compressed, hanging format.

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