Debian Codegraph @

clib/heap.ml

raw symbols SCIP
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 (*s 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 (*s Functional implementation *)
53
54 module Functional(X : Ordered) = struct
55
56 (* Heaps are encoded as Braun trees, that are binary trees
57 where size r <= size l <= size r + 1 for each node Node (l, x, r) *)
58
59 type t =
60 | Leaf
61 | Node of t * X.t * t
62
63 type elt = X.t
64
65 let empty = Leaf
66
67 let rec add x = function
68 | Leaf ->
69 Node (Leaf, x, Leaf)
70 | Node (l, y, r) ->
71 if X.compare x y >= 0 then
72 Node (add y r, x, l)
73 else
74 Node (add x r, y, l)
75
76 let rec extract = function
77 | Leaf ->
78 assert false
79 | Node (Leaf, y, r) ->
80 assert (r = Leaf);
81 y, Leaf
82 | Node (l, y, r) ->
83 let x, l = extract l in
84 x, Node (r, y, l)
85
86 let is_above x = function
87 | Leaf -> true
88 | Node (_, y, _) -> X.compare x y >= 0
89
90 let rec replace_min x = function
91 | Node (l, _, r) when is_above x l && is_above x r ->
92 Node (l, x, r)
93 | Node ((Node (_, lx, _) as l), _, r) when is_above lx r ->
94 (* lx <= x, rx necessarily *)
95 Node (replace_min x l, lx, r)
96 | Node (l, _, (Node (_, rx, _) as r)) ->
97 (* rx <= x, lx necessarily *)
98 Node (l, rx, replace_min x r)
99 | Leaf | Node (Leaf, _, _) | Node (_, _, Leaf) ->
100 assert false
101
102 (* merges two Braun trees [l] and [r],
103 with the assumption that [size r <= size l <= size r + 1] *)
104 let rec merge l r = match l, r with
105 | _, Leaf ->
106 l
107 | Node (ll, lx, lr), Node (_, ly, _) ->
108 if X.compare lx ly >= 0 then
109 Node (r, lx, merge ll lr)
110 else
111 let x, l = extract l in
112 Node (replace_min x r, ly, l)
113 | Leaf, _ ->
114 assert false (* contradicts the assumption *)
115
116 let maximum = function
117 | Leaf -> raise EmptyHeap
118 | Node (_, x, _) -> x
119
120 let remove = function
121 | Leaf ->
122 raise EmptyHeap
123 | Node (l, _, r) ->
124 merge l r
125
126 let rec iter f = function
127 | Leaf -> ()
128 | Node (l, x, r) -> iter f l; f x; iter f r
129
130 let rec fold f h x0 = match h with
131 | Leaf ->
132 x0
133 | Node (l, x, r) ->
134 fold f l (fold f r (f x x0))
135
136 end