ghc-lib-parser-9.12.2.20250421: The GHC API, decoupled from GHC versions

Copyright | (c) Daan Leijen 2002 (c) Joachim Breitner 2011
License | BSD-style
Maintainer | libraries@haskell.org
Portability | portable
Safe Haskell | Ignore
Language | GHC2021

GHC.Data.Word64Set

Contents

- Strictness properties
- Set type
- Construction
- Insertion
- Deletion
- Generalized insertion/deletion
- Query
- Combine
- Filter
- Map
- Folds
  - Strict folds
  - Legacy folds
- Min/Max
- Conversion
  - List
- Debugging

Description

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

Synopsis

- data Word64Set
- type Key = Word64
- empty :: Word64Set
- singleton :: Key -> Word64Set
- fromList :: [Key] -> Word64Set
- fromAscList :: [Key] -> Word64Set
- fromDistinctAscList :: [Key] -> Word64Set
- insert :: Key -> Word64Set -> Word64Set
- delete :: Key -> Word64Set -> Word64Set
- alterF :: Functor f => (Bool -> f Bool) -> Key -> Word64Set -> f Word64Set
- member :: Key -> Word64Set -> Bool
- notMember :: Key -> Word64Set -> Bool
- lookupLT :: Key -> Word64Set -> Maybe Key
- lookupGT :: Key -> Word64Set -> Maybe Key
- lookupLE :: Key -> Word64Set -> Maybe Key
- lookupGE :: Key -> Word64Set -> Maybe Key
- null :: Word64Set -> Bool
- size :: Word64Set -> Int
- isSubsetOf :: Word64Set -> Word64Set -> Bool
- isProperSubsetOf :: Word64Set -> Word64Set -> Bool
- disjoint :: Word64Set -> Word64Set -> Bool
- union :: Word64Set -> Word64Set -> Word64Set
- unions :: [Word64Set] -> Word64Set
- difference :: Word64Set -> Word64Set -> Word64Set
- (\\) :: Word64Set -> Word64Set -> Word64Set
- intersection :: Word64Set -> Word64Set -> Word64Set
- filter :: (Key -> Bool) -> Word64Set -> Word64Set
- partition :: (Key -> Bool) -> Word64Set -> (Word64Set, Word64Set)
- takeWhileAntitone :: (Key -> Bool) -> Word64Set -> Word64Set
- dropWhileAntitone :: (Key -> Bool) -> Word64Set -> Word64Set
- spanAntitone :: (Key -> Bool) -> Word64Set -> (Word64Set, Word64Set)
- split :: Key -> Word64Set -> (Word64Set, Word64Set)
- splitMember :: Key -> Word64Set -> (Word64Set, Bool, Word64Set)
- splitRoot :: Word64Set -> [Word64Set]
- map :: (Key -> Key) -> Word64Set -> Word64Set
- mapMonotonic :: (Key -> Key) -> Word64Set -> Word64Set
- foldr :: (Key -> b -> b) -> b -> Word64Set -> b
- foldl :: (a -> Key -> a) -> a -> Word64Set -> a
- foldr' :: (Key -> b -> b) -> b -> Word64Set -> b
- foldl' :: (a -> Key -> a) -> a -> Word64Set -> a
- fold :: (Key -> b -> b) -> b -> Word64Set -> b
- findMin :: Word64Set -> Key
- findMax :: Word64Set -> Key
- deleteMin :: Word64Set -> Word64Set
- deleteMax :: Word64Set -> Word64Set
- deleteFindMin :: Word64Set -> (Key, Word64Set)
- deleteFindMax :: Word64Set -> (Key, Word64Set)
- maxView :: Word64Set -> Maybe (Key, Word64Set)
- minView :: Word64Set -> Maybe (Key, Word64Set)
- elems :: Word64Set -> [Key]
- toList :: Word64Set -> [Key]
- toAscList :: Word64Set -> [Key]
- toDescList :: Word64Set -> [Key]
- showTree :: Word64Set -> String
- showTreeWith :: Bool -> Bool -> Word64Set -> String

# 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

data Word64Set Source #

A set of integers.

#### Instances

Instances details

NFData Word64Set Source # |
Instance details Defined in GHC.Data.Word64Set.Internal Methods rnf :: Word64Set -> () Source #
Monoid Word64Set Source # |
Instance details Defined in GHC.Data.Word64Set.Internal Methods mempty :: Word64Set Source # mappend :: Word64Set -> Word64Set -> Word64Set Source # mconcat :: [Word64Set] -> Word64Set Source #
Semigroup Word64Set Source # | Since: 0.5.7
Instance details Defined in GHC.Data.Word64Set.Internal Methods (<>) :: Word64Set -> Word64Set -> Word64Set Source # sconcat :: NonEmpty Word64Set -> Word64Set Source # stimes :: Integral b => b -> Word64Set -> Word64Set Source #
Data Word64Set Source # |

Instance details

Defined in GHC.Data.Word64Set.Internal

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Word64Set -> c Word64Set Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Word64Set Source #

toConstr :: Word64Set -> Constr Source #

dataTypeOf :: Word64Set -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Word64Set) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Word64Set) Source #

gmapT :: (forall b. Data b => b -> b) -> Word64Set -> Word64Set Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Word64Set -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Word64Set -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> Word64Set -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Word64Set -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Word64Set -> m Word64Set Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Word64Set -> m Word64Set Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Word64Set -> m Word64Set Source #

IsList Word64Set Source # | Since: 0.5.6.2

Instance details

Defined in GHC.Data.Word64Set.Internal

Associated Types

type Item Word64Set |
Instance details Defined in GHC.Data.Word64Set.Internal type Item Word64Set = Key

Methods

fromList :: [Item Word64Set] -> Word64Set Source #

fromListN :: Int -> [Item Word64Set] -> Word64Set Source #

toList :: Word64Set -> [Item Word64Set] Source #

Read Word64Set Source # |
Instance details Defined in GHC.Data.Word64Set.Internal Methods readsPrec :: Int -> ReadS Word64Set Source # readList :: ReadS [Word64Set] Source # readPrec :: ReadPrec Word64Set Source # readListPrec :: ReadPrec [Word64Set] Source #
Show Word64Set Source # |
Instance details Defined in GHC.Data.Word64Set.Internal Methods showsPrec :: Int -> Word64Set -> ShowS Source # show :: Word64Set -> String Source # showList :: [Word64Set] -> ShowS Source #
Outputable Word64Set Source # |
Instance details Defined in GHC.Utils.Outputable Methods ppr :: Word64Set -> SDoc Source #
Eq Word64Set Source # |
Instance details Defined in GHC.Data.Word64Set.Internal Methods (==) :: Word64Set -> Word64Set -> Bool Source # (/=) :: Word64Set -> Word64Set -> Bool Source #
Ord Word64Set Source # |

Instance details

Defined in GHC.Data.Word64Set.Internal

Methods

compare :: Word64Set -> Word64Set -> Ordering Source #

(<) :: Word64Set -> Word64Set -> Bool Source #

(<=) :: Word64Set -> Word64Set -> Bool Source #

(>) :: Word64Set -> Word64Set -> Bool Source #

(>=) :: Word64Set -> Word64Set -> Bool Source #

max :: Word64Set -> Word64Set -> Word64Set Source #

min :: Word64Set -> Word64Set -> Word64Set Source #

type Item Word64Set Source # |
Instance details Defined in GHC.Data.Word64Set.Internal type Item Word64Set = Key

type Key = Word64 Source #

# Construction

empty :: Word64Set Source #

\(O(1)\). The empty set.

singleton :: Key -> Word64Set Source #

\(O(1)\). A set of one element.

fromList :: [Key] -> Word64Set Source #

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

fromAscList :: [Key] -> Word64Set Source #

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

fromDistinctAscList :: [Key] -> Word64Set Source #

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

# Insertion

insert :: Key -> Word64Set -> Word64Set Source #

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

# Deletion

delete :: Key -> Word64Set -> Word64Set Source #

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

# Generalized insertion/deletion

alterF :: Functor f => (Bool -> f Bool) -> Key -> Word64Set -> f Word64Set Source #

\(O(\min(n,W))\). `(`alterF` f x s)` can delete or insert `x` in `s` depending on whether it is already present in `s`.

In short:

    member x <$> alterF f x s = f (member x s)

Note: `alterF` is a variant of the `at` combinator from Control.Lens.At.

Since: 0.6.3.1

# Query

member :: Key -> Word64Set -> Bool Source #

\(O(\min(n,W))\). Is the value a member of the set?

notMember :: Key -> Word64Set -> Bool Source #

\(O(\min(n,W))\). Is the element not in the set?

lookupLT :: Key -> Word64Set -> Maybe Key Source #

\(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

lookupGT :: Key -> Word64Set -> Maybe Key Source #

\(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

lookupLE :: Key -> Word64Set -> Maybe Key Source #

\(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

lookupGE :: Key -> Word64Set -> Maybe Key Source #

\(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

null :: Word64Set -> Bool Source #

\(O(1)\). Is the set empty?

size :: Word64Set -> Int Source #

\(O(n)\). Cardinality of the set.

isSubsetOf :: Word64Set -> Word64Set -> Bool Source #

\(O(n+m)\). Is this a subset? `(s1 `isSubsetOf` s2)` tells whether `s1` is a subset of `s2`.

isProperSubsetOf :: Word64Set -> Word64Set -> Bool Source #

\(O(n+m)\). Is this a proper subset? (ie. a subset but not equal).

disjoint :: Word64Set -> Word64Set -> Bool Source #

\(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

Since: 0.5.11

# Combine

union :: Word64Set -> Word64Set -> Word64Set Source #

\(O(n+m)\). The union of two sets.

unions :: [Word64Set] -> Word64Set Source #

The union of a list of sets.

difference :: Word64Set -> Word64Set -> Word64Set Source #

\(O(n+m)\). Difference between two sets.

(\\) :: Word64Set -> Word64Set -> Word64Set infixl 9 Source #

\(O(n+m)\). See `difference`.

intersection :: Word64Set -> Word64Set -> Word64Set Source #

\(O(n+m)\). The intersection of two sets.

# Filter

filter :: (Key -> Bool) -> Word64Set -> Word64Set Source #

\(O(n)\). Filter all elements that satisfy some predicate.

partition :: (Key -> Bool) -> Word64Set -> (Word64Set, Word64Set) Source #

\(O(n)\). partition the set according to some predicate.

takeWhileAntitone :: (Key -> Bool) -> Word64Set -> Word64Set Source #

\(O(\min(n,W))\). Take while a predicate on the elements holds. The user is responsible for ensuring that for all `Int`s, `j < k ==> p j >= p k`. See note at `spanAntitone`.

    takeWhileAntitone p = fromDistinctAscList . takeWhile p . toList
    takeWhileAntitone p = filter p

Since: 0.6.7

dropWhileAntitone :: (Key -> Bool) -> Word64Set -> Word64Set Source #

\(O(\min(n,W))\). Drop while a predicate on the elements holds. The user is responsible for ensuring that for all `Int`s, `j < k ==> p j >= p k`. See note at `spanAntitone`.

    dropWhileAntitone p = fromDistinctAscList . dropWhile p . toList
    dropWhileAntitone p = filter (not . p)

Since: 0.6.7

spanAntitone :: (Key -> Bool) -> Word64Set -> (Word64Set, Word64Set) Source #

\(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 `Int`s, `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.

Since: 0.6.7

split :: Key -> Word64Set -> (Word64Set, Word64Set) Source #

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

splitMember :: Key -> Word64Set -> (Word64Set, Bool, Word64Set) Source #

\(O(\min(n,W))\). Performs a `split` but also returns whether the pivot element was found in the original set.

splitRoot :: Word64Set -> [Word64Set] Source #

\(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

map :: (Key -> Key) -> Word64Set -> Word64Set Source #

\(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`

mapMonotonic :: (Key -> Key) -> Word64Set -> Word64Set Source #

\(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

Since: 0.6.3.1

# Folds

foldr :: (Key -> b -> b) -> b -> Word64Set -> b Source #

\(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

foldl :: (a -> Key -> a) -> a -> Word64Set -> a Source #

\(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

foldr' :: (Key -> b -> b) -> b -> Word64Set -> b Source #

\(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.

foldl' :: (a -> Key -> a) -> a -> Word64Set -> a Source #

\(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

fold :: (Key -> b -> b) -> b -> Word64Set -> b Source #

\(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

findMin :: Word64Set -> Key Source #

\(O(\min(n,W))\). The minimal element of the set.

findMax :: Word64Set -> Key Source #

\(O(\min(n,W))\). The maximal element of a set.

deleteMin :: Word64Set -> Word64Set Source #

\(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.

deleteMax :: Word64Set -> Word64Set Source #

\(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.

deleteFindMin :: Word64Set -> (Key, Word64Set) Source #

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

    deleteFindMin set = (findMin set, deleteMin set)

deleteFindMax :: Word64Set -> (Key, Word64Set) Source #

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

    deleteFindMax set = (findMax set, deleteMax set)

maxView :: Word64Set -> Maybe (Key, Word64Set) Source #

\(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.

minView :: Word64Set -> Maybe (Key, Word64Set) Source #

\(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

## List

elems :: Word64Set -> [Key] Source #

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

toList :: Word64Set -> [Key] Source #

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

toAscList :: Word64Set -> [Key] Source #

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

toDescList :: Word64Set -> [Key] Source #

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

# Debugging

showTree :: Word64Set -> String Source #

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

showTreeWith :: Bool -> Bool -> Word64Set -> String Source #

\(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.