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

Moduleghc-10.0.0.20260917GHC2024

GHC.Data.Word64Set

Finite Int Sets

The Word64Set type represents a set of elements of type Int.

For a walkthrough of the most commonly used functions see their sets introduction.

These modules are intended to be imported qualified, to avoid name clashes with Prelude functions, e.g.

 import Data.Word64Set (Word64Set)
 import qualified Data.Word64Set as Word64Set

Performance information

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

Implementation

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

  • Chris Okasaki and Andy Gill, "Fast Mergeable Integer Maps", Workshop on ML, September 1998, pages 77-86, http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.37.5452

  • D.R. Morrison, "PATRICIA -- Practical Algorithm To Retrieve Information Coded In Alphanumeric", Journal of the ACM, 15(4), October 1968, pages 514-534.

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.

Contents
  1. Strictness properties
  2. Set type
  3. Construction
  4. Insertion
  5. Deletion
  6. Generalized insertion/deletion
  7. Query
  8. Combine
  9. Filter
  10. Map
  11. Folds
  12. Strict folds
  13. Legacy folds
  14. Min/Max
  15. Conversion
  16. List
  17. Debugging
Synopsis

Strictness properties

This module satisfies the following strictness property:

  • Key arguments are evaluated to WHNF

Here are some examples that illustrate the property:

delete undefined s  ==  undefined

Set type

datadata Word64Set

A set of integers.

Instances11IsList, Eq, Data, Ord, Read, Show, …
  • IsList Word64SetDefined in ghc-10.0.0.20260917 · GHC.Data.Word64Set.Internal
  • Eq Word64SetDefined in ghc-10.0.0.20260917 · GHC.Data.Word64Set.Internal
  • Data Word64SetDefined in ghc-10.0.0.20260917 · GHC.Data.Word64Set.Internal
  • Ord Word64SetDefined in ghc-10.0.0.20260917 · GHC.Data.Word64Set.Internal
  • Read Word64SetDefined in ghc-10.0.0.20260917 · GHC.Data.Word64Set.Internal
  • Show Word64SetDefined in ghc-10.0.0.20260917 · GHC.Data.Word64Set.Internal
  • Semigroup Word64SetDefined in ghc-10.0.0.20260917 · GHC.Data.Word64Set.Internal
  • Monoid Word64SetDefined in ghc-10.0.0.20260917 · GHC.Data.Word64Set.Internal
  • NFData Word64SetDefined in ghc-10.0.0.20260917 · GHC.Data.Word64Set.Internal
  • Outputable Word64SetDefined in ghc-10.0.0.20260917 · GHC.Utils.Outputable
  • type Item Word64Set = KeyDefined in ghc-10.0.0.20260917 · GHC.Data.Word64Set.Internal

Construction

valuefromAscList :: [Key] -> Word64Set

O(n). Build a set from an ascending list of elements. The precondition (input list is ascending) is not checked.

Insertion

Deletion

Generalized insertion/deletion

Query

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

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

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

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

O(n+m). 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

Combine

Filter

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

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

valuemap :: (Key -> Key) -> Word64Set -> Word64Set

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

O(n). The

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

Folds

valuefoldr :: (Key -> b -> b) -> b -> Word64Set -> 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 -> Word64Set -> 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

Strict folds

valuefoldr' :: (Key -> b -> b) -> b -> Word64Set -> 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 -> Word64Set -> 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 -> Word64Set -> b

O(n). Fold the elements in the set using the given right-associative binary operator. This function is an equivalent of foldr and is present for compatibility only.

Please note that fold will be deprecated in the future and removed.

Min/Max

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 Word64Set was already empty.

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 Word64Set was already empty.

Conversion

List

Debugging

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