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

Modulepqueue-1.5.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.

  • 1 type
  • 44 values
  • Packagepqueue-1.5.0.0
  • Exports47
  • 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.5.0.0 · Data.PQueue.Internals
  • (Ord a, Data a) => Data (MinQueue a)Defined in pqueue-1.5.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.5.0.0 · Data.PQueue.Internals
  • Read a => Read (MinQueue a)Defined in pqueue-1.5.0.0 · Data.PQueue.Internals
  • (Ord a, Show a) => Show (MinQueue a)Defined in pqueue-1.5.0.0 · Data.PQueue.Internals
  • Ord a => Semigroup (MinQueue a)Defined in pqueue-1.5.0.0 · Data.PQueue.Internals
  • Ord a => Monoid (MinQueue a)Defined in pqueue-1.5.0.0 · Data.PQueue.Internals
  • NFData a => NFData (MinQueue a)Defined in pqueue-1.5.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

3 declarations

Query operations

5 declarations
valuefindMin :: MinQueue a -> a
#

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

valuegetMin :: MinQueue a -> Maybe a
#

O(1). Returns the minimum element of the queue, if the queue is nonempty.

Construction operations

4 declarations
valuesingleton :: a -> MinQueue a
#

O(1). Construct a priority queue with a single element.

valueinsert :: Ord a => a -> MinQueue a -> MinQueue a
#

Amortized O(1), worst-case O(\log n). Insert an element into the priority queue.

Subsets

0 declarations

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.

valuedrop :: Ord a => Int -> MinQueue a -> MinQueue a
#

O(k \log n)/. drop k, applied to a queue queue, returns queue with the smallest k elements deleted, or an empty queue 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

4 declarations
valuepartition :: Ord a => (a -> Bool) -> MinQueue a -> (MinQueue a, MinQueue a)
#

O(n). Returns a pair where the first queue contains all elements satisfying p, and the second queue contains all elements not satisfying p.

Fold/Functor/Traversable variations

5 declarations
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 . map f . toList.

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

6 declarations
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.

valuetoAscList :: Ord a => MinQueue a -> [a]
#

O(n \log n). Extracts the elements of the priority queue in ascending order.

valuetoDescList :: Ord a => MinQueue a -> [a]
#

O(n \log n). Extracts the elements of the priority queue in descending order.

valuefromList :: Ord a => [a] -> MinQueue a
#

O(n). Constructs a priority queue from an unordered list.

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

7 declarations
valuemapU :: (a -> b) -> MinQueue a -> MinQueue b
#

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

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.

valuetoListU :: MinQueue a -> [a]
#

O(n). Returns the elements of the queue, in no particular order.

Miscellaneous operations

2 declarations
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.