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.IntMultiSet

An efficient implementation of multisets of integers, also sometimes called bags.

A multiset is like a set, but it can contain multiple copies of the same element.

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

 import Data.IntMultiSet (IntMultiSet)
 import qualified Data.IntMultiSet as IntMultiSet

The implementation of IntMultiSet is based on the Data.IntMap module.

Many operations have a worst-case complexity of O(min(n,W)). This means that the operation can become linear in the number of elements with a maximum of W -- the number of bits in an Int (32 or 64). Here 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
  • SourceIntMultiSet.hs

MultiSet type

newtypenewtype IntMultiSet

A multiset of integers. The same value can occur multiple times.

Instances8Eq, Data, Ord, Read, Show, Semigroup, …

Operators

Query

Construction

Combine

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

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

Filter

Map

Monadic

Fold

valuefold :: (Key -> b -> b) -> b -> IntMultiSet -> b

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

Min/Max

O(log n). Retrieves the maximal element of the multiset, and the set stripped from that element fails (in the monad) when passed an empty multiset.

Examples:

Example1 expression
maxView $ fromList [100, 100, 200, 300]Just (300,fromOccurList [(100,2),(200,1)])

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

Examples:

Example1 expression
minView $ fromList [100, 100, 200, 300]Just (100,fromOccurList [(100,1),(200,1),(300,1)])

Conversion

List

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

distinctElems = map fst . toOccurList

Ordered list

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

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

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

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

Set

Debugging

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