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

Modulepqueue-1.7.0.0Haskell2010

Data.PQueue.Min

General purpose priority queue, supporting extract-minimum operations.

An amortized running time is given for each operation, with n referring to the length of the sequence and k being the integral index used by some operations. These bounds hold even in a persistent (shared) setting.

This implementation is based on a binomial heap augmented with a global root.

This implementation does not guarantee stable behavior.

This implementation offers a number of methods of the form xxxU, where U stands for unordered. No guarantees whatsoever are made on the execution or traversal order of these functions.

Contents
  1. Basic operations
  2. Query operations
  3. Construction operations
  4. Subsets
  5. Extracting subsets
  6. Predicates
  7. Filter/Map
  8. Fold/Functor/Traversable variations
  9. List operations
  10. Unordered operations
  11. Miscellaneous operations
Synopsis
  • Packagepqueue-1.7.0.0
  • Exports48
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMin.hs
datadata MinQueue a

A priority queue with elements of type a. Supports extracting the minimum element.

Instances8Eq, Data, Ord, Read, Show, Semigroup, …
  • Ord a => Eq (MinQueue a)Defined in pqueue-1.7.0.0 · Data.PQueue.Internals
  • (Ord a, Data a) => Data (MinQueue a)Defined in pqueue-1.7.0.0 · Data.PQueue.Internals

    Treats the priority queue as an empty queue or a minimal element and a priority queue. The constructors, conceptually, are Empty and (Data.PQueue.Min.:<). All constructed queues maintain the queue invariants.

  • Ord a => Ord (MinQueue a)Defined in pqueue-1.7.0.0 · Data.PQueue.Internals
  • (Ord a, Read a) => Read (MinQueue a)Defined in pqueue-1.7.0.0 · Data.PQueue.Internals
  • (Ord a, Show a) => Show (MinQueue a)Defined in pqueue-1.7.0.0 · Data.PQueue.Internals
  • Ord a => Semigroup (MinQueue a)Defined in pqueue-1.7.0.0 · Data.PQueue.Internals
  • Ord a => Monoid (MinQueue a)Defined in pqueue-1.7.0.0 · Data.PQueue.Internals
  • NFData a => NFData (MinQueue a)Defined in pqueue-1.7.0.0 · Data.PQueue.Internals
patternpattern Empty :: MinQueue a

A bidirectional pattern synonym for an empty priority queue.

patternpattern (:<) :: Ord a => a -> MinQueue a -> MinQueue a

A bidirectional pattern synonym for working with the minimum view of a MinQueue. Using :< to construct a queue performs an insertion in O(1) amortized time. When matching on a :< q, forcing q takes O(\log n) time.

Basic operations

Query operations

valuefindMin :: MinQueue a -> a

O(1). Returns the minimum element. Throws an error on an empty queue.

Construction operations

Subsets

Extracting subsets

value(!!) :: Ord a => MinQueue a -> Int -> a

O(k \log n). Index (subscript) operator, starting from 0. queue !! k returns the (k+1)th smallest element in the queue. Equivalent to toAscList queue !! k.

valuetake :: Ord a => Int -> MinQueue a -> [a]

O(k \log n). take k, applied to a queue queue, returns a list of the smallest k elements of queue, or all elements of queue itself if k >= size queue.

Predicates

valuetakeWhile :: Ord a => (a -> Bool) -> MinQueue a -> [a]

takeWhile, applied to a predicate p and a queue queue, returns the longest prefix (possibly empty) of queue of elements that satisfy p.

valuespan :: Ord a => (a -> Bool) -> MinQueue a -> ([a], MinQueue a)

span, applied to a predicate p and a queue queue, returns a tuple where first element is longest prefix (possibly empty) of queue of elements that satisfy p and second element is the remainder of the queue.

valuebreak :: Ord a => (a -> Bool) -> MinQueue a -> ([a], MinQueue a)

break, applied to a predicate p and a queue queue, returns a tuple where first element is longest prefix (possibly empty) of queue of elements that do not satisfy p and second element is the remainder of the queue.

Filter/Map

Fold/Functor/Traversable variations

valuemap :: Ord b => (a -> b) -> MinQueue a -> MinQueue b

O(n). Creates a new priority queue containing the images of the elements of this queue. Equivalent to fromList . Data.List.map f . toList.

valuemapMonotonic :: (a -> b) -> MinQueue a -> MinQueue b

O(n). Assumes that the function it is given is (weakly) monotonic (meaning that x <= y implies f x <= f y), and applies this function to every element of the priority queue, as in fmap. If the function is not monotonic, the result is undefined.

valuefoldrAsc :: Ord a => (a -> b -> b) -> b -> MinQueue a -> b

O(n \log n). Performs a right fold on the elements of a priority queue in ascending order.

valuefoldlAsc :: Ord a => (b -> a -> b) -> b -> MinQueue a -> b

O(n \log n). Performs a left fold on the elements of a priority queue in ascending order.

valuefoldrDesc :: Ord a => (a -> b -> b) -> b -> MinQueue a -> b

O(n \log n). Performs a right fold on the elements of a priority queue in descending order. foldrDesc f z q == foldlAsc (flip f) z q.

valuefoldlDesc :: Ord a => (b -> a -> b) -> b -> MinQueue a -> b

O(n \log n). Performs a left fold on the elements of a priority queue in descending order. foldlDesc f z q == foldrAsc (flip f) z q.

List operations

valuetoList :: Ord a => MinQueue a -> [a]

O(n \log n). Returns the elements of the priority queue in ascending order. Equivalent to toAscList.

If the order of the elements is irrelevant, consider using toListU.

valuefromAscList :: [a] -> MinQueue a

O(n). Constructs a priority queue from an ascending list. Warning: Does not check the precondition.

Performance note: Code using this function in a performance-sensitive context with an argument that is a "good producer" for list fusion should be compiled with -fspec-constr or -O2. For example, fromAscList . map f needs one of these options for best results.

valuefromDescList :: [a] -> MinQueue a

O(n). Constructs a priority queue from an descending list. Warning: Does not check the precondition.

Unordered operations

valuefoldrU :: (a -> b -> b) -> b -> MinQueue a -> b

O(n). Unordered right fold on a priority queue.

valuefoldlU :: (b -> a -> b) -> b -> MinQueue a -> b

O(n). Unordered left fold on a priority queue. This is rarely what you want; foldrU and foldlU' are more likely to perform well.

valuefoldlU' :: (b -> a -> b) -> b -> MinQueue a -> b

O(n). Unordered strict left fold on a priority queue.

Miscellaneous operations

valueseqSpine :: MinQueue a -> b -> b

Deprecated. This function is no longer necessary or useful.

O(\log n). seqSpine q r forces the spine of q and returns r.

Note: The spine of a MinQueue is stored somewhat lazily. In earlier versions of this package, some operations could produce chains of thunks along the spine, occasionally necessitating manual forcing. Now, all operations are careful to force enough to avoid this problem.