Pqueue(3)
π Pqueue(3) β priority queues, bestie π
okay so this is the Pqueue page from the OCaml library, manual section 3 β and it's literally all about priority queues, no cap. π
π NAME π
Pqueue β priority queues. That's the whole name section, periodt. β¨
π¦ Module
Module Pqueue
β¨ Documentation β¨
Module Pqueue : sig end
Priority queues. π«Ά
The Pqueue module implements a data structure of priority queues, given a totally ordered type for elements. This is a mutable data structure. Both min- and max-priority queues are provided. π
The implementation uses a heap stored in a dynamic array, and is therefore reasonably efficient: accessing the minimum (resp. maximum) element takes constant time, and insertion and removal take time logarithmic in the size of the priority queue. Note that of_array runs in linear time (and thus must be preferred to repeated insertions with add). β‘
It is fine to have several elements with the same priority. Nothing is guaranteed regarding the order in which they will be popped. However, it is guaranteed that the element returned by min_elt (or get_min_elt) is the one that is removed from the priority queue by pop_min (or remove_min). This is important in many algorithms, (e.g. when peeking at several priority queues and then selecting one to remove from). π
Since 5.4 β¨
module type OrderedType = sig endβ input signature of the functorsPqueue.MakeMinandPqueue.MakeMax. πmodule type Min = sig endβ output signature of the functorPqueue.MakeMin. πmodule MakeMin : (E : OrderedType) -> sig endβ functor building an implementation of the min-priority queue structure given a totally ordered type for elements. πΈmodule type Max = sig endβ output signature of the functorPqueue.MakeMax. πmodule MakeMax : (E : OrderedType) -> sig endβ functor building an implementation of the max-priority queue structure given a totally ordered type for elements. π¦
π Polymorphic priority queues π
The following, more complex functors create polymorphic queues of type 'a t, just like other polymorphic containers (lists, arrays...). They require a notion of "polymorphic elements" 'a elt that can be compared without depending on the values of 'a. π§
One usage scenario is when the user wants to pass priorities separately from the value stored in the queue. This is done by using pairs priority * 'a as elements. π
module Prio : OrderedType = ...
module PrioQueue = Pqueue.MakeMinPoly(struct
type 'a t = Prio.t * 'a
let compare (p1, _) (p2, _) = Prio.compare p1 p2
end)
(* for example, we now have: *)
PrioQueue.add: 'a PrioQueue.t -> Prio.t * 'a -> unit
PrioQueue.min_elt: 'a PrioQueue.t -> (Prio.t * 'a) option
module type OrderedPolyType = sig endβ input signature of the functorsPqueue.MakeMinPolyandPqueue.MakeMaxPoly. π«module type MinPoly = sig endβ output signature of the functorPqueue.MakeMinPoly. πmodule MakeMinPoly : (E : OrderedPolyType) -> sig endβ functor building an implementation of min-priority queues given a totally ordered type for the elements. πmodule type MaxPoly = sig endβ output signature of the functorPqueue.MakeMaxPoly. πmodule MakeMaxPoly : (E : OrderedPolyType) -> sig endβ functor building an implementation of max-priority queues given a totally ordered type for the elements. π₯
πβ¨ this page's footer credits OCamldoc, dated 2026-06-22 β source/version and date, bestie, periodt. β¨π
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms