HORIZON HASKELLDocslts/ghc-10.0.xaa25b892026-10-10Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · aa25b89 · 2026-10-10

Modulecontainers-0.8Haskell2010

Data.IntSet.Internal

WARNING

This module is considered internal.

The Package Versioning Policy does not apply.

The contents of this module may change in any way whatsoever and without any warning between minor versions of this package.

Authors importing this module are expected to track development closely.

Finite Int Sets (internals)

The IntSet type represents a set of elements of type Int. An IntSet is strict in its elements.

Implementation

The implementation is based on big-endian patricia trees. This data structure performs especially well on binary operations like union and intersection. Additionally, benchmarks show that it is also (much) faster on insertions and deletions when compared to a generic size-balanced set implementation (see Data.Set).

Additionally, this implementation places bitmaps in the leaves of the tree. Their size is the natural size of a machine word (32 or 64 bits) and greatly reduces the memory footprint and execution times for dense sets, e.g. sets where it is likely that many values lie close to each other. The asymptotics are not affected by this optimization.

  • 4 types
  • 62 values
  • Packagecontainers-0.8
  • Exports66
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceInternal.hs

Set type

3 declarations
datadata IntSet
#

A set of integers.

Instances11IsList, Eq, Data, Ord, Read, Show, …
  • IsList IntSetDefined in containers-0.8 · Data.IntSet.Internal
  • Eq IntSetDefined in containers-0.8 · Data.IntSet.Internal
  • Data IntSetDefined in containers-0.8 · Data.IntSet.Internal
  • Ord IntSetDefined in containers-0.8 · Data.IntSet.Internal
  • Read IntSetDefined in containers-0.8 · Data.IntSet.Internal
  • Show IntSetDefined in containers-0.8 · Data.IntSet.Internal
  • Semigroup IntSetDefined in containers-0.8 · Data.IntSet.Internal

    (<>) = union

  • Monoid IntSetDefined in containers-0.8 · Data.IntSet.Internal

    mempty = empty

  • NFData IntSetDefined in containers-0.8 · Data.IntSet.Internal
  • Lift IntSetDefined in containers-0.8 · Data.IntSet.Internal
  • type Item IntSet = KeyDefined in containers-0.8 · Data.IntSet.Internal

Operators

1 declaration

Query

11 declarations
valuelookupLT :: Key -> IntSet -> Maybe Key
#

O(\min(n,W)). Find largest element smaller than the given one.

lookupLT 3 (fromList [3, 5]) == Nothing
lookupLT 5 (fromList [3, 5]) == Just 3
valuelookupGT :: Key -> IntSet -> Maybe Key
#

O(\min(n,W)). Find smallest element greater than the given one.

lookupGT 4 (fromList [3, 5]) == Just 5
lookupGT 5 (fromList [3, 5]) == Nothing
valuelookupLE :: Key -> IntSet -> Maybe Key
#

O(\min(n,W)). Find largest element smaller or equal to the given one.

lookupLE 2 (fromList [3, 5]) == Nothing
lookupLE 4 (fromList [3, 5]) == Just 3
lookupLE 5 (fromList [3, 5]) == Just 5
valuelookupGE :: Key -> IntSet -> Maybe Key
#

O(\min(n,W)). Find smallest element greater or equal to the given one.

lookupGE 3 (fromList [3, 5]) == Just 3
lookupGE 4 (fromList [3, 5]) == Just 5
lookupGE 6 (fromList [3, 5]) == Nothing
valueisSubsetOf :: IntSet -> IntSet -> Bool
#

O(\min(n, m \log \frac{2^W}{m})), m \leq n. Is this a subset? (s1 `isSubsetOf` s2) tells whether s1 is a subset of s2.

valuedisjoint :: IntSet -> IntSet -> Bool
#

O(\min(n, m \log \frac{2^W}{m})), m \leq n. Check whether two sets are disjoint (i.e. their intersection is empty).

disjoint (fromList [2,4,6])   (fromList [1,3])     == True
disjoint (fromList [2,4,6,8]) (fromList [2,3,5,7]) == False
disjoint (fromList [1,2])     (fromList [1,2,3,4]) == False
disjoint (fromList [])        (fromList [])        == True

Construction

6 declarations
valuefromRange :: (Key, Key) -> IntSet
#

O(n / W). Create a set from a range of integers.

fromRange (low, high) == fromList [low..high]
valueinsert :: Key -> IntSet -> IntSet
#

O(\min(n,W)). Add a value to the set. There is no left- or right bias for IntSets.

valuedelete :: Key -> IntSet -> IntSet
#

O(\min(n,W)). Delete a value in the set. Returns the original set when the value was not present.

Combine

7 declarations

O(\min(n, m \log \frac{2^W}{m})), m \leq n. The symmetric difference of two sets.

The result contains elements that appear in exactly one of the two sets.

symmetricDifference (fromList [0,2,4,6]) (fromList [0,3,6,9]) == fromList [2,3,4,9]
newtypenewtype Intersection
#

IntSets form a Semigroup under intersection.

A Monoid instance is not defined because it would be impractical to construct mempty, the IntSet containing all Ints.

Instances4Eq, Ord, Show, Semigroup

Filter

8 declarations
valuespanAntitone :: (Key -> Bool) -> IntSet -> (IntSet, IntSet)
#

O(\min(n,W)). Divide a set at the point where a predicate on the elements stops holding. The user is responsible for ensuring that for all Ints, j < k ==> p j >= p k.

spanAntitone p xs = (takeWhileAntitone p xs, dropWhileAntitone p xs)
spanAntitone p xs = partition p xs

Note: if p is not actually antitone, then spanAntitone will split the set at some unspecified point.

valuesplit :: Key -> IntSet -> (IntSet, IntSet)
#

O(\min(n,W)). The expression (split x set) is a pair (set1,set2) where set1 comprises the elements of set less than x and set2 comprises the elements of set greater than x.

split 3 (fromList [1..5]) == (fromList [1,2], fromList [4,5])
valuesplitRoot :: IntSet -> [IntSet]
#

O(1). Decompose a set into pieces based on the structure of the underlying tree. This function is useful for consuming a set in parallel.

No guarantee is made as to the sizes of the pieces; an internal, but deterministic process determines this. However, it is guaranteed that the pieces returned will be in ascending order (all elements in the first submap less than all elements in the second, and so on).

Examples:

splitRoot (fromList [1..120]) == [fromList [1..63],fromList [64..120]]
splitRoot empty == []

Note that the current implementation does not return more than two subsets, but you should not depend on this behaviour because it can change in the future without notice. Also, the current version does not continue splitting all the way to individual singleton sets -- it stops at some point.

Map

2 declarations
valuemap :: (Key -> Key) -> IntSet -> IntSet
#

O(n \min(n,W)). map f s is the set obtained by applying f to each element of s.

It's worth noting that the size of the result may be smaller if, for some (x,y), x /= y && f x == f y

valuemapMonotonic :: (Key -> Key) -> IntSet -> IntSet
#

O(n). The

mapMonotonic f s == map f s, but works only when f is strictly increasing. 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

Warning: This function should be used only if f is monotonically strictly increasing. This precondition is not checked. Use map if the precondition may not hold.

Folds

3 declarations
valuefoldr :: (Key -> b -> b) -> b -> IntSet -> b
#

O(n). Fold the elements in the set using the given right-associative binary operator, such that foldr f z == foldr f z . toAscList.

For example,

toAscList set = foldr (:) [] set
valuefoldl :: (a -> Key -> a) -> a -> IntSet -> a
#

O(n). Fold the elements in the set using the given left-associative binary operator, such that foldl f z == foldl f z . toAscList.

For example,

toDescList set = foldl (flip (:)) [] set
valuefoldMap :: Monoid a => (Key -> a) -> IntSet -> a
#

O(n). Map the elements in the set to a monoid and combine with (<>).

Strict folds

valuefoldr' :: (Key -> b -> b) -> b -> IntSet -> b
#

O(n). A strict version of foldr. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuefoldl' :: (a -> Key -> a) -> a -> IntSet -> a
#

O(n). A strict version of foldl. Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

Legacy folds

valuefold :: (Key -> b -> b) -> b -> IntSet -> b
#

Deprecated. Use Data.IntSet.foldr instead

O(n). Fold the elements in the set using the given right-associative binary operator.

Min/Max

10 declarations
valuefindMin :: IntSet -> Key
#

O(\min(n,W)). The minimal element of the set. Calls error if the set is empty.

valuefindMax :: IntSet -> Key
#

O(\min(n,W)). The maximal element of the set. Calls error if the set is empty.

valuedeleteMin :: IntSet -> IntSet
#

O(\min(n,W)). Delete the minimal element. Returns an empty set if the set is empty.

Note that this is a change of behaviour for consistency with Set – versions prior to 0.5 threw an error if the IntSet was already empty.

valuedeleteMax :: IntSet -> IntSet
#

O(\min(n,W)). Delete the maximal element. Returns an empty set if the set is empty.

Note that this is a change of behaviour for consistency with Set – versions prior to 0.5 threw an error if the IntSet was already empty.

valuedeleteFindMin :: IntSet -> (Key, IntSet)
#

O(\min(n,W)). Delete and find the minimal element.

deleteFindMin set = (findMin set, deleteMin set)
valuedeleteFindMax :: IntSet -> (Key, IntSet)
#

O(\min(n,W)). Delete and find the maximal element.

deleteFindMax set = (findMax set, deleteMax set)
valuemaxView :: IntSet -> Maybe (Key, IntSet)
#

O(\min(n,W)). Retrieves the maximal key of the set, and the set stripped of that element, or Nothing if passed an empty set.

valueminView :: IntSet -> Maybe (Key, IntSet)
#

O(\min(n,W)). Retrieves the minimal key of the set, and the set stripped of that element, or Nothing if passed an empty set.

Conversion

0 declarations

List

valueelems :: IntSet -> [Key]
#

O(n). An alias of toAscList. The elements of a set in ascending order. Subject to list fusion.

valuetoList :: IntSet -> [Key]
#

O(n). Convert the set to a list of elements. Subject to list fusion.

valuefromList :: [Key] -> IntSet
#

O(n \min(n,W)). Create a set from a list of integers.

Ordered list

valuetoAscList :: IntSet -> [Key]
#

O(n). Convert the set to an ascending list of elements. Subject to list fusion.

valuetoDescList :: IntSet -> [Key]
#

O(n). Convert the set to a descending list of elements. Subject to list fusion.

valuefromAscList :: [Key] -> IntSet
#

O(n). Build a set from an ascending list of elements.

Warning: This function should be used only if the elements are in non-decreasing order. This precondition is not checked. Use fromList if the precondition may not hold.

valuefromDistinctAscList :: [Key] -> IntSet
#

O(n). Build a set from an ascending list of distinct elements.

Warning: This function should be used only if the elements are in strictly increasing order. This precondition is not checked. Use fromList if the precondition may not hold.

Debugging

2 declarations
valueshowTree :: IntSet -> String
#

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

valueshowTreeWith :: Bool -> Bool -> IntSet -> String
#

O(n \min(n,W)). 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.

Internals

3 declarations