mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
imperative heaps on top of splay trees
This commit is contained in:
parent
9e18a807ce
commit
51afd6d74d
2 changed files with 26 additions and 33 deletions
55
heap.ml
55
heap.ml
|
|
@ -25,54 +25,43 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
(** {1 Imperative priority queue} *)
|
(** {1 Imperative priority queue} *)
|
||||||
|
|
||||||
type 'a t = {
|
type 'a t = ('a, unit) SplayTree.t ref
|
||||||
tree: 'a tree;
|
(** The heap is a reference to a splay tree *)
|
||||||
lt: 'a -> 'a -> bool;
|
|
||||||
} (** A heap containing values of type 'a *)
|
|
||||||
and 'a tree =
|
|
||||||
| Empty
|
|
||||||
| Node of int * 'a tree * 'a * 'a tree
|
|
||||||
(** The complete binary tree (int is max depth) *)
|
|
||||||
|
|
||||||
(** Create an empty heap *)
|
(** Create an empty heap *)
|
||||||
let empty ~lt =
|
let empty ~cmp =
|
||||||
{ tree = Empty;
|
ref (SplayTree.empty ~cmp)
|
||||||
lt;
|
|
||||||
}
|
|
||||||
|
|
||||||
(** Insert a value in the heap *)
|
(** Insert a value in the heap *)
|
||||||
let insert heap x =
|
let insert heap x =
|
||||||
let rec insert tree x =
|
heap := SplayTree.insert !heap x ()
|
||||||
match tree with
|
|
||||||
| Empty -> Node (1, Empty, x, Empty)
|
|
||||||
| Node (d, l, y, r) -> failwith "TODO"
|
|
||||||
in
|
|
||||||
{ heap with tree = insert heap.tree x }
|
|
||||||
|
|
||||||
(** Check whether the heap is empty *)
|
(** Check whether the heap is empty *)
|
||||||
let is_empty heap =
|
let is_empty heap =
|
||||||
match heap.tree with
|
SplayTree.is_empty !heap
|
||||||
| Empty -> true
|
|
||||||
| _ -> false
|
|
||||||
|
|
||||||
(** Access the minimal value of the heap, or raises Empty *)
|
(** Access the minimal value of the heap, or raises Empty *)
|
||||||
let min heap =
|
let min (heap : 'a t) : 'a =
|
||||||
match heap.tree with
|
let elt, _ = SplayTree.min !heap in
|
||||||
| Node (_, _, x, _) -> x
|
elt
|
||||||
| Empty -> raise (Invalid_argument "Heap.min on empty heap")
|
|
||||||
|
|
||||||
(** Discard the minimal element *)
|
(** Discard the minimal element *)
|
||||||
let junk heap = failwith "TODO: Heap.junk"
|
let junk heap =
|
||||||
|
let _, (), tree' = SplayTree.delete_min !heap in
|
||||||
|
heap := tree'
|
||||||
|
|
||||||
(** Remove and return the mininal value (or raise Invalid_argument) *)
|
(** Remove and return the mininal value (or raise Invalid_argument) *)
|
||||||
let pop heap =
|
let pop heap =
|
||||||
|
let elt, (), tree' = SplayTree.delete_min !heap in
|
||||||
|
heap := tree';
|
||||||
|
elt
|
||||||
|
|
||||||
(** Iterate on the elements, in an unspecified order *)
|
(** Iterate on the elements, in an unspecified order *)
|
||||||
let iter heap k =
|
let iter heap k =
|
||||||
let rec iter tree = match tree with
|
SplayTree.iter !heap (fun elt _ -> k elt)
|
||||||
| Empty -> ()
|
|
||||||
| Node (_, l, x, r) ->
|
let to_seq heap =
|
||||||
iter l;
|
Sequence.from_iter (fun k -> iter heap k)
|
||||||
k x;
|
|
||||||
iter r
|
let of_seq heap seq =
|
||||||
in iter heap.tree
|
Sequence.iter (fun elt -> insert heap elt) seq
|
||||||
|
|
|
||||||
4
heap.mli
4
heap.mli
|
|
@ -48,3 +48,7 @@ val pop : 'a t -> 'a
|
||||||
|
|
||||||
val iter : 'a t -> ('a -> unit) -> unit
|
val iter : 'a t -> ('a -> unit) -> unit
|
||||||
(** Iterate on the elements, in an unspecified order *)
|
(** Iterate on the elements, in an unspecified order *)
|
||||||
|
|
||||||
|
val to_seq : 'a t -> 'a Sequence.t
|
||||||
|
|
||||||
|
val of_seq : 'a t -> 'a Sequence.t -> unit
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue