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.
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 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.
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 , but has the
performance advantage of only evaluating `Data.Function.on` f)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 will be faster than
`Data.Function.on` f)sortOn f.
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.
O(n \log n) . A generalization of unstableSort, unstableSortBy
takes an arbitrary comparator and sorts the specified sequence.
The sort is not stable. This algorithm is frequently faster and
uses less memory than sortBy.
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 ,
but has the performance advantage of only evaluating `Data.Function.on` f)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 will be faster than
`Data.Function.on` f)unstableSortOn f.
Heaps
8 declarationsThe following are definitions for various specialized pairing heaps.
All of the heaps are defined to be non-empty, which speeds up the merge functions.
Constructors
IQNilIQCons !(IndexedQueue e) (IQList e)infixr 8
A pairing heap tagged with some key for sorting elements, for use in unstableSortOn.
Constructors
TQNilTQCons !(TaggedQueue a b) (TQList a b)infixr 8
Constructors
ITQNilITQCons !(IndexedTaggedQueue e a) (ITQList e a)infixr 8
Merges
4 declarationsThe following are definitions for "merge" for each of the heaps above. Each takes a comparison function which is used to order the elements.
mergeIQ merges two IndexedQueues, taking into account the original position of the elements.
mergeTQ merges two TaggedQueues, based on the tag value.
mergeITQ merges two IndexedTaggedQueues, based on the tag value, taking into account the original position of the elements.
popMin
4 declarationsThe 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.
Pop the smallest element from the queue, using the supplied comparator.
Pop the smallest element from the queue, using the supplied comparator, deferring to the item's original position when the comparator returns EQ.
Pop the smallest element from the queue, using the supplied comparator on the tag.
Pop the smallest element from the queue, using the supplied comparator on the tag, deferring to the item's original position when the comparator returns EQ.
Building
4 declarationsThe following are definitions for functions to build queues, given a comparison function.
Special folds
2 declarationsA 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).
A foldMapWithIndex-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.