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

Modulecontainers-0.7Haskell2010

Data.Sequence.Internal.Sorting

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.

Description

This module provides the various sorting implementations for Data.Sequence. Further notes are available in the file sorting.md (in this directory).

  • 8 types
  • 20 values
  • Packagecontainers-0.7
  • Exports28
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceSorting.hs

Sort Functions

6 declarations
valuesort :: Ord a => Seq a -> Seq a
#

O(n \log n) . sort sorts the specified Seq by the natural ordering of its elements. The sort is stable. If stability is not required, unstableSort can be slightly faster.

valuesortBy :: (a -> a -> Ordering) -> Seq a -> Seq a
#

O(n \log n) . sortBy sorts the specified Seq according to the specified comparator. The sort is stable. If stability is not required, unstableSortBy can be slightly faster.

valuesortOn :: Ord b => (a -> b) -> Seq a -> Seq a
#

O(n \log n) . sortOn sorts the specified Seq by comparing the results of a key function applied to each element. sortOn f is equivalent to sortBy (compare `Data.Function.on` f), but has the performance advantage of only evaluating f once for each element in the input list. This is called the decorate-sort-undecorate paradigm, or Schwartzian transform.

An example of using sortOn might be to sort a Seq of strings according to their length:

sortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]

If, instead, sortBy had been used, length would be evaluated on every comparison, giving O(n \log n) evaluations, rather than O(n) .

If f is very cheap (for example a record selector, or fst), sortBy (compare `Data.Function.on` f) will be faster than sortOn f.

valueunstableSort :: Ord a => Seq a -> Seq a
#

O(n \log n) . unstableSort sorts the specified Seq by the natural ordering of its elements, but the sort is not stable. This algorithm is frequently faster and uses less memory than sort.

valueunstableSortOn :: Ord b => (a -> b) -> Seq a -> Seq a
#

O(n \log n) . unstableSortOn sorts the specified Seq by comparing the results of a key function applied to each element. unstableSortOn f is equivalent to unstableSortBy (compare `Data.Function.on` f), but has the performance advantage of only evaluating f once for each element in the input list. This is called the decorate-sort-undecorate paradigm, or Schwartzian transform.

An example of using unstableSortOn might be to sort a Seq of strings according to their length:

unstableSortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]

If, instead, unstableSortBy had been used, length would be evaluated on every comparison, giving O(n \log n) evaluations, rather than O(n) .

If f is very cheap (for example a record selector, or fst), unstableSortBy (compare `Data.Function.on` f) will be faster than unstableSortOn f.

Heaps

8 declarations

The following are definitions for various specialized pairing heaps.

All of the heaps are defined to be non-empty, which speeds up the merge functions.

datadata Queue e
#

A simple pairing heap.

Constructors

datadata IndexedQueue e
#

A pairing heap tagged with the original position of elements, to allow for stable sorting.

Constructors

Merges

4 declarations

The following are definitions for "merge" for each of the heaps above. Each takes a comparison function which is used to order the elements.

popMin

4 declarations

The following are definitions for popMin, a function which constructs a stateful action which pops the smallest element from the queue, where "smallest" is according to the supplied comparison function.

All of the functions fail on an empty queue.

Each of these functions is structured something like this:

popMinQ cmp (Q x ts) = (mergeQs ts, x)

The reason the call to mergeQs is lazy is that it will be bottom for the last element in the queue, preventing us from evaluating the fully sorted sequence.

valuepopMinQ :: (e -> e -> Ordering) -> Queue e -> (Queue e, e)
#

Pop the smallest element from the queue, using the supplied comparator.

valuepopMinIQ :: (e -> e -> Ordering) -> IndexedQueue e -> (IndexedQueue e, e)
#

Pop the smallest element from the queue, using the supplied comparator, deferring to the item's original position when the comparator returns EQ.

Building

4 declarations

The following are definitions for functions to build queues, given a comparison function.

Special folds

2 declarations

A big part of what makes the heaps fast is that they're non empty, so the merge function can avoid an extra case match. To take advantage of this, though, we need specialized versions of foldMap and foldMapWithIndex, which can alternate between calling the faster semigroup-like merge when folding over non empty structures (like Node and Digit), and the Data.Semirgroup.Option-like mappend, when folding over structures which can be empty (like FingerTree).

valuefoldToMaybeTree :: (b -> b -> b) -> (a -> b) -> FingerTree a -> Maybe b
#

A foldMap-like function, specialized to the Data.Semigroup.Option monoid, which takes advantage of the internal structure of Seq to avoid wrapping in Maybe at certain points.