From cc2dd6d829783412db63f1149d308907a787cd87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Glen=20M=C3=A9vel?= Date: Sat, 27 Jul 2024 01:47:09 +0200 Subject: [PATCH] doc/CCHeap: move filter down --- src/core/CCHeap.ml | 22 +++++++++++----------- src/core/CCHeap.mli | 10 +++++----- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/core/CCHeap.ml b/src/core/CCHeap.ml index bab1fcda..b0224446 100644 --- a/src/core/CCHeap.ml +++ b/src/core/CCHeap.ml @@ -49,11 +49,6 @@ module type S = sig val add : t -> elt -> t (** [add h x] is [insert x h]. *) - val filter : (elt -> bool) -> t -> t - (** [filter p h] filters values, only retaining the ones that satisfy the predicate [p]. - Complexity: [O(n log n)]. - *) - val find_min : t -> elt option (** [find_min h] find the minimal element of the heap [h]. Complexity: [O(1)]. @@ -91,6 +86,11 @@ module type S = sig Complexity: [O(n log n)]. @since 2.0 *) + val filter : (elt -> bool) -> t -> t + (** [filter p h] filters values, only retaining the ones that satisfy the predicate [p]. + Complexity: [O(n log n)]. + *) + val iter : (elt -> unit) -> t -> unit (** [iter f h] iterates over the heap [h] invoking [f] with the current element. *) @@ -251,12 +251,6 @@ module Make (E : PARTIAL_ORD) : S with type elt = E.t = struct let insert x h = merge (N (1, x, E, E)) h let add h x = insert x h - let rec filter p h = - match h with - | E -> E - | N (_, x, l, r) when p x -> _make_node x (filter p l) (filter p r) - | N (_, _, l, r) -> merge (filter p l) (filter p r) - let find_min_exn = function | E -> raise Empty | N (_, x, _, _) -> x @@ -306,6 +300,12 @@ module Make (E : PARTIAL_ORD) : S with type elt = E.t = struct else h + let rec filter p h = + match h with + | E -> E + | N (_, x, l, r) when p x -> _make_node x (filter p l) (filter p r) + | N (_, _, l, r) -> merge (filter p l) (filter p r) + let rec iter f h = match h with | E -> () diff --git a/src/core/CCHeap.mli b/src/core/CCHeap.mli index 4b7d858d..ec14bc23 100644 --- a/src/core/CCHeap.mli +++ b/src/core/CCHeap.mli @@ -54,11 +54,6 @@ module type S = sig val add : t -> elt -> t (** [add h x] is [insert x h]. *) - val filter : (elt -> bool) -> t -> t - (** [filter p h] filters values, only retaining the ones that satisfy the predicate [p]. - Complexity: [O(n log n)]. - *) - val find_min : t -> elt option (** [find_min h] find the minimal element of the heap [h]. Complexity: [O(1)]. @@ -95,6 +90,11 @@ module type S = sig Complexity: [O(n log n)]. @since 2.0 *) + val filter : (elt -> bool) -> t -> t + (** [filter p h] filters values, only retaining the ones that satisfy the predicate [p]. + Complexity: [O(n log n)]. + *) + val iter : (elt -> unit) -> t -> unit (** [iter f h] iterates over the heap [h] invoking [f] with the current element. *)