mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
Add CCHeap.delete_all
This commit is contained in:
parent
2007d7ad37
commit
7c7f66cd7b
2 changed files with 18 additions and 0 deletions
|
|
@ -118,6 +118,10 @@ module type S = sig
|
|||
(** Same as {!take}, but can fail.
|
||||
@raise Empty if the heap is empty *)
|
||||
|
||||
val delete_all : (elt -> elt -> bool) -> elt -> t -> t
|
||||
(** Delete value if it exist in the heap.
|
||||
[delete_all eq x h], use [eq] to find all [x] in [h] and delete them *)
|
||||
|
||||
val iter : (elt -> unit) -> t -> unit
|
||||
(** Iterate on elements *)
|
||||
|
||||
|
|
@ -243,6 +247,16 @@ module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct
|
|||
| E -> raise Empty
|
||||
| N (_, x, l, r) -> merge l r, x
|
||||
|
||||
let rec delete_all eq x = function
|
||||
| E -> E
|
||||
| N (_, y, l, r) as h ->
|
||||
if eq x y then merge (delete_all eq x l) (delete_all eq x r)
|
||||
else begin
|
||||
if E.leq y x
|
||||
then _make_node y (delete_all eq x l) (delete_all eq x r)
|
||||
else h
|
||||
end
|
||||
|
||||
let rec iter f h = match h with
|
||||
| E -> ()
|
||||
| N(_,x,l,r) -> f x; iter f l; iter f r
|
||||
|
|
|
|||
|
|
@ -55,6 +55,10 @@ module type S = sig
|
|||
(** Same as {!take}, but can fail.
|
||||
@raise Empty if the heap is empty *)
|
||||
|
||||
val delete_all : (elt -> elt -> bool) -> elt -> t -> t
|
||||
(** Delete value if it exist in the heap.
|
||||
[delete_all eq x h], use [eq] to find all [x] in [h] and delete them *)
|
||||
|
||||
val iter : (elt -> unit) -> t -> unit
|
||||
(** Iterate on elements *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue