| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (** Heaps *) |
| 12 | |
| 13 | module type Ordered = sig |
| 14 | type t |
| 15 | val compare : t -> t -> int |
| 16 | end |
| 17 | |
| 18 | module type S =sig |
| 19 | |
| 20 | (** Type of functional heaps *) |
| 21 | type t |
| 22 | |
| 23 | (** Type of elements *) |
| 24 | type elt |
| 25 | |
| 26 | (** The empty heap *) |
| 27 | val empty : t |
| 28 | |
| 29 | (** [add x h] returns a new heap containing the elements of [h], plus [x]; |
| 30 | complexity {% $ %}O(log(n)){% $ %} *) |
| 31 | val add : elt -> t -> t |
| 32 | |
| 33 | (** [maximum h] returns the maximum element of [h]; raises [EmptyHeap] |
| 34 | when [h] is empty; complexity {% $ %}O(1){% $ %} *) |
| 35 | val maximum : t -> elt |
| 36 | |
| 37 | (** [remove h] returns a new heap containing the elements of [h], except |
| 38 | the maximum of [h]; raises [EmptyHeap] when [h] is empty; |
| 39 | complexity {% $ %}O(log(n)){% $ %} *) |
| 40 | val remove : t -> t |
| 41 | |
| 42 | (** usual iterators and combinators; elements are presented in |
| 43 | arbitrary order *) |
| 44 | val iter : (elt -> unit) -> t -> unit |
| 45 | |
| 46 | val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a |
| 47 | |
| 48 | end |
| 49 | |
| 50 | exception EmptyHeap |
| 51 | |
| 52 | (** {6 Functional implementation. } *) |
| 53 | |
| 54 | module Functional(X: Ordered) : S with type elt=X.t |