HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

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.

  • 3 types
  • 64 values
  • Packagemultiset-0.3.4.3
  • Exports67
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceIntMultiSet.hs

MultiSet type

3 declarations
newtypenewtype IntMultiSet
#

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

Instances8Eq, Data, Ord, Read, Show, Semigroup, …
typetype Key = Int
#

Key type for IntMultiSet

typetype Occur = Int
#

The number of occurrences of an element

Operators

1 declaration

Query

8 declarations

Construction

7 declarations

Combine

5 declarations

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

4 declarations

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
valuemapMonotonic :: (Key -> Key) -> IntMultiSet -> IntMultiSet
#

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 :: (Key -> b -> b) -> b -> IntMultiSet -> b
#

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

Min/Max

10 declarations

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

0 declarations

List

valuedistinctElems :: IntMultiSet -> [Key]
#

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

distinctElems = map fst . toOccurList

Ordered list

valuefromAscList :: [Int] -> IntMultiSet
#

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

valuefromOccurList :: [(Int, Int)] -> IntMultiSet
#

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.

valuefromAscOccurList :: [(Int, Int)] -> IntMultiSet
#

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 an IntMap from elements to occurrences to a multiset. Assumes that the IntMap contains only values larger than zero. The precondition (all elements > 0) is not checked.

Set

Debugging

2 declarations
valueshowTree :: IntMultiSet -> String
#

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

valueshowTreeWith :: Bool -> Bool -> IntMultiSet -> 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